Skip to content

Commit

Permalink
Add unit tests for 'parseContainerImageVersion' and 'checkVersionSkew'
Browse files Browse the repository at this point in the history
  • Loading branch information
xmudrii committed Feb 20, 2019
1 parent f07c474 commit aa92c99
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 7 deletions.
14 changes: 7 additions & 7 deletions pkg/upgrader/upgrade/preflight_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ func verifyVersion(ctx *util.Context, nodes *corev1.NodeList, verbose bool) erro
}
// This ensures all API server pods are running the same apiserver version
for _, p := range apiserverPods.Items {
ver, err := parseContainerImageVersion(p.Spec.Containers[0])
if err != nil {
ver, apiserverErr := parseContainerImageVersion(p.Spec.Containers[0].Image)
if apiserverErr != nil {
return fmt.Errorf("unable to parse apiserver version: %v", err)
}
if verbose {
Expand All @@ -182,8 +182,8 @@ func verifyVersion(ctx *util.Context, nodes *corev1.NodeList, verbose bool) erro

// Check Kubelet version
for _, n := range nodes.Items {
kubeletVer, err := semver.NewVersion(n.Status.NodeInfo.KubeletVersion)
if err != nil {
kubeletVer, kubeletErr := semver.NewVersion(n.Status.NodeInfo.KubeletVersion)
if kubeletErr != nil {
return fmt.Errorf("unable to parse kubelet version: %v", err)
}
if verbose {
Expand All @@ -202,10 +202,10 @@ func verifyVersion(ctx *util.Context, nodes *corev1.NodeList, verbose bool) erro
return nil
}

func parseContainerImageVersion(container corev1.Container) (*semver.Version, error) {
ver := strings.Split(container.Image, ":")
func parseContainerImageVersion(image string) (*semver.Version, error) {
ver := strings.Split(image, ":")
if len(ver) != 2 {
return nil, fmt.Errorf("invalid apiserver container image format: %s", container.Image)
return nil, fmt.Errorf("invalid container image format: %s", image)
}
return semver.NewVersion(ver[1])
}
Expand Down
241 changes: 241 additions & 0 deletions pkg/upgrader/upgrade/preflight_checks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
package upgrade

import (
"fmt"
"testing"

"github.com/Masterminds/semver"
)

func TestParseContainerImageVersionValid(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
image string
expectedVersion *semver.Version
}{
{
name: "docker image",
image: "test/test-image:v1.13.3",
expectedVersion: semver.MustParse("v1.13.3"),
},
{
name: "docker.io image",
image: "docker.io/test/test-image:v1.13.2",
expectedVersion: semver.MustParse("v1.13.2"),
},
{
name: "gcr.io image",
image: "gcr.io/kubernetes/kubernetes:v1.14.0",
expectedVersion: semver.MustParse("v1.14.0"),
},
{
name: "gcr.io image without v prefix",
image: "gcr.io/kubernetes:1.14.0",
expectedVersion: semver.MustParse("1.14.0"),
},
{
name: "gcr.io image without v prefix and patch version",
image: "gcr.io/kubernetes:1.14",
expectedVersion: semver.MustParse("v1.14.0"),
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
ver, err := parseContainerImageVersion(tc.image)
if err != nil {
t.Fatal(err)
}
if !ver.Equal(tc.expectedVersion) {
t.Fatalf("expected version %s, but got version %s", tc.expectedVersion.String(), ver.String())
}
})
}
}

func TestParseContainerImageVersionInvalid(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
image string
expectedError error
}{
{
name: "docker image without version",
image: "test/test-image",
expectedError: fmt.Errorf("invalid container image format: test/test-image"),
},
{
name: "docker image with the latest tag",
image: "test/test-image:latest",
expectedError: fmt.Errorf("Invalid Semantic Version"),
},
{
name: "gcr.io image without version",
image: "gcr.io/kubernetes/kube-apiserver",
expectedError: fmt.Errorf("invalid container image format: gcr.io/kubernetes/kube-apiserver"),
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
_, err := parseContainerImageVersion(tc.image)
if err.Error() != tc.expectedError.Error() {
t.Fatal(err)
}
})
}
}

func TestCheckVersionSkewValid(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
currentVersion *semver.Version
desiredVersion *semver.Version
diff int64
}{
{
name: "upgrade 1.13.3 to 1.14.0 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.14.0"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.14.1 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.14.1"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.14.3 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.14.3"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.14.5 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.14.5"),
diff: 1,
},
{
name: "upgrade 1.13.0 to 1.14.0 with diff of 1",
currentVersion: semver.MustParse("1.13.0"),
desiredVersion: semver.MustParse("1.14.0"),
diff: 1,
},
{
name: "upgrade 1.13.0 to 1.14.1 with diff of 1",
currentVersion: semver.MustParse("1.13.0"),
desiredVersion: semver.MustParse("1.14.1"),
diff: 1,
},
{
name: "upgrade 1.13.0 to 1.15.1 with diff of 2",
currentVersion: semver.MustParse("1.13.0"),
desiredVersion: semver.MustParse("1.15.1"),
diff: 2,
},
{
name: "upgrade 1.13.0 to 1.13.3 with diff of 1",
currentVersion: semver.MustParse("1.13.0"),
desiredVersion: semver.MustParse("1.13.3"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.13.4 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.13.4"),
diff: 1,
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
err := checkVersionSkew(tc.desiredVersion, tc.currentVersion, tc.diff)
if err != nil {
t.Fatal(err)
}
})
}
}

func TestCheckVersionSkewInvalid(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
currentVersion *semver.Version
desiredVersion *semver.Version
diff int64
}{
{
name: "upgrade 1.13.3 to 1.15.0 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.15.0"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.15.3 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.15.3"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.15.5 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.15.5"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.16.2 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.16.2"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.16.2 with diff of 2",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.16.2"),
diff: 2,
},
{
name: "upgrade 1.13.0 to 1.12.1 with diff of 1",
currentVersion: semver.MustParse("1.13.0"),
desiredVersion: semver.MustParse("1.12.1"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.12.1 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.12.1"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.12.3 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.12.3"),
diff: 1,
},
{
name: "upgrade 1.13.3 to 1.12.4 with diff of 1",
currentVersion: semver.MustParse("1.13.3"),
desiredVersion: semver.MustParse("1.12.4"),
diff: 1,
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
err := checkVersionSkew(tc.desiredVersion, tc.currentVersion, tc.diff)
if err == nil {
t.Fatalf("expected error but test succeed instead")
}
})
}
}

0 comments on commit aa92c99

Please sign in to comment.