Skip to content

Commit a7a7972

Browse files
committed
test: add namespace inspect test cases
Signed-off-by: Park jungtae <jtpark1957@gmail.com>
1 parent 5ea5532 commit a7a7972

File tree

1 file changed

+155
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)