From e0a4d980f6f800444f8f8af959a0c2bef6184e9d Mon Sep 17 00:00:00 2001 From: "Yasin, Siraj (SY495P)" Date: Fri, 14 Feb 2020 07:05:50 +0000 Subject: [PATCH] [#22] - use table test for set context tests * Added a helper function InitContextConfig to setup actual and expected configs * used testutils.runTest * added golden files with expected output Change-Id: I720da38103354a8eb0665ec077529584581e6923 --- cmd/config/set_context_test.go | 179 ++++++------------ .../modify-context.golden | 1 + .../set-context.golden | 1 + .../set-current-context.golden | 1 + 4 files changed, 64 insertions(+), 118 deletions(-) create mode 100644 cmd/config/testdata/TestSetContextGoldenOutput/modify-context.golden create mode 100644 cmd/config/testdata/TestSetContextGoldenOutput/set-context.golden create mode 100644 cmd/config/testdata/TestSetContextGoldenOutput/set-current-context.golden diff --git a/cmd/config/set_context_test.go b/cmd/config/set_context_test.go index b017e7b64..5b0463306 100644 --- a/cmd/config/set_context_test.go +++ b/cmd/config/set_context_test.go @@ -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) } } diff --git a/cmd/config/testdata/TestSetContextGoldenOutput/modify-context.golden b/cmd/config/testdata/TestSetContextGoldenOutput/modify-context.golden new file mode 100644 index 000000000..449c394f5 --- /dev/null +++ b/cmd/config/testdata/TestSetContextGoldenOutput/modify-context.golden @@ -0,0 +1 @@ +Context "def_target" modified. diff --git a/cmd/config/testdata/TestSetContextGoldenOutput/set-context.golden b/cmd/config/testdata/TestSetContextGoldenOutput/set-context.golden new file mode 100644 index 000000000..ddc6cc4d3 --- /dev/null +++ b/cmd/config/testdata/TestSetContextGoldenOutput/set-context.golden @@ -0,0 +1 @@ +Context "dummycontext" created. diff --git a/cmd/config/testdata/TestSetContextGoldenOutput/set-current-context.golden b/cmd/config/testdata/TestSetContextGoldenOutput/set-current-context.golden new file mode 100644 index 000000000..a48fc22ec --- /dev/null +++ b/cmd/config/testdata/TestSetContextGoldenOutput/set-current-context.golden @@ -0,0 +1 @@ +Context "def_target" not modified. No new options provided.