forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsync.go
223 lines (188 loc) · 8.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
package cli
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/labels"
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", "jenkins", "jenkins-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() {
oc.SetOutputDir(exutil.TestContext.OutputDir)
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 jenkins service get endpoints")
err = oc.KubeFramework().WaitForAnEndpoint("jenkins")
o.Expect(err).NotTo(o.HaveOccurred())
g.By("Getting the jenkins pod name")
selector, _ := labels.Parse("name=jenkins")
pods, err := oc.KubeREST().Pods(oc.Namespace()).List(kapi.ListOptions{LabelSelector: selector})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(len(pods.Items)).ToNot(o.BeZero())
podName = pods.Items[0].Name
})
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 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 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 jenkins directory from the pod to the temp directory: oc rsync %s:/var/lib/jenkins %s", podName, tempDir))
err = oc.Run("rsync").Args(
fmt.Sprintf("%s:/var/lib/jenkins", podName),
tempDir).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
localJenkinsDir := filepath.Join(tempDir, "jenkins")
g.By("By changing the permissions on the local jenkins directory")
err = os.Chmod(localJenkinsDir, 0700)
o.Expect(err).NotTo(o.HaveOccurred())
g.By(fmt.Sprintf("Copying the local jenkins directory to the pod with no flags: oc rsync %s/ %s:/var/lib/jenkins", localJenkinsDir, podName))
err = oc.Run("rsync").Args(
fmt.Sprintf("%s/", localJenkinsDir),
fmt.Sprintf("%s:/var/lib/jenkins", 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 jenkins directory to the pod with: oc rsync %s/ %s:/var/lib/jenkins --no-perms", localJenkinsDir, podName))
err = oc.Run("rsync").Args(
fmt.Sprintf("%s/", localJenkinsDir),
fmt.Sprintf("%s:/var/lib/jenkins", podName),
"--no-perms").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
})
})
})