Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions server/container_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
return "", errors.New(`attribute "image" missing from container definition`)
}

if createConfig.Metadata == nil && createConfig.Metadata.Name == "" {
if createConfig.Metadata == nil || createConfig.Metadata.Name == "" {
return "", errors.New(`attribute "metadata" missing from container definition`)
}

Expand Down Expand Up @@ -287,14 +287,16 @@

bindMountFound := false
for _, createMount := range createMounts {
if createMount.ContainerPath == m.Destination {
mount.HostPath = createMount.HostPath
mount.Readonly = createMount.Readonly
mount.RecursiveReadOnly = createMount.RecursiveReadOnly
mount.Propagation = createMount.Propagation
mount.RecursiveReadOnly = createMount.RecursiveReadOnly
bindMountFound = true
if createMount.ContainerPath != m.Destination {
continue

Check warning on line 291 in server/container_restore.go

View check run for this annotation

Codecov / codecov/patch

server/container_restore.go#L291

Added line #L291 was not covered by tests
}

bindMountFound = true
mount.HostPath = createMount.HostPath
mount.Readonly = createMount.Readonly
mount.RecursiveReadOnly = createMount.RecursiveReadOnly
mount.Propagation = createMount.Propagation
break
}
if !bindMountFound {
missingMount = append(missingMount, m.Destination)
Expand Down
66 changes: 14 additions & 52 deletions server/container_restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var _ = t.Describe("ContainerRestore", func() {
)

containerConfig := &types.ContainerConfig{
Metadata: &types.ContainerMetadata{Name: "name"},
Image: &types.ImageSpec{
Image: "does-not-exist.tar",
},
Expand All @@ -93,6 +94,7 @@ var _ = t.Describe("ContainerRestore", func() {
archive.Close()
defer os.RemoveAll("empty.tar")
containerConfig := &types.ContainerConfig{
Metadata: &types.ContainerMetadata{Name: "name"},
Image: &types.ImageSpec{
Image: "empty.tar",
},
Expand All @@ -115,6 +117,7 @@ var _ = t.Describe("ContainerRestore", func() {
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll("no.tar")
containerConfig := &types.ContainerConfig{
Metadata: &types.ContainerMetadata{Name: "name"},
Image: &types.ImageSpec{
Image: "no.tar",
},
Expand Down Expand Up @@ -149,6 +152,7 @@ var _ = t.Describe("ContainerRestore", func() {
_, err = io.Copy(outFile, input)
Expect(err).ToNot(HaveOccurred())
containerConfig := &types.ContainerConfig{
Metadata: &types.ContainerMetadata{Name: "name"},
Image: &types.ImageSpec{
Image: "archive.tar",
},
Expand Down Expand Up @@ -186,6 +190,7 @@ var _ = t.Describe("ContainerRestore", func() {
_, err = io.Copy(outFile, input)
Expect(err).ToNot(HaveOccurred())
containerConfig := &types.ContainerConfig{
Metadata: &types.ContainerMetadata{Name: "name"},
Image: &types.ImageSpec{
Image: "archive.tar",
},
Expand All @@ -199,7 +204,7 @@ var _ = t.Describe("ContainerRestore", func() {
)

// Then
Expect(err.Error()).To(ContainSubstring(`failed to read "io.kubernetes.cri-o.Metadata": unexpected end of JSON input`))
Expect(err.Error()).To(ContainSubstring(`failed to read "io.kubernetes.cri-o.Annotations": unexpected end of JSON input`))
})
})
t.Describe("ContainerRestore from archive into new pod", func() {
Expand All @@ -224,6 +229,7 @@ var _ = t.Describe("ContainerRestore", func() {
_, err = io.Copy(outFile, input)
Expect(err).ToNot(HaveOccurred())
containerConfig := &types.ContainerConfig{
Metadata: &types.ContainerMetadata{Name: "name"},
Image: &types.ImageSpec{
Image: "archive.tar",
},
Expand Down Expand Up @@ -269,6 +275,7 @@ var _ = t.Describe("ContainerRestore", func() {
_, err = io.Copy(outFile, input)
Expect(err).ToNot(HaveOccurred())
containerConfig := &types.ContainerConfig{
Metadata: &types.ContainerMetadata{Name: "name"},
Image: &types.ImageSpec{
Image: "archive.tar",
},
Expand Down Expand Up @@ -317,6 +324,7 @@ var _ = t.Describe("ContainerRestore", func() {
_, err = io.Copy(outFile, input)
Expect(err).ToNot(HaveOccurred())
containerConfig := &types.ContainerConfig{
Metadata: &types.ContainerMetadata{Name: "name"},
Image: &types.ImageSpec{
Image: "archive.tar",
},
Expand All @@ -334,57 +342,6 @@ var _ = t.Describe("ContainerRestore", func() {
Expect(err.Error()).To(Equal(`failed to read "io.kubernetes.cri-o.Annotations": unexpected end of JSON input`))
})
})
t.Describe("ContainerRestore from archive into new pod", func() {
It("should fail because archive contains no io.kubernetes.cri-o.Labels", func() {
// Given
addContainerAndSandbox()
testContainer.SetStateAndSpoofPid(&oci.ContainerState{
State: specs.State{Status: oci.ContainerStateRunning},
})

err := os.WriteFile(
"spec.dump",
[]byte(
`{"annotations":{"io.kubernetes.cri-o.Metadata"`+
`:"{\"name\":\"container-to-restore\"}",`+
`"io.kubernetes.cri-o.Annotations": "{\"name\":\"NAME\"}"}}`),
0o644,
)
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll("spec.dump")
err = os.WriteFile("config.dump", []byte(`{"rootfsImageName": "image"}`), 0o644)
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll("config.dump")
outFile, err := os.Create("archive.tar")
Expect(err).ToNot(HaveOccurred())
defer outFile.Close()
input, err := archive.TarWithOptions(".", &archive.TarOptions{
Compression: archive.Uncompressed,
IncludeSourceDir: true,
IncludeFiles: []string{"spec.dump", "config.dump"},
})
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll("archive.tar")
_, err = io.Copy(outFile, input)
Expect(err).ToNot(HaveOccurred())
containerConfig := &types.ContainerConfig{
Image: &types.ImageSpec{
Image: "archive.tar",
},
}
// When

_, err = sut.CRImportCheckpoint(
context.Background(),
containerConfig,
testSandbox,
"",
)

// Then
Expect(err.Error()).To(Equal(`failed to read "io.kubernetes.cri-o.Labels": unexpected end of JSON input`))
})
})
t.Describe("ContainerRestore from archive into new pod", func() {
images := []struct {
config string
Expand Down Expand Up @@ -456,6 +413,10 @@ var _ = t.Describe("ContainerRestore", func() {
Metadata: &types.ContainerMetadata{
Name: "new-container-name",
},
Mounts: []*types.Mount{{
ContainerPath: "/data",
HostPath: "/data",
}},
}

size := uint64(100)
Expand Down Expand Up @@ -563,6 +524,7 @@ var _ = t.Describe("ContainerRestore", func() {
Return(false, nil),
)
containerConfig := &types.ContainerConfig{
Metadata: &types.ContainerMetadata{Name: "name"},
Image: &types.ImageSpec{
Image: "localhost/checkpoint-image:tag1",
},
Expand Down