mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
kubeadm: apply a number of _test fixes
- common_test.go: use constants.CurrentKubernetesVersion - diff_test.go: write temporary files instead of using testdata. this allows us to not have to bump kubernetesVersions in the testdata files (now removed) - policy_test.go: apply fixes to tests that were previously passing, but a bump in constants.go breaks them. these tests now work for any version.
This commit is contained in:
parent
39af3cccd6
commit
e379164bc7
@ -57,7 +57,9 @@ go_test(
|
|||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
deps = [
|
deps = [
|
||||||
"//cmd/kubeadm/app/apis/kubeadm:go_default_library",
|
"//cmd/kubeadm/app/apis/kubeadm:go_default_library",
|
||||||
|
"//cmd/kubeadm/app/constants:go_default_library",
|
||||||
"//cmd/kubeadm/app/phases/upgrade:go_default_library",
|
"//cmd/kubeadm/app/phases/upgrade:go_default_library",
|
||||||
|
"//vendor/github.com/pkg/errors:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -23,16 +23,15 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||||
)
|
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||||
|
|
||||||
const (
|
|
||||||
validConfig = `apiVersion: kubeadm.k8s.io/v1beta2
|
|
||||||
kind: ClusterConfiguration
|
|
||||||
kubernetesVersion: 1.13.0
|
|
||||||
`
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetK8sVersionFromUserInput(t *testing.T) {
|
func TestGetK8sVersionFromUserInput(t *testing.T) {
|
||||||
|
currentVersion := "v" + constants.CurrentKubernetesVersion.String()
|
||||||
|
validConfig := "apiVersion: kubeadm.k8s.io/v1beta2\n" +
|
||||||
|
"kind: ClusterConfiguration\n" +
|
||||||
|
"kubernetesVersion: " + currentVersion
|
||||||
|
|
||||||
var tcases = []struct {
|
var tcases = []struct {
|
||||||
name string
|
name string
|
||||||
isVersionMandatory bool
|
isVersionMandatory bool
|
||||||
@ -62,7 +61,7 @@ func TestGetK8sVersionFromUserInput(t *testing.T) {
|
|||||||
name: "Valid config, but no version specified",
|
name: "Valid config, but no version specified",
|
||||||
isVersionMandatory: true,
|
isVersionMandatory: true,
|
||||||
clusterConfig: validConfig,
|
clusterConfig: validConfig,
|
||||||
expectedVersion: "v1.13.0",
|
expectedVersion: currentVersion,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Valid config and different version specified",
|
name: "Valid config and different version specified",
|
||||||
@ -105,7 +104,7 @@ func TestGetK8sVersionFromUserInput(t *testing.T) {
|
|||||||
t.Errorf("Unexpected error: %+v", err)
|
t.Errorf("Unexpected error: %+v", err)
|
||||||
}
|
}
|
||||||
if userVersion != tt.expectedVersion {
|
if userVersion != tt.expectedVersion {
|
||||||
t.Errorf("Expected '%s', but got '%s'", tt.expectedVersion, userVersion)
|
t.Errorf("Expected %q, but got %q", tt.expectedVersion, userVersion)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -18,15 +18,48 @@ package upgrade
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
func createTestRunDiffFile(contents []byte) (string, error) {
|
||||||
testUpgradeDiffConfig = `testdata/diff_controlplane_config.yaml`
|
file, err := ioutil.TempFile("", "kubeadm-upgrade-diff-config-*.yaml")
|
||||||
testUpgradeDiffManifest = `testdata/diff_dummy_manifest.yaml`
|
if err != nil {
|
||||||
)
|
return "", errors.Wrap(err, "failed to create temporary test file")
|
||||||
|
}
|
||||||
|
if _, err := file.Write([]byte(contents)); err != nil {
|
||||||
|
return "", errors.Wrap(err, "failed to write to temporary test file")
|
||||||
|
}
|
||||||
|
if err := file.Close(); err != nil {
|
||||||
|
return "", errors.Wrap(err, "failed to close temporary test file")
|
||||||
|
}
|
||||||
|
return file.Name(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunDiff(t *testing.T) {
|
func TestRunDiff(t *testing.T) {
|
||||||
|
currentVersion := "v" + constants.CurrentKubernetesVersion.String()
|
||||||
|
|
||||||
|
// create a temporary file with valid ClusterConfiguration
|
||||||
|
testUpgradeDiffConfigContents := []byte("apiVersion: kubeadm.k8s.io/v1beta2\n" +
|
||||||
|
"kind: ClusterConfiguration\n" +
|
||||||
|
"kubernetesVersion: " + currentVersion)
|
||||||
|
testUpgradeDiffConfig, err := createTestRunDiffFile(testUpgradeDiffConfigContents)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(testUpgradeDiffConfig)
|
||||||
|
|
||||||
|
// create a temporary manifest file with dummy contents
|
||||||
|
testUpgradeDiffManifestContents := []byte("some-contents")
|
||||||
|
testUpgradeDiffManifest, err := createTestRunDiffFile(testUpgradeDiffManifestContents)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(testUpgradeDiffManifest)
|
||||||
|
|
||||||
flags := &diffFlags{
|
flags := &diffFlags{
|
||||||
cfgPath: "",
|
cfgPath: "",
|
||||||
out: ioutil.Discard,
|
out: ioutil.Discard,
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
apiVersion: kubeadm.k8s.io/v1beta2
|
|
||||||
kind: ClusterConfiguration
|
|
||||||
kubernetesVersion: 1.13.0
|
|
@ -1 +0,0 @@
|
|||||||
some-empty-file-to-diff
|
|
@ -93,13 +93,12 @@ func TestEnforceVersionPolicies(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "downgrading two minor versions in one go is not supported",
|
name: "downgrading two minor versions in one go is not supported",
|
||||||
vg: &fakeVersionGetter{
|
vg: &fakeVersionGetter{
|
||||||
clusterVersion: "v1.15.3",
|
clusterVersion: constants.CurrentKubernetesVersion.WithMinor(constants.CurrentKubernetesVersion.Minor() + 2).String(),
|
||||||
kubeletVersion: "v1.15.3",
|
kubeletVersion: constants.CurrentKubernetesVersion.WithMinor(constants.CurrentKubernetesVersion.Minor() + 2).String(),
|
||||||
kubeadmVersion: "v1.15.0",
|
kubeadmVersion: constants.CurrentKubernetesVersion.String(),
|
||||||
},
|
},
|
||||||
newK8sVersion: constants.MinimumControlPlaneVersion.WithPatch(3).String(),
|
newK8sVersion: constants.CurrentKubernetesVersion.String(),
|
||||||
expectedMandatoryErrs: 1, // can't downgrade two minor versions
|
expectedMandatoryErrs: 1, // can't downgrade two minor versions
|
||||||
expectedSkippableErrs: 1, // can't upgrade old k8s with newer kubeadm
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "kubeadm version must be higher than the new kube version. However, patch version skews may be forced",
|
name: "kubeadm version must be higher than the new kube version. However, patch version skews may be forced",
|
||||||
@ -213,10 +212,10 @@ func TestEnforceVersionPolicies(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(actualSkewErrs.Skippable) != rt.expectedSkippableErrs {
|
if len(actualSkewErrs.Skippable) != rt.expectedSkippableErrs {
|
||||||
t.Errorf("failed TestEnforceVersionPolicies\n\texpected skippable errors: %d\n\tgot skippable errors: %d %v", rt.expectedSkippableErrs, len(actualSkewErrs.Skippable), *rt.vg)
|
t.Errorf("failed TestEnforceVersionPolicies\n\texpected skippable errors: %d\n\tgot skippable errors: %d\n%#v\n%#v", rt.expectedSkippableErrs, len(actualSkewErrs.Skippable), *rt.vg, actualSkewErrs)
|
||||||
}
|
}
|
||||||
if len(actualSkewErrs.Mandatory) != rt.expectedMandatoryErrs {
|
if len(actualSkewErrs.Mandatory) != rt.expectedMandatoryErrs {
|
||||||
t.Errorf("failed TestEnforceVersionPolicies\n\texpected mandatory errors: %d\n\tgot mandatory errors: %d %v", rt.expectedMandatoryErrs, len(actualSkewErrs.Mandatory), *rt.vg)
|
t.Errorf("failed TestEnforceVersionPolicies\n\texpected mandatory errors: %d\n\tgot mandatory errors: %d\n%#v\n%#v", rt.expectedMandatoryErrs, len(actualSkewErrs.Mandatory), *rt.vg, actualSkewErrs)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user