forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsync.go
395 lines (343 loc) · 14.5 KB
/
rsync.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package cli
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
e2e "k8s.io/kubernetes/test/e2e/framework"
exutil "github.com/openshift/origin/test/extended/util"
)
var _ = g.Describe("[cli][Slow] can use rsync to upload files to pods", func() {
defer g.GinkgoRecover()
var (
oc = exutil.NewCLI("cli-rsync", exutil.KubeConfigPath())
templatePath = exutil.FixturePath("..", "..", "examples", "db-templates", "mariadb-ephemeral-template.json")
sourcePath1 = exutil.FixturePath("..", "..", "examples", "image-streams")
sourcePath2 = exutil.FixturePath("..", "..", "examples", "sample-app")
strategies = []string{"rsync", "rsync-daemon", "tar"}
)
var podName string
g.JustBeforeEach(func() {
g.By(fmt.Sprintf("calling oc new-app -f %q", templatePath))
err := oc.Run("new-app").Args("-f", templatePath).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("expecting the mariadb service get endpoints")
err = e2e.WaitForEndpoint(oc.KubeFramework().ClientSet, oc.Namespace(), "mariadb")
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Getting the mariadb pod name")
selector, _ := labels.Parse("name=mariadb")
pods, err := oc.KubeClient().Core().Pods(oc.Namespace()).List(metav1.ListOptions{LabelSelector: selector.String()})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(len(pods.Items)).ToNot(o.BeZero())
podName = pods.Items[0].Name
})
g.Describe("using a watch", func() {
g.It("should watch for changes and rsync them", func() {
g.By("Creating a local temporary directory")
tempDir, err := ioutil.TempDir("", "rsync")
o.Expect(err).NotTo(o.HaveOccurred())
g.By("creating a subdirectory in that temp directory")
subdir1 := filepath.Join(tempDir, "subdir1")
err = os.Mkdir(subdir1, 0777)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("creating a file in the subdirectory")
subdir1file1 := filepath.Join(subdir1, "file1")
_, err = os.Create(subdir1file1)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Creating a scratch directory in the pod")
_, err = oc.Run("rsh").Args(podName, "mkdir", "/tmp/rsync").Output()
o.Expect(err).NotTo(o.HaveOccurred())
g.By(fmt.Sprintf("Calling oc rsync %s/ %s:/tmp/rsync --delete --watch", tempDir, podName))
cmd, stdout, stderr, err := oc.Run("rsync").Args(
fmt.Sprintf("%s/", tempDir),
fmt.Sprintf("%s:/tmp/rsync", podName),
"--loglevel=5",
"--delete",
"--watch").Background()
failed := true
defer cmd.Process.Kill()
defer func() {
if failed {
writer := cmd.Stdout.(*bufio.Writer)
writer.Flush()
writer2 := cmd.Stderr.(*bufio.Writer)
writer2.Flush()
fmt.Fprintf(g.GinkgoWriter, "Dumping rsync output: \n%s\n%s\n", stdout.String(), stderr.String())
}
}()
o.Expect(err).NotTo(o.HaveOccurred())
var result string
found := false
for i := 0; i < 12; i++ {
g.By("Verifying that files are copied to the container")
result, _ = oc.Run("rsh").Args(podName, "ls", "/tmp/rsync/subdir1").Output()
if strings.Contains(result, "file1") {
found = true
break
}
time.Sleep(5 * time.Second)
}
if !found {
e2e.Failf("Directory does not contain expected files: \n%s", result)
}
g.By("renaming file1 to file2")
subdir1file2 := filepath.Join(subdir1, "file2")
err = os.Rename(subdir1file1, subdir1file2)
o.Expect(err).NotTo(o.HaveOccurred())
found = false
for i := 0; i < 12; i++ {
g.By("Verifying that files are copied to the container")
result, _ = oc.Run("rsh").Args(podName, "ls", "/tmp/rsync/subdir1").Output()
if strings.Contains(result, "file2") && !strings.Contains(result, "file1") {
found = true
break
}
time.Sleep(5 * time.Second)
}
if !found {
e2e.Failf("Directory does not contain expected files: \n%s", result)
}
g.By("removing file2")
err = os.Remove(subdir1file2)
o.Expect(err).NotTo(o.HaveOccurred())
found = false
for i := 0; i < 12; i++ {
g.By("Verifying that files are copied to the container")
result, _ = oc.Run("rsh").Args(podName, "ls", "/tmp/rsync/subdir1").Output()
if !strings.Contains(result, "file2") {
found = true
break
}
time.Sleep(5 * time.Second)
}
if !found {
e2e.Failf("Directory does not contain expected files: \n%s", result)
}
g.By("renaming subdir1 to subdir2")
subdir2 := filepath.Join(tempDir, "subdir2")
err = os.Rename(subdir1, subdir2)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("creating a file in the subdir2")
subdir2file1 := filepath.Join(subdir2, "file1")
_, err = os.Create(subdir2file1)
o.Expect(err).NotTo(o.HaveOccurred())
found = false
for i := 0; i < 12; i++ {
g.By("Verifying that files are copied to the container")
result, _ = oc.Run("rsh").Args(podName, "ls", "/tmp/rsync/subdir2").Output()
if !strings.Contains(result, "file1") {
found = true
break
}
time.Sleep(5 * time.Second)
}
if !found {
e2e.Failf("Directory does not contain expected files: \n%s", result)
}
g.By("removing subdir2")
err = os.RemoveAll(subdir2)
o.Expect(err).NotTo(o.HaveOccurred())
found = false
for i := 0; i < 12; i++ {
g.By("Verifying that files are copied to the container")
result, _ = oc.Run("rsh").Args(podName, "ls", "/tmp/rsync").Output()
if !strings.Contains(result, "subdir2") {
found = true
break
}
time.Sleep(5 * time.Second)
}
if !found {
e2e.Failf("Directory does not contain expected files: \n%s", result)
}
failed = false
})
})
g.Describe("copy by strategy", func() {
testRsyncFn := func(strategy string) func() {
return func() {
g.By(fmt.Sprintf("Calling oc rsync %s %s:/tmp --strategy=%s", sourcePath1, podName, strategy))
err := oc.Run("rsync").Args(
sourcePath1,
fmt.Sprintf("%s:/tmp", podName),
fmt.Sprintf("--strategy=%s", strategy)).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Verifying that files are copied to the container")
result, err := oc.Run("rsh").Args(podName, "ls", "/tmp/image-streams").Output()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(result).To(o.ContainSubstring("image-streams-centos7.json"))
g.By(fmt.Sprintf("Calling oc rsync %s/ %s:/tmp/image-streams --strategy=%s --delete", sourcePath2, podName, strategy))
err = oc.Run("rsync").Args(
sourcePath2+"/",
fmt.Sprintf("%s:/tmp/image-streams", podName),
fmt.Sprintf("--strategy=%s", strategy),
"--delete").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Verifying that the expected files are in the container")
result, err = oc.Run("rsh").Args(podName, "ls", "/tmp/image-streams").Output()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(result).To(o.ContainSubstring("application-template-stibuild.json"))
o.Expect(result).NotTo(o.ContainSubstring("image-streams-centos7.json"))
g.By("Creating a local temporary directory")
tempDir, err := ioutil.TempDir("", "rsync")
o.Expect(err).NotTo(o.HaveOccurred())
g.By(fmt.Sprintf("Copying files from container to local directory: oc rsync %s:/tmp/image-streams/ %s --strategy=%s", podName, tempDir, strategy))
err = oc.Run("rsync").Args(
fmt.Sprintf("%s:/tmp/image-streams/", podName),
tempDir,
fmt.Sprintf("--strategy=%s", strategy)).Execute()
g.By(fmt.Sprintf("Verifying that files were copied to the local directory"))
files, err := ioutil.ReadDir(tempDir)
o.Expect(err).NotTo(o.HaveOccurred())
found := false
for _, f := range files {
if strings.Contains(f.Name(), "application-template-stibuild.json") {
found = true
break
}
}
o.Expect(found).To(o.BeTrue())
g.By(fmt.Sprintf("Copying files from container to local directory with --delete: oc rsync %s:/tmp/image-streams/ %s --strategy=%s", podName, tempDir, strategy))
originalName := "application-template-stibuild.json"
modifiedName := "application-template-stirenamed.json"
err = os.Rename(filepath.Join(tempDir, originalName), filepath.Join(tempDir, modifiedName))
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.Run("rsync").Args(
fmt.Sprintf("%s:/tmp/image-streams/", podName),
tempDir,
"--delete",
fmt.Sprintf("--strategy=%s", strategy)).Execute()
g.By(fmt.Sprintf("Verifying that the expected files are in the local directory"))
o.Expect(err).NotTo(o.HaveOccurred())
// After the copy with --delete, the file with 'modifiedName' should have been deleted
// and the file with 'originalName' should have been restored.
foundOriginal := false
foundModified := false
files, err = ioutil.ReadDir(tempDir)
for _, f := range files {
if strings.Contains(f.Name(), originalName) {
foundOriginal = true
}
if strings.Contains(f.Name(), modifiedName) {
foundModified = true
}
}
g.By("Verifying original file is in the local directory")
o.Expect(foundOriginal).To(o.BeTrue())
g.By("Verifying renamed file is not in the local directory")
o.Expect(foundModified).To(o.BeFalse())
g.By("Getting an error if copying to a destination directory where there is no write permission")
result, err = oc.Run("rsync").Args(
sourcePath1,
fmt.Sprintf("%s:/", podName),
fmt.Sprintf("--strategy=%s", strategy)).Output()
o.Expect(err).To(o.HaveOccurred())
}
}
for _, strategy := range strategies {
g.It(fmt.Sprintf("should copy files with the %s strategy", strategy), testRsyncFn(strategy))
}
})
g.Describe("rsync specific flags", func() {
g.It("should honor the --exclude flag", func() {
g.By(fmt.Sprintf("Calling oc rsync %s %s:/tmp --exclude=image-streams-rhel7.json", sourcePath1, podName))
err := oc.Run("rsync").Args(
sourcePath1,
fmt.Sprintf("%s:/tmp", podName),
"--exclude=image-streams-rhel7.json").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Verifying that files are copied to the container")
result, err := oc.Run("rsh").Args(podName, "ls", "/tmp/image-streams").Output()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(result).To(o.ContainSubstring("image-streams-centos7.json"))
o.Expect(result).NotTo(o.ContainSubstring("image-streams-rhel7.json"))
})
g.It("should honor multiple --exclude flags", func() {
g.By(fmt.Sprintf("Calling oc rsync %s %s:/tmp --exclude=application-template-custombuild.json --exclude=application-template-dockerbuild.json", sourcePath2, podName))
err := oc.Run("rsync").Args(
sourcePath2,
fmt.Sprintf("%s:/tmp", podName),
"--exclude=application-template-custombuild.json",
"--exclude=application-template-dockerbuild.json").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Verifying that files are copied to the container")
result, err := oc.Run("rsh").Args(podName, "ls", "/tmp/sample-app").Output()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(result).NotTo(o.ContainSubstring("application-template-custombuild.json"))
o.Expect(result).NotTo(o.ContainSubstring("application-template-dockerbuild.json"))
o.Expect(result).To(o.ContainSubstring("application-template-stibuild.json"))
})
g.It("should honor the --include flag", func() {
g.By(fmt.Sprintf("Calling oc rsync %s %s:/tmp --exclude=*.json --include=image-streams-rhel7.json", sourcePath1, podName))
err := oc.Run("rsync").Args(
sourcePath1,
fmt.Sprintf("%s:/tmp", podName),
"--exclude=*.json",
"--include=image-streams-rhel7.json").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Verifying that files are copied to the container")
result, err := oc.Run("rsh").Args(podName, "ls", "/tmp/image-streams").Output()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(result).To(o.ContainSubstring("image-streams-rhel7.json"))
o.Expect(result).NotTo(o.ContainSubstring("image-streams-centos7.json"))
})
g.It("should honor multiple --include flags", func() {
g.By(fmt.Sprintf("Calling oc rsync %s %s:/tmp --exclude=*.json --include=application-template-custombuild.json --include=application-template-dockerbuild.json", sourcePath2, podName))
err := oc.Run("rsync").Args(
sourcePath2,
fmt.Sprintf("%s:/tmp", podName),
"--exclude=*.json",
"--include=application-template-custombuild.json",
"--include=application-template-dockerbuild.json").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Verifying that files are copied to the container")
result, err := oc.Run("rsh").Args(podName, "ls", "/tmp/sample-app").Output()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(result).To(o.ContainSubstring("application-template-custombuild.json"))
o.Expect(result).To(o.ContainSubstring("application-template-dockerbuild.json"))
o.Expect(result).NotTo(o.ContainSubstring("application-template-stibuild.json"))
})
g.It("should honor the --progress flag", func() {
g.By(fmt.Sprintf("Calling oc rsync %s %s:/tmp --progress", sourcePath1, podName))
result, err := oc.Run("rsync").Args(
sourcePath1,
fmt.Sprintf("%s:/tmp", podName),
"--progress").Output()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(result).To(o.ContainSubstring("100%"))
})
g.It("should honor the --no-perms flag", func() {
g.By("Creating a temporary destination directory")
tempDir, err := ioutil.TempDir("", "rsync")
o.Expect(err).NotTo(o.HaveOccurred())
g.By(fmt.Sprintf("Copying the mariadb directory from the pod to the temp directory: oc rsync %s:/var/lib/mysql %s", podName, tempDir))
err = oc.Run("rsync").Args(
fmt.Sprintf("%s:/var/lib/mysql", podName),
tempDir).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
localmariadbDir := filepath.Join(tempDir, "mysql")
g.By("By changing the permissions on the local mariadb directory")
err = os.Chmod(localmariadbDir, 0700)
o.Expect(err).NotTo(o.HaveOccurred())
g.By(fmt.Sprintf("Copying the local mariadb directory to the pod with no flags: oc rsync %s/ %s:/var/lib/mysql", localmariadbDir, podName))
err = oc.Run("rsync").Args(
fmt.Sprintf("%s/", localmariadbDir),
fmt.Sprintf("%s:/var/lib/mysql", podName)).Execute()
// An error should occur trying to set permissions on the directory
o.Expect(err).To(o.HaveOccurred())
g.By(fmt.Sprintf("Copying the local mariadb directory to the pod with: oc rsync %s/ %s:/var/lib/mysql --no-perms", localmariadbDir, podName))
err = oc.Run("rsync").Args(
fmt.Sprintf("%s/", localmariadbDir),
fmt.Sprintf("%s:/var/lib/mysql", podName),
"--no-perms").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
})
})
})