Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions cmd/oras/internal/display/content/discard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package content

import (
"testing"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

func TestNewDiscardHandler(t *testing.T) {
handler := NewDiscardHandler()
if handler != (DiscardHandler{}) {
t.Errorf("NewDiscardHandler() returned unexpected handler")
}
}

func TestDiscardHandler_OnContentFetched(t *testing.T) {
handler := NewDiscardHandler()
desc := ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Size: 0,
}
content := []byte("test content")

err := handler.OnContentFetched(desc, content)
if err != nil {
t.Errorf("DiscardHandler.OnContentFetched() error = %v, want nil", err)
}
}

func TestDiscardHandler_OnContentCreated(t *testing.T) {
handler := NewDiscardHandler()
content := []byte("test content")

err := handler.OnContentCreated(content)
if err != nil {
t.Errorf("DiscardHandler.OnContentCreated() error = %v, want nil", err)
}
}

func TestDiscardHandler_ImplementsInterfaces(t *testing.T) {
handler := NewDiscardHandler()

// Verify DiscardHandler implements ManifestFetchHandler
var _ ManifestFetchHandler = handler

// Verify DiscardHandler implements ManifestIndexCreateHandler
var _ ManifestIndexCreateHandler = handler
}
106 changes: 106 additions & 0 deletions cmd/oras/internal/display/metadata/json/pull_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package json

import (
"bytes"
"encoding/json"
"testing"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/option"
)

func TestNewPullHandler(t *testing.T) {
buf := &bytes.Buffer{}
handler := NewPullHandler(buf, "localhost:5000/test")
if handler == nil {
t.Fatal("NewPullHandler() returned nil")
}
}

func TestPullHandler_OnLayerSkipped(t *testing.T) {
buf := &bytes.Buffer{}
handler := NewPullHandler(buf, "localhost:5000/test").(*PullHandler)

desc := ocispec.Descriptor{
MediaType: "application/vnd.oci.image.layer.v1.tar+gzip",
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Size: 1024,
}

err := handler.OnLayerSkipped(desc)
if err != nil {
t.Errorf("PullHandler.OnLayerSkipped() error = %v, want nil", err)
}
}
Comment on lines +35 to +49
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only verifies that OnLayerSkipped returns no error, but since this is a no-op method that always returns nil, this test provides minimal value. Consider whether this test is necessary, or if it should be testing integration with other methods to ensure the layer being skipped doesn't affect the final output.

Copilot uses AI. Check for mistakes.

func TestPullHandler_OnFilePulled(t *testing.T) {
buf := &bytes.Buffer{}
handler := NewPullHandler(buf, "localhost:5000/test").(*PullHandler)

desc := ocispec.Descriptor{
MediaType: "application/vnd.oci.image.layer.v1.tar+gzip",
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Size: 1024,
}

err := handler.OnFilePulled("test.txt", "/output", desc, "blobs/sha256/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
if err != nil {
t.Errorf("PullHandler.OnFilePulled() error = %v, want nil", err)
}
}
Comment on lines +51 to +65
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only verifies that OnFilePulled returns no error, but doesn't verify that the file information was actually added to the handler's internal state. Consider adding assertions to check that the file was properly recorded in the pulled files list after calling OnFilePulled.

Copilot uses AI. Check for mistakes.

func TestPullHandler_OnPulled(t *testing.T) {
buf := &bytes.Buffer{}
handler := NewPullHandler(buf, "localhost:5000/test").(*PullHandler)

desc := ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Size: 100,
}

handler.OnPulled(&option.Target{}, desc)

if handler.root.Digest != desc.Digest {
t.Errorf("PullHandler.OnPulled() did not set root descriptor correctly")
}
}

func TestPullHandler_Render(t *testing.T) {
buf := &bytes.Buffer{}
handler := NewPullHandler(buf, "localhost:5000/test").(*PullHandler)

desc := ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Size: 100,
}

handler.OnPulled(&option.Target{}, desc)

err := handler.Render()
if err != nil {
t.Errorf("PullHandler.Render() error = %v, want nil", err)
}

