func TestCreateSSHShell(t *testing.T) { api := tests.NewMockAPI() s, _ := tests.NewSSHServer() port, err := s.Start() if err != nil { t.Fatalf("Error starting ssh server: %s", err) } d := &tests.MockDriver{ Port: port, CurrentState: state.Running, BaseDriver: drivers.BaseDriver{ IPAddress: "127.0.0.1", SSHKeyPath: "", }, } api.Hosts[constants.MachineName] = &host.Host{Driver: d} cliArgs := []string{"exit"} if err := CreateSSHShell(api, cliArgs); err != nil { t.Fatalf("Error running ssh command: %s", err) } if s.HadASessionRequested != true { t.Fatalf("Expected ssh session to be run") } }
func TestNewSSHClient(t *testing.T) { s, _ := tests.NewSSHServer() port, err := s.Start() if err != nil { t.Fatalf("Error starting ssh server: %s", err) } d := &tests.MockDriver{ Port: port, BaseDriver: drivers.BaseDriver{ IPAddress: "127.0.0.1", SSHKeyPath: "", }, } c, err := NewSSHClient(d) if err != nil { t.Fatalf("Unexpected error: %s", err) } cmd := "foo" RunCommand(c, cmd) if !s.Connected { t.Fatalf("Error!") } if _, ok := s.Commands[cmd]; !ok { t.Fatalf("Expected command: %s", cmd) } }
func TestUpdateKubernetesVersion(t *testing.T) { tempDir := tests.MakeTempDir() defer os.RemoveAll(tempDir) s, _ := tests.NewSSHServer() port, err := s.Start() if err != nil { t.Fatalf("Error starting ssh server: %s", err) } h := tests.NewMockHost() d := &tests.MockDriver{ Port: port, BaseDriver: drivers.BaseDriver{ IPAddress: "127.0.0.1", SSHKeyPath: "", }, } handler := &K8sVersionHandlerCorrect{} server := httptest.NewServer(handler) kubernetesConfig := KubernetesConfig{ KubernetesVersion: server.URL, } if err := UpdateCluster(h, d, kubernetesConfig); err != nil { t.Fatalf("Error updating cluster: %s", err) } transferred := s.Transfers.Bytes() //test that localkube is transferred properly contents := []byte(testLocalkubeBin) if !bytes.Contains(transferred, contents) { t.Fatalf("File not copied. Expected transfers to contain: %s. It was: %s", contents, transferred) } }
func TestHostGetLogs(t *testing.T) { api := tests.NewMockAPI() s, _ := tests.NewSSHServer() port, err := s.Start() if err != nil { t.Fatalf("Error starting ssh server: %s", err) } d := &tests.MockDriver{ Port: port, BaseDriver: drivers.BaseDriver{ IPAddress: "127.0.0.1", SSHKeyPath: "", }, } api.Hosts[constants.MachineName] = &host.Host{Driver: d} if _, err := GetHostLogs(api); err != nil { t.Fatalf("Error getting host logs: %s", err) } if _, ok := s.Commands[logsCommand]; !ok { t.Fatalf("Expected command not run: %s", logsCommand) } }
func TestSetupCerts(t *testing.T) { s, _ := tests.NewSSHServer() port, err := s.Start() if err != nil { t.Fatalf("Error starting ssh server: %s", err) } d := &tests.MockDriver{ Port: port, BaseDriver: drivers.BaseDriver{ IPAddress: "127.0.0.1", SSHKeyPath: "", }, } tempDir := tests.MakeTempDir() defer os.RemoveAll(tempDir) if err := SetupCerts(d); err != nil { t.Fatalf("Error starting cluster: %s", err) } for _, cert := range certs { contents, _ := ioutil.ReadFile(cert) transferred := s.Transfers.Bytes() if !bytes.Contains(transferred, contents) { t.Fatalf("Certificate not copied. Expected transfers to contain: %s. It was: %s", contents, transferred) } } }
func TestUpdate(t *testing.T) { s, _ := tests.NewSSHServer() port, err := s.Start() if err != nil { t.Fatalf("Error starting ssh server: %s", err) } d := &tests.MockDriver{ Port: port, BaseDriver: drivers.BaseDriver{ IPAddress: "127.0.0.1", SSHKeyPath: "", }, } if err := UpdateCluster(d); err != nil { t.Fatalf("Error updating cluster: %s", err) } transferred := s.Transfers.Bytes() for _, a := range assets { contents, _ := Asset(a.AssetName) if !bytes.Contains(transferred, contents) { t.Fatalf("File not copied. Expected transfers to contain: %s. It was: %s", contents, transferred) } } }
func TestUpdateCustomAddons(t *testing.T) { tempDir := tests.MakeTempDir() os.Mkdir(constants.MakeMiniPath("addons", "subdir"), 0777) defer os.RemoveAll(tempDir) s, _ := tests.NewSSHServer() port, err := s.Start() if err != nil { t.Fatalf("Error starting ssh server: %s", err) } h := tests.NewMockHost() d := &tests.MockDriver{ Port: port, BaseDriver: drivers.BaseDriver{ IPAddress: "127.0.0.1", SSHKeyPath: "", }, } //write a file into ~/.minikube/addons path := filepath.Join(constants.MakeMiniPath("addons"), "dir-addon.yaml") testContent1 := []byte("CUSTOM ADDON TEST STRING#1, In Addons Dir") err = ioutil.WriteFile(path, testContent1, 0644) if err != nil { t.Fatalf("Error writing custom addon: %s", err) } path = filepath.Join(constants.MakeMiniPath("addons", "subdir"), "subdir-addon.yaml") testContent2 := []byte("CUSTOM ADDON TEST STRING#2, In Addons SubDir") err = ioutil.WriteFile(path, testContent2, 0644) if err != nil { t.Fatalf("Error writing custom addon: %s", err) } //run update kubernetesConfig := KubernetesConfig{ KubernetesVersion: constants.DefaultKubernetesVersion, } if err := UpdateCluster(h, d, kubernetesConfig); err != nil { t.Fatalf("Error updating cluster: %s", err) } transferred := s.Transfers.Bytes() //test that custom addons are transferred properly if !bytes.Contains(transferred, testContent1) { t.Fatalf("Custom addon not copied. Expected transfers to contain custom addon with content: %s. It was: %s", testContent1, transferred) } if !bytes.Contains(transferred, testContent2) { t.Fatalf("Custom addon not copied. Expected transfers to contain custom addon with content: %s. It was: %s", testContent2, transferred) } }
func TestUpdateDefault(t *testing.T) { s, _ := tests.NewSSHServer() port, err := s.Start() if err != nil { t.Fatalf("Error starting ssh server: %s", err) } h := tests.NewMockHost() d := &tests.MockDriver{ Port: port, BaseDriver: drivers.BaseDriver{ IPAddress: "127.0.0.1", SSHKeyPath: "", }, } kubernetesConfig := KubernetesConfig{ KubernetesVersion: constants.DefaultKubernetesVersion, } if err := UpdateCluster(h, d, kubernetesConfig); err != nil { t.Fatalf("Error updating cluster: %s", err) } transferred := s.Transfers.Bytes() //test that kube-add memoryAssets are transferred properly for _, a := range memoryAssets { contents, _ := Asset(a.GetAssetName()) if !bytes.Contains(transferred, contents) { t.Fatalf("File not copied. Expected transfers to contain: %s. It was: %s", contents, transferred) } } //test that localkube is transferred properly contents, _ := Asset("out/localkube") if !bytes.Contains(transferred, contents) { t.Fatalf("File not copied. Expected transfers to contain: %s. It was: %s", contents, transferred) } }
func TestGetLocalkubeStatus(t *testing.T) { api := tests.NewMockAPI() s, _ := tests.NewSSHServer() port, err := s.Start() if err != nil { t.Fatalf("Error starting ssh server: %s", err) } d := &tests.MockDriver{ Port: port, BaseDriver: drivers.BaseDriver{ IPAddress: "127.0.0.1", SSHKeyPath: "", }, } api.Hosts[constants.MachineName] = &host.Host{Driver: d} s.CommandToOutput = map[string]string{ localkubeStatusCommand: state.Running.String(), } if _, err := GetLocalkubeStatus(api); err != nil { t.Fatalf("Error getting localkube status: %s", err) } s.CommandToOutput = map[string]string{ localkubeStatusCommand: state.Stopped.String(), } if _, err := GetLocalkubeStatus(api); err != nil { t.Fatalf("Error getting localkube status: %s", err) } s.CommandToOutput = map[string]string{ localkubeStatusCommand: "Bad Output", } if _, err := GetLocalkubeStatus(api); err == nil { t.Fatalf("Expected error in getting localkube status as ssh returned bad output") } }
func TestTransfer(t *testing.T) { s, _ := tests.NewSSHServer() port, err := s.Start() if err != nil { t.Fatalf("Error starting ssh server: %s", err) } d := &tests.MockDriver{ Port: port, BaseDriver: drivers.BaseDriver{ IPAddress: "127.0.0.1", SSHKeyPath: "", }, } c, err := NewSSHClient(d) if err != nil { t.Fatalf("Unexpected error: %s", err) } dest := "bar" if err := Transfer([]byte("testcontents"), "/tmp", dest, "0777", c); err != nil { t.Fatalf("Unexpected error: %s", err) } }