Merge pull request #35326 from apprenda/kubeadm-unit-tests-pkg-preflight

Automatic merge from submit-queue

kubeadm: added unit test for app/preflight pkg

Added unit test for kubeadm/app/preflight package testing functionality of checks.go.

This PR is part of the ongoing effort to add tests (#35025)

/cc @pires @jbeda
pull/6/head
Kubernetes Submit Queue 2016-10-30 10:31:56 -07:00 committed by GitHub
commit 9e71a65335
4 changed files with 85 additions and 5 deletions

View File

@ -21,3 +21,11 @@ go_library(
"//pkg/util/node:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["checks_test.go"],
library = "go_default_library",
tags = ["automanaged"],
deps = [],
)

View File

@ -216,7 +216,7 @@ func RunInitMasterChecks(cfg *kubeadmapi.MasterConfiguration) error {
InPathCheck{executable: "touch", mandatory: false},
}
return runChecks(checks)
return runChecks(checks, os.Stderr)
}
func RunJoinNodeChecks() error {
@ -240,7 +240,7 @@ func RunJoinNodeChecks() error {
InPathCheck{executable: "touch", mandatory: false},
}
return runChecks(checks)
return runChecks(checks, os.Stderr)
}
func RunResetCheck() error {
@ -248,17 +248,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)

View File

@ -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(),
)
}
}
}

View File

@ -456,6 +456,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/types,caesarxuchao,0
k8s.io/kubernetes/cmd/libs/go2idl/go-to-protobuf/protobuf,smarterclayton,0

1 name owner auto-assigned
456 k8s.io/kubernetes/cmd/kubeadm/app/cmd davidopp 1
457 k8s.io/kubernetes/cmd/kubeadm/app/images saad-ali 1
458 k8s.io/kubernetes/cmd/kubeadm/app/util eparis 1
459 k8s.io/kubernetes/cmd/kubeadm/app/cmd vishh 1
460 k8s.io/kubernetes/cmd/kubeadm/app/images davidopp 1
461 k8s.io/kubernetes/cmd/kubeadm/app/preflight apprenda 0
462 k8s.io/kubernetes/cmd/kubeadm/app/util krousey 1
463 k8s.io/kubernetes/cmd/kubelet/app hurf 1
464 k8s.io/kubernetes/cmd/libs/go2idl/client-gen/types caesarxuchao 0
465 k8s.io/kubernetes/cmd/libs/go2idl/go-to-protobuf/protobuf smarterclayton 0