// TestGetCloudProvider tests the func getCloudProvider().
// To run this unit test successfully, need to ensure /etc/kubernetes/azure.json nonexistent.
TestGetCloudProvider tests the func getCloudProvider().
To run this unit test successfully, need to ensure /etc/kubernetes/azure.json nonexistent.
// TestGetCloudProvider tests the func getCloudProvider () . // To run this unit test successfully need to ensure / etc / kubernetes / azure . json nonexistent .
func TestGetCloudProvider(t *testing.T) {
	fakeCredFile := "fake-cred-file.json"
	fakeKubeConfig := "fake-kube-config"
	emptyKubeConfig := "empty-kube-config"
	fakeContent := `
apiVersion: v1
clusters:
- cluster:
    server: https://localhost:8080
  name: foo-cluster
contexts:
- context:
    cluster: foo-cluster
    user: foo-user
    namespace: bar
  name: foo-context
current-context: foo-context
kind: Config
users:
- name: foo-user
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1alpha1
      args:
      - arg-1
      - arg-2
      command: foo-command
`
	err := createTestFile(emptyKubeConfig)
	if err != nil {
		t.Error(err)
	}
	defer func() {
		if err := os.Remove(emptyKubeConfig); err != nil {
			t.Error(err)
		}
	}()
	tests := []struct {
		desc                  string
		createFakeCredFile    bool
		createFakeKubeConfig  bool
		kubeconfig            string
		nodeID                string
		userAgent             string
		allowEmptyCloudConfig bool
		expectedErr           error
	}{
		{
			desc:                  "out of cluster, no kubeconfig, no credential file",
			kubeconfig:            "",
			nodeID:                "",
			allowEmptyCloudConfig: true,
			expectedErr:           nil,
		},
		{
			desc:                  "[failure][disallowEmptyCloudConfig]  out of cluster, no kubeconfig, no credential file",
			kubeconfig:            "",
			nodeID:                "",
			allowEmptyCloudConfig: false,
			expectedErr:           nil,
		},
		{
			desc:                  "[failure] out of cluster & in cluster, specify a non-exist kubeconfig, no credential file",
			kubeconfig:            "/tmp/non-exist.json",
			nodeID:                "",
			allowEmptyCloudConfig: true,
			expectedErr:           nil,
		},
		{
			desc:                  "[failure] out of cluster & in cluster, specify a empty kubeconfig, no credential file",
			kubeconfig:            emptyKubeConfig,
			nodeID:                "",
			allowEmptyCloudConfig: true,
			expectedErr:           fmt.Errorf("failed to get KubeClient: invalid configuration: no configuration has been provided, try setting KUBERNETES_MASTER environment variable"),
		},
		{
			desc:                  "[failure] out of cluster & in cluster, specify a fake kubeconfig, no credential file",
			createFakeKubeConfig:  true,
			kubeconfig:            fakeKubeConfig,
			nodeID:                "",
			allowEmptyCloudConfig: true,
			expectedErr:           nil,
		},
		{
			desc:                  "[success] out of cluster & in cluster, no kubeconfig, a fake credential file",
			createFakeCredFile:    true,
			kubeconfig:            "",
			nodeID:                "",
			userAgent:             "useragent",
			allowEmptyCloudConfig: true,
			expectedErr:           nil,
		},
	}
	for _, test := range tests {
		if test.createFakeKubeConfig {
			if err := createTestFile(fakeKubeConfig); err != nil {
				t.Error(err)
			}
			defer func() {
				if err := os.Remove(fakeKubeConfig); err != nil {
					t.Error(err)
				}
			}()
			if err := ioutil.WriteFile(fakeKubeConfig, []byte(fakeContent), 0666); err != nil {
				t.Error(err)
			}
		}
		if test.createFakeCredFile {
			if err := createTestFile(fakeCredFile); err != nil {
				t.Error(err)
			}
			defer func() {
				if err := os.Remove(fakeCredFile); err != nil {
					t.Error(err)
				}
			}()
			originalCredFile, ok := os.LookupEnv(DefaultAzureCredentialFileEnv)
			if ok {
				defer os.Setenv(DefaultAzureCredentialFileEnv, originalCredFile)
			} else {
				defer os.Unsetenv(DefaultAzureCredentialFileEnv)
			}
			os.Setenv(DefaultAzureCredentialFileEnv, fakeCredFile)
		}
		cloud, err := getCloudProvider(test.kubeconfig, test.nodeID, "", "", test.userAgent, test.allowEmptyCloudConfig)
		if !reflect.DeepEqual(err, test.expectedErr) && test.expectedErr != nil && !strings.Contains(err.Error(), test.expectedErr.Error()) {
			t.Errorf("desc: %s,\n input: %q, GetCloudProvider err: %v, expectedErr: %v", test.desc, test.kubeconfig, err, test.expectedErr)
		}
		if cloud == nil {
			t.Errorf("return value of getCloudProvider should not be nil even there is error")
		} else {
			assert.Equal(t, cloud.Environment.StorageEndpointSuffix, storage.DefaultBaseURL)
			assert.Equal(t, cloud.UserAgent, test.userAgent)
		}
	}
}
func TestGetCloudProvider ( t * testing . T ) { fakeCredFile := " " fakeKubeConfig := " " emptyKubeConfig := " " fakeContent := `
apiVersion: v1
clusters:
- cluster:
    server: https://localhost:8080
  name: foo-cluster
contexts:
- context:
    cluster: foo-cluster
    user: foo-user
    namespace: bar
  name: foo-context
current-context: foo-context
kind: Config
users:
- name: foo-user
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1alpha1
      args:
      - arg-1
      - arg-2
      command: foo-command
` err := createTestFile ( emptyKubeConfig ) if err != nil { t . Error ( err ) } defer func ( ) { if err := os . Remove ( emptyKubeConfig ) ; err != nil { t . Error ( err ) } } ( ) tests := [ ] struct { desc string createFakeCredFile bool createFakeKubeConfig bool kubeconfig string nodeID string userAgent string allowEmptyCloudConfig bool expectedErr error } { { desc : " " , kubeconfig : " " , nodeID : " " , allowEmptyCloudConfig : true , expectedErr : nil , } , { desc : " " , kubeconfig : " " , nodeID : " " , allowEmptyCloudConfig : false , expectedErr : nil , } , { desc : " " , kubeconfig : " " , nodeID : " " , allowEmptyCloudConfig : true , expectedErr : nil , } , { desc : " " , kubeconfig : emptyKubeConfig , nodeID : " " , allowEmptyCloudConfig : true , expectedErr : fmt . Errorf ( " " ) , } , { desc : " " , createFakeKubeConfig : true , kubeconfig : fakeKubeConfig , nodeID : " " , allowEmptyCloudConfig : true , expectedErr : nil , } , { desc : " " , createFakeCredFile : true , kubeconfig : " " , nodeID : " " , userAgent : " " , allowEmptyCloudConfig : true , expectedErr : nil , } , } for _ , test := range tests { if test . createFakeKubeConfig { if err := createTestFile ( fakeKubeConfig ) ; err != nil { t . Error ( err ) } defer func ( ) { if err := os . Remove ( fakeKubeConfig ) ; err != nil { t . Error ( err ) } } ( ) if err := ioutil . WriteFile ( fakeKubeConfig , [ ] byte ( fakeContent ) , 0666 ) ; err != nil { t . Error ( err ) } } if test . createFakeCredFile { if err := createTestFile ( fakeCredFile ) ; err != nil { t . Error ( err ) } defer func ( ) { if err := os . Remove ( fakeCredFile ) ; err != nil { t . Error ( err ) } } ( ) originalCredFile , ok := os . LookupEnv ( DefaultAzureCredentialFileEnv ) if ok { defer os . Setenv ( DefaultAzureCredentialFileEnv , originalCredFile ) } else { defer os . Unsetenv ( DefaultAzureCredentialFileEnv ) } os . Setenv ( DefaultAzureCredentialFileEnv , fakeCredFile ) } cloud , err := getCloudProvider ( test . kubeconfig , test . nodeID , " " , " " , test . userAgent , test . allowEmptyCloudConfig ) if ! reflect . DeepEqual ( err , test . expectedErr ) && test . expectedErr != nil && ! strings . Contains ( err . Error ( ) , test . expectedErr . Error ( ) ) { t . Errorf ( " \n " , test . desc , test . kubeconfig , err , test . expectedErr ) } if cloud == nil { t . Errorf ( " " ) } else { assert . Equal ( t , cloud . Environment . StorageEndpointSuffix , storage . DefaultBaseURL ) assert . Equal ( t , cloud . UserAgent , test . userAgent ) } } }
TestGetCloudProvider tests the func getCloudProvider().
TestGetCloudProvider tests the func getCloudProvider () .