From a5919a9340ba92d3259d6c9543d75ab50d64723f Mon Sep 17 00:00:00 2001 From: Derek McQuay Date: Fri, 21 Oct 2016 10:07:36 -0700 Subject: [PATCH 1/3] kubeadm: added unit test for app/preflight pkg --- cmd/kubeadm/app/preflight/checks.go | 10 ++-- cmd/kubeadm/app/preflight/checks_test.go | 68 ++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 cmd/kubeadm/app/preflight/checks_test.go diff --git a/cmd/kubeadm/app/preflight/checks.go b/cmd/kubeadm/app/preflight/checks.go index b7802af3072..9658b965425 100644 --- a/cmd/kubeadm/app/preflight/checks.go +++ b/cmd/kubeadm/app/preflight/checks.go @@ -200,7 +200,7 @@ func RunInitMasterChecks(cfg *kubeadmapi.MasterConfiguration) error { InPathCheck{executable: "touch", mandatory: false}, } - return runChecks(checks) + return runChecks(checks, os.Stderr) } func RunJoinNodeChecks() error { @@ -223,7 +223,7 @@ func RunJoinNodeChecks() error { InPathCheck{executable: "touch", mandatory: false}, } - return runChecks(checks) + return runChecks(checks, os.Stderr) } func RunResetCheck() error { @@ -231,17 +231,17 @@ func RunResetCheck() error { IsRootCheck{root: true}, } - return runChecks(checks) + return runChecks(checks, os.Stderr) } // runChecks runs each check, displays it's warnings/errors, and once all // are processed will exit if any errors occurred. -func runChecks(checks []PreFlightCheck) error { +func runChecks(checks []PreFlightCheck, ww io.Writer) error { found := []error{} for _, c := range checks { warnings, errs := c.Check() for _, w := range warnings { - fmt.Printf("WARNING: %s\n", w) + io.WriteString(ww, fmt.Sprintf("WARNING: %s\n", w)) } for _, e := range errs { found = append(found, e) diff --git a/cmd/kubeadm/app/preflight/checks_test.go b/cmd/kubeadm/app/preflight/checks_test.go new file mode 100644 index 00000000000..fb8b1f381aa --- /dev/null +++ b/cmd/kubeadm/app/preflight/checks_test.go @@ -0,0 +1,68 @@ +/* +Copyright 2016 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package preflight + +import ( + "bytes" + "fmt" + "testing" +) + +type preflightCheckTest struct { + msg string +} + +func (pfct preflightCheckTest) Check() (warning, errors []error) { + if pfct.msg == "warning" { + return []error{fmt.Errorf("warning")}, nil + } + if pfct.msg != "" { + return nil, []error{fmt.Errorf("fake error")} + } + return +} + +func TestRunChecks(t *testing.T) { + var tokenTest = []struct { + p []PreFlightCheck + expected bool + output string + }{ + {[]PreFlightCheck{}, true, ""}, + {[]PreFlightCheck{preflightCheckTest{"warning"}}, true, "WARNING: warning\n"}, // should just print warning + {[]PreFlightCheck{preflightCheckTest{"error"}}, false, ""}, + {[]PreFlightCheck{preflightCheckTest{"test"}}, false, ""}, + } + for _, rt := range tokenTest { + buf := new(bytes.Buffer) + actual := runChecks(rt.p, buf) + if (actual == nil) != rt.expected { + t.Errorf( + "failed runChecks:\n\texpected: %t\n\t actual: %t", + rt.expected, + (actual == nil), + ) + } + if buf.String() != rt.output { + t.Errorf( + "failed runChecks:\n\texpected: %s\n\t actual: %s", + rt.output, + buf.String(), + ) + } + } +} From b054117c24ac65b1d8d5f702ab8447ba8b9dba2e Mon Sep 17 00:00:00 2001 From: Paulo Pires Date: Fri, 28 Oct 2016 21:21:40 -0400 Subject: [PATCH 2/3] kubeadm: updated cmd/kubeadm/app/preflight package related BUILD file. --- cmd/kubeadm/app/preflight/BUILD | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/kubeadm/app/preflight/BUILD b/cmd/kubeadm/app/preflight/BUILD index dd77ec83085..0285668b7be 100644 --- a/cmd/kubeadm/app/preflight/BUILD +++ b/cmd/kubeadm/app/preflight/BUILD @@ -19,3 +19,11 @@ go_library( "//pkg/util/initsystem:go_default_library", ], ) + +go_test( + name = "go_default_test", + srcs = ["checks_test.go"], + library = "go_default_library", + tags = ["automanaged"], + deps = [], +) From 1157548e3cdf014e918d7e6dd7934f917c2b83e6 Mon Sep 17 00:00:00 2001 From: Paulo Pires Date: Fri, 28 Oct 2016 22:04:19 -0400 Subject: [PATCH 3/3] kubeadm: added tests owner. --- test/test_owners.csv | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test_owners.csv b/test/test_owners.csv index 3439773c5cc..4a6789d4e3e 100644 --- a/test/test_owners.csv +++ b/test/test_owners.csv @@ -455,6 +455,10 @@ k8s.io/kubernetes/cmd/kube-proxy/app,luxas,1 k8s.io/kubernetes/cmd/kubeadm/app/cmd,davidopp,1 k8s.io/kubernetes/cmd/kubeadm/app/images,saad-ali,1 k8s.io/kubernetes/cmd/kubeadm/app/util,eparis,1 +k8s.io/kubernetes/cmd/kubeadm/app/cmd,vishh,1 +k8s.io/kubernetes/cmd/kubeadm/app/images,davidopp,1 +k8s.io/kubernetes/cmd/kubeadm/app/preflight,apprenda,0 +k8s.io/kubernetes/cmd/kubeadm/app/util,krousey,1 k8s.io/kubernetes/cmd/kubelet/app,hurf,1 k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testoutput/clientset_generated/test_internalclientset/typed/testgroup.k8s.io/unversioned,eparis,1 k8s.io/kubernetes/cmd/libs/go2idl/openapi-gen/generators,davidopp,1