...

Source file src/k8s.io/kubectl/pkg/cmd/config/get_users_test.go

Documentation: k8s.io/kubectl/pkg/cmd/config

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/cli-runtime/pkg/genericiooptions"
    23  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    24  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    25  )
    26  
    27  func TestGetUsersRun(t *testing.T) {
    28  	var tests = []struct {
    29  		name     string
    30  		config   clientcmdapi.Config
    31  		expected string
    32  	}{
    33  		{
    34  			name:     "no users",
    35  			config:   clientcmdapi.Config{},
    36  			expected: "NAME\n",
    37  		},
    38  		{
    39  			name: "some users",
    40  			config: clientcmdapi.Config{
    41  				AuthInfos: map[string]*clientcmdapi.AuthInfo{
    42  					"minikube": {Username: "minikube"},
    43  					"admin":    {Username: "admin"},
    44  				},
    45  			},
    46  			expected: `NAME
    47  admin
    48  minikube
    49  `,
    50  		},
    51  	}
    52  
    53  	for i := range tests {
    54  		test := tests[i]
    55  		t.Run(test.name, func(t *testing.T) {
    56  			tf := cmdtesting.NewTestFactory()
    57  			defer tf.Cleanup()
    58  
    59  			ioStreams, _, out, _ := genericiooptions.NewTestIOStreams()
    60  			pathOptions, err := tf.PathOptionsWithConfig(test.config)
    61  			if err != nil {
    62  				t.Fatalf("unexpected error executing command: %v", err)
    63  			}
    64  			options := NewGetUsersOptions(ioStreams, pathOptions)
    65  
    66  			if err = options.Run(); err != nil {
    67  				t.Fatalf("unexpected error executing command: %v", err)
    68  			}
    69  
    70  			if got := out.String(); got != test.expected {
    71  				t.Fatalf("expected: %s but got %s", test.expected, got)
    72  			}
    73  		})
    74  	}
    75  }
    76  

View as plain text