Skip to content

Commit 04933f3

Browse files
committed
cmd/kubeadm/test/cmd: refactor _test.go files
Make getKubeadmPath() fetch the KUBEADM_PATH env. variable. Panic if it's missing. Don't handle the "--kubeadm-path" flag. Remove the same flag from the BUILD bazel test rule. Don't handle "--kubeadm-cmd-skip" usage of this flag is missing from the code base. Remove usage of "kubeadmCmdSkip" as the flag "--kubeadm-cmd-skip" is never passed.
1 parent 9bbd321 commit 04933f3

File tree

7 files changed

+11
-118
lines changed

7 files changed

+11
-118
lines changed

cmd/kubeadm/test/cmd/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ go_test(
2525
"token_test.go",
2626
"version_test.go",
2727
],
28-
args = ["--kubeadm-path=$(location //cmd/kubeadm:kubeadm)"],
2928
data = ["//cmd/kubeadm"] + glob(["testdata/**"]),
3029
embed = [":go_default_library"],
3130
tags = [

cmd/kubeadm/test/cmd/completion_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ import "testing"
2020

2121
func TestCmdCompletion(t *testing.T) {
2222
kubeadmPath := getKubeadmPath()
23-
24-
if *kubeadmCmdSkip {
25-
t.Log("kubeadm cmd tests being skipped")
26-
t.Skip()
27-
}
28-
2923
var tests = []struct {
3024
name string
3125
args string

cmd/kubeadm/test/cmd/init_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ func runKubeadmInit(args ...string) (string, string, int, error) {
3737
}
3838

3939
func TestCmdInitToken(t *testing.T) {
40-
if *kubeadmCmdSkip {
41-
t.Log("kubeadm cmd tests being skipped")
42-
t.Skip()
43-
}
44-
4540
initTest := []struct {
4641
name string
4742
args string
@@ -86,11 +81,6 @@ func TestCmdInitToken(t *testing.T) {
8681
}
8782

8883
func TestCmdInitKubernetesVersion(t *testing.T) {
89-
if *kubeadmCmdSkip {
90-
t.Log("kubeadm cmd tests being skipped")
91-
t.Skip()
92-
}
93-
9484
initTest := []struct {
9585
name string
9686
args string
@@ -130,11 +120,6 @@ func TestCmdInitKubernetesVersion(t *testing.T) {
130120
}
131121

132122
func TestCmdInitConfig(t *testing.T) {
133-
if *kubeadmCmdSkip {
134-
t.Log("kubeadm cmd tests being skipped")
135-
t.Skip()
136-
}
137-
138123
initTest := []struct {
139124
name string
140125
args string
@@ -214,11 +199,6 @@ func TestCmdInitConfig(t *testing.T) {
214199
}
215200

216201
func TestCmdInitCertPhaseCSR(t *testing.T) {
217-
if *kubeadmCmdSkip {
218-
t.Log("kubeadm cmd tests being skipped")
219-
t.Skip()
220-
}
221-
222202
tests := []struct {
223203
name string
224204
baseName string
@@ -279,11 +259,6 @@ func TestCmdInitCertPhaseCSR(t *testing.T) {
279259
}
280260

281261
func TestCmdInitAPIPort(t *testing.T) {
282-
if *kubeadmCmdSkip {
283-
t.Log("kubeadm cmd tests being skipped")
284-
t.Skip()
285-
}
286-
287262
initTest := []struct {
288263
name string
289264
args string
@@ -339,11 +314,6 @@ func TestCmdInitAPIPort(t *testing.T) {
339314
func TestCmdInitFeatureGates(t *testing.T) {
340315
const PanicExitcode = 2
341316

342-
if *kubeadmCmdSkip {
343-
t.Log("kubeadm cmd tests being skipped")
344-
t.Skip()
345-
}
346-
347317
initTest := []struct {
348318
name string
349319
args string

cmd/kubeadm/test/cmd/join_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ func kubeadmReset() error {
2626
}
2727

2828
func TestCmdJoinConfig(t *testing.T) {
29-
if *kubeadmCmdSkip {
30-
t.Log("kubeadm cmd tests being skipped")
31-
t.Skip()
32-
}
33-
3429
var initTest = []struct {
3530
name string
3631
args string
@@ -59,11 +54,6 @@ func TestCmdJoinConfig(t *testing.T) {
5954
}
6055

6156
func TestCmdJoinDiscoveryFile(t *testing.T) {
62-
if *kubeadmCmdSkip {
63-
t.Log("kubeadm cmd tests being skipped")
64-
t.Skip()
65-
}
66-
6757
var initTest = []struct {
6858
name string
6959
args string
@@ -92,11 +82,6 @@ func TestCmdJoinDiscoveryFile(t *testing.T) {
9282
}
9383

9484
func TestCmdJoinDiscoveryToken(t *testing.T) {
95-
if *kubeadmCmdSkip {
96-
t.Log("kubeadm cmd tests being skipped")
97-
t.Skip()
98-
}
99-
10085
var initTest = []struct {
10186
name string
10287
args string
@@ -125,11 +110,6 @@ func TestCmdJoinDiscoveryToken(t *testing.T) {
125110
}
126111

127112
func TestCmdJoinNodeName(t *testing.T) {
128-
if *kubeadmCmdSkip {
129-
t.Log("kubeadm cmd tests being skipped")
130-
t.Skip()
131-
}
132-
133113
var initTest = []struct {
134114
name string
135115
args string
@@ -157,11 +137,6 @@ func TestCmdJoinNodeName(t *testing.T) {
157137
}
158138

159139
func TestCmdJoinTLSBootstrapToken(t *testing.T) {
160-
if *kubeadmCmdSkip {
161-
t.Log("kubeadm cmd tests being skipped")
162-
t.Skip()
163-
}
164-
165140
var initTest = []struct {
166141
name string
167142
args string
@@ -190,11 +165,6 @@ func TestCmdJoinTLSBootstrapToken(t *testing.T) {
190165
}
191166

192167
func TestCmdJoinToken(t *testing.T) {
193-
if *kubeadmCmdSkip {
194-
t.Log("kubeadm cmd tests being skipped")
195-
t.Skip()
196-
}
197-
198168
var initTest = []struct {
199169
name string
200170
args string
@@ -223,11 +193,6 @@ func TestCmdJoinToken(t *testing.T) {
223193
}
224194

225195
func TestCmdJoinBadArgs(t *testing.T) {
226-
if *kubeadmCmdSkip {
227-
t.Log("kubeadm cmd tests being skipped")
228-
t.Skip()
229-
}
230-
231196
kubeadmPath := getKubeadmPath()
232197
var initTest = []struct {
233198
name string
@@ -256,11 +221,6 @@ func TestCmdJoinBadArgs(t *testing.T) {
256221
}
257222

258223
func TestCmdJoinArgsMixed(t *testing.T) {
259-
if *kubeadmCmdSkip {
260-
t.Log("kubeadm cmd tests being skipped")
261-
t.Skip()
262-
}
263-
264224
var initTest = []struct {
265225
name string
266226
args string

cmd/kubeadm/test/cmd/token_test.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ limitations under the License.
1717
package kubeadm
1818

1919
import (
20-
"flag"
21-
"os"
22-
"path"
23-
"path/filepath"
2420
"regexp"
2521
"testing"
2622
)
@@ -29,25 +25,8 @@ const (
2925
TokenExpectedRegex = "^\\S{6}\\.\\S{16}\n$"
3026
)
3127

32-
var kubeadmPathFlag = flag.String("kubeadm-path", filepath.Join(os.Getenv("KUBE_ROOT"), "cluster/kubeadm.sh"), "Location of kubeadm")
33-
34-
func getKubeadmPath() string {
35-
kubeadmPath := *kubeadmPathFlag // TEST_SRCDIR is provided by Bazel.
36-
if srcDir := os.Getenv("TEST_SRCDIR"); srcDir != "" {
37-
kubeadmPath = path.Join(srcDir, os.Getenv("TEST_WORKSPACE"), kubeadmPath)
38-
}
39-
40-
return kubeadmPath
41-
}
42-
43-
var kubeadmCmdSkip = flag.Bool("kubeadm-cmd-skip", false, "Skip kubeadm cmd tests")
44-
4528
func TestCmdTokenGenerate(t *testing.T) {
4629
kubeadmPath := getKubeadmPath()
47-
if *kubeadmCmdSkip {
48-
t.Log("kubeadm cmd tests being skipped")
49-
t.Skip()
50-
}
5130
stdout, _, _, err := RunCmd(kubeadmPath, "token", "generate")
5231
if err != nil {
5332
t.Fatalf("'kubeadm token generate' exited uncleanly: %v", err)
@@ -72,23 +51,13 @@ func TestCmdTokenGenerateTypoError(t *testing.T) {
7251
with a non-zero status code after showing the command's usage, so that
7352
the usage itself isn't captured as a token without the user noticing.
7453
*/
75-
if *kubeadmCmdSkip {
76-
t.Log("kubeadm cmd tests being skipped")
77-
t.Skip()
78-
}
79-
8054
kubeadmPath := getKubeadmPath()
8155
_, _, _, err := RunCmd(kubeadmPath, "token", "genorate") // subtle typo
8256
if err == nil {
8357
t.Error("'kubeadm token genorate' (a deliberate typo) exited without an error when we expected non-zero exit status")
8458
}
8559
}
8660
func TestCmdTokenDelete(t *testing.T) {
87-
if *kubeadmCmdSkip {
88-
t.Log("kubeadm cmd tests being skipped")
89-
t.Skip()
90-
}
91-
9261
var tests = []struct {
9362
name string
9463
args string

cmd/kubeadm/test/cmd/util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package kubeadm
1818

1919
import (
2020
"bytes"
21+
"os"
2122
"os/exec"
2223
"testing"
2324

@@ -79,3 +80,13 @@ func getSubCommand(t *testing.T, subCmds []*cobra.Command, name string) *cobra.C
7980

8081
return nil
8182
}
83+
84+
// getKubeadmPath returns the contents of the environment variable KUBEADM_PATH
85+
// or panics if it's empty
86+
func getKubeadmPath() string {
87+
kubeadmPath := os.Getenv("KUBEADM_PATH")
88+
if len(kubeadmPath) == 0 {
89+
panic("the environment variable KUBEADM_PATH must point to the kubeadm binary path")
90+
}
91+
return kubeadmPath
92+
}

cmd/kubeadm/test/cmd/version_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ var (
3434
)
3535

3636
func TestCmdVersion(t *testing.T) {
37-
if *kubeadmCmdSkip {
38-
t.Log("kubeadm cmd tests being skipped")
39-
t.Skip()
40-
}
41-
4237
var versionTest = []struct {
4338
name string
4439
args string
@@ -78,11 +73,6 @@ func TestCmdVersion(t *testing.T) {
7873
}
7974

8075
func TestCmdVersionOutputJsonOrYaml(t *testing.T) {
81-
if *kubeadmCmdSkip {
82-
t.Log("kubeadm cmd tests being skipped")
83-
t.Skip()
84-
}
85-
8676
var versionTest = []struct {
8777
name string
8878
args string

0 commit comments

Comments
 (0)