mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #40878 from apprenda/kubeadm_preflight_tests
Automatic merge from submit-queue (batch tested with PRs 40796, 40878, 36033, 40838, 41210) kubeadm: added tests for preflight checks **What this PR does / why we need it**: There hadn't been much care to add more unit tests as more preflight checks were added. I added tests that increased coverage from ~9% to ~71% Adding unit tests is a WIP from https://github.com/kubernetes/kubernetes/issues/34136 **Special notes for your reviewer**: /cc @luxas @pires **Release note**: ```release-note NONE ```
This commit is contained in:
commit
9134da4ade
@ -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(
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user