|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | + "gotest.tools/assert" |
| 12 | + "gotest.tools/fs" |
| 13 | + "gotest.tools/icmd" |
| 14 | +) |
| 15 | + |
| 16 | +func lineByLineComparator(t *testing.T, actual string, length int, expectedLines map[int]func(string) error) { |
| 17 | + t.Helper() |
| 18 | + lines := strings.Split(actual, "\n") |
| 19 | + assert.Equal(t, len(lines), length) |
| 20 | + if len(expectedLines) == 0 { |
| 21 | + return |
| 22 | + } |
| 23 | + for i, line := range lines { |
| 24 | + cmp, ok := expectedLines[i] |
| 25 | + if !ok { |
| 26 | + continue |
| 27 | + } |
| 28 | + if err := cmp(line); err != nil { |
| 29 | + t.Errorf("line %d: %s", i, err) |
| 30 | + } |
| 31 | + } |
| 32 | + if t.Failed() { |
| 33 | + t.Log(actual) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func prefix(expected string) func(string) error { |
| 38 | + return func(actual string) error { |
| 39 | + if strings.HasPrefix(actual, expected) { |
| 40 | + return nil |
| 41 | + } |
| 42 | + return errors.Errorf("expected %q to start with %q", actual, expected) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func equals(expected string) func(string) error { |
| 47 | + return func(actual string) error { |
| 48 | + if expected == actual { |
| 49 | + return nil |
| 50 | + } |
| 51 | + return errors.Errorf("got %q, expected %q", actual, expected) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func extractImageID(t *testing.T, line string) string { |
| 56 | + fields := strings.Fields(line) |
| 57 | + assert.Assert(t, len(fields) > 3) |
| 58 | + return fields[2] |
| 59 | +} |
| 60 | + |
| 61 | +type commandConfig struct { |
| 62 | + env string |
| 63 | + exe string |
| 64 | + dir string |
| 65 | + t *testing.T |
| 66 | +} |
| 67 | + |
| 68 | +func (c *commandConfig) run(args ...string) *icmd.Result { |
| 69 | + c.t.Helper() |
| 70 | + cmd := icmd.Command(c.exe, args...) |
| 71 | + cmd.Env = append(os.Environ(), c.env) |
| 72 | + cmd.Dir = c.dir |
| 73 | + result := icmd.RunCmd(cmd) |
| 74 | + result.Assert(c.t, icmd.Success) |
| 75 | + return result |
| 76 | +} |
| 77 | + |
| 78 | +func TestLsCmd(t *testing.T) { |
| 79 | + app, _ := getBinary(t) |
| 80 | + dind := startDind(t) |
| 81 | + defer dind.stop(t) |
| 82 | + dockerApp := &commandConfig{ |
| 83 | + env: fmt.Sprintf("DOCKER_HOST=%v", dind.getAddress(t)), |
| 84 | + exe: app, |
| 85 | + t: t, |
| 86 | + dir: fs.NewDir(t, "test_docker_app_ls_cmd").Path(), |
| 87 | + } |
| 88 | + result := dockerApp.run("ls") |
| 89 | + lineByLineComparator(t, result.Stdout(), 2, map[int]func(string) error{ |
| 90 | + 0: equals("REPOSITORY TAG IMAGE ID CREATED SIZE"), |
| 91 | + 1: equals(""), |
| 92 | + }) |
| 93 | + // Create two apps. |
| 94 | + dockerApp.run("init", "ls_myapp") |
| 95 | + dockerApp.run("save", "ls_myapp.dockerapp") |
| 96 | + // We need to sleep between creation to avoid timestamp collision. |
| 97 | + time.Sleep(1 * time.Second) |
| 98 | + dockerApp.run("init", "anotherapp") |
| 99 | + dockerApp.run("save", "anotherapp.dockerapp") |
| 100 | + // We need to sleep again to avoid: CREATED `Less than a second ago`, otherwise, the header size is not constant. |
| 101 | + time.Sleep(1 * time.Second) |
| 102 | + |
| 103 | + // Except the output to contain both applications. |
| 104 | + result = dockerApp.run("ls") |
| 105 | + lineByLineComparator(t, result.Stdout(), 4, map[int]func(string) error{ |
| 106 | + 0: equals("REPOSITORY TAG IMAGE ID CREATED SIZE"), |
| 107 | + 1: prefix("anotherapp.dockerapp 0.1.0"), |
| 108 | + 2: prefix("ls_myapp.dockerapp 0.1.0"), |
| 109 | + 3: equals(""), |
| 110 | + }) |
| 111 | + |
| 112 | + // Except quiet flag to return IDs only. |
| 113 | + var ids []string |
| 114 | + for _, line := range strings.Split(result.Stdout(), "\n")[1:2] { |
| 115 | + ids = append(ids, extractImageID(t, line)) |
| 116 | + } |
| 117 | + result = dockerApp.run("ls", "--quiet") |
| 118 | + assert.DeepEqual(t, strings.Split(result.Stdout(), "\n")[0:1], ids) |
| 119 | + |
| 120 | + // Except the output to contain only ls_myapp. |
| 121 | + result = dockerApp.run("ls", "ls_myapp.dockerapp") |
| 122 | + lineByLineComparator(t, result.Stdout(), 3, map[int]func(string) error{ |
| 123 | + 0: equals("REPOSITORY TAG IMAGE ID CREATED SIZE"), |
| 124 | + 1: prefix("ls_myapp.dockerapp 0.1.0"), |
| 125 | + 2: equals(""), |
| 126 | + }) |
| 127 | + |
| 128 | + // Except quiet flag to return only one ID. |
| 129 | + id := extractImageID(t, strings.Split(result.Stdout(), "\n")[1]) |
| 130 | + result = dockerApp.run("ls", "-q", "ls_myapp.dockerapp") |
| 131 | + assert.Equal(t, result.Stdout(), id+"\n") |
| 132 | +} |
0 commit comments