diff options
author | Toshiki Sonoda <sonoda.toshiki@fujitsu.com> | 2022-07-26 10:00:52 +0900 |
---|---|---|
committer | Matthew Heon <mheon@redhat.com> | 2022-08-10 16:46:05 -0400 |
commit | 24f219efb75bbe6c155552c4a33fc79bc55ba2eb (patch) | |
tree | c2843318433dba50e8935f43c1ea212034c3b74a | |
parent | 32848b95f6fcf9e8dcd51f23f43846d66886f1ac (diff) | |
download | podman-24f219efb75bbe6c155552c4a33fc79bc55ba2eb.tar.gz podman-24f219efb75bbe6c155552c4a33fc79bc55ba2eb.tar.bz2 podman-24f219efb75bbe6c155552c4a33fc79bc55ba2eb.zip |
Fix: Restore a container which name is equal to a image name
If there is a match for both container and image, we restore the container.
Fixes: https://github.com/containers/podman/issues/15055
Signed-off-by: Toshiki Sonoda <sonoda.toshiki@fujitsu.com>
-rw-r--r-- | cmd/podman/containers/restore.go | 51 | ||||
-rw-r--r-- | test/e2e/checkpoint_test.go | 20 |
2 files changed, 54 insertions, 17 deletions
diff --git a/cmd/podman/containers/restore.go b/cmd/podman/containers/restore.go index 1e4745354..6106f2bed 100644 --- a/cmd/podman/containers/restore.go +++ b/cmd/podman/containers/restore.go @@ -93,32 +93,49 @@ func init() { } func restore(cmd *cobra.Command, args []string) error { - var errs utils.OutputErrors + var ( + e error + errs utils.OutputErrors + ) podmanStart := time.Now() if rootless.IsRootless() { return fmt.Errorf("restoring a container requires root") } - // Find out if this is an image - inspectOpts := entities.InspectOptions{} - imgData, _, err := registry.ImageEngine().Inspect(context.Background(), args, inspectOpts) - if err != nil { - return err + // Check if the container exists (#15055) + exists := &entities.BoolReport{Value: false} + for _, ctr := range args { + exists, e = registry.ContainerEngine().ContainerExists(registry.GetContext(), ctr, entities.ContainerExistsOptions{}) + if e != nil { + return e + } + if exists.Value { + break + } } - hostInfo, err := registry.ContainerEngine().Info(context.Background()) - if err != nil { - return err - } + if !exists.Value { + // Find out if this is an image + inspectOpts := entities.InspectOptions{} + imgData, _, err := registry.ImageEngine().Inspect(context.Background(), args, inspectOpts) + if err != nil { + return err + } - for i := range imgData { - restoreOptions.CheckpointImage = true - checkpointRuntimeName, found := imgData[i].Annotations[define.CheckpointAnnotationRuntimeName] - if !found { - return fmt.Errorf("image is not a checkpoint: %s", imgData[i].ID) + hostInfo, err := registry.ContainerEngine().Info(context.Background()) + if err != nil { + return err } - if hostInfo.Host.OCIRuntime.Name != checkpointRuntimeName { - return fmt.Errorf("container image \"%s\" requires runtime: \"%s\"", imgData[i].ID, checkpointRuntimeName) + + for i := range imgData { + restoreOptions.CheckpointImage = true + checkpointRuntimeName, found := imgData[i].Annotations[define.CheckpointAnnotationRuntimeName] + if !found { + return fmt.Errorf("image is not a checkpoint: %s", imgData[i].ID) + } + if hostInfo.Host.OCIRuntime.Name != checkpointRuntimeName { + return fmt.Errorf("container image \"%s\" requires runtime: \"%s\"", imgData[i].ID, checkpointRuntimeName) + } } } diff --git a/test/e2e/checkpoint_test.go b/test/e2e/checkpoint_test.go index d288c278e..03c9fc97e 100644 --- a/test/e2e/checkpoint_test.go +++ b/test/e2e/checkpoint_test.go @@ -222,6 +222,26 @@ var _ = Describe("Podman checkpoint", func() { Expect(result).Should(Exit(0)) Expect(podmanTest.NumberOfContainersRunning()).To(Equal(1)) Expect(podmanTest.GetContainerStatus()).To(ContainSubstring("Up")) + + // Restore a container which name is equal to a image name (#15055) + localRunString = getRunString([]string{"--name", "alpine", "quay.io/libpod/alpine:latest", "top"}) + session = podmanTest.Podman(localRunString) + session.WaitWithDefaultTimeout() + Expect(session).Should(Exit(0)) + + result = podmanTest.Podman([]string{"container", "checkpoint", "alpine"}) + result.WaitWithDefaultTimeout() + + Expect(result).Should(Exit(0)) + Expect(podmanTest.NumberOfContainersRunning()).To(Equal(1)) + Expect(podmanTest.GetContainerStatus()).To(ContainSubstring("Exited")) + + result = podmanTest.Podman([]string{"container", "restore", "alpine"}) + result.WaitWithDefaultTimeout() + + Expect(result).Should(Exit(0)) + Expect(podmanTest.NumberOfContainersRunning()).To(Equal(2)) + Expect(podmanTest.GetContainerStatus()).To(ContainSubstring("Up")) }) It("podman pause a checkpointed container by id", func() { |