|
| 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 | + "testing" |
| 22 | + |
| 23 | + "github.com/containerd/nerdctl/mod/tigron/expect" |
| 24 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 25 | + |
| 26 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 27 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 28 | +) |
| 29 | + |
| 30 | +func TestManifestCreateErrors(t *testing.T) { |
| 31 | + testCase := nerdtest.Setup() |
| 32 | + manifestListName := "test-list:v1" |
| 33 | + manifestName := "example.com/alpine:latest" |
| 34 | + invalidName := "invalid/name/with/special@chars" |
| 35 | + testCase.SubTests = []*test.Case{ |
| 36 | + { |
| 37 | + Description: "too-few-arguments", |
| 38 | + Command: test.Command("manifest", "create", manifestListName), |
| 39 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 40 | + return &test.Expected{ |
| 41 | + ExitCode: 1, |
| 42 | + Errors: []error{errors.New(data.Labels().Get("error"))}, |
| 43 | + } |
| 44 | + }, |
| 45 | + Data: test.WithLabels(map[string]string{ |
| 46 | + "error": "requires at least 2 arg", |
| 47 | + }), |
| 48 | + }, |
| 49 | + { |
| 50 | + Description: "invalid-list-name", |
| 51 | + Command: test.Command("manifest", "create", invalidName, manifestName), |
| 52 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 53 | + return &test.Expected{ |
| 54 | + ExitCode: 1, |
| 55 | + Errors: []error{errors.New(data.Labels().Get("error"))}, |
| 56 | + } |
| 57 | + }, |
| 58 | + Data: test.WithLabels(map[string]string{ |
| 59 | + "error": "invalid reference format", |
| 60 | + }), |
| 61 | + }, |
| 62 | + { |
| 63 | + Description: "invalid-manifest-reference", |
| 64 | + Command: test.Command("manifest", "create", manifestListName, invalidName), |
| 65 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 66 | + return &test.Expected{ |
| 67 | + ExitCode: 1, |
| 68 | + Errors: []error{errors.New(data.Labels().Get("error"))}, |
| 69 | + } |
| 70 | + }, |
| 71 | + Data: test.WithLabels(map[string]string{ |
| 72 | + "error": "invalid reference format", |
| 73 | + }), |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + testCase.Run(t) |
| 78 | +} |
| 79 | + |
| 80 | +func TestManifestCreate(t *testing.T) { |
| 81 | + testCase := nerdtest.Setup() |
| 82 | + manifestListName := "test-list-create:v1" |
| 83 | + manifestRef := testutil.GetTestImageWithoutTag("alpine") + "@" + testutil.GetTestImageManifestDigest("alpine", "linux/amd64") |
| 84 | + testCase.SubTests = []*test.Case{ |
| 85 | + { |
| 86 | + Description: "create-manifest-list", |
| 87 | + Command: test.Command("manifest", "create", manifestListName, manifestRef), |
| 88 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 89 | + return &test.Expected{ |
| 90 | + ExitCode: 0, |
| 91 | + Output: expect.Contains(data.Labels().Get("output")), |
| 92 | + } |
| 93 | + }, |
| 94 | + Data: test.WithLabels(map[string]string{ |
| 95 | + "output": "Created manifest list ", |
| 96 | + }), |
| 97 | + }, |
| 98 | + { |
| 99 | + Description: "create-existed-manifest-list-without-amend-flag", |
| 100 | + Setup: func(data test.Data, helpers test.Helpers) { |
| 101 | + cmd := helpers.Command("manifest", "create", manifestListName+"-without-amend-flag", manifestRef) |
| 102 | + cmd.Run(&test.Expected{ExitCode: 0}) |
| 103 | + }, |
| 104 | + Command: test.Command("manifest", "create", manifestListName+"-without-amend-flag", manifestRef), |
| 105 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 106 | + return &test.Expected{ |
| 107 | + ExitCode: 1, |
| 108 | + Errors: []error{errors.New(data.Labels().Get("error"))}, |
| 109 | + } |
| 110 | + }, |
| 111 | + Data: test.WithLabels(map[string]string{ |
| 112 | + "error": "refusing to amend an existing manifest list with no --amend flag", |
| 113 | + }), |
| 114 | + }, |
| 115 | + { |
| 116 | + Description: "create-manifest-list-with-amend-flag", |
| 117 | + Setup: func(data test.Data, helpers test.Helpers) { |
| 118 | + cmd := helpers.Command("manifest", "create", manifestListName+"-with-amend-flag", manifestRef) |
| 119 | + cmd.Run(&test.Expected{ExitCode: 0}) |
| 120 | + }, |
| 121 | + Command: test.Command("manifest", "create", "--amend", manifestListName+"-with-amend-flag", manifestRef), |
| 122 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 123 | + return &test.Expected{ |
| 124 | + ExitCode: 0, |
| 125 | + Output: expect.Contains(data.Labels().Get("output")), |
| 126 | + } |
| 127 | + }, |
| 128 | + Data: test.WithLabels(map[string]string{ |
| 129 | + "output": "Created manifest list", |
| 130 | + }), |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + testCase.Run(t) |
| 135 | +} |
0 commit comments