Skip to content

Commit 1d8c32c

Browse files
committed
checkpoint: add unit tests for checkpoint create command
add unit tests for checkpoint create command. Signed-off-by: ChengyuZhu6 <hudson@cyzhu.com>
1 parent bedf61a commit 1d8c32c

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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.Custom("rm", "-rf", checkpointDir).Run(&test.Expected{
81+
ExitCode: expect.ExitCodeSuccess,
82+
})
83+
helpers.Anyhow("rm", "-f", data.Identifier("container-running"))
84+
},
85+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
86+
return helpers.Command("checkpoint", "create", "--leave-running", "--checkpoint-dir", checkpointDir, data.Identifier("container-running"), checkpointName)
87+
},
88+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
89+
return &test.Expected{
90+
ExitCode: 0,
91+
Output: expect.Equals(checkpointName + "\n"),
92+
}
93+
},
94+
},
95+
{
96+
Description: "leave-running=false",
97+
Setup: func(data test.Data, helpers test.Helpers) {
98+
helpers.Ensure("run", "-d", "--name", data.Identifier("container-exit"), testutil.CommonImage, "sleep", "infinity")
99+
},
100+
Cleanup: func(data test.Data, helpers test.Helpers) {
101+
helpers.Custom("rm", "-rf", checkpointDir).Run(&test.Expected{
102+
ExitCode: expect.ExitCodeSuccess,
103+
})
104+
helpers.Anyhow("rm", "-f", data.Identifier("container-exit"))
105+
},
106+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
107+
return helpers.Command("checkpoint", "create", "--checkpoint-dir", checkpointDir, data.Identifier("container-exit"), checkpointName)
108+
},
109+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
110+
return &test.Expected{
111+
ExitCode: 0,
112+
Output: expect.Equals(checkpointName + "\n"),
113+
}
114+
},
115+
},
116+
}
117+
118+
testCase.Run(t)
119+
}
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)