Make sure to use sudo for load/save docker images

Add a test for SaveImage, similar to LoadImage
pull/12265/head
Anders F Björklund 2021-08-15 20:25:37 +02:00
parent 9e588e6ba1
commit ab61a07f95
3 changed files with 106 additions and 3 deletions

View File

@ -201,7 +201,7 @@ func (r *Docker) ListImages(ListImagesOptions) ([]string, error) {
// LoadImage loads an image into this runtime
func (r *Docker) LoadImage(path string) error {
klog.Infof("Loading image: %s", path)
c := exec.Command("docker", "load", "-i", path)
c := exec.Command("/bin/bash", "-c", fmt.Sprintf("sudo cat %s | docker load", path))
if _, err := r.Runner.RunCmd(c); err != nil {
return errors.Wrap(err, "loadimage docker.")
}
@ -224,7 +224,7 @@ func (r *Docker) PullImage(name string) error {
// SaveImage saves an image from this runtime
func (r *Docker) SaveImage(name string, path string) error {
klog.Infof("Saving image %s: %s", name, path)
c := exec.Command("docker", "save", name, "-o", path)
c := exec.Command("/bin/bash", "-c", fmt.Sprintf("docker save '%s' | sudo tee %s >/dev/null", name, path))
if _, err := r.Runner.RunCmd(c); err != nil {
return errors.Wrap(err, "saveimage docker.")
}

View File

@ -84,6 +84,12 @@ makes sure that `minikube image load` works from a local file
#### validateRemoveImage
makes sures that `minikube image rm` works as expected
#### validateSaveImage
makes sure that `minikube image save` works as expected
#### validateSaveImageToFile
makes sure that `minikube image save` works to a local file
#### validateBuildImage
makes sures that `minikube image build` works as expected

View File

@ -151,8 +151,10 @@ func TestFunctional(t *testing.T) {
{"PodmanEnv", validatePodmanEnv},
{"NodeLabels", validateNodeLabels},
{"LoadImage", validateLoadImage},
{"SaveImage", validateSaveImage},
{"RemoveImage", validateRemoveImage},
{"LoadImageFromFile", validateLoadImageFromFile},
{"SaveImageToFile", validateSaveImageToFile},
{"BuildImage", validateBuildImage},
{"ListImages", validateListImages},
{"NonActiveRuntimeDisabled", validateNotActiveRuntimeDisabled},
@ -249,7 +251,7 @@ func validateLoadImage(ctx context.Context, t *testing.T, profile string) {
}
// try to load the new image into minikube
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "load", newImage))
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "load", "--daemon", newImage))
if err != nil {
t.Fatalf("loading image into minikube: %v\n%s", err, rr.Output())
}
@ -363,6 +365,101 @@ func validateRemoveImage(ctx context.Context, t *testing.T, profile string) {
}
// validateSaveImage makes sure that `minikube image save` works as expected
func validateSaveImage(ctx context.Context, t *testing.T, profile string) {
if NoneDriver() {
t.Skip("load image not available on none driver")
}
if GithubActionRunner() && runtime.GOOS == "darwin" {
t.Skip("skipping on github actions and darwin, as this test requires a running docker daemon")
}
defer PostMortemLogs(t, profile)
// pull busybox
busyboxImage := "docker.io/library/busybox:1.29"
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "pull", busyboxImage))
if err != nil {
t.Fatalf("failed to setup test (pull image): %v\n%s", err, rr.Output())
}
// tag busybox
name := "busybox"
tag := fmt.Sprintf("save-%s", profile)
newImage := fmt.Sprintf("docker.io/library/%s:%s", name, tag)
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "tag", busyboxImage, newImage))
if err != nil {
t.Fatalf("failed to setup test (tag image) : %v\n%s", err, rr.Output())
}
// try to save the new image from minikube
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "save", "--daemon", newImage))
if err != nil {
t.Fatalf("loading image into minikube: %v\n%s", err, rr.Output())
}
// make sure the image was correctly loaded
rr, err = Run(t, exec.CommandContext(ctx, "docker", "images", name))
if err != nil {
t.Fatalf("listing images: %v\n%s", err, rr.Output())
}
if !strings.Contains(rr.Output(), fmt.Sprintf("save-%s", profile)) {
t.Fatalf("expected %s to be loaded into minikube but the image is not there", newImage)
}
}
// validateSaveImageToFile makes sure that `minikube image save` works to a local file
func validateSaveImageToFile(ctx context.Context, t *testing.T, profile string) {
if NoneDriver() {
t.Skip("save image not available on none driver")
}
if GithubActionRunner() && runtime.GOOS == "darwin" {
t.Skip("skipping on github actions and darwin, as this test requires a running docker daemon")
}
defer PostMortemLogs(t, profile)
// pull busybox
busyboxImage := "docker.io/library/busybox:1.30"
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "pull", busyboxImage))
if err != nil {
t.Fatalf("failed to setup test (pull image): %v\n%s", err, rr.Output())
}
name := "busybox"
tag := fmt.Sprintf("save-to-file-%s", profile)
taggedImage := fmt.Sprintf("docker.io/library/%s:%s", name, tag)
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "tag", busyboxImage, taggedImage))
if err != nil {
t.Fatalf("failed to setup test (tag image) : %v\n%s", err, rr.Output())
}
// try to save the new image from minikube
imageFile := "busybox.tar"
imagePath, err := filepath.Abs(imageFile)
if err != nil {
t.Fatalf("failed to get absolute path of file %q: %v", imageFile, err)
}
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "save", taggedImage, imagePath))
if err != nil {
t.Fatalf("saving image from minikube: %v\n%s", err, rr.Output())
}
// load image from file
rr, err = Run(t, exec.CommandContext(ctx, "docker", "load", "-i", imagePath))
if err != nil {
t.Fatalf("failed to load image to file: %v\n%s", err, rr.Output())
}
defer os.Remove(imageFile)
// make sure the image was correctly loaded
rr, err = Run(t, exec.CommandContext(ctx, "docker", "images", name))
if err != nil {
t.Fatalf("listing images: %v\n%s", err, rr.Output())
}
if !strings.Contains(rr.Output(), tag) {
t.Fatalf("expected %s to be loaded but the image is not there", taggedImage)
}
}
func inspectImage(ctx context.Context, t *testing.T, profile string, image string) (*RunResult, error) {
var cmd *exec.Cmd
if ContainerRuntime() == "docker" {