|
| 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 namespace |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "gotest.tools/v3/assert" |
| 25 | + |
| 26 | + "github.com/containerd/nerdctl/mod/tigron/expect" |
| 27 | + "github.com/containerd/nerdctl/mod/tigron/require" |
| 28 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 29 | + "github.com/containerd/nerdctl/mod/tigron/tig" |
| 30 | + |
| 31 | + "github.com/containerd/nerdctl/v2/pkg/inspecttypes/native" |
| 32 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 33 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 34 | +) |
| 35 | + |
| 36 | +func TestNamespaceInspect(t *testing.T) { |
| 37 | + testCase := nerdtest.Setup() |
| 38 | + testCase.Require = require.Not(nerdtest.Docker) |
| 39 | + |
| 40 | + testCase.Setup = func(data test.Data, helpers test.Helpers) { |
| 41 | + helpers.Ensure("namespace", "create", data.Identifier("first")) |
| 42 | + helpers.Ensure("namespace", "create", "--label", "foo=fooval", "--label", "bar=barval", data.Identifier("second")) |
| 43 | + |
| 44 | + // Create some resources in the namespaces |
| 45 | + helpers.Ensure("--namespace", data.Identifier("first"), "run", "-d", "--name", data.Identifier("container1"), testutil.CommonImage) |
| 46 | + helpers.Ensure("--namespace", data.Identifier("first"), "run", "-d", "--name", data.Identifier("container2"), testutil.CommonImage) |
| 47 | + helpers.Ensure("--namespace", data.Identifier("second"), "run", "-d", "--name", data.Identifier("container3"), testutil.CommonImage) |
| 48 | + // Create a volume |
| 49 | + helpers.Ensure("--namespace", data.Identifier("first"), "volume", "create", data.Identifier("volume1")) |
| 50 | + |
| 51 | + data.Labels().Set("ns1", data.Identifier("first")) |
| 52 | + data.Labels().Set("ns2", data.Identifier("second")) |
| 53 | + data.Labels().Set("container1", data.Identifier("container1")) |
| 54 | + data.Labels().Set("container2", data.Identifier("container2")) |
| 55 | + data.Labels().Set("container3", data.Identifier("container3")) |
| 56 | + data.Labels().Set("volume1", data.Identifier("volume1")) |
| 57 | + } |
| 58 | + |
| 59 | + testCase.Cleanup = func(data test.Data, helpers test.Helpers) { |
| 60 | + helpers.Anyhow("--namespace", data.Identifier("first"), "image", "rm", "-f", testutil.CommonImage) |
| 61 | + helpers.Anyhow("--namespace", data.Identifier("second"), "image", "rm", "-f", testutil.CommonImage) |
| 62 | + helpers.Anyhow("--namespace", data.Identifier("first"), "rm", "-f", data.Identifier("container1")) |
| 63 | + helpers.Anyhow("--namespace", data.Identifier("first"), "rm", "-f", data.Identifier("container2")) |
| 64 | + helpers.Anyhow("--namespace", data.Identifier("second"), "rm", "-f", data.Identifier("container3")) |
| 65 | + helpers.Anyhow("--namespace", data.Identifier("first"), "volume", "rm", "-f", data.Identifier("volume1")) |
| 66 | + helpers.Anyhow("namespace", "remove", data.Identifier("first")) |
| 67 | + helpers.Anyhow("namespace", "remove", data.Identifier("second")) |
| 68 | + } |
| 69 | + |
| 70 | + testCase.SubTests = []*test.Case{ |
| 71 | + { |
| 72 | + Description: "arg missing should fail", |
| 73 | + Command: test.Command("namespace", "inspect"), |
| 74 | + Expected: test.Expects(1, []error{errors.New("requires at least 1 arg")}, nil), |
| 75 | + }, |
| 76 | + { |
| 77 | + Description: "non existent namespace returns empty array", |
| 78 | + Command: test.Command("namespace", "inspect", "doesnotexist"), |
| 79 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 80 | + return &test.Expected{ |
| 81 | + Output: expect.All( |
| 82 | + expect.JSON([]native.Namespace{}, func(dc []native.Namespace, t tig.T) { |
| 83 | + assert.Assert(t, len(dc) == 0, "expected empty array") |
| 84 | + }), |
| 85 | + ), |
| 86 | + } |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + Description: "inspect labels", |
| 91 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 92 | + return helpers.Command("namespace", "inspect", data.Labels().Get("ns2")) |
| 93 | + }, |
| 94 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 95 | + return &test.Expected{ |
| 96 | + Output: expect.All( |
| 97 | + expect.Contains(data.Labels().Get("ns2")), |
| 98 | + expect.JSON([]native.Namespace{}, func(dc []native.Namespace, t tig.T) { |
| 99 | + labels := *dc[0].Labels |
| 100 | + assert.Assert(t, len(labels) == 2, fmt.Sprintf("two labels, not %d", len(labels))) |
| 101 | + assert.Assert(t, labels["foo"] == "fooval", |
| 102 | + fmt.Sprintf("label foo should be fooval, not %s", labels["foo"])) |
| 103 | + assert.Assert(t, labels["bar"] == "barval", |
| 104 | + fmt.Sprintf("label bar should be barval, not %s", labels["bar"])) |
| 105 | + }), |
| 106 | + ), |
| 107 | + } |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + Description: "inspect details single namespace", |
| 112 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 113 | + return helpers.Command("namespace", "inspect", data.Labels().Get("ns1")) |
| 114 | + }, |
| 115 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 116 | + return &test.Expected{ |
| 117 | + Output: expect.All( |
| 118 | + expect.Contains(data.Labels().Get("ns1")), |
| 119 | + expect.JSON([]native.Namespace{}, func(dc []native.Namespace, t tig.T) { |
| 120 | + assert.Assert(t, len(dc[0].Volumes.Names) == 1, fmt.Sprintf("expected 1 volume name (was %d)", len(dc[0].Volumes.Names))) |
| 121 | + assert.Assert(t, dc[0].Containers.Count == 2, fmt.Sprintf("expected 2 container (was %d)", dc[0].Containers.Count)) |
| 122 | + assert.Assert(t, len(dc[0].Images.IDs) == 1, fmt.Sprintf("expected 1 image (was %d)", dc[0].Images.Count)) |
| 123 | + }), |
| 124 | + ), |
| 125 | + } |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + Description: "inspect details both namespaces", |
| 130 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 131 | + return helpers.Command("namespace", "inspect", data.Labels().Get("ns1"), data.Labels().Get("ns2")) |
| 132 | + }, |
| 133 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 134 | + return &test.Expected{ |
| 135 | + Output: expect.All( |
| 136 | + expect.Contains(data.Labels().Get("ns1")), |
| 137 | + expect.JSON([]native.Namespace{}, func(dc []native.Namespace, t tig.T) { |
| 138 | + assert.Assert(t, len(dc[0].Volumes.Names) == 1, fmt.Sprintf("expected 1 volume (was %d)", len(dc[0].Volumes.Names))) |
| 139 | + assert.Assert(t, dc[0].Containers.Count == 2, fmt.Sprintf("expected 2 container (was %d)", dc[0].Containers.Count)) |
| 140 | + assert.Assert(t, len(dc[0].Images.IDs) == 1, fmt.Sprintf("expected 1 image (was %d)", dc[0].Images.Count)) |
| 141 | + }), |
| 142 | + |
| 143 | + expect.Contains(data.Labels().Get("ns2")), |
| 144 | + expect.JSON([]native.Namespace{}, func(dc []native.Namespace, t tig.T) { |
| 145 | + assert.Assert(t, len(dc[1].Volumes.Names) == 0, fmt.Sprintf("expected 0 volume (was %d)", len(dc[1].Volumes.Names))) |
| 146 | + assert.Assert(t, dc[1].Containers.Count == 1, fmt.Sprintf("expected 1 container (was %d)", dc[1].Containers.Count)) |
| 147 | + assert.Assert(t, dc[1].Images.Count == 1, fmt.Sprintf("expected 1 image (was %d)", dc[1].Images.Count)) |
| 148 | + }), |
| 149 | + ), |
| 150 | + } |
| 151 | + }, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + testCase.Run(t) |
| 156 | +} |
0 commit comments