예제 #1
0
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)
	}
}
예제 #2
0
func TestShouldCheckURL(t *testing.T) {
	tempDir := tests.MakeTempDir()
	defer os.RemoveAll(tempDir)

	lastUpdateCheckFilePath := filepath.Join(tempDir, "last_update_check")

	// test that if users disable update notification in config, the URL version does not get checked
	viper.Set(config.WantUpdateNotification, false)
	if shouldCheckURLVersion(lastUpdateCheckFilePath) {
		t.Fatalf("Error: shouldCheckURLVersion returned true even though config had WantUpdateNotification: false")
	}

	// test that if users want update notification, the URL version does get checked
	viper.Set(config.WantUpdateNotification, true)
	if shouldCheckURLVersion(lastUpdateCheckFilePath) == false {
		t.Fatalf("Error: shouldCheckURLVersion returned false even though there was no last_update_check file")
	}

	// test that update notifications get triggered if it has been longer than 24 hours
	viper.Set(config.ReminderWaitPeriodInHours, 24)
	writeTimeToFile(lastUpdateCheckFilePath, time.Time{}) //time.Time{} returns time -> January 1, year 1, 00:00:00.000000000 UTC.
	if shouldCheckURLVersion(lastUpdateCheckFilePath) == false {
		t.Fatalf("Error: shouldCheckURLVersion returned false even though longer than 24 hours since last update")
	}

	// test that update notifications do not get triggered if it has been less than 24 hours
	writeTimeToFile(lastUpdateCheckFilePath, time.Now().UTC())
	if shouldCheckURLVersion(lastUpdateCheckFilePath) == true {
		t.Fatalf("Error: shouldCheckURLVersion returned false even though longer than 24 hours since last update")
	}

}
예제 #3
0
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)
		}
	}
}
예제 #4
0
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: %s", 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)
		}
	}
}
예제 #5
0
func TestGenerateCerts(t *testing.T) {
	tempDir := tests.MakeTempDir()
	defer os.RemoveAll(tempDir)
	os.Mkdir(filepath.Join(tempDir, "certs"), 0777)

	_, ipRange, _ := net.ParseCIDR("10.0.0.0/24")
	lk := LocalkubeServer{
		LocalkubeDirectory:    tempDir,
		ServiceClusterIPRange: *ipRange,
	}

	if err := lk.GenerateCerts(); err != nil {
		t.Fatalf("Unexpected error generating certs: %s", err)
	}

	for _, f := range []string{"apiserver.crt", "apiserver.key"} {
		p := filepath.Join(tempDir, "certs", f)
		_, err := os.Stat(p)
		if os.IsNotExist(err) {
			t.Fatalf("Certificate not created: %s", p)
		}
	}
	_, err := lk.loadCert(filepath.Join(tempDir, "certs", "apiserver.crt"))
	if err != nil {
		t.Fatalf("Error parsing cert: %s", err)
	}
}
예제 #6
0
func TestShouldGenerateCertsOneFile(t *testing.T) {
	tempDir := tests.MakeTempDir()
	defer os.RemoveAll(tempDir)
	os.Mkdir(filepath.Join(tempDir, "certs"), 0777)
	ioutil.WriteFile(filepath.Join(tempDir, "certs", "apiserver.crt"), []byte(""), 0644)
	lk := LocalkubeServer{LocalkubeDirectory: tempDir}
	if !lk.shouldGenerateCerts(testIPs) {
		t.Fatalf("Not all certs exist, we should generate.")
	}
}
예제 #7
0
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)
	}
}
예제 #8
0
func TestShouldGenerateCertsBadFiles(t *testing.T) {
	tempDir := tests.MakeTempDir()
	defer os.RemoveAll(tempDir)
	os.Mkdir(filepath.Join(tempDir, "certs"), 0777)
	for _, f := range []string{"apiserver.crt", "apiserver.key"} {
		ioutil.WriteFile(filepath.Join(tempDir, "certs", f), []byte(""), 0644)
	}
	lk := LocalkubeServer{LocalkubeDirectory: tempDir}
	if !lk.shouldGenerateCerts(testIPs) {
		t.Fatalf("Certs are badly formatted, we should generate.")
	}
}
예제 #9
0
func TestPreRunDirectories(t *testing.T) {
	// Make sure we create the required directories.
	tempDir := tests.MakeTempDir()
	defer os.RemoveAll(tempDir)

	runCommand(RootCmd.PersistentPreRun)

	for _, dir := range dirs {
		_, err := os.Stat(dir)
		if os.IsNotExist(err) {
			t.Fatalf("Directory %s does not exist.", dir)
		}
	}
}
예제 #10
0
func TestShouldNotGenerateCerts(t *testing.T) {
	tempDir := tests.MakeTempDir()
	defer os.RemoveAll(tempDir)
	os.Mkdir(filepath.Join(tempDir, "certs"), 0777)

	_, ipRange, _ := net.ParseCIDR("10.0.0.0/24")
	lk := LocalkubeServer{
		LocalkubeDirectory:    tempDir,
		ServiceClusterIPRange: *ipRange,
	}
	lk.GenerateCerts()
	ips, _ := lk.getAllIPs()
	if lk.shouldGenerateCerts(ips) {
		t.Fatalf("IPs match, we should not generate.")
	}
}
예제 #11
0
func TestShouldGenerateCertsMismatchedIP(t *testing.T) {
	tempDir := tests.MakeTempDir()
	defer os.RemoveAll(tempDir)
	os.Mkdir(filepath.Join(tempDir, "certs"), 0777)

	_, ipRange, _ := net.ParseCIDR("10.0.0.0/24")
	lk := LocalkubeServer{
		LocalkubeDirectory:    tempDir,
		ServiceClusterIPRange: *ipRange,
	}

	lk.GenerateCerts()

	if !lk.shouldGenerateCerts([]net.IP{net.ParseIP("4.3.2.1")}) {
		t.Fatalf("IPs don't match, we should generate.")
	}
}
예제 #12
0
func TestIsLocalkubeCached(t *testing.T) {
	tempDir := tests.MakeTempDir()
	defer os.RemoveAll(tempDir)

	inputArr := [...]string{
		"v1.3.3",
		"1.3.0",
		"http://test-url.localkube.com/localkube-binary",
		"file:///test/dir/to/localkube-binary",
	}

	readCloser := nopCloser{}

	localkubeCacher := localkubeCacher{
		k8sConf: KubernetesConfig{},
	}

	inner := func(input string) {
		localkubeCacher.k8sConf = KubernetesConfig{
			KubernetesVersion: input,
		}
		if localkubeCacher.isLocalkubeCached() {
			t.Errorf("IsLocalKubeCached returned true even though %s was not cached",
				localkubeCacher.getLocalkubeCacheFilepath())
		}

		readCloser = nopCloser{bytes.NewBufferString("test-localkube-binary-data")}
		localkubeCacher.cacheLocalkube(readCloser)
		if !localkubeCacher.isLocalkubeCached() {
			t.Errorf("IsLocalKubeCached returned false even though %s was cached",
				localkubeCacher.getLocalkubeCacheFilepath())
		}

	}
	for _, input := range inputArr {
		inner(input)
	}
}
예제 #13
0
func TestMaybePrintUpdateText(t *testing.T) {
	tempDir := tests.MakeTempDir()
	defer os.RemoveAll(tempDir)

	viper.Set(config.WantUpdateNotification, true)
	viper.Set(config.ReminderWaitPeriodInHours, 24)

	var outputBuffer bytes.Buffer
	lastUpdateCheckFilePath := filepath.Join(tempDir, "last_update_check")

	// test that no update text is printed if the latest version is lower/equal to the current version
	latestVersionFromURL := "0.0.0-dev"
	handler := &URLHandlerCorrect{
		releases: []Release{{Name: version.VersionPrefix + latestVersionFromURL}},
	}
	server := httptest.NewServer(handler)
	defer server.Close()

	MaybePrintUpdateText(&outputBuffer, server.URL, lastUpdateCheckFilePath)
	if len(outputBuffer.String()) != 0 {
		t.Fatalf("Expected MaybePrintUpdateText to not output text as the current version is %s and version %s was served from URL but output was [%s]",
			version.GetVersion(), latestVersionFromURL, outputBuffer.String())
	}

	// test that update text is printed if the latest version is greater than the current version
	latestVersionFromURL = "100.0.0-dev"
	handler = &URLHandlerCorrect{
		releases: []Release{{Name: version.VersionPrefix + latestVersionFromURL}},
	}
	server = httptest.NewServer(handler)
	defer server.Close()

	MaybePrintUpdateText(&outputBuffer, server.URL, lastUpdateCheckFilePath)
	if len(outputBuffer.String()) == 0 {
		t.Fatalf("Expected MaybePrintUpdateText to output text as the current version is %s and version %s was served from URL but output was [%s]",
			version.GetVersion(), latestVersionFromURL, outputBuffer.String())
	}
}