forked from fergusstrange/embedded-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_fetch_test.go
236 lines (181 loc) · 6.31 KB
/
remote_fetch_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
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
package embeddedpostgres
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/mholt/archiver/v3"
"github.com/stretchr/testify/assert"
)
func Test_defaultRemoteFetchStrategy_ErrorWhenHttpGet(t *testing.T) {
remoteFetchStrategy := defaultRemoteFetchStrategy("http://localhost:1234",
testVersionStrategy(),
testCacheLocator())
err := remoteFetchStrategy()
assert.EqualError(t, err, "unable to connect to http://localhost:1234")
}
func Test_defaultRemoteFetchStrategy_ErrorWhenHttpStatusNot200(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL,
testVersionStrategy(),
testCacheLocator())
err := remoteFetchStrategy()
assert.EqualError(t, err, "no version found matching 1.2.3")
}
func Test_defaultRemoteFetchStrategy_ErrorWhenBodyReadIssue(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "1")
}))
defer server.Close()
remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL,
testVersionStrategy(),
testCacheLocator())
err := remoteFetchStrategy()
assert.EqualError(t, err, "error fetching postgres: unexpected EOF")
}
func Test_defaultRemoteFetchStrategy_ErrorWhenCannotUnzipSubFile(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
defer server.Close()
remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL,
testVersionStrategy(),
testCacheLocator())
err := remoteFetchStrategy()
assert.EqualError(t, err, "error fetching postgres: creating reader: zip: not a valid zip file")
}
func Test_defaultRemoteFetchStrategy_ErrorWhenCannotUnzip(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte("lolz")); err != nil {
panic(err)
}
}))
defer server.Close()
remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL,
testVersionStrategy(),
testCacheLocator())
err := remoteFetchStrategy()
assert.EqualError(t, err, "error fetching postgres: creating reader: zip: not a valid zip file")
}
func Test_defaultRemoteFetchStrategy_ErrorWhenNoSubTarArchive(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
zip := archiver.NewZip()
defer func() {
if err := zip.Close(); err != nil {
panic(err)
}
}()
if err := zip.Create(w); err != nil {
panic(err)
}
}))
defer server.Close()
remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL,
testVersionStrategy(),
testCacheLocator())
err := remoteFetchStrategy()
assert.EqualError(t, err, "error fetching postgres: cannot find binary in archive retrieved from "+server.URL+"/maven2/io/zonky/test/postgres/embedded-postgres-binaries-darwin-amd64/1.2.3/embedded-postgres-binaries-darwin-amd64-1.2.3.jar")
}
func Test_defaultRemoteFetchStrategy_ErrorWhenCannotExtractSubArchive(t *testing.T) {
jarFile, cleanUp := createTempZipArchive()
defer cleanUp()
dirBlockingExtract := filepath.Join(filepath.Dir(jarFile), "some_dir")
if err := os.MkdirAll(dirBlockingExtract, 0400); err != nil {
panic(err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bytes, err := ioutil.ReadFile(jarFile)
if err != nil {
panic(err)
}
if _, err := w.Write(bytes); err != nil {
panic(err)
}
}))
defer server.Close()
remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL,
testVersionStrategy(),
func() (s string, b bool) {
return dirBlockingExtract, false
})
err := remoteFetchStrategy()
assert.EqualError(t, err, "unable to extract postgres archive to "+dirBlockingExtract)
}
func Test_defaultRemoteFetchStrategy_ErrorWhenCannotCreateCacheDirectory(t *testing.T) {
jarFile, cleanUp := createTempZipArchive()
defer cleanUp()
fileBlockingExtractDirectory := filepath.Join(filepath.Dir(jarFile), "a_file_blocking_extract")
if _, err := os.Create(fileBlockingExtractDirectory); err != nil {
panic(err)
}
cacheLocation := filepath.Join(fileBlockingExtractDirectory, "cache_file.jar")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bytes, err := ioutil.ReadFile(jarFile)
if err != nil {
panic(err)
}
if _, err := w.Write(bytes); err != nil {
panic(err)
}
}))
defer server.Close()
remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL,
testVersionStrategy(),
func() (s string, b bool) {
return cacheLocation, false
})
err := remoteFetchStrategy()
assert.EqualError(t, err, "unable to extract postgres archive to "+cacheLocation)
}
func Test_defaultRemoteFetchStrategy_ErrorWhenCannotCreateSubArchiveFile(t *testing.T) {
jarFile, cleanUp := createTempZipArchive()
defer cleanUp()
cacheLocation := filepath.Join(filepath.Dir(jarFile), "extract_directory", "cache_file.jar")
if err := os.MkdirAll(cacheLocation, 0755); err != nil {
panic(err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bytes, err := ioutil.ReadFile(jarFile)
if err != nil {
panic(err)
}
if _, err := w.Write(bytes); err != nil {
panic(err)
}
}))
defer server.Close()
remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL,
testVersionStrategy(),
func() (s string, b bool) {
return cacheLocation, false
})
err := remoteFetchStrategy()
assert.EqualError(t, err, "unable to extract postgres archive to "+cacheLocation)
}
func Test_defaultRemoteFetchStrategy(t *testing.T) {
jarFile, cleanUp := createTempZipArchive()
defer cleanUp()
cacheLocation := filepath.Join(filepath.Dir(jarFile), "extract_location", "cache.jar")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bytes, err := ioutil.ReadFile(jarFile)
if err != nil {
panic(err)
}
if _, err := w.Write(bytes); err != nil {
panic(err)
}
}))
defer server.Close()
remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL,
testVersionStrategy(),
func() (s string, b bool) {
return cacheLocation, false
})
err := remoteFetchStrategy()
assert.NoError(t, err)
assert.FileExists(t, cacheLocation)
}