From 63327647a53a09a85a173306407a9876af7e32a8 Mon Sep 17 00:00:00 2001 From: Derek McQuay Date: Thu, 2 Feb 2017 09:17:22 -0800 Subject: [PATCH] kubeadm: added tests for preflight checks increased coverage from ~9% to ~71% --- cmd/kubeadm/app/preflight/BUILD | 1 + cmd/kubeadm/app/preflight/checks_test.go | 59 ++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/cmd/kubeadm/app/preflight/BUILD b/cmd/kubeadm/app/preflight/BUILD index 23933156815..9db917d2b1f 100644 --- a/cmd/kubeadm/app/preflight/BUILD +++ b/cmd/kubeadm/app/preflight/BUILD @@ -29,6 +29,7 @@ go_test( srcs = ["checks_test.go"], library = ":go_default_library", tags = ["automanaged"], + deps = ["//cmd/kubeadm/app/apis/kubeadm:go_default_library"], ) filegroup( diff --git a/cmd/kubeadm/app/preflight/checks_test.go b/cmd/kubeadm/app/preflight/checks_test.go index 569d0595651..d7473b9b1c0 100644 --- a/cmd/kubeadm/app/preflight/checks_test.go +++ b/cmd/kubeadm/app/preflight/checks_test.go @@ -20,6 +20,9 @@ import ( "bytes" "fmt" "testing" + + "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" + kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" ) type preflightCheckTest struct { @@ -36,6 +39,54 @@ func (pfct preflightCheckTest) Check() (warning, errors []error) { return } +func TestRunInitMasterChecks(t *testing.T) { + var tests = []struct { + cfg *kubeadmapi.MasterConfiguration + expected bool + }{ + { + cfg: &kubeadmapi.MasterConfiguration{ + API: kubeadm.API{AdvertiseAddresses: []string{"foo"}}, + }, + expected: false, + }, + } + + for _, rt := range tests { + actual := RunInitMasterChecks(rt.cfg) + if (actual == nil) != rt.expected { + t.Errorf( + "failed RunInitMasterChecks:\n\texpected: %t\n\t actual: %t", + rt.expected, + (actual != nil), + ) + } + } +} + +func TestRunJoinNodeChecks(t *testing.T) { + var tests = []struct { + cfg *kubeadmapi.NodeConfiguration + expected bool + }{ + { + cfg: &kubeadmapi.NodeConfiguration{}, + expected: false, + }, + } + + for _, rt := range tests { + actual := RunJoinNodeChecks(rt.cfg) + if (actual == nil) != rt.expected { + t.Errorf( + "failed RunJoinNodeChecks:\n\texpected: %t\n\t actual: %t", + rt.expected, + (actual != nil), + ) + } + } +} + func TestRunChecks(t *testing.T) { var tokenTest = []struct { p []Checker @@ -46,6 +97,14 @@ func TestRunChecks(t *testing.T) { {[]Checker{preflightCheckTest{"warning"}}, true, "[preflight] WARNING: warning\n"}, // should just print warning {[]Checker{preflightCheckTest{"error"}}, false, ""}, {[]Checker{preflightCheckTest{"test"}}, false, ""}, + {[]Checker{DirAvailableCheck{Path: "/does/not/exist"}}, true, ""}, + {[]Checker{DirAvailableCheck{Path: "/"}}, false, ""}, + {[]Checker{FileAvailableCheck{Path: "/does/not/exist"}}, true, ""}, + {[]Checker{FileContentCheck{Path: "/does/not/exist"}}, false, ""}, + {[]Checker{FileContentCheck{Path: "/"}}, true, ""}, + {[]Checker{FileContentCheck{Path: "/", Content: []byte("does not exist")}}, false, ""}, + {[]Checker{InPathCheck{executable: "foobarbaz"}}, true, "[preflight] WARNING: foobarbaz not found in system path\n"}, + {[]Checker{InPathCheck{executable: "foobarbaz", mandatory: true}}, false, ""}, } for _, rt := range tokenTest { buf := new(bytes.Buffer)