Skip to content

Commit 9aaec41

Browse files
author
Florian
committed
Remove-Test applied
1 parent dc2046f commit 9aaec41

1 file changed

Lines changed: 117 additions & 179 deletions

File tree

pkg/devspace/sync/sync_config_test.go

Lines changed: 117 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package sync
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"os"
76
"path"
7+
"path/filepath"
88
"runtime"
9+
"strings"
910
"testing"
1011
"time"
1112

@@ -37,87 +38,6 @@ func createTestSyncClient(testLocalPath, testRemotePath string) *SyncConfig {
3738
}
3839
}
3940

40-
func removeFolderAndWait(from, to, postfix string) error {
41-
foldernameFrom := path.Join(from, "testFolder"+postfix)
42-
foldernameTo := path.Join(to, "testFolder"+postfix)
43-
44-
os.RemoveAll(foldernameFrom)
45-
46-
for i := 0; i < 50; i++ {
47-
if _, err := os.Stat(foldernameTo); err != nil {
48-
return nil
49-
}
50-
51-
time.Sleep(time.Millisecond * 100)
52-
}
53-
54-
return fmt.Errorf("Removing folder %s wasn't correctly synced to %s", foldernameFrom, foldernameTo)
55-
}
56-
57-
func removeFileAndWait(from, to, postfix string) error {
58-
filenameFrom := path.Join(from, "testFile"+postfix)
59-
filenameTo := path.Join(to, "testFile"+postfix)
60-
61-
os.Remove(filenameFrom)
62-
63-
for i := 0; i < 50; i++ {
64-
if _, err := os.Stat(filenameTo); err != nil {
65-
return nil
66-
}
67-
68-
time.Sleep(time.Millisecond * 100)
69-
}
70-
71-
return fmt.Errorf("Removing file %s wasn't correctly synced to %s", filenameFrom, filenameTo)
72-
}
73-
74-
func createFolderAndWait(from, to, postfix string) error {
75-
foldernameFrom := path.Join(from, "testFolder"+postfix)
76-
foldernameTo := path.Join(to, "testFolder"+postfix)
77-
78-
os.Mkdir(foldernameFrom, 0755)
79-
80-
for i := 0; i < 50; i++ {
81-
if stat, err := os.Stat(foldernameTo); err == nil {
82-
if stat.IsDir() == false {
83-
return fmt.Errorf("Created folder %s from is a file in destination %s", foldernameFrom, foldernameTo)
84-
}
85-
86-
return nil
87-
}
88-
89-
time.Sleep(time.Millisecond * 100)
90-
}
91-
92-
return fmt.Errorf("Created folder %s wasn't correctly synced to %s", foldernameFrom, foldernameTo)
93-
}
94-
95-
func createFileAndWait(from, to, postfix string) error {
96-
filenameFrom := path.Join(from, "testFile"+postfix)
97-
filenameTo := path.Join(to, "testFile"+postfix)
98-
fileContents := "testFile" + postfix
99-
100-
ioutil.WriteFile(filenameFrom, []byte(fileContents), 0666)
101-
102-
for i := 0; i < 50; i++ {
103-
time.Sleep(time.Millisecond * 100)
104-
105-
if _, err := os.Stat(filenameTo); err == nil {
106-
data, err := ioutil.ReadFile(filenameTo)
107-
if err != nil {
108-
continue
109-
}
110-
if string(data) != fileContents {
111-
continue
112-
}
113-
114-
return nil
115-
}
116-
}
117-
118-
return fmt.Errorf("Created file %s wasn't correctly synced to %s", filenameFrom, filenameTo)
119-
}
120-
12141
func TestInitialSync(t *testing.T) {
12242
if runtime.GOOS == "windows" {
12343
t.Skip("Skipping test on windows")
@@ -154,7 +74,7 @@ func TestInitialSync(t *testing.T) {
15474
return
15575
}
15676

157-
filesToCheck, foldersToCheck := createTestFilesAndFolders(local, remote, syncClient)
77+
filesToCheck, foldersToCheck := createTestFilesAndFolders(local, remote)
15878

15979
go syncClient.startUpstream()
16080

@@ -210,109 +130,18 @@ func TestNormalSync(t *testing.T) {
210130

211131
<-syncClient.readyChan
212132

213-
filesToCheck, foldersToCheck := createTestFilesAndFolders(local, remote, syncClient)
214-
215-
checkFilesAndFolders(t, filesToCheck, foldersToCheck, local, remote, 10*time.Second)
216-
217-
return
218-
}
219-
220-
func TestRunningSync(t *testing.T) {
221-
if runtime.GOOS == "windows" {
222-
t.Skip("Skipping test on windows")
223-
}
224-
225-
remote, local := initTestDirs(t)
226-
defer os.RemoveAll(remote)
227-
defer os.RemoveAll(local)
228-
229-
syncClient := createTestSyncClient(local, remote)
230-
defer syncClient.Stop()
231-
232-
syncClient.errorChan = make(chan error)
233-
234-
// Start client
235-
err := syncClient.setup()
236-
if err != nil {
237-
t.Errorf("Couldn't init test sync client: %v", err)
238-
return
239-
}
240-
241-
// Start upstream
242-
err = syncClient.upstream.start()
133+
filesToCheck, foldersToCheck := createTestFilesAndFolders(local, remote)
243134
if err != nil {
244135
t.Error(err)
245-
return
246-
}
247-
248-
// Start downstream
249-
err = syncClient.downstream.start()
250-
if err != nil {
251-
t.Error(err)
252-
return
253-
}
254-
255-
// Start sync and do initial sync
256-
go syncClient.startUpstream()
257-
go syncClient.startDownstream()
258-
259-
// Create
260-
err = createFileAndWait(remote, local, "2")
261-
if err != nil {
262-
t.Error(err)
263-
return
264-
}
265-
266-
err = createFileAndWait(local, remote, "1")
267-
if err != nil {
268-
t.Error(err)
269-
return
270-
}
271-
272-
err = createFolderAndWait(local, remote, "1")
273-
if err != nil {
274-
t.Error(err)
275-
return
276-
}
277-
278-
err = createFolderAndWait(remote, local, "2")
279-
if err != nil {
280-
t.Error(err)
281-
return
282-
}
283-
284-
// Remove
285-
err = removeFileAndWait(local, remote, "1")
286-
if err != nil {
287-
t.Error(err)
288-
return
289-
}
290-
291-
err = removeFileAndWait(remote, local, "2")
292-
if err != nil {
293-
t.Error(err)
294-
return
295-
}
296-
297-
err = removeFolderAndWait(local, remote, "1")
298-
if err != nil {
299-
t.Error(err)
300-
return
301136
}
137+
checkFilesAndFolders(t, filesToCheck, foldersToCheck, local, remote, 10*time.Second)
302138

303-
err = removeFolderAndWait(remote, local, "2")
139+
filesToCheck, foldersToCheck, err = removeSomeTestFilesAndFolders(local, remote, filesToCheck, foldersToCheck, "_Remove")
304140
if err != nil {
305141
t.Error(err)
306-
return
307142
}
143+
checkFilesAndFolders(t, filesToCheck, foldersToCheck, local, remote, 10*time.Second)
308144

309-
// Check if there is an error in the error channel
310-
select {
311-
case err = <-syncClient.errorChan:
312-
t.Error(err)
313-
return
314-
default:
315-
}
316145
}
317146

318147
func setExcludePaths(syncClient *SyncConfig) {
@@ -324,6 +153,12 @@ func setExcludePaths(syncClient *SyncConfig) {
324153
"ignoreFileRemote",
325154
"ignoreFolderRemote",
326155
"testFolder/ignoreFileRemote",
156+
"ignoreFileLocal_Remove",
157+
"ignoreFolderLocal_Remove",
158+
"testFolder/ignoreFileLocal_Remove",
159+
"ignoreFileRemote_Remove",
160+
"ignoreFolderRemote_Remove",
161+
"testFolder/ignoreFileRemote_Remove",
327162
}
328163

329164
syncClient.DownloadExcludePaths = []string{
@@ -333,6 +168,12 @@ func setExcludePaths(syncClient *SyncConfig) {
333168
"noDownloadFileRemote",
334169
"noDownloadFolderRemote",
335170
"testFolder/noDownloadFileRemote",
171+
"noDownloadFileLocal_Remove",
172+
"noDownloadFolderLocal_Remove",
173+
"testFolder/noDownloadFileLocal_Remove",
174+
"noDownloadFileRemote_Remove",
175+
"noDownloadFolderRemote_Remove",
176+
"testFolder/noDownloadFileRemote_Remove",
336177
}
337178

338179
syncClient.UploadExcludePaths = []string{
@@ -342,13 +183,19 @@ func setExcludePaths(syncClient *SyncConfig) {
342183
"noUploadFileRemote",
343184
"noUploadFolderRemote",
344185
"testFolder/noUploadFileRemote",
186+
"noUploadFileLocal_Remove",
187+
"noUploadFolderLocal_Remove",
188+
"testFolder/noUploadFileLocal_Remove",
189+
"noUploadFileRemote_Remove",
190+
"noUploadFolderRemote_Remove",
191+
"testFolder/noUploadFileRemote_Remove",
345192
}
346193

347194
syncClient.initIgnoreParsers()
348195

349196
}
350197

351-
func createTestFilesAndFolders(local string, remote string, syncClient *SyncConfig) ([]checkedFileOrFolder, []checkedFileOrFolder) {
198+
func createTestFilesAndFolders(local string, remote string) ([]checkedFileOrFolder, []checkedFileOrFolder) {
352199

353200
//Write local files
354201
ioutil.WriteFile(path.Join(local, "testFileLocal1"), []byte(fileContents), 0666)
@@ -388,6 +235,45 @@ func createTestFilesAndFolders(local string, remote string, syncClient *SyncConf
388235
ioutil.WriteFile(path.Join(remote, "testFolder", "noDownloadFileRemote"), []byte(fileContents), 0666)
389236
ioutil.WriteFile(path.Join(remote, "testFolder", "noUploadFileRemote"), []byte(fileContents), 0666)
390237

238+
//-----------The following files will be removed later-------------------------------------
239+
//Write local files
240+
ioutil.WriteFile(path.Join(local, "testFileLocal1_Remove"), []byte(fileContents), 0666)
241+
ioutil.WriteFile(path.Join(local, "testFileLocal2_Remove"), []byte(fileContents), 0666)
242+
ioutil.WriteFile(path.Join(local, "ignoreFileLocal_Remove"), []byte(fileContents), 0666)
243+
ioutil.WriteFile(path.Join(local, "noDownloadFileLocal_Remove"), []byte(fileContents), 0666)
244+
ioutil.WriteFile(path.Join(local, "noUploadFileLocal_Remove"), []byte(fileContents), 0666)
245+
246+
os.Mkdir(path.Join(local, "testFolder_Remove"), 0755)
247+
os.Mkdir(path.Join(local, "testFolderLocal_Remove"), 0755)
248+
os.Mkdir(path.Join(local, "ignoreFolderLocal_Remove"), 0755)
249+
os.Mkdir(path.Join(local, "noDownloadFolderLocal_Remove"), 0755)
250+
os.Mkdir(path.Join(local, "noUploadFolderLocal_Remove"), 0755)
251+
252+
ioutil.WriteFile(path.Join(local, "testFolder", "testFileLocal1_Remove"), []byte(fileContents), 0666)
253+
ioutil.WriteFile(path.Join(local, "testFolder", "testFileLocal2_Remove"), []byte(fileContents), 0666)
254+
ioutil.WriteFile(path.Join(local, "testFolder", "ignoreFileLocal_Remove"), []byte(fileContents), 0666)
255+
ioutil.WriteFile(path.Join(local, "testFolder", "noDownloadFileLocal_Remove"), []byte(fileContents), 0666)
256+
ioutil.WriteFile(path.Join(local, "testFolder", "noUploadFileLocal_Remove"), []byte(fileContents), 0666)
257+
258+
// Write remote files
259+
ioutil.WriteFile(path.Join(remote, "testFileRemote1_Remove"), []byte(fileContents), 0666)
260+
ioutil.WriteFile(path.Join(remote, "testFileRemote2_Remove"), []byte(fileContents), 0666)
261+
ioutil.WriteFile(path.Join(remote, "ignoreFileRemote_Remove"), []byte(fileContents), 0666)
262+
ioutil.WriteFile(path.Join(remote, "noDownloadFileRemote_Remove"), []byte(fileContents), 0666)
263+
ioutil.WriteFile(path.Join(remote, "noUploadFileRemote_Remove"), []byte(fileContents), 0666)
264+
265+
os.Mkdir(path.Join(remote, "testFolder_Remove"), 0755)
266+
os.Mkdir(path.Join(remote, "testFolderRemote_Remove"), 0755)
267+
os.Mkdir(path.Join(remote, "ignoreFolderRemote_Remove"), 0755)
268+
os.Mkdir(path.Join(remote, "noDownloadFolderRemote_Remove"), 0755)
269+
os.Mkdir(path.Join(remote, "noUploadFolderRemote_Remove"), 0755)
270+
271+
ioutil.WriteFile(path.Join(remote, "testFolder", "testFileRemote1_Remove"), []byte(fileContents), 0666)
272+
ioutil.WriteFile(path.Join(remote, "testFolder", "testFileRemote2_Remove"), []byte(fileContents), 0666)
273+
ioutil.WriteFile(path.Join(remote, "testFolder", "ignoreFileRemote_Remove"), []byte(fileContents), 0666)
274+
ioutil.WriteFile(path.Join(remote, "testFolder", "noDownloadFileRemote_Remove"), []byte(fileContents), 0666)
275+
ioutil.WriteFile(path.Join(remote, "testFolder", "noUploadFileRemote_Remove"), []byte(fileContents), 0666)
276+
391277
filesToCheck := []checkedFileOrFolder{
392278
checkedFileOrFolder{
393279
path: "testFileLocal1",
@@ -541,9 +427,61 @@ func createTestFilesAndFolders(local string, remote string, syncClient *SyncConf
541427
},
542428
}
543429

430+
for _, f := range filesToCheck {
431+
removeEquivalent := checkedFileOrFolder{
432+
path: f.path + "_Remove",
433+
shouldExistInLocal: f.shouldExistInLocal,
434+
shouldExistInRemote: f.shouldExistInRemote,
435+
}
436+
filesToCheck = append(filesToCheck, removeEquivalent)
437+
}
438+
for _, f := range foldersToCheck {
439+
removeEquivalent := checkedFileOrFolder{
440+
path: f.path + "_Remove",
441+
shouldExistInLocal: f.shouldExistInLocal,
442+
shouldExistInRemote: f.shouldExistInRemote,
443+
}
444+
foldersToCheck = append(foldersToCheck, removeEquivalent)
445+
}
446+
544447
return filesToCheck, foldersToCheck
545448
}
546449

450+
func removeSomeTestFilesAndFolders(local string, remote string, filesToCheck []checkedFileOrFolder, foldersToCheck []checkedFileOrFolder, removeSuffix string) ([]checkedFileOrFolder, []checkedFileOrFolder, error) {
451+
452+
removeIfSuffixMatch := func(path string, f os.FileInfo, err error) error {
453+
if strings.HasSuffix(path, removeSuffix) {
454+
return os.RemoveAll(path)
455+
}
456+
return nil
457+
}
458+
459+
err := filepath.Walk(remote, removeIfSuffixMatch)
460+
if err != nil {
461+
return nil, nil, err
462+
}
463+
err = filepath.Walk(local, removeIfSuffixMatch)
464+
if err != nil {
465+
return nil, nil, err
466+
}
467+
468+
for n, f := range filesToCheck {
469+
if strings.HasSuffix(f.path, removeSuffix) {
470+
filesToCheck[n].shouldExistInLocal = false
471+
filesToCheck[n].shouldExistInRemote = false
472+
}
473+
}
474+
475+
for n, f := range foldersToCheck {
476+
if strings.HasSuffix(f.path, removeSuffix) {
477+
foldersToCheck[n].shouldExistInLocal = false
478+
foldersToCheck[n].shouldExistInRemote = false
479+
}
480+
}
481+
482+
return filesToCheck, foldersToCheck, nil
483+
}
484+
547485
func TestCreateDirInFileMap(t *testing.T) {
548486
sync := SyncConfig{
549487
fileIndex: newFileIndex(),

0 commit comments

Comments
 (0)