-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
173 lines (140 loc) · 4.09 KB
/
main_test.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
package main
import (
"os"
"sort"
"strings"
"sync"
"testing"
)
func TestFilesList(t *testing.T) {
cacheFile := opts.CacheFile
opts.CacheFile = "test/.cacheEmpty.txt"
current, diff := filesLists()
if current["barbaz.txt"] != "dac2e8bd758efb58a30f9fcd7ac28b1b" ||
current["foobar.html"] != "01677e4c0ae5468b9b8b823487f14524" {
t.Error("Current list does not match expectation")
}
sort.Strings(diff)
if strings.Join(diff, ":") != "barbaz.txt:foobar.html" {
t.Error("Expected diff to hold barbaz.txt and foobar.html")
}
opts.CacheFile = cacheFile
}
func TestUpload(t *testing.T) {
upFn, uploads := fakeUploaderGen()
up := make(chan *sourceFile)
rejected := &syncedlist{}
wgUploads, wgWorkers := new(sync.WaitGroup), new(sync.WaitGroup)
wgUploads.Add(2)
wgWorkers.Add(1)
opts.verbose = false
opts.quiet = true
go upload("a", upFn, up, rejected, wgUploads, wgWorkers)
up <- newSourceFile("foobar.html")
up <- newSourceFile("barbaz.txt")
wgUploads.Wait()
close(up)
wgWorkers.Wait()
opts.quiet = false
if len(*uploads) != 2 {
t.Fatal("Expected to upload 2 files, got", *uploads)
}
}
func TestUploadDryRun(t *testing.T) {
upFn, uploads := fakeUploaderGen()
up := make(chan *sourceFile)
rejected := &syncedlist{}
wgUploads, wgWorkers := new(sync.WaitGroup), new(sync.WaitGroup)
wgUploads.Add(2)
wgWorkers.Add(1)
origDry := opts.dryRun
opts.dryRun = true
opts.verbose = false
opts.quiet = true
go upload("b", upFn, up, rejected, wgUploads, wgWorkers)
up <- newSourceFile("foobar.html")
up <- newSourceFile("barbaz.txt")
wgUploads.Wait()
close(up)
wgWorkers.Wait()
opts.dryRun = origDry
opts.quiet = false
if len(*uploads) > 0 {
t.Fatal("Expected to get a blank uploads list, got", *uploads)
}
}
func TestUploadUnrecoverable(t *testing.T) {
upFn, uploads := fakeUploaderGen(fatalError)
up := make(chan *sourceFile)
rejected := &syncedlist{}
wgUploads, wgWorkers := new(sync.WaitGroup), new(sync.WaitGroup)
wgUploads.Add(2)
wgWorkers.Add(1)
opts.verbose = false
opts.quiet = true
go upload("c", upFn, up, rejected, wgUploads, wgWorkers)
up <- newSourceFile("foobar.html")
up <- newSourceFile("barbaz.txt")
wgUploads.Wait()
close(up)
wgWorkers.Wait()
opts.quiet = false
if len(*uploads) != 2 {
t.Fatal("Expected both uploads to be processed, got", *uploads)
}
if len(rejected.list) != 2 {
t.Fatal("Expected all of the uploads to be rejected, got", rejected.list)
}
}
func TestUploadRecoverable(t *testing.T) {
upFn, uploads := fakeUploaderGen(recoverableError)
_ = uploads
up := make(chan *sourceFile)
rejected := &syncedlist{}
wgUploads, wgWorkers := new(sync.WaitGroup), new(sync.WaitGroup)
wgUploads.Add(2)
wgWorkers.Add(2)
opts.quiet = true
opts.verbose = false
go upload("d", upFn, up, rejected, wgUploads, wgWorkers)
go upload("e", upFn, up, rejected, wgUploads, wgWorkers)
sf1, sf2 := newSourceFile("barbaz.txt"), newSourceFile("foobar.html")
up <- sf1
up <- sf2
wgUploads.Wait()
close(up)
wgWorkers.Wait()
opts.quiet = false
if lu := len(*uploads); lu != 2*maxTries {
t.Fatal("Expected both uploads to be processed maxTries, got", lu, "attempts")
}
if sf1.attempts != maxTries || sf2.attempts != maxTries {
t.Fatal("Expected both files to have their attempts exhausted got", sf1.attempts, "and", sf2.attempts)
}
if len(rejected.list) != 2 {
t.Fatal("Expected all of the uploads to be rejected, got", rejected.list)
}
}
func TestIntegrationMain(t *testing.T) {
if _, err := os.Create(opts.CacheFile); err != nil {
t.Fatal("Failed to truncate the cache file")
}
upFn, uploads := fakeUploaderGen()
_ = upFn
opts.Region = "us-west-1"
opts.quiet = true
main()
opts.quiet = false
fnames := make([]string, len(*uploads))
for k, v := range *uploads {
fnames[k] = v.fname
}
sort.Strings(fnames)
t.Skip("Not really tested for now, other than seeing that it does not break. Will need some assertions.")
// if expected, actual := "barbaz.txt:foobar.html", strings.Join(fnames, ":"); expected != actual {
// t.Fatalf("Expected %s to be uploaded got %s", expected, actual)
// }
}
func TestIntegrationPartialUpload(t *testing.T) {
t.Skip()
}