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
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
kubeconfig "k8s.io/client-go/tools/clientcmd/api"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
@ -32,16 +30,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
testUser = "admin@kubernetes"
|
testUser = "admin@kubernetes"
|
||||||
|
defaultManifest = "edge_cloud"
|
||||||
|
defaultNamespace = "kube-system"
|
||||||
|
testManifest = "test_manifest"
|
||||||
)
|
)
|
||||||
|
|
||||||
type setContextTest struct {
|
type setContextTest struct {
|
||||||
description string
|
givenConfig *config.Config
|
||||||
givenConfig *config.Config
|
cmdTest *testutil.CmdTest
|
||||||
args []string
|
contextName string
|
||||||
flags []string
|
manifest string
|
||||||
expectedOutput string
|
|
||||||
expectedConfig *config.Config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfigSetContext(t *testing.T) {
|
func TestConfigSetContext(t *testing.T) {
|
||||||
@ -73,96 +72,56 @@ func TestSetContext(t *testing.T) {
|
|||||||
given, cleanupGiven := config.InitConfig(t)
|
given, cleanupGiven := config.InitConfig(t)
|
||||||
defer cleanupGiven(t)
|
defer cleanupGiven(t)
|
||||||
|
|
||||||
tname := "dummycontext"
|
tests := []struct {
|
||||||
tctype := config.Ephemeral
|
testName string
|
||||||
|
contextName string
|
||||||
expected, cleanupExpected := config.InitConfig(t)
|
flags []string
|
||||||
defer cleanupExpected(t)
|
givenConfig *config.Config
|
||||||
|
manifest string
|
||||||
expected.Contexts[tname] = config.NewContext()
|
}{
|
||||||
clusterName := config.NewClusterComplexName()
|
{
|
||||||
clusterName.WithType(tname, tctype)
|
testName: "set-context",
|
||||||
expected.Contexts[tname].NameInKubeconf = clusterName.Name()
|
contextName: "dummycontext",
|
||||||
expected.Contexts[tname].Manifest = "edge_cloud"
|
flags: []string{
|
||||||
|
"--" + config.FlagClusterType + "=" + config.Target,
|
||||||
expkContext := kubeconfig.NewContext()
|
"--" + config.FlagAuthInfoName + "=" + testUser,
|
||||||
expkContext.AuthInfo = testUser
|
"--" + config.FlagManifest + "=" + defaultManifest,
|
||||||
expkContext.Namespace = "kube-system"
|
"--" + config.FlagNamespace + "=" + defaultNamespace,
|
||||||
expected.KubeConfig().Contexts[expected.Contexts[tname].NameInKubeconf] = expkContext
|
},
|
||||||
|
givenConfig: given,
|
||||||
test := setContextTest{
|
manifest: defaultManifest,
|
||||||
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",
|
|
||||||
},
|
},
|
||||||
expectedOutput: fmt.Sprintf("Context %q created.\n", tname),
|
{
|
||||||
expectedConfig: expected,
|
testName: "set-current-context",
|
||||||
}
|
contextName: "def_target",
|
||||||
test.run(t)
|
flags: []string{},
|
||||||
}
|
givenConfig: given,
|
||||||
|
},
|
||||||
func TestSetCurrentContextNoOptions(t *testing.T) {
|
{
|
||||||
tname := "def_target"
|
testName: "modify-context",
|
||||||
given, cleanupGiven := config.InitConfig(t)
|
contextName: "def_target",
|
||||||
defer cleanupGiven(t)
|
flags: []string{
|
||||||
|
"--" + config.FlagManifest + "=" + testManifest,
|
||||||
expected, cleanupExpected := config.InitConfig(t)
|
},
|
||||||
defer cleanupExpected(t)
|
givenConfig: given,
|
||||||
|
manifest: testManifest,
|
||||||
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,
|
|
||||||
},
|
},
|
||||||
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) {
|
func (test setContextTest) run(t *testing.T) {
|
||||||
@ -170,36 +129,20 @@ func (test setContextTest) run(t *testing.T) {
|
|||||||
settings := &environment.AirshipCTLSettings{}
|
settings := &environment.AirshipCTLSettings{}
|
||||||
settings.SetConfig(test.givenConfig)
|
settings.SetConfig(test.givenConfig)
|
||||||
|
|
||||||
buf := bytes.NewBuffer([]byte{})
|
test.cmdTest.Cmd = NewCmdConfigSetContext(settings)
|
||||||
|
testutil.RunTest(t, test.cmdTest)
|
||||||
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)
|
|
||||||
|
|
||||||
afterRunConf := settings.Config()
|
afterRunConf := settings.Config()
|
||||||
|
|
||||||
// Find the Context Created or Modified
|
// Find the Context Created or Modified
|
||||||
afterRunContext, err := afterRunConf.GetContext(test.args[0])
|
afterRunContext, err := afterRunConf.GetContext(test.contextName)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, afterRunContext)
|
require.NotNil(t, afterRunContext)
|
||||||
|
|
||||||
afterKcontext := afterRunContext.KubeContext()
|
afterKcontext := afterRunContext.KubeContext()
|
||||||
require.NotNil(t, afterKcontext)
|
require.NotNil(t, afterKcontext)
|
||||||
|
|
||||||
testKcontext := test.expectedConfig.KubeConfig().Contexts[test.expectedConfig.Contexts[test.args[0]].NameInKubeconf]
|
if test.manifest != "" {
|
||||||
require.NotNil(t, testKcontext)
|
assert.EqualValues(t, afterRunContext.Manifest, test.manifest)
|
||||||
|
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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