This repository was archived by the owner on Jul 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathlist_test.go
More file actions
133 lines (124 loc) · 4.18 KB
/
list_test.go
File metadata and controls
133 lines (124 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package image
import (
"bufio"
"bytes"
"testing"
"time"
"github.com/docker/app/internal/store"
"gotest.tools/fs"
"github.com/deislabs/cnab-go/bundle"
"github.com/docker/app/internal/relocated"
"github.com/docker/cli/cli/command"
"github.com/docker/distribution/reference"
"gotest.tools/assert"
)
func TestListCmd(t *testing.T) {
refs := []reference.Named{
parseReference(t, "foo/bar@sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865"),
parseReference(t, "foo/bar:1.0"),
nil,
}
bundles := []relocated.Bundle{
{
Bundle: &bundle.Bundle{
Name: "Digested App",
},
},
{
Bundle: &bundle.Bundle{
Version: "1.0.0",
SchemaVersion: "1.0.0",
Name: "Foo App",
},
},
{
Bundle: &bundle.Bundle{
Name: "Quiet App",
},
},
}
testCases := []struct {
name string
expectedOutput string
options imageListOption
}{
{
name: "TestList",
expectedOutput: `REPOSITORY TAG APP IMAGE ID APP NAME CREATED
<none> <none> ad2828ea5653 Quiet App N/A
foo/bar 1.0 9aae408ee04f Foo App N/A
foo/bar <none> 3f825b2d0657 Digested App N/A
`,
options: imageListOption{format: "table"},
},
{
name: "TestTemplate",
expectedOutput: `APP IMAGE ID DIGEST
ad2828ea5653 <none>
9aae408ee04f <none>
3f825b2d0657 sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865
`,
options: imageListOption{format: "table {{.ID}}", digests: true},
},
{
name: "TestListWithDigests",
//nolint:lll
expectedOutput: `REPOSITORY TAG DIGEST APP IMAGE ID APP NAME CREATED
<none> <none> <none> ad2828ea5653 Quiet App N/A
foo/bar 1.0 <none> 9aae408ee04f Foo App N/A
foo/bar <none> sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865 3f825b2d0657 Digested App N/A
`,
options: imageListOption{format: "table", digests: true},
},
{
name: "TestListWithQuiet",
expectedOutput: `ad2828ea5653
9aae408ee04f
3f825b2d0657
`,
options: imageListOption{format: "table", quiet: true},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testRunList(t, refs, bundles, tc.options, tc.expectedOutput)
})
}
}
func TestSortImages(t *testing.T) {
images := []imageDesc{
{ID: "1", Created: time.Date(2016, time.August, 15, 0, 0, 0, 0, time.UTC)},
{ID: "2"},
{ID: "3"},
{ID: "4", Created: time.Date(2018, time.August, 15, 0, 0, 0, 0, time.UTC)},
{ID: "5", Created: time.Date(2017, time.August, 15, 0, 0, 0, 0, time.UTC)},
}
sortImages(images)
assert.Equal(t, "4", images[0].ID)
assert.Equal(t, "5", images[1].ID)
assert.Equal(t, "1", images[2].ID)
assert.Equal(t, "2", images[3].ID)
assert.Equal(t, "3", images[4].ID)
}
func parseReference(t *testing.T, s string) reference.Named {
ref, err := reference.ParseNormalizedNamed(s)
assert.NilError(t, err)
return ref
}
func testRunList(t *testing.T, refs []reference.Named, bundles []relocated.Bundle, options imageListOption, expectedOutput string) {
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
dockerCli, err := command.NewDockerCli(command.WithOutputStream(w))
assert.NilError(t, err)
bundleStore, err := store.NewBundleStore(fs.NewDir(t, "store").Path())
assert.NilError(t, err)
for i, ref := range refs {
_, err = bundleStore.Store(&bundles[i], ref)
assert.NilError(t, err)
}
err = runList(dockerCli, options, bundleStore)
assert.NilError(t, err)
w.Flush()
actualOutput := buf.String()
assert.Equal(t, actualOutput, expectedOutput)
}