提交 db1cdc42 编写于 作者: A Anders F Björklund

Fix usage of quotes in cruntime format strings

And add a missing test for ImageExists function
上级 07c5f6bc
......@@ -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
......
......@@ -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" {
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册