Merge pull request #46277 from zjj2wry/credentials
Automatic merge from submit-queue (batch tested with PRs 46252, 45524, 46236, 46277, 46522) add test in create authinfo **What this PR does / why we need it**: **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
		| @@ -18,10 +18,14 @@ package config | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"io/ioutil" | ||||
| 	"os" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
|  | ||||
| 	"k8s.io/apiserver/pkg/util/flag" | ||||
| 	"k8s.io/client-go/tools/clientcmd" | ||||
| 	clientcmdapi "k8s.io/client-go/tools/clientcmd/api" | ||||
| ) | ||||
|  | ||||
| func stringFlagFor(s string) flag.StringFlag { | ||||
| @@ -188,3 +192,69 @@ func TestCreateAuthInfoOptions(t *testing.T) { | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| type createAuthInfoTest struct { | ||||
| 	description    string | ||||
| 	config         clientcmdapi.Config | ||||
| 	args           []string | ||||
| 	flags          []string | ||||
| 	expected       string | ||||
| 	expectedConfig clientcmdapi.Config | ||||
| } | ||||
|  | ||||
| func TestCreateAuthInfo(t *testing.T) { | ||||
| 	conf := clientcmdapi.Config{} | ||||
| 	test := createAuthInfoTest{ | ||||
| 		description: "Testing for create aythinfo", | ||||
| 		config:      conf, | ||||
| 		args:        []string{"cluster-admin"}, | ||||
| 		flags: []string{ | ||||
| 			"--username=admin", | ||||
| 			"--password=uXFGweU9l35qcif", | ||||
| 		}, | ||||
| 		expected: `User "cluster-admin" set.` + "\n", | ||||
| 		expectedConfig: clientcmdapi.Config{ | ||||
| 			AuthInfos: map[string]*clientcmdapi.AuthInfo{ | ||||
| 				"cluster-admin": {Username: "admin", Password: "uXFGweU9l35qcif"}}, | ||||
| 		}, | ||||
| 	} | ||||
| 	test.run(t) | ||||
| } | ||||
| func (test createAuthInfoTest) run(t *testing.T) { | ||||
| 	fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("unexpected error: %v", err) | ||||
| 	} | ||||
| 	defer os.Remove(fakeKubeFile.Name()) | ||||
| 	err = clientcmd.WriteToFile(test.config, fakeKubeFile.Name()) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("unexpected error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	pathOptions := clientcmd.NewDefaultPathOptions() | ||||
| 	pathOptions.GlobalFile = fakeKubeFile.Name() | ||||
| 	pathOptions.EnvVar = "" | ||||
| 	buf := bytes.NewBuffer([]byte{}) | ||||
| 	cmd := NewCmdConfigSetAuthInfo(buf, pathOptions) | ||||
| 	cmd.SetArgs(test.args) | ||||
| 	cmd.Flags().Parse(test.flags) | ||||
| 	if err := cmd.Execute(); err != nil { | ||||
| 		t.Fatalf("unexpected error executing command: %v,kubectl config set-credentials  args: %v,flags: %v", err, test.args, test.flags) | ||||
| 	} | ||||
| 	config, err := clientcmd.LoadFromFile(fakeKubeFile.Name()) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("unexpected error loading kubeconfig file: %v", err) | ||||
| 	} | ||||
| 	if len(test.expected) != 0 { | ||||
| 		if buf.String() != test.expected { | ||||
| 			t.Errorf("Fail in %q:\n expected %v\n but got %v\n", test.description, test.expected, buf.String()) | ||||
| 		} | ||||
| 	} | ||||
| 	if test.expectedConfig.AuthInfos != nil { | ||||
| 		expectAuthInfo := test.expectedConfig.AuthInfos[test.args[0]] | ||||
| 		actualAuthInfo := config.AuthInfos[test.args[0]] | ||||
| 		if expectAuthInfo.Username != actualAuthInfo.Username || expectAuthInfo.Password != actualAuthInfo.Password { | ||||
| 			t.Errorf("Fail in %q:\n expected AuthInfo%v\n but found %v in kubeconfig\n", test.description, expectAuthInfo, actualAuthInfo) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Kubernetes Submit Queue
					Kubernetes Submit Queue