Skip to content

Commit 5724d89

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 1cf0f64 commit 5724d89

File tree

2 files changed

+140
-0
lines changed

2 files changed

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