mirror of https://github.com/k3s-io/k3s
706 lines
24 KiB
Go
706 lines
24 KiB
Go
/*
|
|
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"
|
|
"io/ioutil"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/renstrom/dedent"
|
|
|
|
"net/http"
|
|
"os"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
|
"k8s.io/utils/exec"
|
|
fakeexec "k8s.io/utils/exec/testing"
|
|
)
|
|
|
|
var (
|
|
externalEtcdRootCAFileContent = dedent.Dedent(`
|
|
-----BEGIN CERTIFICATE-----
|
|
MIIFrjCCA5agAwIBAgIUJAM5bQz/Ann8qye8T7Uyl+cAt3wwDQYJKoZIhvcNAQEN
|
|
BQAwbzEOMAwGA1UEBhMFQ2hpbmExDzANBgNVBAgTBkhhaW5hbjEOMAwGA1UEBxMF
|
|
U2FueWExDTALBgNVBAoTBGV0Y2QxFjAUBgNVBAsTDWV0Y2Qgc2VjdXJpdHkxFTAT
|
|
BgNVBAMTDGV0Y2Qtcm9vdC1jYTAeFw0xNzAyMjIwNzEyMDBaFw0yMjAyMjEwNzEy
|
|
MDBaMG8xDjAMBgNVBAYTBUNoaW5hMQ8wDQYDVQQIEwZIYWluYW4xDjAMBgNVBAcT
|
|
BVNhbnlhMQ0wCwYDVQQKEwRldGNkMRYwFAYDVQQLEw1ldGNkIHNlY3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qWiI9puSEcxQ=
|
|
-----END RSA PRIVATE KEY-----
|
|
`)
|
|
)
|
|
|
|
type preflightCheckTest struct {
|
|
msg string
|
|
}
|
|
|
|
func (pfct preflightCheckTest) Name() string {
|
|
return "preflightCheckTest"
|
|
}
|
|
|
|
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 TestRunInitMasterChecks(t *testing.T) {
|
|
var tests = []struct {
|
|
name string
|
|
cfg *kubeadmapi.MasterConfiguration
|
|
expected bool
|
|
}{
|
|
{name: "Test valid advertised address",
|
|
cfg: &kubeadmapi.MasterConfiguration{
|
|
API: kubeadmapi.API{AdvertiseAddress: "foo"},
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Test CA file exists if specfied",
|
|
cfg: &kubeadmapi.MasterConfiguration{
|
|
Etcd: kubeadmapi.Etcd{CAFile: "/foo"},
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Test Cert file exists if specfied",
|
|
cfg: &kubeadmapi.MasterConfiguration{
|
|
Etcd: kubeadmapi.Etcd{CertFile: "/foo"},
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "Test Key file exists if specfied",
|
|
cfg: &kubeadmapi.MasterConfiguration{
|
|
Etcd: kubeadmapi.Etcd{CertFile: "/foo"},
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
cfg: &kubeadmapi.MasterConfiguration{
|
|
API: kubeadmapi.API{AdvertiseAddress: "2001:1234::1:15"},
|
|
},
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, rt := range tests {
|
|
actual := RunInitMasterChecks(exec.New(), rt.cfg, sets.NewString())
|
|
if (actual == nil) != rt.expected {
|
|
t.Errorf(
|
|
"failed RunInitMasterChecks:\n\texpected: %t\n\t actual: %t\n\t error: %v",
|
|
rt.expected,
|
|
(actual == nil),
|
|
actual,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunJoinNodeChecks(t *testing.T) {
|
|
var tests = []struct {
|
|
cfg *kubeadmapi.NodeConfiguration
|
|
expected bool
|
|
}{
|
|
{
|
|
cfg: &kubeadmapi.NodeConfiguration{},
|
|
expected: false,
|
|
},
|
|
{
|
|
cfg: &kubeadmapi.NodeConfiguration{
|
|
DiscoveryTokenAPIServers: []string{"192.168.1.15"},
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
cfg: &kubeadmapi.NodeConfiguration{
|
|
DiscoveryTokenAPIServers: []string{"2001:1234::1:15"},
|
|
},
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, rt := range tests {
|
|
actual := RunJoinNodeChecks(exec.New(), rt.cfg, sets.NewString())
|
|
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
|
|
expected bool
|
|
output string
|
|
}{
|
|
{[]Checker{}, true, ""},
|
|
{[]Checker{preflightCheckTest{"warning"}}, true, "\t[WARNING preflightCheckTest]: 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", exec: exec.New()}}, true, "\t[WARNING FileExisting-foobarbaz]: foobarbaz not found in system path\n"},
|
|
{[]Checker{InPathCheck{executable: "foobarbaz", mandatory: true, exec: exec.New()}}, false, ""},
|
|
{[]Checker{ExtraArgsCheck{
|
|
APIServerExtraArgs: map[string]string{"secure-port": "1234"},
|
|
ControllerManagerExtraArgs: map[string]string{"use-service-account-credentials": "true"},
|
|
SchedulerExtraArgs: map[string]string{"leader-elect": "true"},
|
|
}}, true, ""},
|
|
{[]Checker{ExtraArgsCheck{
|
|
APIServerExtraArgs: map[string]string{"secure-port": "foo"},
|
|
}}, true, "\t[WARNING ExtraArgs]: kube-apiserver: failed to parse extra argument --secure-port=foo\n"},
|
|
{[]Checker{ExtraArgsCheck{
|
|
APIServerExtraArgs: map[string]string{"invalid-argument": "foo"},
|
|
}}, true, "\t[WARNING ExtraArgs]: kube-apiserver: failed to parse extra argument --invalid-argument=foo\n"},
|
|
{[]Checker{InPathCheck{executable: "foobar", mandatory: false, exec: exec.New(), suggestion: "install foobar"}}, true, "\t[WARNING FileExisting-foobar]: foobar not found in system path\nSuggestion: install foobar\n"},
|
|
}
|
|
for _, rt := range tokenTest {
|
|
buf := new(bytes.Buffer)
|
|
actual := RunChecks(rt.p, buf, sets.NewString())
|
|
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(),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
func TestConfigRootCAs(t *testing.T) {
|
|
f, err := ioutil.TempFile(os.TempDir(), "kubeadm-external-etcd-test-cafile")
|
|
if err != nil {
|
|
t.Errorf("failed configRootCAs:\n\texpected: succeed creating temp CA file\n\tactual:%v", err)
|
|
}
|
|
defer os.Remove(f.Name())
|
|
if err := ioutil.WriteFile(f.Name(), []byte(externalEtcdRootCAFileContent), 0644); err != nil {
|
|
t.Errorf("failed configRootCAs:\n\texpected: succeed writing contents to temp CA file %s\n\tactual:%v", f.Name(), err)
|
|
}
|
|
|
|
c := ExternalEtcdVersionCheck{Etcd: kubeadmapi.Etcd{CAFile: f.Name()}}
|
|
|
|
config, err := c.configRootCAs(nil)
|
|
if err != nil {
|
|
t.Errorf(
|
|
"failed configRootCAs:\n\texpected: has no error\n\tactual:%v",
|
|
err,
|
|
)
|
|
}
|
|
if config.RootCAs == nil {
|
|
t.Errorf(
|
|
"failed configRootCAs:\n\texpected: RootCAs not equal to nil\n\tactual:%v",
|
|
config.RootCAs,
|
|
)
|
|
}
|
|
}
|
|
func TestConfigCertAndKey(t *testing.T) {
|
|
certFile, err := ioutil.TempFile(os.TempDir(), "kubeadm-external-etcd-test-certfile")
|
|
if err != nil {
|
|
t.Errorf(
|
|
"failed configCertAndKey:\n\texpected: succeed creating temp CertFile file\n\tactual:%v",
|
|
err,
|
|
)
|
|
}
|
|
defer os.Remove(certFile.Name())
|
|
if err := ioutil.WriteFile(certFile.Name(), []byte(externalEtcdCertFileContent), 0644); err != nil {
|
|
t.Errorf(
|
|
"failed configCertAndKey:\n\texpected: succeed writing contents to temp CertFile file %s\n\tactual:%v",
|
|
certFile.Name(),
|
|
err,
|
|
)
|
|
}
|
|
|
|
keyFile, err := ioutil.TempFile(os.TempDir(), "kubeadm-external-etcd-test-keyfile")
|
|
if err != nil {
|
|
t.Errorf(
|
|
"failed configCertAndKey:\n\texpected: succeed creating temp KeyFile file\n\tactual:%v",
|
|
err,
|
|
)
|
|
}
|
|
defer os.Remove(keyFile.Name())
|
|
if err := ioutil.WriteFile(keyFile.Name(), []byte(externalEtcdKeyFileContent), 0644); err != nil {
|
|
t.Errorf(
|
|
"failed configCertAndKey:\n\texpected: succeed writing contents to temp KeyFile file %s\n\tactual:%v",
|
|
keyFile.Name(),
|
|
err,
|
|
)
|
|
}
|
|
c := ExternalEtcdVersionCheck{Etcd: kubeadmapi.Etcd{
|
|
CertFile: certFile.Name(),
|
|
KeyFile: keyFile.Name(),
|
|
}}
|
|
|
|
config, err := c.configCertAndKey(nil)
|
|
if err != nil {
|
|
t.Errorf(
|
|
"failed configCertAndKey:\n\texpected: has no error\n\tactual:%v",
|
|
err,
|
|
)
|
|
}
|
|
if config.Certificates == nil {
|
|
t.Errorf(
|
|
"failed configCertAndKey:\n\texpected: Certificates not equal to nil\n\tactual:%v",
|
|
config.Certificates,
|
|
)
|
|
}
|
|
}
|
|
|
|
func TestKubernetesVersionCheck(t *testing.T) {
|
|
var tests = []struct {
|
|
check KubernetesVersionCheck
|
|
expectWarnings bool
|
|
}{
|
|
{
|
|
check: KubernetesVersionCheck{
|
|
KubeadmVersion: "v1.6.6", //Same version
|
|
KubernetesVersion: "v1.6.6",
|
|
},
|
|
expectWarnings: false,
|
|
},
|
|
{
|
|
check: KubernetesVersionCheck{
|
|
KubeadmVersion: "v1.6.6", //KubernetesVersion version older than KubeadmVersion
|
|
KubernetesVersion: "v1.5.5",
|
|
},
|
|
expectWarnings: false,
|
|
},
|
|
{
|
|
check: KubernetesVersionCheck{
|
|
KubeadmVersion: "v1.6.6", //KubernetesVersion newer than KubeadmVersion, within the same minor release (new patch)
|
|
KubernetesVersion: "v1.6.7",
|
|
},
|
|
expectWarnings: false,
|
|
},
|
|
{
|
|
check: KubernetesVersionCheck{
|
|
KubeadmVersion: "v1.6.6", //KubernetesVersion newer than KubeadmVersion, in a different minor/in pre-release
|
|
KubernetesVersion: "v1.7.0-alpha.0",
|
|
},
|
|
expectWarnings: true,
|
|
},
|
|
{
|
|
check: KubernetesVersionCheck{
|
|
KubeadmVersion: "v1.6.6", //KubernetesVersion newer than KubeadmVersion, in a different minor/stable
|
|
KubernetesVersion: "v1.7.0",
|
|
},
|
|
expectWarnings: true,
|
|
},
|
|
{
|
|
check: KubernetesVersionCheck{
|
|
KubeadmVersion: "v0.0.0", //"super-custom" builds - Skip this check
|
|
KubernetesVersion: "v1.7.0",
|
|
},
|
|
expectWarnings: false,
|
|
},
|
|
}
|
|
|
|
for _, rt := range tests {
|
|
warning, _ := rt.check.Check()
|
|
if (warning != nil) != rt.expectWarnings {
|
|
t.Errorf(
|
|
"failed KubernetesVersionCheck:\n\texpected: %t\n\t actual: %t (KubeadmVersion:%s, KubernetesVersion: %s)",
|
|
rt.expectWarnings,
|
|
(warning != nil),
|
|
rt.check.KubeadmVersion,
|
|
rt.check.KubernetesVersion,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHTTPProxyCIDRCheck(t *testing.T) {
|
|
var tests = []struct {
|
|
check HTTPProxyCIDRCheck
|
|
expectWarnings bool
|
|
}{
|
|
{
|
|
check: HTTPProxyCIDRCheck{
|
|
Proto: "https",
|
|
CIDR: "127.0.0.0/8",
|
|
}, // Loopback addresses never should produce proxy warnings
|
|
expectWarnings: false,
|
|
},
|
|
{
|
|
check: HTTPProxyCIDRCheck{
|
|
Proto: "https",
|
|
CIDR: "10.96.0.0/12",
|
|
}, // Expected to be accessed directly, we set NO_PROXY to 10.0.0.0/8
|
|
expectWarnings: false,
|
|
},
|
|
{
|
|
check: HTTPProxyCIDRCheck{
|
|
Proto: "https",
|
|
CIDR: "192.168.0.0/16",
|
|
}, // Expected to go via proxy as this range is not listed in NO_PROXY
|
|
expectWarnings: true,
|
|
},
|
|
{
|
|
check: HTTPProxyCIDRCheck{
|
|
Proto: "https",
|
|
CIDR: "2001:db8::/56",
|
|
}, // Expected to be accessed directly, part of 2001:db8::/48 in NO_PROXY
|
|
expectWarnings: false,
|
|
},
|
|
{
|
|
check: HTTPProxyCIDRCheck{
|
|
Proto: "https",
|
|
CIDR: "2001:db8:1::/56",
|
|
}, // Expected to go via proxy, range is not in 2001:db8::/48
|
|
expectWarnings: true,
|
|
},
|
|
}
|
|
|
|
// Save current content of *_proxy and *_PROXY variables.
|
|
savedEnv := resetProxyEnv(t)
|
|
defer restoreEnv(savedEnv)
|
|
|
|
for _, rt := range tests {
|
|
warning, _ := rt.check.Check()
|
|
if (warning != nil) != rt.expectWarnings {
|
|
t.Errorf(
|
|
"failed HTTPProxyCIDRCheck:\n\texpected: %t\n\t actual: %t (CIDR:%s). Warnings: %v",
|
|
rt.expectWarnings,
|
|
(warning != nil),
|
|
rt.check.CIDR,
|
|
warning,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHTTPProxyCheck(t *testing.T) {
|
|
var tests = []struct {
|
|
name string
|
|
check HTTPProxyCheck
|
|
expectWarnings bool
|
|
}{
|
|
{
|
|
name: "Loopback address",
|
|
check: HTTPProxyCheck{
|
|
Proto: "https",
|
|
Host: "127.0.0.1",
|
|
}, // Loopback addresses never should produce proxy warnings
|
|
expectWarnings: false,
|
|
},
|
|
{
|
|
name: "IPv4 direct access",
|
|
check: HTTPProxyCheck{
|
|
Proto: "https",
|
|
Host: "10.96.0.1",
|
|
}, // Expected to be accessed directly, we set NO_PROXY to 10.0.0.0/8
|
|
expectWarnings: false,
|
|
},
|
|
{
|
|
name: "IPv4 via proxy",
|
|
check: HTTPProxyCheck{
|
|
Proto: "https",
|
|
Host: "192.168.0.1",
|
|
}, // Expected to go via proxy as this range is not listed in NO_PROXY
|
|
expectWarnings: true,
|
|
},
|
|
{
|
|
name: "IPv6 direct access",
|
|
check: HTTPProxyCheck{
|
|
Proto: "https",
|
|
Host: "[2001:db8::1:15]",
|
|
}, // Expected to be accessed directly, part of 2001:db8::/48 in NO_PROXY
|
|
expectWarnings: false,
|
|
},
|
|
{
|
|
name: "IPv6 via proxy",
|
|
check: HTTPProxyCheck{
|
|
Proto: "https",
|
|
Host: "[2001:db8:1::1:15]",
|
|
}, // Expected to go via proxy, range is not in 2001:db8::/48
|
|
expectWarnings: true,
|
|
},
|
|
}
|
|
|
|
// Save current content of *_proxy and *_PROXY variables.
|
|
savedEnv := resetProxyEnv(t)
|
|
defer restoreEnv(savedEnv)
|
|
|
|
for _, rt := range tests {
|
|
warning, _ := rt.check.Check()
|
|
if (warning != nil) != rt.expectWarnings {
|
|
t.Errorf(
|
|
"%s failed HTTPProxyCheck:\n\texpected: %t\n\t actual: %t (Host:%s). Warnings: %v",
|
|
rt.name,
|
|
rt.expectWarnings,
|
|
(warning != nil),
|
|
rt.check.Host,
|
|
warning,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
// resetProxyEnv is helper function that unsets all *_proxy variables
|
|
// and return previously set values as map. This can be used to restore
|
|
// original state of the environment.
|
|
func resetProxyEnv(t *testing.T) map[string]string {
|
|
savedEnv := make(map[string]string)
|
|
for _, e := range os.Environ() {
|
|
pair := strings.Split(e, "=")
|
|
if strings.HasSuffix(strings.ToLower(pair[0]), "_proxy") {
|
|
savedEnv[pair[0]] = pair[1]
|
|
os.Unsetenv(pair[0])
|
|
}
|
|
}
|
|
t.Log("Saved environment: ", savedEnv)
|
|
|
|
os.Setenv("HTTP_PROXY", "http://proxy.example.com:3128")
|
|
os.Setenv("NO_PROXY", "example.com,10.0.0.0/8,2001:db8::/48")
|
|
// Check if we can reliably execute tests:
|
|
// ProxyFromEnvironment caches the *_proxy environment variables and
|
|
// if ProxyFromEnvironment already executed before our test with empty
|
|
// HTTP_PROXY it will make these tests return false positive failures
|
|
req, err := http.NewRequest("GET", "http://host.fake.tld/", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected err: %v", err)
|
|
}
|
|
proxy, err := http.ProxyFromEnvironment(req)
|
|
if err != nil {
|
|
t.Fatalf("unexpected err: %v", err)
|
|
}
|
|
if proxy == nil {
|
|
t.Skip("test skipped as ProxyFromEnvironment already initialized in environment without defined HTTP proxy")
|
|
}
|
|
t.Log("http.ProxyFromEnvironment is usable, continue executing test")
|
|
return savedEnv
|
|
}
|
|
|
|
// restoreEnv is helper function to restores values
|
|
// of environment variables from saved state in the map
|
|
func restoreEnv(e map[string]string) {
|
|
for k, v := range e {
|
|
os.Setenv(k, v)
|
|
}
|
|
}
|
|
|
|
func TestKubeletVersionCheck(t *testing.T) {
|
|
cases := []struct {
|
|
kubeletVersion string
|
|
k8sVersion string
|
|
expectErrors bool
|
|
expectWarnings bool
|
|
}{
|
|
{"v1.10.2", "", false, false}, // check minimally supported version when there is no information about control plane
|
|
{"v1.7.3", "v1.7.8", true, false}, // too old kubelet (older than kubeadmconstants.MinimumKubeletVersion), should fail.
|
|
{"v1.9.0", "v1.9.5", false, false}, // kubelet within same major.minor as control plane
|
|
{"v1.9.5", "v1.9.1", false, false}, // kubelet is newer, but still within same major.minor as control plane
|
|
{"v1.9.0", "v1.10.1", false, false}, // kubelet is lower than control plane, but newer than minimally supported
|
|
{"v1.10.0-alpha.1", "v1.9.1", true, false}, // kubelet is newer (development build) than control plane, should fail.
|
|
{"v1.10.0", "v1.9.5", true, false}, // kubelet is newer (release) than control plane, should fail.
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
fcmd := fakeexec.FakeCmd{
|
|
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
|
|
func() ([]byte, error) { return []byte("Kubernetes " + tc.kubeletVersion), nil },
|
|
},
|
|
}
|
|
fexec := &fakeexec.FakeExec{
|
|
CommandScript: []fakeexec.FakeCommandAction{
|
|
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
|
|
},
|
|
}
|
|
|
|
check := KubeletVersionCheck{KubernetesVersion: tc.k8sVersion, exec: fexec}
|
|
warnings, errors := check.Check()
|
|
|
|
switch {
|
|
case warnings != nil && !tc.expectWarnings:
|
|
t.Errorf("KubeletVersionCheck: unexpected warnings for kubelet version %q and kubernetes version %q. Warnings: %v", tc.kubeletVersion, tc.k8sVersion, warnings)
|
|
case warnings == nil && tc.expectWarnings:
|
|
t.Errorf("KubeletVersionCheck: expected warnings for kubelet version %q and kubernetes version %q but got nothing", tc.kubeletVersion, tc.k8sVersion)
|
|
case errors != nil && !tc.expectErrors:
|
|
t.Errorf("KubeletVersionCheck: unexpected errors for kubelet version %q and kubernetes version %q. errors: %v", tc.kubeletVersion, tc.k8sVersion, errors)
|
|
case errors == nil && tc.expectErrors:
|
|
t.Errorf("KubeletVersionCheck: expected errors for kubelet version %q and kubernetes version %q but got nothing", tc.kubeletVersion, tc.k8sVersion)
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestSetHasItemOrAll(t *testing.T) {
|
|
var tests = []struct {
|
|
ignoreSet sets.String
|
|
testString string
|
|
expectedResult bool
|
|
}{
|
|
{sets.NewString(), "foo", false},
|
|
{sets.NewString("all"), "foo", true},
|
|
{sets.NewString("all", "bar"), "foo", true},
|
|
{sets.NewString("bar"), "foo", false},
|
|
{sets.NewString("baz", "foo", "bar"), "foo", true},
|
|
{sets.NewString("baz", "bar", "foo"), "Foo", true},
|
|
}
|
|
|
|
for _, rt := range tests {
|
|
result := setHasItemOrAll(rt.ignoreSet, rt.testString)
|
|
if result != rt.expectedResult {
|
|
t.Errorf(
|
|
"setHasItemOrAll: expected: %v actual: %v (arguments: %q, %q)",
|
|
rt.expectedResult, result,
|
|
rt.ignoreSet,
|
|
rt.testString,
|
|
)
|
|
}
|
|
}
|
|
}
|