Merge "[#22] - use table test for set context tests"
This commit is contained in:
commit
84e677344d
@ -17,12 +17,10 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
kubeconfig "k8s.io/client-go/tools/clientcmd/api"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
@ -32,16 +30,17 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
testUser = "admin@kubernetes"
|
||||
testUser = "admin@kubernetes"
|
||||
defaultManifest = "edge_cloud"
|
||||
defaultNamespace = "kube-system"
|
||||
testManifest = "test_manifest"
|
||||
)
|
||||
|
||||
type setContextTest struct {
|
||||
description string
|
||||
givenConfig *config.Config
|
||||
args []string
|
||||
flags []string
|
||||
expectedOutput string
|
||||
expectedConfig *config.Config
|
||||
givenConfig *config.Config
|
||||
cmdTest *testutil.CmdTest
|
||||
contextName string
|
||||
manifest string
|
||||
}
|
||||
|
||||
func TestConfigSetContext(t *testing.T) {
|
||||
@ -73,96 +72,56 @@ func TestSetContext(t *testing.T) {
|
||||
given, cleanupGiven := config.InitConfig(t)
|
||||
defer cleanupGiven(t)
|
||||
|
||||
tname := "dummycontext"
|
||||
tctype := config.Ephemeral
|
||||
|
||||
expected, cleanupExpected := config.InitConfig(t)
|
||||
defer cleanupExpected(t)
|
||||
|
||||
expected.Contexts[tname] = config.NewContext()
|
||||
clusterName := config.NewClusterComplexName()
|
||||
clusterName.WithType(tname, tctype)
|
||||
expected.Contexts[tname].NameInKubeconf = clusterName.Name()
|
||||
expected.Contexts[tname].Manifest = "edge_cloud"
|
||||
|
||||
expkContext := kubeconfig.NewContext()
|
||||
expkContext.AuthInfo = testUser
|
||||
expkContext.Namespace = "kube-system"
|
||||
expected.KubeConfig().Contexts[expected.Contexts[tname].NameInKubeconf] = expkContext
|
||||
|
||||
test := setContextTest{
|
||||
description: "Testing 'airshipctl config set-context' with a new context",
|
||||
givenConfig: given,
|
||||
args: []string{tname},
|
||||
flags: []string{
|
||||
"--" + config.FlagClusterType + "=" + config.Target,
|
||||
"--" + config.FlagAuthInfoName + "=" + testUser,
|
||||
"--" + config.FlagManifest + "=edge_cloud",
|
||||
"--" + config.FlagNamespace + "=kube-system",
|
||||
tests := []struct {
|
||||
testName string
|
||||
contextName string
|
||||
flags []string
|
||||
givenConfig *config.Config
|
||||
manifest string
|
||||
}{
|
||||
{
|
||||
testName: "set-context",
|
||||
contextName: "dummycontext",
|
||||
flags: []string{
|
||||
"--" + config.FlagClusterType + "=" + config.Target,
|
||||
"--" + config.FlagAuthInfoName + "=" + testUser,
|
||||
"--" + config.FlagManifest + "=" + defaultManifest,
|
||||
"--" + config.FlagNamespace + "=" + defaultNamespace,
|
||||
},
|
||||
givenConfig: given,
|
||||
manifest: defaultManifest,
|
||||
},
|
||||
expectedOutput: fmt.Sprintf("Context %q created.\n", tname),
|
||||
expectedConfig: expected,
|
||||
}
|
||||
test.run(t)
|
||||
}
|
||||
|
||||
func TestSetCurrentContextNoOptions(t *testing.T) {
|
||||
tname := "def_target"
|
||||
given, cleanupGiven := config.InitConfig(t)
|
||||
defer cleanupGiven(t)
|
||||
|
||||
expected, cleanupExpected := config.InitConfig(t)
|
||||
defer cleanupExpected(t)
|
||||
|
||||
expected.CurrentContext = "def_target"
|
||||
|
||||
test := setContextTest{
|
||||
givenConfig: given,
|
||||
args: []string{tname},
|
||||
expectedOutput: fmt.Sprintf("Context %q not modified. No new options provided.\n", tname),
|
||||
expectedConfig: expected,
|
||||
}
|
||||
test.run(t)
|
||||
}
|
||||
|
||||
func TestModifyContext(t *testing.T) {
|
||||
tname := testCluster
|
||||
tctype := config.Ephemeral
|
||||
|
||||
given, cleanupGiven := config.InitConfig(t)
|
||||
defer cleanupGiven(t)
|
||||
|
||||
given.Contexts[testCluster] = config.NewContext()
|
||||
|
||||
clusterName := config.NewClusterComplexName()
|
||||
clusterName.WithType(tname, tctype)
|
||||
given.Contexts[tname].NameInKubeconf = clusterName.Name()
|
||||
kContext := kubeconfig.NewContext()
|
||||
kContext.AuthInfo = testUser
|
||||
given.KubeConfig().Contexts[clusterName.Name()] = kContext
|
||||
given.Contexts[tname].SetKubeContext(kContext)
|
||||
|
||||
expected, cleanupExpected := config.InitConfig(t)
|
||||
defer cleanupExpected(t)
|
||||
|
||||
expected.Contexts[tname] = config.NewContext()
|
||||
expected.Contexts[tname].NameInKubeconf = clusterName.Name()
|
||||
expkContext := kubeconfig.NewContext()
|
||||
expkContext.AuthInfo = testUser
|
||||
expected.KubeConfig().Contexts[clusterName.Name()] = expkContext
|
||||
expected.Contexts[tname].SetKubeContext(expkContext)
|
||||
|
||||
test := setContextTest{
|
||||
description: "Testing 'airshipctl config set-context' with an existing context",
|
||||
givenConfig: given,
|
||||
args: []string{tname},
|
||||
flags: []string{
|
||||
"--" + config.FlagAuthInfoName + "=" + testUser,
|
||||
{
|
||||
testName: "set-current-context",
|
||||
contextName: "def_target",
|
||||
flags: []string{},
|
||||
givenConfig: given,
|
||||
},
|
||||
{
|
||||
testName: "modify-context",
|
||||
contextName: "def_target",
|
||||
flags: []string{
|
||||
"--" + config.FlagManifest + "=" + testManifest,
|
||||
},
|
||||
givenConfig: given,
|
||||
manifest: testManifest,
|
||||
},
|
||||
expectedOutput: fmt.Sprintf("Context %q modified.\n", tname),
|
||||
expectedConfig: expected,
|
||||
}
|
||||
test.run(t)
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
cmd := &testutil.CmdTest{
|
||||
Name: tt.testName,
|
||||
CmdLine: fmt.Sprintf("%s %s", tt.contextName, strings.Join(tt.flags, " ")),
|
||||
}
|
||||
test := setContextTest{
|
||||
givenConfig: tt.givenConfig,
|
||||
cmdTest: cmd,
|
||||
contextName: tt.contextName,
|
||||
manifest: tt.manifest,
|
||||
}
|
||||
test.run(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (test setContextTest) run(t *testing.T) {
|
||||
@ -170,36 +129,20 @@ func (test setContextTest) run(t *testing.T) {
|
||||
settings := &environment.AirshipCTLSettings{}
|
||||
settings.SetConfig(test.givenConfig)
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
|
||||
cmd := NewCmdConfigSetContext(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)
|
||||
test.cmdTest.Cmd = NewCmdConfigSetContext(settings)
|
||||
testutil.RunTest(t, test.cmdTest)
|
||||
|
||||
afterRunConf := settings.Config()
|
||||
|
||||
// Find the Context Created or Modified
|
||||
afterRunContext, err := afterRunConf.GetContext(test.args[0])
|
||||
afterRunContext, err := afterRunConf.GetContext(test.contextName)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, afterRunContext)
|
||||
|
||||
afterKcontext := afterRunContext.KubeContext()
|
||||
require.NotNil(t, afterKcontext)
|
||||
|
||||
testKcontext := test.expectedConfig.KubeConfig().Contexts[test.expectedConfig.Contexts[test.args[0]].NameInKubeconf]
|
||||
require.NotNil(t, testKcontext)
|
||||
|
||||
assert.EqualValues(t, afterKcontext.AuthInfo, testKcontext.AuthInfo)
|
||||
|
||||
// Test that the Return Message looks correct
|
||||
if len(test.expectedOutput) != 0 {
|
||||
assert.EqualValues(t, test.expectedOutput, buf.String())
|
||||
if test.manifest != "" {
|
||||
assert.EqualValues(t, afterRunContext.Manifest, test.manifest)
|
||||
}
|
||||
}
|
||||
|
1
cmd/config/testdata/TestSetContextGoldenOutput/modify-context.golden
vendored
Normal file
1
cmd/config/testdata/TestSetContextGoldenOutput/modify-context.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
Context "def_target" modified.
|
1
cmd/config/testdata/TestSetContextGoldenOutput/set-context.golden
vendored
Normal file
1
cmd/config/testdata/TestSetContextGoldenOutput/set-context.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
Context "dummycontext" created.
|
1
cmd/config/testdata/TestSetContextGoldenOutput/set-current-context.golden
vendored
Normal file
1
cmd/config/testdata/TestSetContextGoldenOutput/set-current-context.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
Context "def_target" not modified. No new options provided.
|
Loading…
Reference in New Issue
Block a user