mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #6913 from unclejack/map_randomization_fixes
integcli: fix failures caused by tiny map randomization
This commit is contained in:
commit
ac6ec9df1d
4 changed files with 68 additions and 9 deletions
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -592,8 +591,12 @@ func TestBuildRm(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildWithVolumes(t *testing.T) {
|
func TestBuildWithVolumes(t *testing.T) {
|
||||||
name := "testbuildvolumes"
|
var (
|
||||||
expected := "map[/test1:map[] /test2:map[]]"
|
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)
|
defer deleteImages(name)
|
||||||
_, err := buildImage(name,
|
_, err := buildImage(name,
|
||||||
`FROM scratch
|
`FROM scratch
|
||||||
|
@ -603,13 +606,22 @@ func TestBuildWithVolumes(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
res, err := inspectField(name, "Config.Volumes")
|
res, err := inspectFieldJSON(name, "Config.Volumes")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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")
|
logDone("build - with volumes")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -93,16 +93,30 @@ func TestIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInspectLinksStarted(t *testing.T) {
|
func TestInspectLinksStarted(t *testing.T) {
|
||||||
|
var (
|
||||||
|
expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
|
||||||
|
result []string
|
||||||
|
)
|
||||||
defer deleteAllContainers()
|
defer deleteAllContainers()
|
||||||
cmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
|
cmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
|
||||||
cmd(t, "run", "-d", "--name", "container2", "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")
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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")
|
logDone("link - links in started container inspect")
|
||||||
}
|
}
|
||||||
|
|
|
@ -211,6 +211,16 @@ func inspectField(name, field string) (string, error) {
|
||||||
return strings.TrimSpace(out), nil
|
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) {
|
func getIDByName(name string) (string, error) {
|
||||||
return inspectField(name, "Id")
|
return inspectField(name, "Id")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -111,3 +113,24 @@ func errorOutOnNonNilError(err error, t *testing.T, message string) {
|
||||||
func nLines(s string) int {
|
func nLines(s string) int {
|
||||||
return strings.Count(s, "\n")
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue