Skip to content

Commit 6b3d661

Browse files
committed
Container create tests cleanup
Signed-off-by: apostasie <[email protected]>
1 parent acbec64 commit 6b3d661

File tree

3 files changed

+127
-71
lines changed

3 files changed

+127
-71
lines changed

cmd/nerdctl/container/container_create_linux_test.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@ import (
3636
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
3737
)
3838

39-
func TestCreate(t *testing.T) {
40-
t.Parallel()
41-
base := testutil.NewBase(t)
42-
tID := testutil.Identifier(t)
43-
44-
base.Cmd("create", "--name", tID, testutil.CommonImage, "echo", "foo").AssertOK()
45-
defer base.Cmd("rm", "-f", tID).Run()
46-
base.Cmd("ps", "-a").AssertOutContains("Created")
47-
base.Cmd("start", tID).AssertOK()
48-
base.Cmd("logs", tID).AssertOutContains("foo")
49-
}
50-
5139
func TestCreateWithLabel(t *testing.T) {
5240
t.Parallel()
5341
base := testutil.NewBase(t)
@@ -217,8 +205,7 @@ func TestIssue2993(t *testing.T) {
217205
h := getAddrHash(defaults.DefaultAddress)
218206
dataStore := filepath.Join(dataRoot, h)
219207

220-
// FIXME: update with next tooling iteration to retrieve from the command
221-
namespace := "nerdctl-test"
208+
namespace := string(helpers.Read(nerdtest.Namespace))
222209

223210
containersPath := filepath.Join(dataStore, "containers", namespace)
224211
containersDirs, err := os.ReadDir(containersPath)
@@ -265,8 +252,7 @@ func TestIssue2993(t *testing.T) {
265252
h := getAddrHash(defaults.DefaultAddress)
266253
dataStore := filepath.Join(dataRoot, h)
267254

268-
// FIXME: update with next tooling iteration to retrieve from the command
269-
namespace := "nerdctl-test"
255+
namespace := string(helpers.Read(nerdtest.Namespace))
270256

271257
containersPath := filepath.Join(dataStore, "containers", namespace)
272258
containersDirs, err := os.ReadDir(containersPath)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 container
18+
19+
import (
20+
"encoding/json"
21+
"testing"
22+
"time"
23+
24+
"gotest.tools/v3/assert"
25+
26+
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
27+
"github.com/containerd/nerdctl/v2/pkg/testutil"
28+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
29+
"github.com/containerd/nerdctl/v2/pkg/testutil/test"
30+
)
31+
32+
func TestCreate(t *testing.T) {
33+
testCase := nerdtest.Setup()
34+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
35+
helpers.Ensure("create", "--name", data.Identifier("container"), testutil.CommonImage, "echo", "foo")
36+
data.Set("cID", data.Identifier("container"))
37+
}
38+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
39+
helpers.Anyhow("rm", "-f", data.Identifier("container"))
40+
}
41+
42+
testCase.SubTests = []*test.Case{
43+
{
44+
Description: "ps -a",
45+
NoParallel: true,
46+
Command: test.Command("ps", "-a"),
47+
// FIXME: this might get a false positive if other tests have created a container
48+
Expected: test.Expects(0, nil, test.Contains("Created")),
49+
},
50+
{
51+
Description: "start",
52+
NoParallel: true,
53+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
54+
return helpers.Command("start", data.Get("cID"))
55+
},
56+
Expected: test.Expects(0, nil, nil),
57+
},
58+
{
59+
Description: "logs",
60+
NoParallel: true,
61+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
62+
return helpers.Command("logs", data.Get("cID"))
63+
},
64+
Expected: test.Expects(0, nil, test.Contains("foo")),
65+
},
66+
}
67+
68+
testCase.Run(t)
69+
}
70+
71+
func TestCreateHyperVContainer(t *testing.T) {
72+
testCase := nerdtest.Setup()
73+
74+
testCase.Require = nerdtest.HyperV
75+
76+
testCase.Setup = func(data test.Data, helpers test.Helpers) {
77+
helpers.Ensure("create", "--isolation", "hyperv", "--name", data.Identifier("container"), testutil.CommonImage, "echo", "foo")
78+
data.Set("cID", data.Identifier("container"))
79+
}
80+
81+
testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
82+
helpers.Anyhow("rm", "-f", data.Identifier("container"))
83+
}
84+
85+
testCase.SubTests = []*test.Case{
86+
{
87+
Description: "ps -a",
88+
NoParallel: true,
89+
Command: test.Command("ps", "-a"),
90+
// FIXME: this might get a false positive if other tests have created a container
91+
Expected: test.Expects(0, nil, test.Contains("Created")),
92+
},
93+
{
94+
Description: "start",
95+
NoParallel: true,
96+
Setup: func(data test.Data, helpers test.Helpers) {
97+
helpers.Ensure("start", data.Get("cID"))
98+
ran := false
99+
for i := 0; i < 10 && !ran; i++ {
100+
helpers.Command("container", "inspect", data.Get("cID")).
101+
Run(&test.Expected{
102+
ExitCode: test.ExitCodeNoCheck,
103+
Output: func(stdout string, info string, t *testing.T) {
104+
var dc []dockercompat.Container
105+
err := json.Unmarshal([]byte(stdout), &dc)
106+
if err != nil || len(dc) == 0 {
107+
return
108+
}
109+
assert.Equal(t, len(dc), 1, "Unexpectedly got multiple results\n"+info)
110+
ran = dc[0].State.Status == "exited"
111+
},
112+
})
113+
time.Sleep(time.Second)
114+
}
115+
assert.Assert(t, ran, "container did not ran after 10 seconds")
116+
},
117+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
118+
return helpers.Command("logs", data.Get("cID"))
119+
},
120+
Expected: test.Expects(0, nil, test.Contains("foo")),
121+
},
122+
}
123+
124+
testCase.Run(t)
125+
}

cmd/nerdctl/container/container_create_windows_test.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)