From db1cdc42cfe40b78e3026f01430e50714fb358a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Fri, 7 Feb 2020 22:17:59 +0100 Subject: [PATCH] Fix usage of quotes in cruntime format strings And add a missing test for ImageExists function --- pkg/minikube/cruntime/crio.go | 2 +- pkg/minikube/cruntime/cruntime_test.go | 61 +++++++++++++++++++++++++- pkg/minikube/cruntime/docker.go | 6 +-- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/pkg/minikube/cruntime/crio.go b/pkg/minikube/cruntime/crio.go index dc4670cca765..4b2ad5df0a06 100644 --- a/pkg/minikube/cruntime/crio.go +++ b/pkg/minikube/cruntime/crio.go @@ -140,7 +140,7 @@ func (r *CRIO) Disable() error { // ImageExists checks if an image exists func (r *CRIO) ImageExists(name string, sha string) bool { // expected output looks like [NAME@sha256:SHA] - c := exec.Command("sudo", "podman", "inspect", "--format='{{.Id}}'", name) + c := exec.Command("sudo", "podman", "inspect", "--format", "{{.Id}}", name) rr, err := r.Runner.RunCmd(c) if err != nil { return false diff --git a/pkg/minikube/cruntime/cruntime_test.go b/pkg/minikube/cruntime/cruntime_test.go index 7764e1b7140a..8e23437b950e 100644 --- a/pkg/minikube/cruntime/cruntime_test.go +++ b/pkg/minikube/cruntime/cruntime_test.go @@ -54,6 +54,33 @@ func TestName(t *testing.T) { } } +func TestImageExists(t *testing.T) { + var tests = []struct { + runtime string + name string + sha string + want bool + }{ + {"docker", "missing", "0000000000000000000000000000000000000000000000000000000000000000", false}, + {"docker", "image", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", true}, + {"crio", "missing", "0000000000000000000000000000000000000000000000000000000000000000", false}, + {"crio", "image", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", true}, + } + for _, tc := range tests { + t.Run(tc.runtime, func(t *testing.T) { + r, err := New(Config{Type: tc.runtime, Runner: NewFakeRunner(t)}) + if err != nil { + t.Fatalf("New(%s): %v", tc.runtime, err) + } + + got := r.ImageExists(tc.name, tc.sha) + if diff := cmp.Diff(tc.want, got); diff != "" { + t.Errorf("ImageExists(%s) returned diff (-want +got):\n%s", tc.runtime, diff) + } + }) + } +} + func TestCGroupDriver(t *testing.T) { var tests = []struct { runtime string @@ -174,6 +201,8 @@ func (f *FakeRunner) RunCmd(cmd *exec.Cmd) (*command.RunResult, error) { return buffer(f.which(args, root)) case "docker": return buffer(f.docker(args, root)) + case "podman": + return buffer(f.podman(args, root)) case "crictl", "/usr/bin/crictl": return buffer(f.crictl(args, root)) case "crio": @@ -225,19 +254,47 @@ func (f *FakeRunner) docker(args []string, _ bool) (string, error) { } case "version": - if args[1] == "--format" && args[2] == "'{{.Server.Version}}'" { + if args[1] == "--format" && args[2] == "{{.Server.Version}}" { return "18.06.2-ce", nil } + case "inspect": + + if args[1] == "--format" && args[2] == "{{.Id}}" { + if args[3] == "missing" { + return "", &exec.ExitError{Stderr: []byte("Error: No such object: missing")} + } + return "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", nil + } + case "info": - if args[1] == "--format" && args[2] == "'{{.CgroupDriver}}'" { + if args[1] == "--format" && args[2] == "{{.CgroupDriver}}" { return "cgroupfs", nil } } return "", nil } +// podman is a fake implementation of podman +func (f *FakeRunner) podman(args []string, _ bool) (string, error) { + switch cmd := args[0]; cmd { + case "--version": + return "podman version 1.6.4", nil + + case "inspect": + + if args[1] == "--format" && args[2] == "{{.Id}}" { + if args[3] == "missing" { + return "", &exec.ExitError{Stderr: []byte("Error: error getting image \"missing\": unable to find a name and tag match for missing in repotags: no such image")} + } + return "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", nil + } + + } + return "", nil +} + // crio is a fake implementation of crio func (f *FakeRunner) crio(args []string, _ bool) (string, error) { //nolint (result 1 (error) is always nil) if args[0] == "--version" { diff --git a/pkg/minikube/cruntime/docker.go b/pkg/minikube/cruntime/docker.go index 24fb506c94e1..82d7516dea4e 100644 --- a/pkg/minikube/cruntime/docker.go +++ b/pkg/minikube/cruntime/docker.go @@ -48,7 +48,7 @@ func (r *Docker) Style() out.StyleEnum { // Version retrieves the current version of this runtime func (r *Docker) Version() (string, error) { // Note: the server daemon has to be running, for this call to return successfully - c := exec.Command("docker", "version", "--format", "'{{.Server.Version}}'") + c := exec.Command("docker", "version", "--format", "{{.Server.Version}}") rr, err := r.Runner.RunCmd(c) if err != nil { return "", err @@ -105,7 +105,7 @@ func (r *Docker) Disable() error { // ImageExists checks if an image exists func (r *Docker) ImageExists(name string, sha string) bool { // expected output looks like [SHA_ALGO:SHA] - c := exec.Command("docker", "inspect", "--format='{{.Id}}'", name) + c := exec.Command("docker", "inspect", "--format", "{{.Id}}", name) rr, err := r.Runner.RunCmd(c) if err != nil { return false @@ -130,7 +130,7 @@ func (r *Docker) LoadImage(path string) error { // CGroupDriver returns cgroup driver ("cgroupfs" or "systemd") func (r *Docker) CGroupDriver() (string, error) { // Note: the server daemon has to be running, for this call to return successfully - c := exec.Command("docker", "info", "--format", "'{{.CgroupDriver}}'") + c := exec.Command("docker", "info", "--format", "{{.CgroupDriver}}") rr, err := r.Runner.RunCmd(c) if err != nil { return "", err