-
Notifications
You must be signed in to change notification settings - Fork 226
test: add coverage for display content and json handlers #1950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } |
| 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) | ||
| } | ||
| } | ||
|
|
||
| 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
|
||
|
|
||
| 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
|
||
| } | ||
| 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
|
||
|
|
||
| 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
|
||
|
|
||
| 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
|
||
|
|
||
| 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
|
||
| } | ||
There was a problem hiding this comment.
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.