// Verify JSON output is valid
var result map[string]interface{}
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
t.Errorf("PullHandler.Render() produced invalid JSON: %v", err)
}
Comment on lines +101 to +105
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test validates that the output is valid JSON but doesn't verify the actual content or structure of the rendered output. Consider adding assertions to check for expected fields such as the digest, path, and files array in the result to ensure the Render method produces the correct output structure.

Copilot uses AI. Check for mistakes.
}
125 changes: 125 additions & 0 deletions cmd/oras/internal/display/metadata/json/push_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package json

import (
"bytes"
"encoding/json"
"testing"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/option"
)

func TestNewPushHandler(t *testing.T) {
buf := &bytes.Buffer{}
handler := NewPushHandler(buf)
if handler == nil {
t.Fatal("NewPushHandler() returned nil")
}
}

func TestPushHandler_OnTagged(t *testing.T) {
buf := &bytes.Buffer{}
handler := NewPushHandler(buf).(*PushHandler)

desc := ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Size: 100,
}

err := handler.OnTagged(desc, "v1.0.0")
if err != nil {
t.Errorf("PushHandler.OnTagged() error = %v, want nil", err)
}
}
Comment on lines +35 to +49
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only verifies that OnTagged returns no error, but doesn't verify that the tag was actually added to the handler's internal state. Consider adding assertions to check that the tag is properly stored by verifying it appears in the handler's tags list after calling OnTagged.

Copilot uses AI. Check for mistakes.

func TestPushHandler_OnCopied(t *testing.T) {
buf := &bytes.Buffer{}
handler := NewPushHandler(buf).(*PushHandler)

desc := ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Size: 100,
}

opts := &option.Target{
RawReference: "localhost:5000/test:v1.0.0",
Path: "localhost:5000/test",
Reference: "v1.0.0",
}

err := handler.OnCopied(opts, desc)
if err != nil {
t.Errorf("PushHandler.OnCopied() error = %v, want nil", err)
}
}
Comment on lines +51 to +71
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only verifies that OnCopied returns no error, but doesn't verify that the descriptor and options were actually stored in the handler's internal state. Consider adding assertions to check that handler.root equals the descriptor and handler.path equals opts.Path after calling OnCopied.

Copilot uses AI. Check for mistakes.

func TestPushHandler_OnCopied_WithDigest(t *testing.T) {
buf := &bytes.Buffer{}
handler := NewPushHandler(buf).(*PushHandler)

desc := ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Size: 100,
}

opts := &option.Target{
RawReference: "localhost:5000/test@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Path: "localhost:5000/test",
Reference: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}

err := handler.OnCopied(opts, desc)
if err != nil {
t.Errorf("PushHandler.OnCopied() error = %v, want nil", err)
}
}
Comment on lines +73 to +93
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only verifies that OnCopied returns no error, but doesn't verify that the tag was NOT added to the handler's internal state when the reference is a digest. According to the OnCopied implementation, tags should only be added when the reference is not a digest. Consider adding assertions to verify that no tag was added in this digest-based reference scenario.

Copilot uses AI. Check for mistakes.

func TestPushHandler_Render(t *testing.T) {
buf := &bytes.Buffer{}
handler := NewPushHandler(buf).(*PushHandler)

desc := ocispec.Descriptor{
MediaType: "application/vnd.oci.image.manifest.v1+json",
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
Size: 100,
}

opts := &option.Target{
RawReference: "localhost:5000/test:v1.0.0",
Path: "localhost:5000/test",
Reference: "v1.0.0",
}

if err := handler.OnCopied(opts, desc); err != nil {
t.Fatalf("PushHandler.OnCopied() error = %v", err)
}

err := handler.Render()
if err != nil {
t.Errorf("PushHandler.Render() error = %v, want nil", err)
}

// Verify JSON output is valid
var result map[string]interface{}
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
t.Errorf("PushHandler.Render() produced invalid JSON: %v", err)
}
Comment on lines +120 to +124
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test validates that the output is valid JSON but doesn't verify the actual content or structure of the rendered output. Consider adding assertions to check for expected fields such as the digest, mediaType, path, and tags in the result to ensure the Render method produces the correct output structure.

Copilot uses AI. Check for mistakes.
}