kubeadm: fix runtime.ImageExists API

This API return error when underlying 'inspect' command
fails. This makes ImagePullCheck to fail as it doesn't expect
runtime.ImageExists to return an error even if image doesn't exist.

Fixed this by returning error nil even when inspect command fails.
This commit is contained in:
Ed Bartosh 2018-07-31 13:43:59 +03:00
parent 10688257e6
commit c086c235f2
3 changed files with 33 additions and 32 deletions

View File

@ -701,14 +701,24 @@ func TestSetHasItemOrAll(t *testing.T) {
func TestImagePullCheck(t *testing.T) { func TestImagePullCheck(t *testing.T) {
fcmd := fakeexec.FakeCmd{ fcmd := fakeexec.FakeCmd{
RunScript: []fakeexec.FakeRunAction{
// Test case 1: img1 and img2 exist, img3 doesn't exist
func() ([]byte, []byte, error) { return nil, nil, nil },
func() ([]byte, []byte, error) { return nil, nil, nil },
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} },
// Test case 2: images don't exist
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} },
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} },
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} },
},
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{ CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
func() ([]byte, error) { return nil, nil }, // Test case 1 // Test case1: pull only img3
func() ([]byte, error) { return nil, nil },
func() ([]byte, error) { return nil, nil },
func() ([]byte, error) { return []byte("error"), &fakeexec.FakeExitError{Status: 1} }, // Test case 2
func() ([]byte, error) { return nil, nil },
func() ([]byte, error) { return nil, nil }, func() ([]byte, error) { return nil, nil },
// Test case 2: fail to pull image2 and image3
func() ([]byte, error) { return nil, nil }, func() ([]byte, error) { return nil, nil },
func() ([]byte, error) { return []byte("error"), &fakeexec.FakeExitError{Status: 1} },
func() ([]byte, error) { return []byte("error"), &fakeexec.FakeExitError{Status: 1} },
}, },
} }
@ -721,6 +731,9 @@ func TestImagePullCheck(t *testing.T) {
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
}, },
LookPathFunc: func(cmd string) (string, error) { return "/usr/bin/docker", nil }, LookPathFunc: func(cmd string) (string, error) { return "/usr/bin/docker", nil },
} }
@ -746,7 +759,7 @@ func TestImagePullCheck(t *testing.T) {
if len(warnings) != 0 { if len(warnings) != 0 {
t.Fatalf("did not expect any warnings but got %q", warnings) t.Fatalf("did not expect any warnings but got %q", warnings)
} }
if len(errors) != 1 { if len(errors) != 2 {
t.Fatalf("expected 1 errors but got %d: %q", len(errors), errors) t.Fatalf("expected 2 errors but got %d: %q", len(errors), errors)
} }
} }

View File

@ -172,18 +172,12 @@ func (runtime *DockerRuntime) PullImage(image string) error {
// ImageExists checks to see if the image exists on the system // ImageExists checks to see if the image exists on the system
func (runtime *CRIRuntime) ImageExists(image string) (bool, error) { func (runtime *CRIRuntime) ImageExists(image string) (bool, error) {
out, err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "inspecti", image).CombinedOutput() err := runtime.exec.Command("crictl", "-r", runtime.criSocket, "inspecti", image).Run()
if err != nil { return err == nil, nil
return false, fmt.Errorf("output: %s, error: %v", string(out), err)
}
return true, nil
} }
// ImageExists checks to see if the image exists on the system // ImageExists checks to see if the image exists on the system
func (runtime *DockerRuntime) ImageExists(image string) (bool, error) { func (runtime *DockerRuntime) ImageExists(image string) (bool, error) {
out, err := runtime.exec.Command("docker", "inspect", image).CombinedOutput() err := runtime.exec.Command("docker", "inspect", image).Run()
if err != nil { return err == nil, nil
return false, fmt.Errorf("output: %s, error: %v", string(out), err)
}
return true, nil
} }

View File

@ -266,15 +266,15 @@ func TestPullImage(t *testing.T) {
func TestImageExists(t *testing.T) { func TestImageExists(t *testing.T) {
fcmd := fakeexec.FakeCmd{ fcmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{ RunScript: []fakeexec.FakeRunAction{
func() ([]byte, error) { return nil, nil }, func() ([]byte, []byte, error) { return nil, nil, nil },
func() ([]byte, error) { return []byte("error"), &fakeexec.FakeExitError{Status: 1} }, func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} },
func() ([]byte, error) { return nil, nil }, func() ([]byte, []byte, error) { return nil, nil, nil },
func() ([]byte, error) { return []byte("error"), &fakeexec.FakeExitError{Status: 1} }, func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} },
}, },
} }
execer := fakeexec.FakeExec{ execer := fakeexec.FakeExec{
CommandScript: genFakeActions(&fcmd, len(fcmd.CombinedOutputScript)), CommandScript: genFakeActions(&fcmd, len(fcmd.RunScript)),
LookPathFunc: func(cmd string) (string, error) { return "/usr/bin/crictl", nil }, LookPathFunc: func(cmd string) (string, error) { return "/usr/bin/crictl", nil },
} }
@ -282,7 +282,7 @@ func TestImageExists(t *testing.T) {
name string name string
criSocket string criSocket string
image string image string
isError bool result bool
}{ }{
{"valid: test if image exists using CRI", "unix:///var/run/crio/crio.sock", "image1", false}, {"valid: test if image exists using CRI", "unix:///var/run/crio/crio.sock", "image1", false},
{"invalid: CRI inspecti failure", "unix:///var/run/crio/crio.sock", "image2", true}, {"invalid: CRI inspecti failure", "unix:///var/run/crio/crio.sock", "image2", true},
@ -298,14 +298,8 @@ func TestImageExists(t *testing.T) {
} }
result, err := runtime.ImageExists(tc.image) result, err := runtime.ImageExists(tc.image)
if result && err != nil { if !tc.result != result {
t.Errorf("unexpected ImageExists result %v and error: %v", result, err) t.Errorf("unexpected ImageExists result: %t, criSocket: %s, image: %s, expected result: %t", err, tc.criSocket, tc.image, tc.result)
}
if !tc.isError && err != nil {
t.Errorf("unexpected ImageExists error: %v, criSocket: %s, image: %s", err, tc.criSocket, tc.image)
}
if tc.isError && err == nil {
t.Errorf("unexpected ImageExists success, criSocket: %s, image: %s", tc.criSocket, tc.image)
} }
}) })
} }