Debian Codegraph @

dockerclient/copyinfo_test.go

raw
1 package dockerclient
2
3 import (
4 "fmt"
5 "reflect"
6 "testing"
7 )
8
9 func TestCalcCopyInfo(t *testing.T) {
10 nilErr := func(err error) bool { return err == nil }
11 tests := []struct {
12 origPath string
13 rootPath string
14 dstPath string
15 allowWildcards bool
16 errFn func(err error) bool
17 paths map[string]struct{}
18 excludes []string
19 rebaseNames map[string]string
20 check map[string]bool
21 download bool
22 }{
23 {
24 origPath: "subdir/*",
25 rootPath: "testdata/dir",
26 allowWildcards: true,
27 errFn: nilErr,
28 paths: map[string]struct{}{"subdir/file2": {}},
29 },
30 {
31 origPath: "*",
32 rootPath: "testdata/dir",
33 allowWildcards: true,
34 errFn: nilErr,
35 paths: map[string]struct{}{
36 "Dockerfile": {},
37 "file": {},
38 "subdir": {},
39 },
40 },
41 {
42 origPath: ".",
43 rootPath: "testdata/dir",
44 allowWildcards: true,
45 errFn: nilErr,
46 paths: map[string]struct{}{
47 "Dockerfile": {},
48 "file": {},
49 "subdir": {},
50 },
51 },
52 {
53 origPath: "/.",
54 rootPath: "testdata/dir",
55 allowWildcards: true,
56 errFn: nilErr,
57 paths: map[string]struct{}{
58 "Dockerfile": {},
59 "file": {},
60 "subdir": {},
61 },
62 },
63 {
64 origPath: "subdir/",
65 rootPath: "testdata/dir",
66 allowWildcards: true,
67 errFn: nilErr,
68 paths: map[string]struct{}{
69 "subdir/": {},
70 },
71 },
72 {
73 origPath: "subdir",
74 rootPath: "testdata/dir",
75 allowWildcards: true,
76 errFn: nilErr,
77 paths: map[string]struct{}{
78 "subdir": {},
79 },
80 },
81 {
82 origPath: ".",
83 dstPath: "copy",
84 rootPath: "testdata/dir",
85 allowWildcards: true,
86 errFn: nilErr,
87 paths: map[string]struct{}{
88 "file": {},
89 "Dockerfile": {},
90 "subdir": {},
91 },
92 rebaseNames: map[string]string{
93 "file": "copy/file",
94 "Dockerfile": "copy/Dockerfile",
95 "subdir": "copy/subdir",
96 },
97 },
98 {
99 origPath: ".",
100 dstPath: "copy",
101 rootPath: "testdata/singlefile",
102 allowWildcards: true,
103 errFn: nilErr,
104 paths: map[string]struct{}{
105 "Dockerfile": {},
106 },
107 rebaseNames: map[string]string{
108 "Dockerfile": "copy/Dockerfile",
109 },
110 },
111 {
112 origPath: "Dockerfile",
113 dstPath: "copy",
114 rootPath: "testdata/singlefile",
115 allowWildcards: true,
116 errFn: nilErr,
117 paths: map[string]struct{}{
118 "Dockerfile": {},
119 },
120 rebaseNames: map[string]string{
121 "Dockerfile": "copy",
122 },
123 },
124 {
125 origPath: "Dockerfile",
126 dstPath: "copy",
127 check: map[string]bool{"copy": true},
128 rootPath: "testdata/singlefile",
129 allowWildcards: true,
130 errFn: nilErr,
131 paths: map[string]struct{}{
132 "Dockerfile": {},
133 },
134 rebaseNames: map[string]string{
135 "Dockerfile": "copy/Dockerfile",
136 },
137 },
138 {
139 origPath: "existing/",
140 dstPath: ".",
141 rootPath: "testdata/overlapdir",
142 allowWildcards: true,
143 errFn: nilErr,
144 paths: map[string]struct{}{
145 "existing/": {},
146 },
147 rebaseNames: map[string]string{
148 "existing": ".",
149 },
150 },
151 {
152 origPath: "existing",
153 dstPath: ".",
154 rootPath: "testdata/overlapdir",
155 allowWildcards: true,
156 errFn: nilErr,
157 paths: map[string]struct{}{
158 "existing": {},
159 },
160 rebaseNames: map[string]string{
161 "existing": ".",
162 },
163 },
164 {
165 origPath: "existing",
166 dstPath: "/",
167 rootPath: "testdata/overlapdir",
168 allowWildcards: true,
169 errFn: nilErr,
170 paths: map[string]struct{}{
171 "existing": {},
172 },
173 rebaseNames: map[string]string{
174 "existing": "/",
175 },
176 },
177 {
178 origPath: "subdir/.",
179 rootPath: "testdata/dir",
180 allowWildcards: true,
181 errFn: nilErr,
182 paths: map[string]struct{}{
183 "subdir/": {},
184 },
185 },
186 {
187 origPath: "testdata/dir/subdir/.",
188 rootPath: "",
189 allowWildcards: true,
190 errFn: nilErr,
191 paths: map[string]struct{}{
192 "testdata/dir/subdir/": {},
193 },
194 },
195 {
196 origPath: "subdir/",
197 rootPath: "testdata/dir",
198 allowWildcards: true,
199 errFn: nilErr,
200 paths: map[string]struct{}{
201 "subdir/": {},
202 },
203 },
204 {
205 origPath: "subdir/",
206 rootPath: "testdata/dir",
207 allowWildcards: true,
208 errFn: nilErr,
209 paths: map[string]struct{}{
210 "subdir/": {},
211 },
212 dstPath: "test/",
213 rebaseNames: map[string]string{
214 "subdir": "test",
215 },
216 },
217 {
218 origPath: "dir",
219 dstPath: "/dir",
220 check: map[string]bool{"dir": false},
221 rootPath: "testdata/copydir",
222 allowWildcards: true,
223 errFn: nilErr,
224 paths: map[string]struct{}{
225 "dir": {},
226 },
227 rebaseNames: map[string]string{
228 "dir": "dir",
229 },
230 },
231 }
232
233 for i, test := range tests {
234 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
235 infos, err := CalcCopyInfo(test.origPath, test.rootPath, test.allowWildcards)
236 if !test.errFn(err) {
237 t.Fatalf("unexpected error: %v", err)
238 }
239 if err != nil {
240 return
241 }
242 expect := make(map[string]struct{})
243 for k := range test.paths {
244 expect[k] = struct{}{}
245 }
246 for _, info := range infos {
247 if _, ok := expect[info.Path]; ok {
248 delete(expect, info.Path)
249 } else {
250 t.Errorf("did not expect path %s", info.Path)
251 }
252 }
253 if len(expect) > 0 {
254 t.Errorf("did not see paths: %#v", expect)
255 }
256
257 options, err := archiveOptionsFor("", infos, test.dstPath, test.excludes, test.download, testDirectoryCheck(test.check))
258 if err != nil {
259 t.Fatal(err)
260 }
261 if !reflect.DeepEqual(test.rebaseNames, options.RebaseNames) {
262 t.Errorf("rebase names did not match:\n%#v\n%#v", test.rebaseNames, options.RebaseNames)
263 }
264 })
265 }
266 }