Skip to content

Commit 0ec9357

Browse files
committed
Fix usage of quotes in cruntime format strings
And add a missing test for ImageExists function
1 parent 10ef863 commit 0ec9357

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

pkg/minikube/cruntime/crio.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (r *CRIO) Disable() error {
140140
// ImageExists checks if an image exists
141141
func (r *CRIO) ImageExists(name string, sha string) bool {
142142
// expected output looks like [NAME@sha256:SHA]
143-
c := exec.Command("sudo", "podman", "inspect", "--format='{{.Id}}'", name)
143+
c := exec.Command("sudo", "podman", "inspect", "--format", "{{.Id}}", name)
144144
rr, err := r.Runner.RunCmd(c)
145145
if err != nil {
146146
return false

pkg/minikube/cruntime/cruntime_test.go

+56-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,33 @@ func TestName(t *testing.T) {
5454
}
5555
}
5656

57+
func TestImageExists(t *testing.T) {
58+
var tests = []struct {
59+
runtime string
60+
name string
61+
sha string
62+
want bool
63+
}{
64+
{"docker", "missing", "0000000000000000000000000000000000000000000000000000000000000000", false},
65+
{"docker", "image", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", true},
66+
{"crio", "missing", "0000000000000000000000000000000000000000000000000000000000000000", false},
67+
{"crio", "image", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", true},
68+
}
69+
for _, tc := range tests {
70+
t.Run(tc.runtime, func(t *testing.T) {
71+
r, err := New(Config{Type: tc.runtime, Runner: NewFakeRunner(t)})
72+
if err != nil {
73+
t.Fatalf("New(%s): %v", tc.runtime, err)
74+
}
75+
76+
got := r.ImageExists(tc.name, tc.sha)
77+
if diff := cmp.Diff(tc.want, got); diff != "" {
78+
t.Errorf("ImageExists(%s) returned diff (-want +got):\n%s", tc.runtime, diff)
79+
}
80+
})
81+
}
82+
}
83+
5784
func TestCGroupDriver(t *testing.T) {
5885
var tests = []struct {
5986
runtime string
@@ -174,6 +201,8 @@ func (f *FakeRunner) RunCmd(cmd *exec.Cmd) (*command.RunResult, error) {
174201
return buffer(f.which(args, root))
175202
case "docker":
176203
return buffer(f.docker(args, root))
204+
case "podman":
205+
return buffer(f.podman(args, root))
177206
case "crictl", "/usr/bin/crictl":
178207
return buffer(f.crictl(args, root))
179208
case "crio":
@@ -225,19 +254,44 @@ func (f *FakeRunner) docker(args []string, _ bool) (string, error) {
225254
}
226255
case "version":
227256

228-
if args[1] == "--format" && args[2] == "'{{.Server.Version}}'" {
257+
if args[1] == "--format" && args[2] == "{{.Server.Version}}" {
229258
return "18.06.2-ce", nil
230259
}
231260

261+
case "inspect":
262+
263+
if args[1] == "--format" && args[2] == "{{.Id}}" {
264+
if args[3] == "missing" {
265+
return "", &exec.ExitError{Stderr: []byte("Error: No such object: missing")}
266+
}
267+
return "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", nil
268+
}
269+
232270
case "info":
233271

234-
if args[1] == "--format" && args[2] == "'{{.CgroupDriver}}'" {
272+
if args[1] == "--format" && args[2] == "{{.CgroupDriver}}" {
235273
return "cgroupfs", nil
236274
}
237275
}
238276
return "", nil
239277
}
240278

279+
// podman is a fake implementation of podman
280+
func (f *FakeRunner) podman(args []string, _ bool) (string, error) {
281+
switch cmd := args[0]; cmd {
282+
case "inspect":
283+
284+
if args[1] == "--format" && args[2] == "{{.Id}}" {
285+
if args[3] == "missing" {
286+
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")}
287+
}
288+
return "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", nil
289+
}
290+
291+
}
292+
return "", nil
293+
}
294+
241295
// crio is a fake implementation of crio
242296
func (f *FakeRunner) crio(args []string, _ bool) (string, error) { //nolint (result 1 (error) is always nil)
243297
if args[0] == "--version" {

pkg/minikube/cruntime/docker.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (r *Docker) Style() out.StyleEnum {
4848
// Version retrieves the current version of this runtime
4949
func (r *Docker) Version() (string, error) {
5050
// Note: the server daemon has to be running, for this call to return successfully
51-
c := exec.Command("docker", "version", "--format", "'{{.Server.Version}}'")
51+
c := exec.Command("docker", "version", "--format", "{{.Server.Version}}")
5252
rr, err := r.Runner.RunCmd(c)
5353
if err != nil {
5454
return "", err
@@ -105,7 +105,7 @@ func (r *Docker) Disable() error {
105105
// ImageExists checks if an image exists
106106
func (r *Docker) ImageExists(name string, sha string) bool {
107107
// expected output looks like [SHA_ALGO:SHA]
108-
c := exec.Command("docker", "inspect", "--format='{{.Id}}'", name)
108+
c := exec.Command("docker", "inspect", "--format", "{{.Id}}", name)
109109
rr, err := r.Runner.RunCmd(c)
110110
if err != nil {
111111
return false
@@ -130,7 +130,7 @@ func (r *Docker) LoadImage(path string) error {
130130
// CGroupDriver returns cgroup driver ("cgroupfs" or "systemd")
131131
func (r *Docker) CGroupDriver() (string, error) {
132132
// Note: the server daemon has to be running, for this call to return successfully
133-
c := exec.Command("docker", "info", "--format", "'{{.CgroupDriver}}'")
133+
c := exec.Command("docker", "info", "--format", "{{.CgroupDriver}}")
134134
rr, err := r.Runner.RunCmd(c)
135135
if err != nil {
136136
return "", err

0 commit comments

Comments
 (0)