summaryrefslogtreecommitdiff
path: root/test/e2e/checkpoint_image_test.go
blob: 94320a70ec31f92f2893730f2cc5b427482f19e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package integration

import (
	"os"
	"os/exec"
	"strconv"
	"strings"

	"github.com/containers/podman/v4/pkg/criu"
	. "github.com/containers/podman/v4/test/utils"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gexec"
)

var _ = Describe("Podman checkpoint", func() {
	var (
		tempdir    string
		err        error
		podmanTest *PodmanTestIntegration
	)

	BeforeEach(func() {
		SkipIfRootless("checkpoint not supported in rootless mode")
		tempdir, err = CreateTempDirInTempDir()
		if err != nil {
			os.Exit(1)
		}
		podmanTest = PodmanTestCreate(tempdir)
		podmanTest.Setup()
		// Check if the runtime implements checkpointing. Currently only
		// runc's checkpoint/restore implementation is supported.
		cmd := exec.Command(podmanTest.OCIRuntime, "checkpoint", "--help")
		if err := cmd.Start(); err != nil {
			Skip("OCI runtime does not support checkpoint/restore")
		}
		if err := cmd.Wait(); err != nil {
			Skip("OCI runtime does not support checkpoint/restore")
		}

		if !criu.CheckForCriu(criu.MinCriuVersion) {
			Skip("CRIU is missing or too old.")
		}
	})

	AfterEach(func() {
		podmanTest.Cleanup()
		f := CurrentGinkgoTestDescription()
		processTestResult(f)
	})

	It("podman checkpoint --create-image with bogus container", func() {
		checkpointImage := "foobar-checkpoint"
		session := podmanTest.Podman([]string{"container", "checkpoint", "--create-image", checkpointImage, "foobar"})
		session.WaitWithDefaultTimeout()
		Expect(session).To(ExitWithError())
		Expect(session.ErrorToString()).To(ContainSubstring("no container with name or ID \"foobar\" found"))
	})

	It("podman checkpoint --create-image with running container", func() {
		// Container image must be lowercase
		checkpointImage := "alpine-checkpoint-" + strings.ToLower(RandomString(6))
		containerName := "alpine-container-" + RandomString(6)

		localRunString := []string{
			"run",
			"-it",
			"-d",
			"--ip", GetRandomIPAddress(),
			"--name", containerName,
			ALPINE,
			"top",
		}
		session := podmanTest.Podman(localRunString)
		session.WaitWithDefaultTimeout()
		Expect(session).Should(Exit(0))
		containerID := session.OutputToString()

		// Checkpoint image should not exist
		session = podmanTest.Podman([]string{"images"})
		session.WaitWithDefaultTimeout()
		Expect(session).Should(Exit(0))
		Expect(session.LineInOutputContainsTag("localhost/"+checkpointImage, "latest")).To(BeFalse())

		// Check if none of the checkpoint/restore specific information is displayed
		// for newly started containers.
		inspect := podmanTest.Podman([]string{"inspect", containerID})
		inspect.WaitWithDefaultTimeout()
		Expect(inspect).Should(Exit(0))
		inspectOut := inspect.InspectContainerToJSON()
		Expect(inspectOut[0].State.Checkpointed).To(BeFalse(), ".State.Checkpointed")
		Expect(inspectOut[0].State.Restored).To(BeFalse(), ".State.Restored")
		Expect(inspectOut[0].State).To(HaveField("CheckpointPath", ""))
		Expect(inspectOut[0].State).To(HaveField("CheckpointLog", ""))
		Expect(inspectOut[0].State).To(HaveField("RestoreLog", ""))

		result := podmanTest.Podman([]string{"container", "checkpoint", "--create-image", checkpointImage, "--keep", containerID})
		result.WaitWithDefaultTimeout()

		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
		Expect(podmanTest.GetContainerStatus()).To(ContainSubstring("Exited"))

		inspect = podmanTest.Podman([]string{"inspect", containerID})
		inspect.WaitWithDefaultTimeout()
		Expect(inspect).Should(Exit(0))
		inspectOut = inspect.InspectContainerToJSON()
		Expect(inspectOut[0].State.Checkpointed).To(BeTrue(), ".State.Checkpointed")
		Expect(inspectOut[0].State.CheckpointPath).To(ContainSubstring("userdata/checkpoint"))
		Expect(inspectOut[0].State.CheckpointLog).To(ContainSubstring("userdata/dump.log"))

		// Check if checkpoint image has been created
		session = podmanTest.Podman([]string{"images"})
		session.WaitWithDefaultTimeout()
		Expect(session).Should(Exit(0))
		Expect(session.LineInOutputContainsTag("localhost/"+checkpointImage, "latest")).To(BeTrue())

		// Check if the checkpoint image contains annotations
		inspect = podmanTest.Podman([]string{"inspect", checkpointImage})
		inspect.WaitWithDefaultTimeout()
		Expect(inspect).Should(Exit(0))
		inspectImageOut := inspect.InspectImageJSON()
		Expect(inspectImageOut[0].Annotations["io.podman.annotations.checkpoint.name"]).To(
			BeEquivalentTo(containerName),
			"io.podman.annotations.checkpoint.name",
		)

		ociRuntimeName := ""
		if strings.Contains(podmanTest.OCIRuntime, "runc") {
			ociRuntimeName = "runc"
		} else if strings.Contains(podmanTest.OCIRuntime, "crun") {
			ociRuntimeName = "crun"
		}
		if ociRuntimeName != "" {
			Expect(inspectImageOut[0].Annotations["io.podman.annotations.checkpoint.runtime.name"]).To(
				BeEquivalentTo(ociRuntimeName),
				"io.podman.annotations.checkpoint.runtime.name",
			)
		}

		// Remove existing container
		result = podmanTest.Podman([]string{"rm", "-t", "1", "-f", containerID})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))

		// Restore container from checkpoint image
		result = podmanTest.Podman([]string{"container", "restore", checkpointImage})
		result.WaitWithDefaultTimeout()

		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(1))
		Expect(podmanTest.GetContainerStatus()).To(ContainSubstring("Up"))

		// Clean-up
		result = podmanTest.Podman([]string{"rm", "-t", "0", "-fa"})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))

		result = podmanTest.Podman([]string{"rmi", checkpointImage})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
	})

	It("podman restore multiple containers from single checkpint image", func() {
		// Container image must be lowercase
		checkpointImage := "alpine-checkpoint-" + strings.ToLower(RandomString(6))
		containerName := "alpine-container-" + RandomString(6)

		localRunString := []string{"run", "-d", "--name", containerName, ALPINE, "top"}
		session := podmanTest.Podman(localRunString)
		session.WaitWithDefaultTimeout()
		Expect(session).Should(Exit(0))
		containerID := session.OutputToString()

		// Checkpoint image should not exist
		session = podmanTest.Podman([]string{"images"})
		session.WaitWithDefaultTimeout()
		Expect(session).Should(Exit(0))
		Expect(session.LineInOutputContainsTag("localhost/"+checkpointImage, "latest")).To(BeFalse())

		result := podmanTest.Podman([]string{"container", "checkpoint", "--create-image", checkpointImage, "--keep", containerID})
		result.WaitWithDefaultTimeout()

		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
		Expect(podmanTest.GetContainerStatus()).To(ContainSubstring("Exited"))

		// Check if checkpoint image has been created
		session = podmanTest.Podman([]string{"images"})
		session.WaitWithDefaultTimeout()
		Expect(session).Should(Exit(0))
		Expect(session.LineInOutputContainsTag("localhost/"+checkpointImage, "latest")).To(BeTrue())

		// Remove existing container
		result = podmanTest.Podman([]string{"rm", "-t", "1", "-f", containerID})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))

		for i := 1; i < 5; i++ {
			// Restore container from checkpoint image
			name := containerName + strconv.Itoa(i)
			result = podmanTest.Podman([]string{"container", "restore", "--name", name, checkpointImage})
			result.WaitWithDefaultTimeout()
			Expect(result).Should(Exit(0))
			Expect(podmanTest.NumberOfContainersRunning()).To(Equal(i))

			// Check that the container is running
			status := podmanTest.Podman([]string{"inspect", name, "--format={{.State.Status}}"})
			status.WaitWithDefaultTimeout()
			Expect(status).Should(Exit(0))
			Expect(status.OutputToString()).To(Equal("running"))
		}

		// Clean-up
		result = podmanTest.Podman([]string{"rm", "-t", "0", "-fa"})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))

		result = podmanTest.Podman([]string{"rmi", checkpointImage})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
	})

	It("podman restore multiple containers from multiple checkpint images", func() {
		// Container image must be lowercase
		checkpointImage1 := "alpine-checkpoint-" + strings.ToLower(RandomString(6))
		checkpointImage2 := "alpine-checkpoint-" + strings.ToLower(RandomString(6))
		containerName1 := "alpine-container-" + RandomString(6)
		containerName2 := "alpine-container-" + RandomString(6)

		// Create first container
		localRunString := []string{"run", "-d", "--name", containerName1, ALPINE, "top"}
		session := podmanTest.Podman(localRunString)
		session.WaitWithDefaultTimeout()
		Expect(session).Should(Exit(0))
		containerID1 := session.OutputToString()

		// Create second container
		localRunString = []string{"run", "-d", "--name", containerName2, ALPINE, "top"}
		session = podmanTest.Podman(localRunString)
		session.WaitWithDefaultTimeout()
		Expect(session).Should(Exit(0))
		containerID2 := session.OutputToString()

		// Checkpoint first container
		result := podmanTest.Podman([]string{"container", "checkpoint", "--create-image", checkpointImage1, "--keep", containerID1})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(1))

		// Checkpoint second container
		result = podmanTest.Podman([]string{"container", "checkpoint", "--create-image", checkpointImage2, "--keep", containerID2})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))

		// Remove existing containers
		result = podmanTest.Podman([]string{"rm", "-t", "1", "-f", containerName1, containerName2})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))

		// Restore both containers from images
		result = podmanTest.Podman([]string{"container", "restore", checkpointImage1, checkpointImage2})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(2))

		// Check if first container is running
		status := podmanTest.Podman([]string{"inspect", containerName1, "--format={{.State.Status}}"})
		status.WaitWithDefaultTimeout()
		Expect(status).Should(Exit(0))
		Expect(status.OutputToString()).To(Equal("running"))

		// Check if second container is running
		status = podmanTest.Podman([]string{"inspect", containerName2, "--format={{.State.Status}}"})
		status.WaitWithDefaultTimeout()
		Expect(status).Should(Exit(0))
		Expect(status.OutputToString()).To(Equal("running"))

		// Clean-up
		result = podmanTest.Podman([]string{"rm", "-t", "0", "-fa"})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))

		result = podmanTest.Podman([]string{"rmi", checkpointImage1, checkpointImage2})
		result.WaitWithDefaultTimeout()
		Expect(result).Should(Exit(0))
		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
	})
})