|
| 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 | + Setup: func(data test.Data, helpers test.Helpers) { |
| 95 | + targetRef := fmt.Sprintf("%s:%d/%s", |
| 96 | + registryTokenAuthHTTPSRandom.IP.String(), registryTokenAuthHTTPSRandom.Port, "test-list-push:v1") |
| 97 | + helpers.Ensure("pull", manifestRef) |
| 98 | + helpers.Ensure("tag", manifestRef, targetRef) |
| 99 | + helpers.Ensure("--hosts-dir", registryTokenAuthHTTPSRandom.HostsDir, "login", "-u", "admin", "-p", "badmin", |
| 100 | + fmt.Sprintf("%s:%d", registryTokenAuthHTTPSRandom.IP.String(), registryTokenAuthHTTPSRandom.Port)) |
| 101 | + helpers.Ensure("push", "--hosts-dir", registryTokenAuthHTTPSRandom.HostsDir, targetRef) |
| 102 | + helpers.Ensure("rmi", targetRef) |
| 103 | + helpers.Ensure("manifest", "create", "--insecure", targetRef+"-success", targetRef) |
| 104 | + }, |
| 105 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 106 | + targetRef := fmt.Sprintf("%s:%d/%s", |
| 107 | + registryTokenAuthHTTPSRandom.IP.String(), registryTokenAuthHTTPSRandom.Port, "test-list-push:v1") |
| 108 | + return helpers.Command("manifest", "push", "--insecure", targetRef+"-success") |
| 109 | + }, |
| 110 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 111 | + return &test.Expected{ |
| 112 | + ExitCode: 0, |
| 113 | + Output: expect.Contains(data.Labels().Get("output")), |
| 114 | + } |
| 115 | + }, |
| 116 | + Data: test.WithLabels(map[string]string{ |
| 117 | + "output": expectedDigest, |
| 118 | + }), |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | + testCase.Run(t) |
| 123 | +} |
0 commit comments