Przeglądaj źródła

Merge pull request #6913 from unclejack/map_randomization_fixes

integcli: fix failures caused by tiny map randomization
Tibor Vass 11 lat temu
rodzic
commit
ac6ec9df1d

+ 18 - 6
integration-cli/docker_cli_build_test.go

@@ -2,7 +2,6 @@ package main
 
 import (
 	"fmt"
-
 	"os"
 	"os/exec"
 	"path/filepath"
@@ -592,8 +591,12 @@ func TestBuildRm(t *testing.T) {
 }
 
 func TestBuildWithVolumes(t *testing.T) {
-	name := "testbuildvolumes"
-	expected := "map[/test1:map[] /test2:map[]]"
+	var (
+		result   map[string]map[string]struct{}
+		name     = "testbuildvolumes"
+		emptyMap = make(map[string]struct{})
+		expected = map[string]map[string]struct{}{"/test1": emptyMap, "/test2": emptyMap}
+	)
 	defer deleteImages(name)
 	_, err := buildImage(name,
 		`FROM scratch
@@ -603,13 +606,22 @@ func TestBuildWithVolumes(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
-	res, err := inspectField(name, "Config.Volumes")
+	res, err := inspectFieldJSON(name, "Config.Volumes")
 	if err != nil {
 		t.Fatal(err)
 	}
-	if res != expected {
-		t.Fatalf("Volumes %s, expected %s", res, expected)
+
+	err = unmarshalJSON([]byte(res), &result)
+	if err != nil {
+		t.Fatal(err)
 	}
+
+	equal := deepEqual(&expected, &result)
+
+	if !equal {
+		t.Fatalf("Volumes %s, expected %s", result, expected)
+	}
+
 	logDone("build - with volumes")
 }
 

+ 17 - 3
integration-cli/docker_cli_links_test.go

@@ -93,16 +93,30 @@ func TestIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
 }
 
 func TestInspectLinksStarted(t *testing.T) {
+	var (
+		expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
+		result   []string
+	)
 	defer deleteAllContainers()
 	cmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
 	cmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
 	cmd(t, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sleep", "10")
-	links, err := inspectField("testinspectlink", "HostConfig.Links")
+	links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
 	if err != nil {
 		t.Fatal(err)
 	}
-	if expected := "[/container1:/testinspectlink/alias1 /container2:/testinspectlink/alias2]"; links != expected {
-		t.Fatalf("Links %s, but expected %s", links, expected)
+
+	err = unmarshalJSON([]byte(links), &result)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	output := convertSliceOfStringsToMap(result)
+
+	equal := deepEqual(expected, output)
+
+	if !equal {
+		t.Fatalf("Links %s, expected %s", result, expected)
 	}
 	logDone("link - links in started container inspect")
 }

+ 10 - 0
integration-cli/docker_utils.go

@@ -211,6 +211,16 @@ func inspectField(name, field string) (string, error) {
 	return strings.TrimSpace(out), nil
 }
 
+func inspectFieldJSON(name, field string) (string, error) {
+	format := fmt.Sprintf("{{json .%s}}", field)
+	inspectCmd := exec.Command(dockerBinary, "inspect", "-f", format, name)
+	out, exitCode, err := runCommandWithOutput(inspectCmd)
+	if err != nil || exitCode != 0 {
+		return "", fmt.Errorf("failed to inspect %s: %s", name, out)
+	}
+	return strings.TrimSpace(out), nil
+}
+
 func getIDByName(name string) (string, error) {
 	return inspectField(name, "Id")
 }

+ 23 - 0
integration-cli/utils.go

@@ -2,9 +2,11 @@ package main
 
 import (
 	"bytes"
+	"encoding/json"
 	"fmt"
 	"io"
 	"os/exec"
+	"reflect"
 	"strings"
 	"syscall"
 	"testing"
@@ -111,3 +113,24 @@ func errorOutOnNonNilError(err error, t *testing.T, message string) {
 func nLines(s string) int {
 	return strings.Count(s, "\n")
 }
+
+func unmarshalJSON(data []byte, result interface{}) error {
+	err := json.Unmarshal(data, result)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func deepEqual(expected interface{}, result interface{}) bool {
+	return reflect.DeepEqual(result, expected)
+}
+
+func convertSliceOfStringsToMap(input []string) map[string]struct{} {
+	output := make(map[string]struct{})
+	for _, v := range input {
+		output[v] = struct{}{}
+	}
+	return output
+}