|
| 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 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 24 | + "gotest.tools/v3/assert" |
| 25 | + |
| 26 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 27 | + "github.com/containerd/nerdctl/mod/tigron/tig" |
| 28 | + |
| 29 | + "github.com/containerd/nerdctl/v2/pkg/manifesttypes" |
| 30 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 31 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 32 | +) |
| 33 | + |
| 34 | +const ( |
| 35 | + testImageName = "alpine" |
| 36 | + testPlatform = "linux/amd64" |
| 37 | +) |
| 38 | + |
| 39 | +type testData struct { |
| 40 | + imageName string |
| 41 | + platform string |
| 42 | + imageRef string |
| 43 | + manifestDigest string |
| 44 | + configDigest string |
| 45 | + rawData string |
| 46 | +} |
| 47 | + |
| 48 | +func newTestData(imageName, platform string) *testData { |
| 49 | + return &testData{ |
| 50 | + imageName: imageName, |
| 51 | + platform: platform, |
| 52 | + imageRef: testutil.GetTestImage(imageName), |
| 53 | + manifestDigest: testutil.GetTestImageManifestDigest(imageName, platform), |
| 54 | + configDigest: testutil.GetTestImageConfigDigest(imageName, platform), |
| 55 | + rawData: testutil.GetTestImageRaw(imageName, platform), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (td *testData) imageWithDigest() string { |
| 60 | + return testutil.GetTestImageWithoutTag(td.imageName) + "@" + td.manifestDigest |
| 61 | +} |
| 62 | + |
| 63 | +func (td *testData) isAmd64Platform(platform *ocispec.Platform) bool { |
| 64 | + return platform != nil && |
| 65 | + platform.Architecture == "amd64" && |
| 66 | + platform.OS == "linux" |
| 67 | +} |
| 68 | + |
| 69 | +func TestManifestInspect(t *testing.T) { |
| 70 | + testCase := nerdtest.Setup() |
| 71 | + td := newTestData(testImageName, testPlatform) |
| 72 | + |
| 73 | + testCase.SubTests = []*test.Case{ |
| 74 | + { |
| 75 | + Description: "tag-non-verbose", |
| 76 | + Command: test.Command("manifest", "inspect", td.imageRef), |
| 77 | + Expected: test.Expects(0, nil, func(stdout string, t tig.T) { |
| 78 | + var manifest manifesttypes.DockerManifestListStruct |
| 79 | + assert.NilError(t, json.Unmarshal([]byte(stdout), &manifest)) |
| 80 | + |
| 81 | + assert.Equal(t, manifest.SchemaVersion, testutil.GetTestImageSchemaVersion(td.imageName)) |
| 82 | + assert.Equal(t, manifest.MediaType, testutil.GetTestImageMediaType(td.imageName)) |
| 83 | + assert.Assert(t, len(manifest.Manifests) > 0) |
| 84 | + |
| 85 | + var foundManifest *ocispec.Descriptor |
| 86 | + for _, m := range manifest.Manifests { |
| 87 | + if td.isAmd64Platform(m.Platform) { |
| 88 | + foundManifest = &m |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + assert.Assert(t, foundManifest != nil, "should find amd64 platform manifest") |
| 93 | + assert.Equal(t, foundManifest.Digest.String(), td.manifestDigest) |
| 94 | + assert.Equal(t, foundManifest.MediaType, testutil.GetTestImagePlatformMediaType(td.imageName, td.platform)) |
| 95 | + }), |
| 96 | + }, |
| 97 | + { |
| 98 | + Description: "tag-verbose", |
| 99 | + Command: test.Command("manifest", "inspect", td.imageRef, "--verbose"), |
| 100 | + Expected: test.Expects(0, nil, func(stdout string, t tig.T) { |
| 101 | + var entries []manifesttypes.DockerManifestEntry |
| 102 | + assert.NilError(t, json.Unmarshal([]byte(stdout), &entries)) |
| 103 | + assert.Assert(t, len(entries) > 0) |
| 104 | + |
| 105 | + var foundEntry *manifesttypes.DockerManifestEntry |
| 106 | + for _, e := range entries { |
| 107 | + if td.isAmd64Platform(e.Descriptor.Platform) { |
| 108 | + foundEntry = &e |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | + assert.Assert(t, foundEntry != nil, "should find amd64 platform entry") |
| 113 | + |
| 114 | + expectedRef := td.imageRef + "@" + td.manifestDigest |
| 115 | + assert.Equal(t, foundEntry.Ref, expectedRef) |
| 116 | + assert.Equal(t, foundEntry.Descriptor.Digest.String(), td.manifestDigest) |
| 117 | + assert.Equal(t, foundEntry.Descriptor.MediaType, testutil.GetTestImagePlatformMediaType(td.imageName, td.platform)) |
| 118 | + assert.Equal(t, foundEntry.Raw, td.rawData) |
| 119 | + }), |
| 120 | + }, |
| 121 | + { |
| 122 | + Description: "digest-non-verbose", |
| 123 | + Command: test.Command("manifest", "inspect", td.imageWithDigest()), |
| 124 | + Expected: test.Expects(0, nil, func(stdout string, t tig.T) { |
| 125 | + var manifest manifesttypes.DockerManifestStruct |
| 126 | + assert.NilError(t, json.Unmarshal([]byte(stdout), &manifest)) |
| 127 | + |
| 128 | + assert.Equal(t, manifest.SchemaVersion, testutil.GetTestImageSchemaVersion(td.imageName)) |
| 129 | + assert.Equal(t, manifest.MediaType, testutil.GetTestImagePlatformMediaType(td.imageName, td.platform)) |
| 130 | + assert.Equal(t, manifest.Config.Digest.String(), td.configDigest) |
| 131 | + }), |
| 132 | + }, |
| 133 | + { |
| 134 | + Description: "digest-verbose", |
| 135 | + Command: test.Command("manifest", "inspect", td.imageWithDigest(), "--verbose"), |
| 136 | + Expected: test.Expects(0, nil, func(stdout string, t tig.T) { |
| 137 | + var entry manifesttypes.DockerManifestEntry |
| 138 | + assert.NilError(t, json.Unmarshal([]byte(stdout), &entry)) |
| 139 | + |
| 140 | + assert.Equal(t, entry.Ref, td.imageWithDigest()) |
| 141 | + assert.Equal(t, entry.Descriptor.Digest.String(), td.manifestDigest) |
| 142 | + assert.Equal(t, entry.Descriptor.MediaType, testutil.GetTestImagePlatformMediaType(td.imageName, td.platform)) |
| 143 | + assert.Equal(t, entry.Raw, td.rawData) |
| 144 | + }), |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + testCase.Run(t) |
| 149 | +} |
0 commit comments