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"
|
||||||
|
|
||||||
@ -33,15 +31,16 @@ 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
|
||||||
args []string
|
cmdTest *testutil.CmdTest
|
||||||
flags []string
|
contextName string
|
||||||
expectedOutput string
|
manifest string
|
||||||
expectedConfig *config.Config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfigSetContext(t *testing.T) {
|
func TestConfigSetContext(t *testing.T) {
|
||||||
@ -73,133 +72,77 @@ 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"
|
|
||||||
|
|
||||||
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{
|
flags: []string{
|
||||||
"--" + config.FlagClusterType + "=" + config.Target,
|
"--" + config.FlagClusterType + "=" + config.Target,
|
||||||
"--" + config.FlagAuthInfoName + "=" + testUser,
|
"--" + config.FlagAuthInfoName + "=" + testUser,
|
||||||
"--" + config.FlagManifest + "=edge_cloud",
|
"--" + config.FlagManifest + "=" + defaultManifest,
|
||||||
"--" + config.FlagNamespace + "=kube-system",
|
"--" + config.FlagNamespace + "=" + defaultNamespace,
|
||||||
},
|
},
|
||||||
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,
|
givenConfig: given,
|
||||||
args: []string{tname},
|
manifest: defaultManifest,
|
||||||
expectedOutput: fmt.Sprintf("Context %q not modified. No new options provided.\n", tname),
|
},
|
||||||
expectedConfig: expected,
|
{
|
||||||
}
|
testName: "set-current-context",
|
||||||
test.run(t)
|
contextName: "def_target",
|
||||||
}
|
flags: []string{},
|
||||||
|
|
||||||
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,
|
givenConfig: given,
|
||||||
args: []string{tname},
|
},
|
||||||
|
{
|
||||||
|
testName: "modify-context",
|
||||||
|
contextName: "def_target",
|
||||||
flags: []string{
|
flags: []string{
|
||||||
"--" + config.FlagAuthInfoName + "=" + testUser,
|
"--" + config.FlagManifest + "=" + testManifest,
|
||||||
},
|
},
|
||||||
expectedOutput: fmt.Sprintf("Context %q modified.\n", tname),
|
givenConfig: given,
|
||||||
expectedConfig: expected,
|
manifest: testManifest,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
test.run(t)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (test setContextTest) run(t *testing.T) {
|
func (test setContextTest) run(t *testing.T) {
|
||||||
// Get the Environment
|
// Get the Environment
|
||||||
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