|
1 | 1 | package cli |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "fmt" |
5 | 6 | "io/ioutil" |
6 | 7 | "os" |
7 | 8 | "path/filepath" |
8 | 9 | "strings" |
| 10 | + "time" |
9 | 11 |
|
10 | 12 | g "github.com/onsi/ginkgo" |
11 | 13 | o "github.com/onsi/gomega" |
12 | 14 |
|
13 | 15 | kapi "k8s.io/kubernetes/pkg/api" |
14 | 16 | "k8s.io/kubernetes/pkg/labels" |
| 17 | + "k8s.io/kubernetes/test/e2e" |
15 | 18 |
|
16 | 19 | exutil "github.com/openshift/origin/test/extended/util" |
17 | 20 | ) |
@@ -47,8 +50,144 @@ var _ = g.Describe("[cli][Slow] can use rsync to upload files to pods", func() { |
47 | 50 | podName = pods.Items[0].Name |
48 | 51 | }) |
49 | 52 |
|
50 | | - g.Describe("copy by strategy", func() { |
| 53 | + g.Describe("using a watch", func() { |
| 54 | + g.It("should watch for changes and rsync them", func() { |
| 55 | + g.By("Creating a local temporary directory") |
| 56 | + tempDir, err := ioutil.TempDir("", "rsync") |
| 57 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 58 | + |
| 59 | + g.By("creating a subdirectory in that temp directory") |
| 60 | + subdir1 := filepath.Join(tempDir, "subdir1") |
| 61 | + err = os.Mkdir(subdir1, 0777) |
| 62 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 63 | + |
| 64 | + g.By("creating a file in the subdirectory") |
| 65 | + subdir1file1 := filepath.Join(subdir1, "file1") |
| 66 | + _, err = os.Create(subdir1file1) |
| 67 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 68 | + |
| 69 | + g.By("Creating a scratch directory in the pod") |
| 70 | + _, err = oc.Run("rsh").Args(podName, "mkdir", "/tmp/rsync").Output() |
| 71 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 72 | + |
| 73 | + g.By(fmt.Sprintf("Calling oc rsync %s/ %s:/tmp/rsync --delete --watch", tempDir, podName)) |
| 74 | + cmd, stdout, stderr, err := oc.Run("rsync").Args( |
| 75 | + fmt.Sprintf("%s/", tempDir), |
| 76 | + fmt.Sprintf("%s:/tmp/rsync", podName), |
| 77 | + "--loglevel=5", |
| 78 | + "--delete", |
| 79 | + "--watch").Background() |
| 80 | + |
| 81 | + failed := true |
| 82 | + defer cmd.Process.Kill() |
| 83 | + defer func() { |
| 84 | + if failed { |
| 85 | + writer := cmd.Stdout.(*bufio.Writer) |
| 86 | + writer.Flush() |
| 87 | + writer2 := cmd.Stderr.(*bufio.Writer) |
| 88 | + writer2.Flush() |
| 89 | + fmt.Fprintf(g.GinkgoWriter, "Dumping rsync output: \n%s\n%s\n", stdout.String(), stderr.String()) |
| 90 | + } |
| 91 | + }() |
| 92 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 93 | + |
| 94 | + var result string |
| 95 | + found := false |
| 96 | + for i := 0; i < 12; i++ { |
| 97 | + g.By("Verifying that files are copied to the container") |
| 98 | + result, _ = oc.Run("rsh").Args(podName, "ls", "/tmp/rsync/subdir1").Output() |
| 99 | + if strings.Contains(result, "file1") { |
| 100 | + found = true |
| 101 | + break |
| 102 | + } |
| 103 | + time.Sleep(5 * time.Second) |
| 104 | + } |
| 105 | + if !found { |
| 106 | + e2e.Failf("Directory does not contain expected files: \n%s", result) |
| 107 | + } |
| 108 | + |
| 109 | + g.By("renaming file1 to file2") |
| 110 | + subdir1file2 := filepath.Join(subdir1, "file2") |
| 111 | + err = os.Rename(subdir1file1, subdir1file2) |
| 112 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 113 | + |
| 114 | + found = false |
| 115 | + for i := 0; i < 12; i++ { |
| 116 | + g.By("Verifying that files are copied to the container") |
| 117 | + result, _ = oc.Run("rsh").Args(podName, "ls", "/tmp/rsync/subdir1").Output() |
| 118 | + if strings.Contains(result, "file2") && !strings.Contains(result, "file1") { |
| 119 | + found = true |
| 120 | + break |
| 121 | + } |
| 122 | + time.Sleep(5 * time.Second) |
| 123 | + } |
| 124 | + if !found { |
| 125 | + e2e.Failf("Directory does not contain expected files: \n%s", result) |
| 126 | + } |
| 127 | + |
| 128 | + g.By("removing file2") |
| 129 | + err = os.Remove(subdir1file2) |
| 130 | + o.Expect(err).NotTo(o.HaveOccurred()) |
51 | 131 |
|
| 132 | + found = false |
| 133 | + for i := 0; i < 12; i++ { |
| 134 | + g.By("Verifying that files are copied to the container") |
| 135 | + result, _ = oc.Run("rsh").Args(podName, "ls", "/tmp/rsync/subdir1").Output() |
| 136 | + if !strings.Contains(result, "file2") { |
| 137 | + found = true |
| 138 | + break |
| 139 | + } |
| 140 | + time.Sleep(5 * time.Second) |
| 141 | + } |
| 142 | + if !found { |
| 143 | + e2e.Failf("Directory does not contain expected files: \n%s", result) |
| 144 | + } |
| 145 | + |
| 146 | + g.By("renaming subdir1 to subdir2") |
| 147 | + subdir2 := filepath.Join(tempDir, "subdir2") |
| 148 | + err = os.Rename(subdir1, subdir2) |
| 149 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 150 | + |
| 151 | + g.By("creating a file in the subdir2") |
| 152 | + subdir2file1 := filepath.Join(subdir2, "file1") |
| 153 | + _, err = os.Create(subdir2file1) |
| 154 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 155 | + |
| 156 | + found = false |
| 157 | + for i := 0; i < 12; i++ { |
| 158 | + g.By("Verifying that files are copied to the container") |
| 159 | + result, _ = oc.Run("rsh").Args(podName, "ls", "/tmp/rsync/subdir2").Output() |
| 160 | + if !strings.Contains(result, "file1") { |
| 161 | + found = true |
| 162 | + break |
| 163 | + } |
| 164 | + time.Sleep(5 * time.Second) |
| 165 | + } |
| 166 | + if !found { |
| 167 | + e2e.Failf("Directory does not contain expected files: \n%s", result) |
| 168 | + } |
| 169 | + |
| 170 | + g.By("removing subdir2") |
| 171 | + err = os.RemoveAll(subdir2) |
| 172 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 173 | + |
| 174 | + found = false |
| 175 | + for i := 0; i < 12; i++ { |
| 176 | + g.By("Verifying that files are copied to the container") |
| 177 | + result, _ = oc.Run("rsh").Args(podName, "ls", "/tmp/rsync").Output() |
| 178 | + if !strings.Contains(result, "subdir2") { |
| 179 | + found = true |
| 180 | + break |
| 181 | + } |
| 182 | + time.Sleep(5 * time.Second) |
| 183 | + } |
| 184 | + if !found { |
| 185 | + e2e.Failf("Directory does not contain expected files: \n%s", result) |
| 186 | + } |
| 187 | + failed = false |
| 188 | + }) |
| 189 | + }) |
| 190 | + g.Describe("copy by strategy", func() { |
52 | 191 | testRsyncFn := func(strategy string) func() { |
53 | 192 | return func() { |
54 | 193 | g.By(fmt.Sprintf("Calling oc rsync %s %s:/tmp --strategy=%s", sourcePath1, podName, strategy)) |
@@ -219,5 +358,4 @@ var _ = g.Describe("[cli][Slow] can use rsync to upload files to pods", func() { |
219 | 358 | o.Expect(err).NotTo(o.HaveOccurred()) |
220 | 359 | }) |
221 | 360 | }) |
222 | | - |
223 | 361 | }) |
0 commit comments