mirror of https://github.com/k3s-io/k3s
Merge pull request #72457 from bart0sh/PR0053-kubeadm-app-cmd-use-T.Run
kubeadm: use T.Run API in app/cmdpull/564/head
commit
b502b99d25
|
@ -52,16 +52,19 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) {
|
|||
}
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
command string
|
||||
withClientCert bool
|
||||
withToken bool
|
||||
additionalFlags []string
|
||||
}{
|
||||
{ // Test user subCommand withClientCert
|
||||
{
|
||||
name: "user subCommand withClientCert",
|
||||
command: "user",
|
||||
withClientCert: true,
|
||||
},
|
||||
{ // Test user subCommand withToken
|
||||
{
|
||||
name: "user subCommand withToken",
|
||||
withToken: true,
|
||||
command: "user",
|
||||
additionalFlags: []string{"--token=123456"},
|
||||
|
@ -69,36 +72,38 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, test := range tests {
|
||||
buf := new(bytes.Buffer)
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
// Get subcommands working in the temporary directory
|
||||
cmd := newCmdUserKubeConfig(buf)
|
||||
// Get subcommands working in the temporary directory
|
||||
cmd := newCmdUserKubeConfig(buf)
|
||||
|
||||
// Execute the subcommand
|
||||
allFlags := append(commonFlags, test.additionalFlags...)
|
||||
cmd.SetArgs(allFlags)
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatal("Could not execute subcommand")
|
||||
}
|
||||
// Execute the subcommand
|
||||
allFlags := append(commonFlags, test.additionalFlags...)
|
||||
cmd.SetArgs(allFlags)
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatal("Could not execute subcommand")
|
||||
}
|
||||
|
||||
// reads kubeconfig written to stdout
|
||||
config, err := clientcmd.Load(buf.Bytes())
|
||||
if err != nil {
|
||||
t.Errorf("couldn't read kubeconfig file from buffer: %v", err)
|
||||
continue
|
||||
}
|
||||
// reads kubeconfig written to stdout
|
||||
config, err := clientcmd.Load(buf.Bytes())
|
||||
if err != nil {
|
||||
t.Errorf("couldn't read kubeconfig file from buffer: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// checks that CLI flags are properly propagated
|
||||
kubeconfigtestutil.AssertKubeConfigCurrentCluster(t, config, "https://1.2.3.4:1234", caCert)
|
||||
// checks that CLI flags are properly propagated
|
||||
kubeconfigtestutil.AssertKubeConfigCurrentCluster(t, config, "https://1.2.3.4:1234", caCert)
|
||||
|
||||
if test.withClientCert {
|
||||
// checks that kubeconfig files have expected client cert
|
||||
kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithClientCert(t, config, caCert, "myUser")
|
||||
}
|
||||
if test.withClientCert {
|
||||
// checks that kubeconfig files have expected client cert
|
||||
kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithClientCert(t, config, caCert, "myUser")
|
||||
}
|
||||
|
||||
if test.withToken {
|
||||
// checks that kubeconfig files have expected token
|
||||
kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithToken(t, config, "myUser", "123456")
|
||||
}
|
||||
if test.withToken {
|
||||
// checks that kubeconfig files have expected token
|
||||
kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithToken(t, config, "myUser", "123456")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -88,8 +88,10 @@ func TestRunCompletion(t *testing.T) {
|
|||
parentCmd.AddCommand(cmd)
|
||||
|
||||
for _, tc := range testCases {
|
||||
if err := RunCompletion(&out, "", cmd, tc.args); (err != nil) != tc.expectedError {
|
||||
t.Errorf("Test case %q: TestRunCompletion expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if err := RunCompletion(&out, "", cmd, tc.args); (err != nil) != tc.expectedError {
|
||||
t.Errorf("Test case %q: TestRunCompletion expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -195,51 +195,53 @@ func TestConfigDirCleaner(t *testing.T) {
|
|||
}
|
||||
|
||||
for name, test := range tests {
|
||||
t.Logf("Running test: %s", name)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Logf("Running test: %s", name)
|
||||
|
||||
// Create a temporary directory for our fake config dir:
|
||||
tmpDir, err := ioutil.TempDir("", "kubeadm-reset-test")
|
||||
if err != nil {
|
||||
t.Errorf("Unable to create temporary directory: %s", err)
|
||||
}
|
||||
|
||||
for _, createDir := range test.setupDirs {
|
||||
err := os.Mkdir(filepath.Join(tmpDir, createDir), 0700)
|
||||
// Create a temporary directory for our fake config dir:
|
||||
tmpDir, err := ioutil.TempDir("", "kubeadm-reset-test")
|
||||
if err != nil {
|
||||
t.Errorf("Unable to setup test config directory: %s", err)
|
||||
t.Errorf("Unable to create temporary directory: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, createFile := range test.setupFiles {
|
||||
fullPath := filepath.Join(tmpDir, createFile)
|
||||
f, err := os.Create(fullPath)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to create test file: %s", err)
|
||||
for _, createDir := range test.setupDirs {
|
||||
err := os.Mkdir(filepath.Join(tmpDir, createDir), 0700)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to setup test config directory: %s", err)
|
||||
}
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
|
||||
if test.resetDir == "" {
|
||||
test.resetDir = "pki"
|
||||
}
|
||||
resetConfigDir(tmpDir, filepath.Join(tmpDir, test.resetDir))
|
||||
for _, createFile := range test.setupFiles {
|
||||
fullPath := filepath.Join(tmpDir, createFile)
|
||||
f, err := os.Create(fullPath)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to create test file: %s", err)
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
|
||||
// Verify the files we cleanup implicitly in every test:
|
||||
assertExists(t, tmpDir)
|
||||
assertNotExists(t, filepath.Join(tmpDir, kubeadmconstants.AdminKubeConfigFileName))
|
||||
assertNotExists(t, filepath.Join(tmpDir, kubeadmconstants.KubeletKubeConfigFileName))
|
||||
assertDirEmpty(t, filepath.Join(tmpDir, "manifests"))
|
||||
assertDirEmpty(t, filepath.Join(tmpDir, "pki"))
|
||||
if test.resetDir == "" {
|
||||
test.resetDir = "pki"
|
||||
}
|
||||
resetConfigDir(tmpDir, filepath.Join(tmpDir, test.resetDir))
|
||||
|
||||
// Verify the files as requested by the test:
|
||||
for _, path := range test.verifyExists {
|
||||
assertExists(t, filepath.Join(tmpDir, path))
|
||||
}
|
||||
for _, path := range test.verifyNotExists {
|
||||
assertNotExists(t, filepath.Join(tmpDir, path))
|
||||
}
|
||||
// Verify the files we cleanup implicitly in every test:
|
||||
assertExists(t, tmpDir)
|
||||
assertNotExists(t, filepath.Join(tmpDir, kubeadmconstants.AdminKubeConfigFileName))
|
||||
assertNotExists(t, filepath.Join(tmpDir, kubeadmconstants.KubeletKubeConfigFileName))
|
||||
assertDirEmpty(t, filepath.Join(tmpDir, "manifests"))
|
||||
assertDirEmpty(t, filepath.Join(tmpDir, "pki"))
|
||||
|
||||
os.RemoveAll(tmpDir)
|
||||
// Verify the files as requested by the test:
|
||||
for _, path := range test.verifyExists {
|
||||
assertExists(t, filepath.Join(tmpDir, path))
|
||||
}
|
||||
for _, path := range test.verifyNotExists {
|
||||
assertNotExists(t, filepath.Join(tmpDir, path))
|
||||
}
|
||||
|
||||
os.RemoveAll(tmpDir)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -309,38 +311,40 @@ func TestGetEtcdDataDir(t *testing.T) {
|
|||
}
|
||||
|
||||
for name, test := range tests {
|
||||
tmpdir := testutil.SetupTempDir(t)
|
||||
defer os.RemoveAll(tmpdir)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
tmpdir := testutil.SetupTempDir(t)
|
||||
defer os.RemoveAll(tmpdir)
|
||||
|
||||
manifestPath := filepath.Join(tmpdir, "etcd.yaml")
|
||||
if test.writeManifest {
|
||||
err := ioutil.WriteFile(manifestPath, []byte(test.podYaml), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf(dedent.Dedent("failed to write pod manifest\n%s\n\tfatal error: %v"), name, err)
|
||||
manifestPath := filepath.Join(tmpdir, "etcd.yaml")
|
||||
if test.writeManifest {
|
||||
err := ioutil.WriteFile(manifestPath, []byte(test.podYaml), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf(dedent.Dedent("failed to write pod manifest\n%s\n\tfatal error: %v"), name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var dataDir string
|
||||
var err error
|
||||
if test.validClient {
|
||||
client := clientsetfake.NewSimpleClientset()
|
||||
dataDir, err = getEtcdDataDir(manifestPath, client)
|
||||
} else {
|
||||
dataDir, err = getEtcdDataDir(manifestPath, nil)
|
||||
}
|
||||
var dataDir string
|
||||
var err error
|
||||
if test.validClient {
|
||||
client := clientsetfake.NewSimpleClientset()
|
||||
dataDir, err = getEtcdDataDir(manifestPath, client)
|
||||
} else {
|
||||
dataDir, err = getEtcdDataDir(manifestPath, nil)
|
||||
}
|
||||
|
||||
if (err != nil) != test.expectErr {
|
||||
t.Fatalf(dedent.Dedent(
|
||||
"getEtcdDataDir failed\n%s\nexpected error: %t\n\tgot: %t\nerror: %v"),
|
||||
name,
|
||||
test.expectErr,
|
||||
(err != nil),
|
||||
err,
|
||||
)
|
||||
}
|
||||
if (err != nil) != test.expectErr {
|
||||
t.Fatalf(dedent.Dedent(
|
||||
"getEtcdDataDir failed\n%s\nexpected error: %t\n\tgot: %t\nerror: %v"),
|
||||
name,
|
||||
test.expectErr,
|
||||
(err != nil),
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
if dataDir != test.dataDir {
|
||||
t.Fatalf(dedent.Dedent("getEtcdDataDir failed\n%s\n\texpected: %s\ngot: %s"), name, test.dataDir, dataDir)
|
||||
}
|
||||
if dataDir != test.dataDir {
|
||||
t.Fatalf(dedent.Dedent("getEtcdDataDir failed\n%s\n\texpected: %s\ngot: %s"), name, test.dataDir, dataDir)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -167,31 +167,33 @@ func TestRunCreateToken(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
bts, err := kubeadmapiv1beta1.NewBootstrapTokenString(tc.token)
|
||||
if err != nil && len(tc.token) != 0 { // if tc.token is "" it's okay as it will be generated later at runtime
|
||||
t.Fatalf("token couldn't be parsed for testing: %v", err)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
bts, err := kubeadmapiv1beta1.NewBootstrapTokenString(tc.token)
|
||||
if err != nil && len(tc.token) != 0 { // if tc.token is "" it's okay as it will be generated later at runtime
|
||||
t.Fatalf("token couldn't be parsed for testing: %v", err)
|
||||
}
|
||||
|
||||
cfg := &kubeadmapiv1beta1.InitConfiguration{
|
||||
ClusterConfiguration: kubeadmapiv1beta1.ClusterConfiguration{
|
||||
// KubernetesVersion is not used, but we set this explicitly to avoid
|
||||
// the lookup of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
|
||||
KubernetesVersion: constants.MinimumControlPlaneVersion.String(),
|
||||
},
|
||||
BootstrapTokens: []kubeadmapiv1beta1.BootstrapToken{
|
||||
{
|
||||
Token: bts,
|
||||
TTL: &metav1.Duration{Duration: 0},
|
||||
Usages: tc.usages,
|
||||
Groups: tc.extraGroups,
|
||||
cfg := &kubeadmapiv1beta1.InitConfiguration{
|
||||
ClusterConfiguration: kubeadmapiv1beta1.ClusterConfiguration{
|
||||
// KubernetesVersion is not used, but we set this explicitly to avoid
|
||||
// the lookup of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
|
||||
KubernetesVersion: constants.MinimumControlPlaneVersion.String(),
|
||||
},
|
||||
},
|
||||
}
|
||||
BootstrapTokens: []kubeadmapiv1beta1.BootstrapToken{
|
||||
{
|
||||
Token: bts,
|
||||
TTL: &metav1.Duration{Duration: 0},
|
||||
Usages: tc.usages,
|
||||
Groups: tc.extraGroups,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err = RunCreateToken(&buf, fakeClient, "", cfg, tc.printJoin, "")
|
||||
if (err != nil) != tc.expectedError {
|
||||
t.Errorf("Test case %s: RunCreateToken expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
|
||||
}
|
||||
err = RunCreateToken(&buf, fakeClient, "", cfg, tc.printJoin, "")
|
||||
if (err != nil) != tc.expectedError {
|
||||
t.Errorf("Test case %s: RunCreateToken expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -253,24 +255,26 @@ func TestNewCmdToken(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
// the command is created for each test so that the kubeConfigFile
|
||||
// variable in NewCmdToken() is reset.
|
||||
cmd := NewCmdToken(&buf, &bufErr)
|
||||
if _, err = f.WriteString(tc.configToWrite); err != nil {
|
||||
t.Errorf("Unable to write test file %q: %v", fullPath, err)
|
||||
}
|
||||
// store the current value of the environment variable.
|
||||
storedEnv := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
|
||||
if tc.kubeConfigEnv != "" {
|
||||
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, tc.kubeConfigEnv)
|
||||
}
|
||||
cmd.SetArgs(tc.args)
|
||||
err := cmd.Execute()
|
||||
if (err != nil) != tc.expectedError {
|
||||
t.Errorf("Test case %q: NewCmdToken expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
|
||||
}
|
||||
// restore the environment variable.
|
||||
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, storedEnv)
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// the command is created for each test so that the kubeConfigFile
|
||||
// variable in NewCmdToken() is reset.
|
||||
cmd := NewCmdToken(&buf, &bufErr)
|
||||
if _, err = f.WriteString(tc.configToWrite); err != nil {
|
||||
t.Errorf("Unable to write test file %q: %v", fullPath, err)
|
||||
}
|
||||
// store the current value of the environment variable.
|
||||
storedEnv := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
|
||||
if tc.kubeConfigEnv != "" {
|
||||
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, tc.kubeConfigEnv)
|
||||
}
|
||||
cmd.SetArgs(tc.args)
|
||||
err := cmd.Execute()
|
||||
if (err != nil) != tc.expectedError {
|
||||
t.Errorf("Test case %q: NewCmdToken expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
|
||||
}
|
||||
// restore the environment variable.
|
||||
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, storedEnv)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -28,11 +28,13 @@ import (
|
|||
|
||||
func TestSetImplicitFlags(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
flags *applyFlags
|
||||
expectedFlags applyFlags
|
||||
errExpected bool
|
||||
}{
|
||||
{ // if not dryRun or force is set; the nonInteractiveMode field should not be touched
|
||||
{
|
||||
name: "if not dryRun or force is set; the nonInteractiveMode field should not be touched",
|
||||
flags: &applyFlags{
|
||||
newK8sVersionStr: "v1.8.0",
|
||||
dryRun: false,
|
||||
|
@ -46,7 +48,8 @@ func TestSetImplicitFlags(t *testing.T) {
|
|||
nonInteractiveMode: false,
|
||||
},
|
||||
},
|
||||
{ // if not dryRun or force is set; the nonInteractiveMode field should not be touched
|
||||
{
|
||||
name: "if not dryRun or force is set; the nonInteractiveMode field should not be touched",
|
||||
flags: &applyFlags{
|
||||
newK8sVersionStr: "v1.8.0",
|
||||
dryRun: false,
|
||||
|
@ -60,7 +63,8 @@ func TestSetImplicitFlags(t *testing.T) {
|
|||
nonInteractiveMode: true,
|
||||
},
|
||||
},
|
||||
{ // if dryRun or force is set; the nonInteractiveMode field should be set to true
|
||||
{
|
||||
name: "if dryRun or force is set; the nonInteractiveMode field should be set to true",
|
||||
flags: &applyFlags{
|
||||
newK8sVersionStr: "v1.8.0",
|
||||
dryRun: true,
|
||||
|
@ -74,7 +78,8 @@ func TestSetImplicitFlags(t *testing.T) {
|
|||
nonInteractiveMode: true,
|
||||
},
|
||||
},
|
||||
{ // if dryRun or force is set; the nonInteractiveMode field should be set to true
|
||||
{
|
||||
name: "if dryRun or force is set; the nonInteractiveMode field should be set to true",
|
||||
flags: &applyFlags{
|
||||
newK8sVersionStr: "v1.8.0",
|
||||
dryRun: false,
|
||||
|
@ -88,7 +93,8 @@ func TestSetImplicitFlags(t *testing.T) {
|
|||
nonInteractiveMode: true,
|
||||
},
|
||||
},
|
||||
{ // if dryRun or force is set; the nonInteractiveMode field should be set to true
|
||||
{
|
||||
name: "if dryRun or force is set; the nonInteractiveMode field should be set to true",
|
||||
flags: &applyFlags{
|
||||
newK8sVersionStr: "v1.8.0",
|
||||
dryRun: true,
|
||||
|
@ -102,7 +108,8 @@ func TestSetImplicitFlags(t *testing.T) {
|
|||
nonInteractiveMode: true,
|
||||
},
|
||||
},
|
||||
{ // if dryRun or force is set; the nonInteractiveMode field should be set to true
|
||||
{
|
||||
name: "if dryRun or force is set; the nonInteractiveMode field should be set to true",
|
||||
flags: &applyFlags{
|
||||
newK8sVersionStr: "v1.8.0",
|
||||
dryRun: true,
|
||||
|
@ -116,7 +123,8 @@ func TestSetImplicitFlags(t *testing.T) {
|
|||
nonInteractiveMode: true,
|
||||
},
|
||||
},
|
||||
{ // if the new version is empty; it should error out
|
||||
{
|
||||
name: "if the new version is empty; it should error out",
|
||||
flags: &applyFlags{
|
||||
newK8sVersionStr: "",
|
||||
},
|
||||
|
@ -127,27 +135,29 @@ func TestSetImplicitFlags(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, rt := range tests {
|
||||
actualErr := SetImplicitFlags(rt.flags)
|
||||
t.Run(rt.name, func(t *testing.T) {
|
||||
actualErr := SetImplicitFlags(rt.flags)
|
||||
|
||||
// If an error was returned; make newK8sVersion nil so it's easy to match using reflect.DeepEqual later (instead of a random pointer)
|
||||
if actualErr != nil {
|
||||
rt.flags.newK8sVersion = nil
|
||||
}
|
||||
// If an error was returned; make newK8sVersion nil so it's easy to match using reflect.DeepEqual later (instead of a random pointer)
|
||||
if actualErr != nil {
|
||||
rt.flags.newK8sVersion = nil
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(*rt.flags, rt.expectedFlags) {
|
||||
t.Errorf(
|
||||
"failed SetImplicitFlags:\n\texpected flags: %v\n\t actual: %v",
|
||||
rt.expectedFlags,
|
||||
*rt.flags,
|
||||
)
|
||||
}
|
||||
if (actualErr != nil) != rt.errExpected {
|
||||
t.Errorf(
|
||||
"failed SetImplicitFlags:\n\texpected error: %t\n\t actual: %t",
|
||||
rt.errExpected,
|
||||
(actualErr != nil),
|
||||
)
|
||||
}
|
||||
if !reflect.DeepEqual(*rt.flags, rt.expectedFlags) {
|
||||
t.Errorf(
|
||||
"failed SetImplicitFlags:\n\texpected flags: %v\n\t actual: %v",
|
||||
rt.expectedFlags,
|
||||
*rt.flags,
|
||||
)
|
||||
}
|
||||
if (actualErr != nil) != rt.errExpected {
|
||||
t.Errorf(
|
||||
"failed SetImplicitFlags:\n\texpected error: %t\n\t actual: %t",
|
||||
rt.errExpected,
|
||||
(actualErr != nil),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,15 +25,18 @@ import (
|
|||
|
||||
func TestPrintConfiguration(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
cfg *kubeadmapi.ClusterConfiguration
|
||||
buf *bytes.Buffer
|
||||
expectedBytes []byte
|
||||
}{
|
||||
{
|
||||
name: "config is nil",
|
||||
cfg: nil,
|
||||
expectedBytes: []byte(""),
|
||||
},
|
||||
{
|
||||
name: "cluster config with local Etcd",
|
||||
cfg: &kubeadmapi.ClusterConfiguration{
|
||||
KubernetesVersion: "v1.7.1",
|
||||
Etcd: kubeadmapi.Etcd{
|
||||
|
@ -67,6 +70,7 @@ func TestPrintConfiguration(t *testing.T) {
|
|||
`),
|
||||
},
|
||||
{
|
||||
name: "cluster config with ServiceSubnet and external Etcd",
|
||||
cfg: &kubeadmapi.ClusterConfiguration{
|
||||
KubernetesVersion: "v1.7.1",
|
||||
Networking: kubeadmapi.Networking{
|
||||
|
@ -108,15 +112,17 @@ func TestPrintConfiguration(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, rt := range tests {
|
||||
rt.buf = bytes.NewBufferString("")
|
||||
printConfiguration(rt.cfg, rt.buf)
|
||||
actualBytes := rt.buf.Bytes()
|
||||
if !bytes.Equal(actualBytes, rt.expectedBytes) {
|
||||
t.Errorf(
|
||||
"failed PrintConfiguration:\n\texpected: %q\n\t actual: %q",
|
||||
string(rt.expectedBytes),
|
||||
string(actualBytes),
|
||||
)
|
||||
}
|
||||
t.Run(rt.name, func(t *testing.T) {
|
||||
rt.buf = bytes.NewBufferString("")
|
||||
printConfiguration(rt.cfg, rt.buf)
|
||||
actualBytes := rt.buf.Bytes()
|
||||
if !bytes.Equal(actualBytes, rt.expectedBytes) {
|
||||
t.Errorf(
|
||||
"failed PrintConfiguration:\n\texpected: %q\n\t actual: %q",
|
||||
string(rt.expectedBytes),
|
||||
string(actualBytes),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,35 +27,42 @@ import (
|
|||
|
||||
func TestSortedSliceFromStringIntMap(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
strMap map[string]uint16
|
||||
expectedSlice []string
|
||||
}{ // The returned slice should be alphabetically sorted based on the string keys in the map
|
||||
}{
|
||||
{
|
||||
name: "the returned slice should be alphabetically sorted based on the string keys in the map",
|
||||
strMap: map[string]uint16{"foo": 1, "bar": 2},
|
||||
expectedSlice: []string{"bar", "foo"},
|
||||
},
|
||||
{ // The int value should not affect this func
|
||||
{
|
||||
name: "the int value should not affect this func",
|
||||
strMap: map[string]uint16{"foo": 2, "bar": 1},
|
||||
expectedSlice: []string{"bar", "foo"},
|
||||
},
|
||||
{
|
||||
name: "slice with 4 keys and different values",
|
||||
strMap: map[string]uint16{"b": 2, "a": 1, "cb": 0, "ca": 1000},
|
||||
expectedSlice: []string{"a", "b", "ca", "cb"},
|
||||
},
|
||||
{ // This should work for version numbers as well; and the lowest version should come first
|
||||
{
|
||||
name: "this should work for version numbers as well; and the lowest version should come first",
|
||||
strMap: map[string]uint16{"v1.7.0": 1, "v1.6.1": 1, "v1.6.2": 1, "v1.8.0": 1, "v1.8.0-alpha.1": 1},
|
||||
expectedSlice: []string{"v1.6.1", "v1.6.2", "v1.7.0", "v1.8.0", "v1.8.0-alpha.1"},
|
||||
},
|
||||
}
|
||||
for _, rt := range tests {
|
||||
actualSlice := sortedSliceFromStringIntMap(rt.strMap)
|
||||
if !reflect.DeepEqual(actualSlice, rt.expectedSlice) {
|
||||
t.Errorf(
|
||||
"failed SortedSliceFromStringIntMap:\n\texpected: %v\n\t actual: %v",
|
||||
rt.expectedSlice,
|
||||
actualSlice,
|
||||
)
|
||||
}
|
||||
t.Run(rt.name, func(t *testing.T) {
|
||||
actualSlice := sortedSliceFromStringIntMap(rt.strMap)
|
||||
if !reflect.DeepEqual(actualSlice, rt.expectedSlice) {
|
||||
t.Errorf(
|
||||
"failed SortedSliceFromStringIntMap:\n\texpected: %v\n\t actual: %v",
|
||||
rt.expectedSlice,
|
||||
actualSlice,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,43 +22,51 @@ import (
|
|||
|
||||
func TestValidateExactArgNumber(t *testing.T) {
|
||||
var tests = []struct {
|
||||
name string
|
||||
args, supportedArgs []string
|
||||
expectedErr bool
|
||||
}{
|
||||
{ // one arg given and one arg expected
|
||||
{
|
||||
name: "one arg given and one arg expected",
|
||||
args: []string{"my-node-1234"},
|
||||
supportedArgs: []string{"node-name"},
|
||||
expectedErr: false,
|
||||
},
|
||||
{ // two args given and two args expected
|
||||
{
|
||||
name: "two args given and two args expected",
|
||||
args: []string{"my-node-1234", "foo"},
|
||||
supportedArgs: []string{"node-name", "second-toplevel-arg"},
|
||||
expectedErr: false,
|
||||
},
|
||||
{ // too few supplied args
|
||||
{
|
||||
name: "too few supplied args",
|
||||
args: []string{},
|
||||
supportedArgs: []string{"node-name"},
|
||||
expectedErr: true,
|
||||
},
|
||||
{ // too few non-empty args
|
||||
{
|
||||
name: "too few non-empty args",
|
||||
args: []string{""},
|
||||
supportedArgs: []string{"node-name"},
|
||||
expectedErr: true,
|
||||
},
|
||||
{ // too many args
|
||||
{
|
||||
name: "too many args",
|
||||
args: []string{"my-node-1234", "foo"},
|
||||
supportedArgs: []string{"node-name"},
|
||||
expectedErr: true,
|
||||
},
|
||||
}
|
||||
for _, rt := range tests {
|
||||
actual := ValidateExactArgNumber(rt.args, rt.supportedArgs)
|
||||
if (actual != nil) != rt.expectedErr {
|
||||
t.Errorf(
|
||||
"failed ValidateExactArgNumber:\n\texpected error: %t\n\t actual error: %t",
|
||||
rt.expectedErr,
|
||||
(actual != nil),
|
||||
)
|
||||
}
|
||||
t.Run(rt.name, func(t *testing.T) {
|
||||
actual := ValidateExactArgNumber(rt.args, rt.supportedArgs)
|
||||
if (actual != nil) != rt.expectedErr {
|
||||
t.Errorf(
|
||||
"failed ValidateExactArgNumber:\n\texpected error: %t\n\t actual error: %t",
|
||||
rt.expectedErr,
|
||||
(actual != nil),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,28 +69,30 @@ func TestRunVersion(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
var err error
|
||||
if len(tc.flag) > 0 {
|
||||
if err = cmd.Flags().Set(flagNameOutput, tc.flag); err != nil {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var err error
|
||||
if len(tc.flag) > 0 {
|
||||
if err = cmd.Flags().Set(flagNameOutput, tc.flag); err != nil {
|
||||
goto error
|
||||
}
|
||||
}
|
||||
buf.Reset()
|
||||
if err = RunVersion(&buf, cmd); err != nil {
|
||||
goto error
|
||||
}
|
||||
}
|
||||
buf.Reset()
|
||||
if err = RunVersion(&buf, cmd); err != nil {
|
||||
goto error
|
||||
}
|
||||
if buf.String() == "" {
|
||||
err = errors.New("empty output")
|
||||
goto error
|
||||
}
|
||||
if tc.shouldBeValidYAML {
|
||||
err = yaml.Unmarshal(buf.Bytes(), &iface)
|
||||
} else if tc.shouldBeValidJSON {
|
||||
err = json.Unmarshal(buf.Bytes(), &iface)
|
||||
}
|
||||
error:
|
||||
if (err != nil) != tc.expectedError {
|
||||
t.Errorf("Test case %q: RunVersion expected error: %v, saw: %v; %v", tc.name, tc.expectedError, err != nil, err)
|
||||
}
|
||||
if buf.String() == "" {
|
||||
err = errors.New("empty output")
|
||||
goto error
|
||||
}
|
||||
if tc.shouldBeValidYAML {
|
||||
err = yaml.Unmarshal(buf.Bytes(), &iface)
|
||||
} else if tc.shouldBeValidJSON {
|
||||
err = json.Unmarshal(buf.Bytes(), &iface)
|
||||
}
|
||||
error:
|
||||
if (err != nil) != tc.expectedError {
|
||||
t.Errorf("Test case %q: RunVersion expected error: %v, saw: %v; %v", tc.name, tc.expectedError, err != nil, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue