[#22] - use table driven test for set auth
* Using table driven test for set_authinfo_test.go * Added required golden files * Review comments implemented * varibale naming updated to be more readable * 2 functions merged into one with tests defined in list and iterated * removed expected config and directly used values (username/password) for verification * Had to adjust the code as per lates merged changes for config cleanup Change-Id: I167d651a2e5bcd2905e453ef47a88e02d08487a7
This commit is contained in:
parent
3ca4d7d266
commit
796e6d0698
@ -17,8 +17,8 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
kubeconfig "k8s.io/client-go/tools/clientcmd/api"
|
||||
@ -32,20 +32,18 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
testUsername = "admin@kubernetes"
|
||||
testPassword = "adminPassword"
|
||||
testNewname = "dummy"
|
||||
testOldname = "def-user"
|
||||
pwdDelta = "_changed"
|
||||
testUsername = "admin@kubernetes"
|
||||
testPassword = "adminPassword"
|
||||
newUserName = "dummy"
|
||||
existingUserName = "def-user"
|
||||
pwdDelta = "_changed"
|
||||
)
|
||||
|
||||
type setAuthInfoTest struct {
|
||||
description string
|
||||
givenConfig *config.Config
|
||||
args []string
|
||||
flags []string
|
||||
expectedOutput string
|
||||
expectedConfig *config.Config
|
||||
inputConfig *config.Config
|
||||
cmdTest *testutil.CmdTest
|
||||
userName string
|
||||
userPassword string
|
||||
}
|
||||
|
||||
func TestConfigSetAuthInfo(t *testing.T) {
|
||||
@ -74,108 +72,85 @@ func TestConfigSetAuthInfo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// initConfig creates an input config and an associated expected config
|
||||
// initInputConfig creates an input config
|
||||
// Each of these config objects are associated with real files. Those files can be
|
||||
// cleaned up by calling cleanup
|
||||
func initConfig(t *testing.T, withUser bool, testname string) (
|
||||
given, expected *config.Config, cleanup func(*testing.T)) {
|
||||
func initInputConfig(t *testing.T) (given *config.Config, cleanup func(*testing.T)) {
|
||||
given, givenCleanup := config.InitConfig(t)
|
||||
if withUser {
|
||||
kAuthInfo := kubeconfig.NewAuthInfo()
|
||||
kAuthInfo.Username = testUsername
|
||||
kAuthInfo.Password = testPassword
|
||||
given.KubeConfig().AuthInfos[testname] = kAuthInfo
|
||||
given.AuthInfos[testname].SetKubeAuthInfo(kAuthInfo)
|
||||
}
|
||||
kubeAuthInfo := kubeconfig.NewAuthInfo()
|
||||
kubeAuthInfo.Username = testUsername
|
||||
kubeAuthInfo.Password = testPassword
|
||||
given.KubeConfig().AuthInfos[existingUserName] = kubeAuthInfo
|
||||
given.AuthInfos[existingUserName].SetKubeAuthInfo(kubeAuthInfo)
|
||||
|
||||
expected, expectedCleanup := config.InitConfig(t)
|
||||
expected.AuthInfos[testname] = config.NewAuthInfo()
|
||||
|
||||
expkAuthInfo := kubeconfig.NewAuthInfo()
|
||||
expkAuthInfo.Username = testUsername
|
||||
expkAuthInfo.Password = testPassword
|
||||
expected.KubeConfig().AuthInfos[testname] = expkAuthInfo
|
||||
expected.AuthInfos[testname].SetKubeAuthInfo(expkAuthInfo)
|
||||
|
||||
return given, expected, func(tt *testing.T) {
|
||||
givenCleanup(tt)
|
||||
expectedCleanup(tt)
|
||||
}
|
||||
return given, givenCleanup
|
||||
}
|
||||
|
||||
func TestSetAuthInfo(t *testing.T) {
|
||||
given, expected, cleanup := initConfig(t, false, testNewname)
|
||||
given, cleanup := config.InitConfig(t)
|
||||
defer cleanup(t)
|
||||
|
||||
test := setAuthInfoTest{
|
||||
description: "Testing 'airshipctl config set-credential' with a new user",
|
||||
givenConfig: given,
|
||||
args: []string{testNewname},
|
||||
flags: []string{
|
||||
"--" + config.FlagUsername + "=" + testUsername,
|
||||
"--" + config.FlagPassword + "=" + testPassword,
|
||||
input, cleanupInput := initInputConfig(t)
|
||||
defer cleanupInput(t)
|
||||
|
||||
tests := []struct {
|
||||
testName string
|
||||
flags []string
|
||||
userName string
|
||||
userPassword string
|
||||
inputConfig *config.Config
|
||||
}{
|
||||
{
|
||||
testName: "set-auth-info",
|
||||
flags: []string{
|
||||
"--" + config.FlagUsername + "=" + testUsername,
|
||||
"--" + config.FlagPassword + "=" + testPassword,
|
||||
},
|
||||
userName: newUserName,
|
||||
userPassword: testPassword,
|
||||
inputConfig: given,
|
||||
},
|
||||
expectedOutput: fmt.Sprintf("User information %q created.\n", testNewname),
|
||||
expectedConfig: expected,
|
||||
}
|
||||
test.run(t)
|
||||
}
|
||||
|
||||
func TestModifyAuthInfo(t *testing.T) {
|
||||
given, expected, cleanup := initConfig(t, true, testOldname)
|
||||
defer cleanup(t)
|
||||
|
||||
expected.AuthInfos[testOldname].KubeAuthInfo().Password = testPassword + pwdDelta
|
||||
|
||||
test := setAuthInfoTest{
|
||||
description: "Testing 'airshipctl config set-credential' with an existing user",
|
||||
givenConfig: given,
|
||||
args: []string{testOldname},
|
||||
flags: []string{
|
||||
"--" + config.FlagPassword + "=" + testPassword + pwdDelta,
|
||||
{
|
||||
testName: "modify-auth-info",
|
||||
flags: []string{
|
||||
"--" + config.FlagPassword + "=" + testPassword + pwdDelta,
|
||||
},
|
||||
userName: existingUserName,
|
||||
userPassword: testPassword + pwdDelta,
|
||||
inputConfig: input,
|
||||
},
|
||||
expectedOutput: fmt.Sprintf("User information %q modified.\n", testOldname),
|
||||
expectedConfig: expected,
|
||||
}
|
||||
test.run(t)
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
cmd := &testutil.CmdTest{
|
||||
Name: tt.testName,
|
||||
CmdLine: fmt.Sprintf("%s %s", tt.userName, strings.Join(tt.flags, " ")),
|
||||
}
|
||||
test := setAuthInfoTest{
|
||||
inputConfig: tt.inputConfig,
|
||||
cmdTest: cmd,
|
||||
userName: tt.userName,
|
||||
userPassword: tt.userPassword,
|
||||
}
|
||||
test.run(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (test setAuthInfoTest) run(t *testing.T) {
|
||||
// Get the Environment
|
||||
settings := &environment.AirshipCTLSettings{}
|
||||
settings.SetConfig(test.givenConfig)
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
|
||||
cmd := NewCmdConfigSetAuthInfo(settings)
|
||||
cmd.SetOut(buf)
|
||||
cmd.SetArgs(test.args)
|
||||
err := cmd.Flags().Parse(test.flags)
|
||||
require.NoErrorf(t, err, "unexpected error flags args to command: %v, flags: %v", err, test.flags)
|
||||
|
||||
// Execute the Command
|
||||
// Which should Persist the File
|
||||
err = cmd.Execute()
|
||||
require.NoErrorf(t, err, "unexpected error executing command: %v, args: %v, flags: %v", err, test.args, test.flags)
|
||||
settings.SetConfig(test.inputConfig)
|
||||
test.cmdTest.Cmd = NewCmdConfigSetAuthInfo(settings)
|
||||
testutil.RunTest(t, test.cmdTest)
|
||||
|
||||
afterRunConf := settings.Config()
|
||||
|
||||
// Find the AuthInfo Created or Modified
|
||||
afterRunAuthInfo, err := afterRunConf.GetAuthInfo(test.args[0])
|
||||
afterRunAuthInfo, err := afterRunConf.GetAuthInfo(test.userName)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, afterRunAuthInfo)
|
||||
|
||||
afterKauthinfo := afterRunAuthInfo.KubeAuthInfo()
|
||||
require.NotNil(t, afterKauthinfo)
|
||||
|
||||
testKauthinfo := test.expectedConfig.KubeConfig().AuthInfos[test.args[0]]
|
||||
require.NotNil(t, testKauthinfo)
|
||||
|
||||
assert.EqualValues(t, testKauthinfo.Username, afterKauthinfo.Username)
|
||||
assert.EqualValues(t, testKauthinfo.Password, afterKauthinfo.Password)
|
||||
|
||||
// Test that the Return Message looks correct
|
||||
if len(test.expectedOutput) != 0 {
|
||||
assert.EqualValues(t, test.expectedOutput, buf.String())
|
||||
}
|
||||
assert.EqualValues(t, afterKauthinfo.Username, testUsername)
|
||||
assert.EqualValues(t, afterKauthinfo.Password, test.userPassword)
|
||||
}
|
||||
|
1
cmd/config/testdata/TestSetAuthInfoGoldenOutput/modify-auth-info.golden
vendored
Normal file
1
cmd/config/testdata/TestSetAuthInfoGoldenOutput/modify-auth-info.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
User information "def-user" modified.
|
1
cmd/config/testdata/TestSetAuthInfoGoldenOutput/set-auth-info.golden
vendored
Normal file
1
cmd/config/testdata/TestSetAuthInfoGoldenOutput/set-auth-info.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
User information "dummy" created.
|
Loading…
Reference in New Issue
Block a user