forked from kubernetes/minikube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_test.go
403 lines (337 loc) · 10.1 KB
/
cluster_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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cluster
import (
"fmt"
"os"
"testing"
"time"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/provision"
"github.com/docker/machine/libmachine/state"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/registry"
"k8s.io/minikube/pkg/minikube/tests"
)
type MockDownloader struct{}
func (d MockDownloader) GetISOFileURI(isoURL string) string { return "" }
func (d MockDownloader) CacheMinikubeISOFromURL(isoURL string) error { return nil }
var defaultMachineConfig = config.MachineConfig{
VMDriver: constants.DefaultVMDriver,
MinikubeISO: constants.DefaultISOURL,
Downloader: MockDownloader{},
}
func TestCreateHost(t *testing.T) {
api := tests.NewMockAPI()
exists, _ := api.Exists(config.GetMachineName())
if exists {
t.Fatal("Machine already exists.")
}
_, err := createHost(api, defaultMachineConfig)
if err != nil {
t.Fatalf("Error creating host: %v", err)
}
exists, _ = api.Exists(config.GetMachineName())
if !exists {
t.Fatal("Machine does not exist, but should.")
}
h, err := api.Load(config.GetMachineName())
if err != nil {
t.Fatalf("Error loading machine: %v", err)
}
if s, _ := h.Driver.GetState(); s != state.Running {
t.Fatalf("Machine is not running. State is: %s", s)
}
found := false
for _, def := range registry.ListDrivers() {
if h.DriverName == def.Name {
found = true
break
}
}
if !found {
t.Fatalf("Wrong driver name: %v. It should be among drivers %v", h.DriverName, registry.ListDrivers())
}
}
func TestStartHostExists(t *testing.T) {
api := tests.NewMockAPI()
// Create an initial host.
_, err := createHost(api, defaultMachineConfig)
if err != nil {
t.Fatalf("Error creating host: %v", err)
}
// Make sure the next call to Create will fail, to assert it doesn't get called again.
api.CreateError = true
if err := api.Create(&host.Host{}); err == nil {
t.Fatal("api.Create did not fail, but should have.")
}
md := &tests.MockDetector{Provisioner: &tests.MockProvisioner{}}
provision.SetDetector(md)
// This should pass without calling Create because the host exists already.
h, err := StartHost(api, defaultMachineConfig)
if err != nil {
t.Fatal("Error starting host.")
}
if h.Name != config.GetMachineName() {
t.Fatalf("Machine created with incorrect name: %s", h.Name)
}
if s, _ := h.Driver.GetState(); s != state.Running {
t.Fatalf("Machine not started.")
}
if !md.Provisioner.Provisioned {
t.Fatalf("Expected provision to be called")
}
}
func TestStartStoppedHost(t *testing.T) {
api := tests.NewMockAPI()
// Create an initial host.
h, err := createHost(api, defaultMachineConfig)
if err != nil {
t.Fatalf("Error creating host: %v", err)
}
d := tests.MockDriver{}
h.Driver = &d
d.CurrentState = state.Stopped
md := &tests.MockDetector{Provisioner: &tests.MockProvisioner{}}
provision.SetDetector(md)
h, err = StartHost(api, defaultMachineConfig)
if err != nil {
t.Fatal("Error starting host.")
}
if h.Name != config.GetMachineName() {
t.Fatalf("Machine created with incorrect name: %s", h.Name)
}
if s, _ := h.Driver.GetState(); s != state.Running {
t.Fatalf("Machine not started.")
}
if !api.SaveCalled {
t.Fatalf("Machine must be saved after starting.")
}
if !md.Provisioner.Provisioned {
t.Fatalf("Expected provision to be called")
}
}
func TestStartHost(t *testing.T) {
api := tests.NewMockAPI()
md := &tests.MockDetector{Provisioner: &tests.MockProvisioner{}}
provision.SetDetector(md)
h, err := StartHost(api, defaultMachineConfig)
if err != nil {
t.Fatal("Error starting host.")
}
if h.Name != config.GetMachineName() {
t.Fatalf("Machine created with incorrect name: %s", h.Name)
}
if exists, _ := api.Exists(h.Name); !exists {
t.Fatal("Machine not saved.")
}
if s, _ := h.Driver.GetState(); s != state.Running {
t.Fatalf("Machine not started.")
}
// Provision regenerates Docker certs. This happens automatically during create,
// so we should only call it again if the host already exists.
if md.Provisioner.Provisioned {
t.Fatalf("Did not expect Provision to be called")
}
}
func TestStartHostConfig(t *testing.T) {
api := tests.NewMockAPI()
md := &tests.MockDetector{Provisioner: &tests.MockProvisioner{}}
provision.SetDetector(md)
config := config.MachineConfig{
VMDriver: constants.DefaultVMDriver,
DockerEnv: []string{"FOO=BAR"},
DockerOpt: []string{"param=value"},
Downloader: MockDownloader{},
}
h, err := StartHost(api, config)
if err != nil {
t.Fatal("Error starting host.")
}
for i := range h.HostOptions.EngineOptions.Env {
if h.HostOptions.EngineOptions.Env[i] != config.DockerEnv[i] {
t.Fatal("Docker env variables were not set!")
}
}
for i := range h.HostOptions.EngineOptions.ArbitraryFlags {
if h.HostOptions.EngineOptions.ArbitraryFlags[i] != config.DockerOpt[i] {
t.Fatal("Docker flags were not set!")
}
}
}
func TestStopHostError(t *testing.T) {
api := tests.NewMockAPI()
if err := StopHost(api); err == nil {
t.Fatal("An error should be thrown when stopping non-existing machine.")
}
}
func TestStopHost(t *testing.T) {
api := tests.NewMockAPI()
h, err := createHost(api, defaultMachineConfig)
if err != nil {
t.Errorf("createHost failed: %v", err)
}
if err := StopHost(api); err != nil {
t.Fatal("An error should be thrown when stopping non-existing machine.")
}
if s, _ := h.Driver.GetState(); s != state.Stopped {
t.Fatalf("Machine not stopped. Currently in state: %s", s)
}
}
func TestDeleteHost(t *testing.T) {
api := tests.NewMockAPI()
if _, err := createHost(api, defaultMachineConfig); err != nil {
t.Errorf("createHost failed: %v", err)
}
if err := DeleteHost(api); err != nil {
t.Fatalf("Unexpected error deleting host: %v", err)
}
}
func TestDeleteHostErrorDeletingVM(t *testing.T) {
api := tests.NewMockAPI()
h, err := createHost(api, defaultMachineConfig)
if err != nil {
t.Errorf("createHost failed: %v", err)
}
d := &tests.MockDriver{RemoveError: true}
h.Driver = d
if err := DeleteHost(api); err == nil {
t.Fatal("Expected error deleting host.")
}
}
func TestDeleteHostErrorDeletingFiles(t *testing.T) {
api := tests.NewMockAPI()
api.RemoveError = true
if _, err := createHost(api, defaultMachineConfig); err != nil {
t.Errorf("createHost failed: %v", err)
}
if err := DeleteHost(api); err == nil {
t.Fatal("Expected error deleting host.")
}
}
func TestGetHostStatus(t *testing.T) {
api := tests.NewMockAPI()
checkState := func(expected string) {
s, err := GetHostStatus(api)
if err != nil {
t.Fatalf("Unexpected error getting status: %v", err)
}
if s != expected {
t.Fatalf("Expected status: %s, got %s", s, expected)
}
}
checkState(state.None.String())
if _, err := createHost(api, defaultMachineConfig); err != nil {
t.Errorf("createHost failed: %v", err)
}
checkState(state.Running.String())
if err := StopHost(api); err != nil {
t.Errorf("StopHost failed: %v", err)
}
checkState(state.Stopped.String())
}
func TestGetHostDockerEnv(t *testing.T) {
tempDir := tests.MakeTempDir()
defer os.RemoveAll(tempDir)
api := tests.NewMockAPI()
h, err := createHost(api, defaultMachineConfig)
if err != nil {
t.Fatalf("Error creating host: %v", err)
}
d := &tests.MockDriver{
BaseDriver: drivers.BaseDriver{
IPAddress: "127.0.0.1",
},
}
h.Driver = d
envMap, err := GetHostDockerEnv(api)
if err != nil {
t.Fatalf("Unexpected error getting env: %v", err)
}
dockerEnvKeys := [...]string{
"DOCKER_TLS_VERIFY",
"DOCKER_HOST",
"DOCKER_CERT_PATH",
}
for _, dockerEnvKey := range dockerEnvKeys {
if _, hasKey := envMap[dockerEnvKey]; !hasKey {
t.Fatalf("Expected envMap[\"%s\"] key to be defined", dockerEnvKey)
}
}
}
func TestGetHostDockerEnvIPv6(t *testing.T) {
tempDir := tests.MakeTempDir()
defer os.RemoveAll(tempDir)
api := tests.NewMockAPI()
h, err := createHost(api, defaultMachineConfig)
if err != nil {
t.Fatalf("Error creating host: %v", err)
}
d := &tests.MockDriver{
BaseDriver: drivers.BaseDriver{
IPAddress: "fe80::215:5dff:fe00:a903",
},
}
h.Driver = d
envMap, err := GetHostDockerEnv(api)
if err != nil {
t.Fatalf("Unexpected error getting env: %v", err)
}
expected := "tcp://[fe80::215:5dff:fe00:a903]:2376"
v := envMap["DOCKER_HOST"]
if v != expected {
t.Fatalf("Expected DOCKER_HOST to be defined as %s but was %s", expected, v)
}
}
func TestCreateSSHShell(t *testing.T) {
api := tests.NewMockAPI()
s, _ := tests.NewSSHServer()
port, err := s.Start()
if err != nil {
t.Fatalf("Error starting ssh server: %v", err)
}
d := &tests.MockDriver{
Port: port,
CurrentState: state.Running,
BaseDriver: drivers.BaseDriver{
IPAddress: "127.0.0.1",
SSHKeyPath: "",
},
}
api.Hosts[config.GetMachineName()] = &host.Host{Driver: d}
cliArgs := []string{"exit"}
if err := CreateSSHShell(api, cliArgs); err != nil {
t.Fatalf("Error running ssh command: %v", err)
}
if !s.IsSessionRequested() {
t.Fatalf("Expected ssh session to be run")
}
}
func TestGuestClockDelta(t *testing.T) {
local := time.Now()
h := tests.NewMockHost()
// Truncate remote clock so that it is between 0 and 1 second behind
h.CommandOutput["date +%s.%N"] = fmt.Sprintf("%d.0000", local.Unix())
got, err := guestClockDelta(h, local)
if err != nil {
t.Fatalf("guestClock: %v", err)
}
if got > (0 * time.Second) {
t.Errorf("unexpected positive delta (remote should be behind): %s", got)
}
if got < (-1 * time.Second) {
t.Errorf("unexpectedly negative delta (remote too far behind): %s", got)
}
}