Skip to content

Commit 8f8eaf0

Browse files
committed
checkpoint: add unit tests for checkpoint create command
add unit tests for checkpoint create command. Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent c8ddcd8 commit 8f8eaf0

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 checkpoint
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
"github.com/containerd/nerdctl/mod/tigron/expect"
24+
"github.com/containerd/nerdctl/mod/tigron/require"
25+
"github.com/containerd/nerdctl/mod/tigron/test"
26+
27+
"github.com/containerd/nerdctl/v2/pkg/testutil"
28+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
29+
)
30+
31+
func TestCheckpointCreateErrors(t *testing.T) {
32+
testCase := nerdtest.Setup()
33+
testCase.Require = require.Not(nerdtest.Rootless)
34+
testCase.SubTests = []*test.Case{
35+
{
36+
Description: "too-few-arguments",
37+
Command: test.Command("checkpoint", "create", "too-few-arguments"),
38+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
39+
return &test.Expected{
40+
ExitCode: 1,
41+
}
42+
},
43+
},
44+
{
45+
Description: "too-many-arguments",
46+
Command: test.Command("checkpoint", "create", "too", "many", "arguments"),
47+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
48+
return &test.Expected{
49+
ExitCode: 1,
50+
}
51+
},
52+
},
53+
{
54+
Description: "invalid-container-id",
55+
Command: test.Command("checkpoint", "create", "foo", "bar"),
56+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
57+
return &test.Expected{
58+
ExitCode: 1,
59+
Errors: []error{errors.New("error creating checkpoint for container: foo")},
60+
}
61+
},
62+
},
63+
}
64+
65+
testCase.Run(t)
66+
}
67+
68+
func TestCheckpointCreate(t *testing.T) {
69+
const (
70+
checkpointName = "checkpoint-bar"
71+
checkpointDir = "/dir/foo"
72+
)
73+
testCase := nerdtest.Setup()
74+
testCase.Require = require.Not(nerdtest.Rootless)
75+
testCase.SubTests = []*test.Case{
76+
{
77+
Description: "leave-running=true",
78+
Setup: func(data test.Data, helpers test.Helpers) {
79+
helpers.Ensure("run", "-d", "--name", data.Identifier("container-running"), testutil.CommonImage, "sleep", "infinity")
80+
},
81+
Cleanup: func(data test.Data, helpers test.Helpers) {
82+
helpers.Anyhow("rm", "-f", data.Identifier("container-running"))
83+
},
84+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
85+
return helpers.Command("checkpoint", "create", "--leave-running", "--checkpoint-dir", checkpointDir, data.Identifier("container-running"), checkpointName+"running")
86+
},
87+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
88+
return &test.Expected{
89+
ExitCode: 0,
90+
Output: expect.Equals(checkpointName + "running\n"),
91+
}
92+
},
93+
},
94+
{
95+
Description: "leave-running=false",
96+
Setup: func(data test.Data, helpers test.Helpers) {
97+
helpers.Ensure("run", "-d", "--name", data.Identifier("container-exit"), testutil.CommonImage, "sleep", "infinity")
98+
},
99+
Cleanup: func(data test.Data, helpers test.Helpers) {
100+
helpers.Anyhow("rm", "-f", data.Identifier("container-exit"))
101+
},
102+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
103+
return helpers.Command("checkpoint", "create", "--checkpoint-dir", checkpointDir, data.Identifier("container-exit"), checkpointName+"exit")
104+
},
105+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
106+
return &test.Expected{
107+
ExitCode: 0,
108+
Output: expect.Equals(checkpointName + "exit\n"),
109+
}
110+
},
111+
},
112+
}
113+
114+
testCase.Run(t)
115+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 checkpoint
18+
19+
import (
20+
"testing"
21+
22+
"github.com/containerd/nerdctl/v2/pkg/testutil"
23+
)
24+
25+
func TestMain(m *testing.M) {
26+
testutil.M(m)
27+
}

0 commit comments

Comments
 (0)