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 != constants.MachineName { 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 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 != constants.MachineName { 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 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 != constants.MachineName { 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 TestStart(t *testing.T) { defer provision.SetDetector(&provision.StandardDetector{}) provision.SetDetector(&provision.FakeDetector{ Provisioner: provision.NewNetstatProvisioner(), }) host := &Host{ Driver: &fakedriver.Driver{ MockState: state.Stopped, }, } if err := host.Start(); err != nil { t.Fatalf("Expected no error but got one: %s", err) } }
func TestCmdProvision(t *testing.T) { testCases := []struct { commandLine CommandLine api libmachine.API expectedErr error }{ { commandLine: &commandstest.FakeCommandLine{ CliArgs: []string{"foo", "bar"}, }, api: &libmachinetest.FakeAPI{ Hosts: []*host.Host{ { Name: "foo", Driver: &fakedriver.Driver{}, HostOptions: &host.Options{ EngineOptions: &engine.Options{}, AuthOptions: &auth.Options{}, SwarmOptions: &swarm.Options{}, }, }, { Name: "bar", Driver: &fakedriver.Driver{}, HostOptions: &host.Options{ EngineOptions: &engine.Options{}, AuthOptions: &auth.Options{}, SwarmOptions: &swarm.Options{}, }, }, }, }, expectedErr: nil, }, } provision.SetDetector(&provision.FakeDetector{ Provisioner: provision.NewFakeProvisioner(nil), }) // fakeprovisioner always returns "true" for compatible host, so we // just need to register it. provision.Register("fakeprovisioner", &provision.RegisteredProvisioner{ New: provision.NewFakeProvisioner, }) for _, tc := range testCases { assert.Equal(t, tc.expectedErr, cmdProvision(tc.commandLine, tc.api)) } }
func TestStartHostConfig(t *testing.T) { api := tests.NewMockAPI() md := &tests.MockDetector{Provisioner: &tests.MockProvisioner{}} provision.SetDetector(md) config := MachineConfig{ VMDriver: constants.DefaultVMDriver, DockerEnv: []string{"FOO=BAR"}, } 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!") } } }
func TestRunActionForeachMachine(t *testing.T) { defer provision.SetDetector(&provision.StandardDetector{}) provision.SetDetector(&provision.FakeDetector{ Provisioner: provision.NewNetstatProvisioner(), }) // Assume a bunch of machines in randomly started or // stopped states. machines := []*host.Host{ { Name: "foo", DriverName: "fakedriver", Driver: &fakedriver.Driver{ MockState: state.Running, }, }, { Name: "bar", DriverName: "fakedriver", Driver: &fakedriver.Driver{ MockState: state.Stopped, }, }, { Name: "baz", // Ssh, don't tell anyone but this // driver only _thinks_ it's named // virtualbox... (to test serial actions) // It's actually FakeDriver! DriverName: "virtualbox", Driver: &fakedriver.Driver{ MockState: state.Stopped, }, }, { Name: "spam", DriverName: "virtualbox", Driver: &fakedriver.Driver{ MockState: state.Running, }, }, { Name: "eggs", DriverName: "fakedriver", Driver: &fakedriver.Driver{ MockState: state.Stopped, }, }, { Name: "ham", DriverName: "fakedriver", Driver: &fakedriver.Driver{ MockState: state.Running, }, }, } runActionForeachMachine("start", machines) for _, machine := range machines { machineState, _ := machine.Driver.GetState() assert.Equal(t, state.Running, machineState) } runActionForeachMachine("stop", machines) for _, machine := range machines { machineState, _ := machine.Driver.GetState() assert.Equal(t, state.Stopped, machineState) } }