Skip to content

Commit 8acb15a

Browse files
committed
manifest: add unit tests for nerdctl manifest push command
add unit tests for nerdctl manifest push command Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 8c80d4f commit 8acb15a

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
"errors"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/containerd/nerdctl/mod/tigron/expect"
25+
"github.com/containerd/nerdctl/mod/tigron/require"
26+
"github.com/containerd/nerdctl/mod/tigron/test"
27+
28+
"github.com/containerd/nerdctl/v2/pkg/testutil"
29+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
30+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest/registry"
31+
)
32+
33+
func TestManifestPushErrors(t *testing.T) {
34+
testCase := nerdtest.Setup()
35+
invalidName := "invalid/name/with/special@chars"
36+
testCase.SubTests = []*test.Case{
37+
{
38+
Description: "require-one-argument",
39+
Command: test.Command("manifest", "push", "arg1", "arg2"),
40+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
41+
return &test.Expected{
42+
ExitCode: 1,
43+
}
44+
},
45+
},
46+
{
47+
Description: "invalid-list-name",
48+
Command: test.Command("manifest", "push", invalidName),
49+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
50+
return &test.Expected{
51+
ExitCode: 1,
52+
Errors: []error{errors.New(data.Labels().Get("error"))},
53+
}
54+
},
55+
Data: test.WithLabels(map[string]string{
56+
"error": "invalid reference format",
57+
}),
58+
},
59+
}
60+
61+
testCase.Run(t)
62+
}
63+
64+
func TestManifestPush(t *testing.T) {
65+
nerdtest.Setup()
66+
67+
var registryTokenAuthHTTPSRandom *registry.Server
68+
var tokenServer *registry.TokenAuthServer
69+
70+
manifestRef := testutil.GetTestImageWithoutTag("alpine") + "@" + testutil.GetTestImageManifestDigest("alpine", "linux/amd64")
71+
expectedDigest := "sha256:5317ce2da263afa23570c692d62c1b01381285b2198b3ea9739ce64bec22aff2"
72+
73+
testCase := &test.Case{
74+
Require: require.All(
75+
require.Linux,
76+
nerdtest.Registry,
77+
),
78+
Setup: func(data test.Data, helpers test.Helpers) {
79+
registryTokenAuthHTTPSRandom, tokenServer = nerdtest.RegistryWithTokenAuth(data, helpers, "admin", "badmin", 0, true)
80+
tokenServer.Setup(data, helpers)
81+
registryTokenAuthHTTPSRandom.Setup(data, helpers)
82+
},
83+
Cleanup: func(data test.Data, helpers test.Helpers) {
84+
if registryTokenAuthHTTPSRandom != nil {
85+
registryTokenAuthHTTPSRandom.Cleanup(data, helpers)
86+
}
87+
if tokenServer != nil {
88+
tokenServer.Cleanup(data, helpers)
89+
}
90+
},
91+
SubTests: []*test.Case{
92+
{
93+
Description: "push-to-registry",
94+
Require: require.Not(nerdtest.Docker),
95+
Setup: func(data test.Data, helpers test.Helpers) {
96+
targetRef := fmt.Sprintf("%s:%d/%s",
97+
registryTokenAuthHTTPSRandom.IP.String(), registryTokenAuthHTTPSRandom.Port, "test-list-push:v1")
98+
helpers.Ensure("pull", manifestRef)
99+
helpers.Ensure("tag", manifestRef, targetRef)
100+
helpers.Ensure("--hosts-dir", registryTokenAuthHTTPSRandom.HostsDir, "login", "-u", "admin", "-p", "badmin",
101+
fmt.Sprintf("%s:%d", registryTokenAuthHTTPSRandom.IP.String(), registryTokenAuthHTTPSRandom.Port))
102+
helpers.Ensure("push", "--hosts-dir", registryTokenAuthHTTPSRandom.HostsDir, targetRef)
103+
helpers.Ensure("rmi", targetRef)
104+
helpers.Ensure("manifest", "create", "--insecure", targetRef+"-success", targetRef)
105+
},
106+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
107+
targetRef := fmt.Sprintf("%s:%d/%s",
108+
registryTokenAuthHTTPSRandom.IP.String(), registryTokenAuthHTTPSRandom.Port, "test-list-push:v1")
109+
return helpers.Command("manifest", "push", "--insecure", targetRef+"-success")
110+
},
111+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
112+
return &test.Expected{
113+
ExitCode: 0,
114+
Output: expect.Contains(data.Labels().Get("output")),
115+
}
116+
},
117+
Data: test.WithLabels(map[string]string{
118+
"output": expectedDigest,
119+
}),
120+
},
121+
},
122+
}
123+
testCase.Run(t)
124+
}

0 commit comments

Comments
 (0)