Skip to content

Commit cbe3cf2

Browse files
committed
test: add coverage for display content and json handlers
Add test coverage for: - content/discard.go: DiscardHandler methods - metadata/json/push.go: PushHandler methods - metadata/json/pull.go: PullHandler methods Partial: #1916 Signed-off-by: Gregorius Bima Kharisma Wicaksana <51526537+bimakw@users.noreply.github.com>
1 parent f272ba7 commit cbe3cf2

File tree

3 files changed

+295
-0
lines changed

3 files changed

+295
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright The ORAS Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package content
17+
18+
import (
19+
"testing"
20+
21+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
22+
)
23+
24+
func TestNewDiscardHandler(t *testing.T) {
25+
handler := NewDiscardHandler()
26+
if handler != (DiscardHandler{}) {
27+
t.Errorf("NewDiscardHandler() returned unexpected handler")
28+
}
29+
}
30+
31+
func TestDiscardHandler_OnContentFetched(t *testing.T) {
32+
handler := NewDiscardHandler()
33+
desc := ocispec.Descriptor{
34+
MediaType: "application/vnd.oci.image.manifest.v1+json",
35+
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
36+
Size: 0,
37+
}
38+
content := []byte("test content")
39+
40+
err := handler.OnContentFetched(desc, content)
41+
if err != nil {
42+
t.Errorf("DiscardHandler.OnContentFetched() error = %v, want nil", err)
43+
}
44+
}
45+
46+
func TestDiscardHandler_OnContentCreated(t *testing.T) {
47+
handler := NewDiscardHandler()
48+
content := []byte("test content")
49+
50+
err := handler.OnContentCreated(content)
51+
if err != nil {
52+
t.Errorf("DiscardHandler.OnContentCreated() error = %v, want nil", err)
53+
}
54+
}
55+
56+
func TestDiscardHandler_ImplementsInterfaces(t *testing.T) {
57+
handler := NewDiscardHandler()
58+
59+
// Verify DiscardHandler implements ManifestFetchHandler
60+
var _ ManifestFetchHandler = handler
61+
62+
// Verify DiscardHandler implements ManifestIndexCreateHandler
63+
var _ ManifestIndexCreateHandler = handler
64+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright The ORAS Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package json
17+
18+
import (
19+
"bytes"
20+
"encoding/json"
21+
"testing"
22+
23+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
24+
"oras.land/oras/cmd/oras/internal/option"
25+
)
26+
27+
func TestNewPullHandler(t *testing.T) {
28+
buf := &bytes.Buffer{}
29+
handler := NewPullHandler(buf, "localhost:5000/test")
30+
if handler == nil {
31+
t.Fatal("NewPullHandler() returned nil")
32+
}
33+
}
34+
35+
func TestPullHandler_OnLayerSkipped(t *testing.T) {
36+
buf := &bytes.Buffer{}
37+
handler := NewPullHandler(buf, "localhost:5000/test").(*PullHandler)
38+
39+
desc := ocispec.Descriptor{
40+
MediaType: "application/vnd.oci.image.layer.v1.tar+gzip",
41+
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
42+
Size: 1024,
43+
}
44+
45+
err := handler.OnLayerSkipped(desc)
46+
if err != nil {
47+
t.Errorf("PullHandler.OnLayerSkipped() error = %v, want nil", err)
48+
}
49+
}
50+
51+
func TestPullHandler_OnFilePulled(t *testing.T) {
52+
buf := &bytes.Buffer{}
53+
handler := NewPullHandler(buf, "localhost:5000/test").(*PullHandler)
54+
55+
desc := ocispec.Descriptor{
56+
MediaType: "application/vnd.oci.image.layer.v1.tar+gzip",
57+
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
58+
Size: 1024,
59+
}
60+
61+
err := handler.OnFilePulled("test.txt", "/output", desc, "blobs/sha256/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
62+
if err != nil {
63+
t.Errorf("PullHandler.OnFilePulled() error = %v, want nil", err)
64+
}
65+
}
66+
67+
func TestPullHandler_OnPulled(t *testing.T) {
68+
buf := &bytes.Buffer{}
69+
handler := NewPullHandler(buf, "localhost:5000/test").(*PullHandler)
70+
71+
desc := ocispec.Descriptor{
72+
MediaType: "application/vnd.oci.image.manifest.v1+json",
73+
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
74+
Size: 100,
75+
}
76+
77+
handler.OnPulled(&option.Target{}, desc)
78+
79+
if handler.root.Digest != desc.Digest {
80+
t.Errorf("PullHandler.OnPulled() did not set root descriptor correctly")
81+
}
82+
}
83+
84+
func TestPullHandler_Render(t *testing.T) {
85+
buf := &bytes.Buffer{}
86+
handler := NewPullHandler(buf, "localhost:5000/test").(*PullHandler)
87+
88+
desc := ocispec.Descriptor{
89+
MediaType: "application/vnd.oci.image.manifest.v1+json",
90+
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
91+
Size: 100,
92+
}
93+
94+
handler.OnPulled(&option.Target{}, desc)
95+
96+
err := handler.Render()
97+
if err != nil {
98+
t.Errorf("PullHandler.Render() error = %v, want nil", err)
99+
}
100+
101+
// Verify JSON output is valid
102+
var result map[string]interface{}
103+
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
104+
t.Errorf("PullHandler.Render() produced invalid JSON: %v", err)
105+
}
106+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
Copyright The ORAS Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package json
17+
18+
import (
19+
"bytes"
20+
"encoding/json"
21+
"testing"
22+
23+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
24+
"oras.land/oras/cmd/oras/internal/option"
25+
)
26+
27+
func TestNewPushHandler(t *testing.T) {
28+
buf := &bytes.Buffer{}
29+
handler := NewPushHandler(buf)
30+
if handler == nil {
31+
t.Fatal("NewPushHandler() returned nil")
32+
}
33+
}
34+
35+
func TestPushHandler_OnTagged(t *testing.T) {
36+
buf := &bytes.Buffer{}
37+
handler := NewPushHandler(buf).(*PushHandler)
38+
39+
desc := ocispec.Descriptor{
40+
MediaType: "application/vnd.oci.image.manifest.v1+json",
41+
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
42+
Size: 100,
43+
}
44+
45+
err := handler.OnTagged(desc, "v1.0.0")
46+
if err != nil {
47+
t.Errorf("PushHandler.OnTagged() error = %v, want nil", err)
48+
}
49+
}
50+
51+
func TestPushHandler_OnCopied(t *testing.T) {
52+
buf := &bytes.Buffer{}
53+
handler := NewPushHandler(buf).(*PushHandler)
54+
55+
desc := ocispec.Descriptor{
56+
MediaType: "application/vnd.oci.image.manifest.v1+json",
57+
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
58+
Size: 100,
59+
}
60+
61+
opts := &option.Target{
62+
RawReference: "localhost:5000/test:v1.0.0",
63+
Path: "localhost:5000/test",
64+
Reference: "v1.0.0",
65+
}
66+
67+
err := handler.OnCopied(opts, desc)
68+
if err != nil {
69+
t.Errorf("PushHandler.OnCopied() error = %v, want nil", err)
70+
}
71+
}
72+
73+
func TestPushHandler_OnCopied_WithDigest(t *testing.T) {
74+
buf := &bytes.Buffer{}
75+
handler := NewPushHandler(buf).(*PushHandler)
76+
77+
desc := ocispec.Descriptor{
78+
MediaType: "application/vnd.oci.image.manifest.v1+json",
79+
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
80+
Size: 100,
81+
}
82+
83+
opts := &option.Target{
84+
RawReference: "localhost:5000/test@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
85+
Path: "localhost:5000/test",
86+
Reference: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
87+
}
88+
89+
err := handler.OnCopied(opts, desc)
90+
if err != nil {
91+
t.Errorf("PushHandler.OnCopied() error = %v, want nil", err)
92+
}
93+
}
94+
95+
func TestPushHandler_Render(t *testing.T) {
96+
buf := &bytes.Buffer{}
97+
handler := NewPushHandler(buf).(*PushHandler)
98+
99+
desc := ocispec.Descriptor{
100+
MediaType: "application/vnd.oci.image.manifest.v1+json",
101+
Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
102+
Size: 100,
103+
}
104+
105+
opts := &option.Target{
106+
RawReference: "localhost:5000/test:v1.0.0",
107+
Path: "localhost:5000/test",
108+
Reference: "v1.0.0",
109+
}
110+
111+
if err := handler.OnCopied(opts, desc); err != nil {
112+
t.Fatalf("PushHandler.OnCopied() error = %v", err)
113+
}
114+
115+
err := handler.Render()
116+
if err != nil {
117+
t.Errorf("PushHandler.Render() error = %v, want nil", err)
118+
}
119+
120+
// Verify JSON output is valid
121+
var result map[string]interface{}
122+
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
123+
t.Errorf("PushHandler.Render() produced invalid JSON: %v", err)
124+
}
125+
}

0 commit comments

Comments
 (0)