forked from adamlaska/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb2d_test.go
More file actions
339 lines (271 loc) · 8.45 KB
/
b2d_test.go
File metadata and controls
339 lines (271 loc) · 8.45 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package mcnutils
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/version"
"github.com/stretchr/testify/assert"
)
func TestGetReleaseURL(t *testing.T) {
testCases := []struct {
apiURL string
isoURL string
machineVersion string
response string
}{
{"/repos/org/repo/releases/latest", "/org/repo/releases/download/v0.1/boot2docker.iso", "v0.7.0", `{"tag_name": "v0.1"}`},
// Note the difference in this one: It's an RC version.
{"/repos/org/repo/releases", "/org/repo/releases/download/v0.2-rc1/boot2docker.iso", "v0.7.0-rc2", `[{"tag_name": "v0.2-rc1"}, {"tag_name": "v0.1"}]`},
{"http://dummy.com/boot2docker.iso", "http://dummy.com/boot2docker.iso", "v0.7.0", `{"tag_name": "v0.1"}`},
}
for _, tt := range testCases {
testServer := newTestServer(tt.response)
// TODO: Modifying this package level variable is not elegant,
// but it is effective. Ideally this should be exposed through
// an interface.
actualMachineVersion := version.Version
version.Version = tt.machineVersion
b := NewB2dUtils("/tmp/isos")
isoURL, err := b.getReleaseURL(testServer.URL + tt.apiURL)
assert.NoError(t, err)
assert.Equal(t, testServer.URL+tt.isoURL, isoURL)
version.Version = actualMachineVersion
testServer.Close()
}
}
func TestGetReleaseURLError(t *testing.T) {
// GitHub API error response in case of rate limit
ts := newTestServer(`{"message": "API rate limit exceeded for 127.0.0.1.",
"documentation_url": "https://developer.github.com/v3/#rate-limiting"}`)
defer ts.Close()
testCases := []struct {
apiURL string
}{
{ts.URL + "/repos/org/repo/releases/latest"},
{"http://127.0.0.1/repos/org/repo/releases/latest"}, // dummy API URL. cannot connect it.
}
for _, tt := range testCases {
b := NewB2dUtils("/tmp/isos")
_, err := b.getReleaseURL(tt.apiURL)
assert.Error(t, err)
}
}
func TestVersion(t *testing.T) {
testCases := []string{
"v0.1.0",
"v0.2.0-rc1",
}
for _, vers := range testCases {
isopath, off, err := newDummyISO("", defaultISOFilename, vers)
assert.NoError(t, err)
b := &b2dISO{
commonIsoPath: isopath,
volumeIDOffset: off,
volumeIDLength: defaultVolumeIDLength,
}
got, err := b.version()
assert.NoError(t, err)
assert.Equal(t, vers, string(got))
removeFileIfExists(isopath)
}
}
func TestDownloadISO(t *testing.T) {
testData := "test-download"
ts := newTestServer(testData)
defer ts.Close()
filename := "test"
tmpDir, err := ioutil.TempDir("", "machine-test-")
assert.NoError(t, err)
b := NewB2dUtils("/tmp/artifacts")
err = b.DownloadISO(tmpDir, filename, ts.URL)
assert.NoError(t, err)
data, err := ioutil.ReadFile(filepath.Join(tmpDir, filename))
assert.NoError(t, err)
assert.Equal(t, testData, string(data))
}
func TestGetRequest(t *testing.T) {
testCases := []struct {
token string
want string
}{
{"", ""},
{"CATBUG", "token CATBUG"},
}
for _, tt := range testCases {
GithubAPIToken = tt.token
req, err := getRequest("http://some.github.api")
assert.NoError(t, err)
assert.Equal(t, tt.want, req.Header.Get("Authorization"))
}
}
type MockReadCloser struct {
blockLengths []int
currentBlock int
}
func (r *MockReadCloser) Read(p []byte) (n int, err error) {
n = r.blockLengths[r.currentBlock]
r.currentBlock++
return
}
func (r *MockReadCloser) Close() error {
return nil
}
func TestReaderWithProgress(t *testing.T) {
readCloser := MockReadCloser{blockLengths: []int{5, 45, 50}}
output := new(bytes.Buffer)
buffer := make([]byte, 100)
readerWithProgress := ReaderWithProgress{
ReadCloser: &readCloser,
out: output,
expectedLength: 100,
}
readerWithProgress.Read(buffer)
assert.Equal(t, "0%..", output.String())
readerWithProgress.Read(buffer)
assert.Equal(t, "0%....10%....20%....30%....40%....50%", output.String())
readerWithProgress.Read(buffer)
assert.Equal(t, "0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%", output.String())
readerWithProgress.Close()
assert.Equal(t, "0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%\n", output.String())
}
type mockReleaseGetter struct {
ver string
apiErr error
verCh chan<- string
}
func (m *mockReleaseGetter) filename() string {
return defaultISOFilename
}
func (m *mockReleaseGetter) getReleaseTag(apiURL string) (string, error) {
return m.ver, m.apiErr
}
func (m *mockReleaseGetter) getReleaseURL(apiURL string) (string, error) {
return "http://127.0.0.1/dummy", m.apiErr
}
func (m *mockReleaseGetter) download(dir, file, isoURL string) error {
path := filepath.Join(dir, file)
var err error
if _, e := os.Stat(path); os.IsNotExist(e) {
err = ioutil.WriteFile(path, dummyISOData(" ", m.ver), 0644)
}
// send a signal of downloading the latest version
m.verCh <- m.ver
return err
}
type mockISO struct {
isopath string
exist bool
ver string
verCh <-chan string
}
func (m *mockISO) path() string {
return m.isopath
}
func (m *mockISO) exists() bool {
return m.exist
}
func (m *mockISO) version() (string, error) {
select {
// receive version of a downloaded iso
case ver := <-m.verCh:
return ver, nil
default:
return m.ver, nil
}
}
func TestCopyDefaultISOToMachine(t *testing.T) {
apiErr := errors.New("api error")
testCases := []struct {
machineName string
create bool
localVer string
latestVer string
apiErr error
wantVer string
}{
{"none", false, "", "v1.0.0", nil, "v1.0.0"}, // none => downloading
{"latest", true, "v1.0.0", "v1.0.0", nil, "v1.0.0"}, // latest iso => as is
{"old-badurl", true, "v0.1.0", "", apiErr, "v0.1.0"}, // old iso with bad api => as is
{"old", true, "v0.1.0", "v1.0.0", nil, "v1.0.0"}, // old iso => updating
}
var isopath string
var err error
verCh := make(chan string, 1)
for _, tt := range testCases {
if tt.create {
isopath, _, err = newDummyISO("cache", defaultISOFilename, tt.localVer)
} else {
if dir, e := ioutil.TempDir("", "machine-test"); e == nil {
isopath = filepath.Join(dir, "cache", defaultISOFilename)
}
}
// isopath: "$TMPDIR/machine-test-xxxxxx/cache/boot2docker.iso"
// tmpDir: "$TMPDIR/machine-test-xxxxxx"
imgCachePath := filepath.Dir(isopath)
storePath := filepath.Dir(imgCachePath)
b := &B2dUtils{
releaseGetter: &mockReleaseGetter{
ver: tt.latestVer,
apiErr: tt.apiErr,
verCh: verCh,
},
iso: &mockISO{
isopath: isopath,
exist: tt.create,
ver: tt.localVer,
verCh: verCh,
},
storePath: storePath,
imgCachePath: imgCachePath,
}
dir := filepath.Join(storePath, "machines", tt.machineName)
err = os.MkdirAll(dir, 0700)
assert.NoError(t, err, "machine: %s", tt.machineName)
err = b.CopyIsoToMachineDir("", tt.machineName)
assert.NoError(t, err)
dest := filepath.Join(dir, b.filename())
_, pathErr := os.Stat(dest)
assert.NoError(t, err, "machine: %s", tt.machineName)
assert.True(t, !os.IsNotExist(pathErr), "machine: %s", tt.machineName)
ver, err := b.version()
assert.NoError(t, err, "machine: %s", tt.machineName)
assert.Equal(t, tt.wantVer, ver, "machine: %s", tt.machineName)
err = removeFileIfExists(isopath)
assert.NoError(t, err, "machine: %s", tt.machineName)
}
}
// newTestServer creates a new httptest.Server that returns respText as a response body.
func newTestServer(respText string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(respText))
}))
}
// newDummyISO creates a dummy ISO file that contains the given version info,
// and returns its path and offset value to fetch the version info.
func newDummyISO(dir, name, version string) (string, int64, error) {
tmpDir, err := ioutil.TempDir("", "machine-test-")
if err != nil {
return "", 0, err
}
tmpDir = filepath.Join(tmpDir, dir)
if e := os.MkdirAll(tmpDir, 755); e != nil {
return "", 0, err
}
isopath := filepath.Join(tmpDir, name)
log.Info("TEST: dummy ISO created at ", isopath)
// dummy ISO data mimicking the real byte data of a Boot2Docker ISO image
padding := " "
data := dummyISOData(padding, version)
return isopath, int64(len(padding)), ioutil.WriteFile(isopath, data, 0644)
}
// dummyISOData returns mock data that contains given padding and version.
func dummyISOData(padding, version string) []byte {
return []byte(fmt.Sprintf("%sBoot2Docker-%s ", padding, version))
}