Skip to content

Commit f3a2ac6

Browse files
committed
manifest: Add unit tests for manifest inspect command
Add tests for tag/digest references and verbose modes Signed-off-by: ChengyuZhu6 <hudson@cyzhu.com>
1 parent dba589c commit f3a2ac6

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package manifest
18+
19+
import (
20+
"encoding/json"
21+
"testing"
22+
23+
"gotest.tools/v3/assert"
24+
25+
"github.com/containerd/nerdctl/mod/tigron/test"
26+
"github.com/containerd/nerdctl/mod/tigron/tig"
27+
28+
"github.com/containerd/nerdctl/v2/pkg/testutil"
29+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
30+
)
31+
32+
var (
33+
alpineDigest = "sha256:ec14c7992a97fc11425907e908340c6c3d6ff602f5f13d899e6b7027c9b4133a"
34+
alpineAmd64ConfigDigest = "sha256:49f356fa4513676c5e22e3a8404aad6c7262cc7aaed15341458265320786c58c"
35+
alpineAmd64Digest = "sha256:e103c1b4bf019dc290bcc7aca538dc2bf7a9d0fc836e186f5fa34945c5168310"
36+
)
37+
38+
func TestManifestInspect(t *testing.T) {
39+
testCase := nerdtest.Setup()
40+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
41+
helpers.Ensure("pull", "--quiet", "--all-platforms", testutil.AlpineImage)
42+
}
43+
testCase.SubTests = []*test.Case{
44+
{
45+
Description: "tag-non-verbose",
46+
Command: test.Command("manifest", "inspect", testutil.AlpineImage),
47+
Expected: test.Expects(0, nil, func(stdout string, t tig.T) {
48+
var arr []map[string]interface{}
49+
assert.NilError(t, json.Unmarshal([]byte(stdout), &arr))
50+
assert.Assert(t, len(arr) > 0)
51+
index, ok := arr[0]["Index"].(map[string]interface{})
52+
assert.Assert(t, ok)
53+
manifests, ok := index["manifests"].([]interface{})
54+
assert.Assert(t, ok)
55+
assert.Assert(t, len(manifests) > 0)
56+
}),
57+
},
58+
{
59+
Description: "tag-verbose",
60+
Command: test.Command("manifest", "inspect", testutil.AlpineImage, "--verbose"),
61+
Expected: test.Expects(0, nil, func(stdout string, t tig.T) {
62+
var arr []map[string]interface{}
63+
assert.NilError(t, json.Unmarshal([]byte(stdout), &arr))
64+
assert.Assert(t, len(arr) > 0)
65+
assert.Equal(t, arr[0]["ImageName"], testutil.AlpineImage)
66+
indexDesc := arr[0]["IndexDesc"].(map[string]interface{})
67+
assert.Equal(t, indexDesc["mediaType"], "application/vnd.docker.distribution.manifest.list.v2+json")
68+
assert.Equal(t, indexDesc["digest"], alpineDigest)
69+
manifests, ok := arr[0]["Manifests"].([]interface{})
70+
assert.Assert(t, ok, "should have Manifests field")
71+
assert.Assert(t, len(manifests) > 0)
72+
manifestObj, ok := manifests[0].(map[string]interface{})
73+
assert.Assert(t, ok, "Manifests[0] should be object")
74+
manifestArr := []map[string]interface{}{manifestObj}
75+
assertManifestConfigDigest(t, manifestArr, alpineAmd64ConfigDigest)
76+
assertManifestDescDigest(t, manifestArr, alpineAmd64Digest)
77+
}),
78+
},
79+
{
80+
Description: "digest-non-verbose",
81+
Command: test.Command("manifest", "inspect", "alpine@"+alpineAmd64Digest),
82+
Expected: test.Expects(0, nil, func(stdout string, t tig.T) {
83+
var arr []map[string]interface{}
84+
assert.NilError(t, json.Unmarshal([]byte(stdout), &arr))
85+
assert.Assert(t, len(arr) > 0)
86+
assertManifestConfigDigest(t, arr, alpineAmd64ConfigDigest)
87+
}),
88+
},
89+
{
90+
Description: "digest-verbose",
91+
Command: test.Command("manifest", "inspect", "alpine@"+alpineAmd64Digest, "--verbose"),
92+
Expected: test.Expects(0, nil, func(stdout string, t tig.T) {
93+
var arr []map[string]interface{}
94+
assert.NilError(t, json.Unmarshal([]byte(stdout), &arr))
95+
assert.Assert(t, len(arr) > 0)
96+
assertManifestConfigDigest(t, arr, alpineAmd64ConfigDigest)
97+
assertManifestDescDigest(t, arr, alpineAmd64Digest)
98+
}),
99+
},
100+
{
101+
Description: "sha256-digest-non-verbose",
102+
Command: test.Command("manifest", "inspect", alpineAmd64Digest),
103+
Expected: test.Expects(0, nil, func(stdout string, t tig.T) {
104+
var arr []map[string]interface{}
105+
assert.NilError(t, json.Unmarshal([]byte(stdout), &arr))
106+
assert.Assert(t, len(arr) > 0)
107+
assertManifestConfigDigest(t, arr, alpineAmd64ConfigDigest)
108+
}),
109+
},
110+
{
111+
Description: "sha256-digest-verbose",
112+
Command: test.Command("manifest", "inspect", alpineAmd64Digest, "--verbose"),
113+
Expected: test.Expects(0, nil, func(stdout string, t tig.T) {
114+
var arr []map[string]interface{}
115+
assert.NilError(t, json.Unmarshal([]byte(stdout), &arr))
116+
assert.Assert(t, len(arr) > 0)
117+
assertManifestConfigDigest(t, arr, alpineAmd64ConfigDigest)
118+
assertManifestDescDigest(t, arr, alpineAmd64Digest)
119+
}),
120+
},
121+
}
122+
123+
testCase.Run(t)
124+
}
125+
126+
func assertManifestConfigDigest(t tig.T, arr []map[string]interface{}, wantDigest string) {
127+
manifest, ok := arr[0]["Manifest"].(map[string]interface{})
128+
assert.Assert(t, ok)
129+
config, ok := manifest["config"].(map[string]interface{})
130+
assert.Assert(t, ok, "should have config field")
131+
assert.Equal(t, config["digest"], wantDigest)
132+
}
133+
134+
func assertManifestDescDigest(t tig.T, arr []map[string]interface{}, wantDigest string) {
135+
manifestDesc, ok := arr[0]["ManifestDesc"].(map[string]interface{})
136+
assert.Assert(t, ok, "should have ManifestDesc field")
137+
assert.Equal(t, manifestDesc["digest"], wantDigest)
138+
}

cmd/nerdctl/manifest/manifest_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package manifest
18+
19+
import (
20+
"testing"
21+
22+
"github.com/containerd/nerdctl/v2/pkg/testutil"
23+
)
24+
25+
func TestMain(m *testing.M) {
26+
testutil.M(m)
27+
}

0 commit comments

Comments
 (0)