Esempio n. 1
0
func TestWriteKubeconfigToDisk(t *testing.T) {
	tmpdir, err := ioutil.TempDir("", "")
	if err != nil {
		t.Fatalf("Couldn't create tmpdir")
	}
	defer os.Remove(tmpdir)

	// set up tmp GlobalEnvParams values for testing
	oldEnv := kubeadmapi.GlobalEnvParams
	kubeadmapi.GlobalEnvParams = kubeadmapi.SetEnvParams()
	kubeadmapi.GlobalEnvParams.KubernetesDir = fmt.Sprintf("%s/etc/kubernetes", tmpdir)
	defer func() { kubeadmapi.GlobalEnvParams = oldEnv }()

	var writeConfig = []struct {
		name        string
		cc          configClient
		ccWithToken configClientWithToken
		expected    error
		file        []byte
	}{
		{"test1", configClient{clusterName: "k8s", userName: "******"}, configClientWithToken{token: "abc"}, nil, []byte(configOut1)},
		{"test2", configClient{clusterName: "kubernetes", userName: "******", serverURL: "localhost:8080"}, configClientWithToken{token: "cba"}, nil, []byte(configOut2)},
	}
	for _, rt := range writeConfig {
		c := MakeClientConfigWithToken(
			rt.cc.serverURL,
			rt.cc.clusterName,
			rt.cc.userName,
			rt.cc.caCert,
			rt.ccWithToken.token,
		)
		configPath := filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, fmt.Sprintf("%s.conf", rt.name))
		err := WriteKubeconfigToDisk(configPath, c)
		if err != rt.expected {
			t.Errorf(
				"failed WriteKubeconfigToDisk with an error:\n\texpected: %s\n\t  actual: %s",
				rt.expected,
				err,
			)
		}
		newFile, err := ioutil.ReadFile(configPath)
		if !bytes.Equal(newFile, rt.file) {
			t.Errorf(
				"failed WriteKubeconfigToDisk config write:\n\texpected: %s\n\t  actual: %s",
				rt.file,
				newFile,
			)
		}
	}
}
Esempio n. 2
0
func TestWriteKubeconfigIfNotExists(t *testing.T) {
	tmpdir, err := ioutil.TempDir("", "")
	if err != nil {
		t.Fatalf("Couldn't create tmpdir")
	}
	defer os.Remove(tmpdir)

	// set up tmp GlobalEnvParams values for testing
	oldEnv := kubeadmapi.GlobalEnvParams
	kubeadmapi.GlobalEnvParams = kubeadmapi.SetEnvParams()
	kubeadmapi.GlobalEnvParams.KubernetesDir = fmt.Sprintf("%s/etc/kubernetes", tmpdir)
	kubeadmapi.GlobalEnvParams.HostPKIPath = fmt.Sprintf("%s/etc/kubernetes/pki", tmpdir)
	kubeadmapi.GlobalEnvParams.HostEtcdPath = fmt.Sprintf("%s/var/lib/etcd", tmpdir)
	kubeadmapi.GlobalEnvParams.DiscoveryImage = fmt.Sprintf("%s/var/lib/etcd", tmpdir)
	defer func() { kubeadmapi.GlobalEnvParams = oldEnv }()

	var writeConfig = []struct {
		name     string
		cc       configClient
		expected error
		file     []byte
	}{
		{"test1", configClient{}, nil, []byte(configOut1)},
		{"test2", configClient{c: "kubernetes"}, nil, []byte(configOut2)},
	}
	for _, rt := range writeConfig {
		c := CreateBasicClientConfig(rt.cc.c, rt.cc.s, rt.cc.ca)
		err := WriteKubeconfigIfNotExists(rt.name, c)
		if err != rt.expected {
			t.Errorf(
				"failed WriteKubeconfigIfNotExists with an error:\n\texpected: %s\n\t  actual: %s",
				err,
				rt.expected,
			)
		}
		configPath := filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, fmt.Sprintf("%s.conf", rt.name))
		newFile, err := ioutil.ReadFile(configPath)
		if !bytes.Equal(newFile, rt.file) {
			t.Errorf(
				"failed WriteKubeconfigIfNotExists config write:\n\texpected: %s\n\t  actual: %s",
				newFile,
				rt.file,
			)
		}
	}
}