Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
fully sanitize upgrade UT from static versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrancis committed Feb 19, 2021
1 parent e2ea29b commit 2676144
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
14 changes: 13 additions & 1 deletion pkg/armhelpers/mockclients.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"

"github.com/Azure/aks-engine/pkg/api/common"
Expand All @@ -32,11 +34,21 @@ import (
)

const (
defaultK8sVersionForFakeVMs = "Kubernetes:1.17.5"
//DefaultFakeVMName is the default name assigned to VMs part of FakeListVirtualMachineScaleSetVMsResult and FakeListVirtualMachineResult
DefaultFakeVMName = "k8s-agentpool1-12345678-0"
)

var defaultK8sVersionForFakeVMs string

func init() {
defaultVersion := common.RationalizeReleaseAndVersion(common.Kubernetes, "", "", false, false, false)
versionSplit := strings.Split(defaultVersion, ".")
minorVersion, _ := strconv.Atoi(versionSplit[1])
minorVersionLessOne := minorVersion - 1
priorVersion := versionSplit[0] + "." + strconv.Itoa(minorVersionLessOne) + "." + versionSplit[2]
defaultK8sVersionForFakeVMs = fmt.Sprintf("Kubernetes:%s", priorVersion)
}

//MockAKSEngineClient is an implementation of AKSEngineClient where all requests error out
type MockAKSEngineClient struct {
FailDeployTemplate bool
Expand Down
51 changes: 29 additions & 22 deletions pkg/operations/kubernetesupgrade/upgradecluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"os"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -55,6 +56,12 @@ func TestUpgradeCluster(t *testing.T) {
}

var _ = Describe("Upgrade Kubernetes cluster tests", func() {
defaultVersion := common.RationalizeReleaseAndVersion(common.Kubernetes, "", "", false, false, false)
versionSplit := strings.Split(defaultVersion, ".")
minorVersion, _ := strconv.Atoi(versionSplit[1])
minorVersionLessOne := minorVersion - 1
priorVersion := versionSplit[0] + "." + strconv.Itoa(minorVersionLessOne) + "." + versionSplit[2]
mockK8sVersionOneLessThanDefault := fmt.Sprintf("Kubernetes:%s", priorVersion)
AfterEach(func() {
// delete temp template directory
os.RemoveAll("_output")
Expand Down Expand Up @@ -262,7 +269,7 @@ var _ = Describe("Upgrade Kubernetes cluster tests", func() {

BeforeEach(func() {
mockClient = armhelpers.MockAKSEngineClient{MockKubernetesClient: &armhelpers.MockKubernetesClient{}}
cs = api.CreateMockContainerService("testcluster", "1.16.15", 3, 3, false)
cs = api.CreateMockContainerService("testcluster", "", 3, 3, false)
uc = UpgradeCluster{
Translator: &i18n.Translator{},
Logger: log.NewEntry(log.New()),
Expand Down Expand Up @@ -291,10 +298,10 @@ var _ = Describe("Upgrade Kubernetes cluster tests", func() {
It("Should skip VMs that are already on desired version", func() {
mockClient.FakeListVirtualMachineScaleSetVMsResult = func() []compute.VirtualMachineScaleSetVM {
return []compute.VirtualMachineScaleSetVM{
mockClient.MakeFakeVirtualMachineScaleSetVM("Kubernetes:1.16.15"),
mockClient.MakeFakeVirtualMachineScaleSetVM("Kubernetes:1.16.10"),
mockClient.MakeFakeVirtualMachineScaleSetVM("Kubernetes:1.16.9"),
mockClient.MakeFakeVirtualMachineScaleSetVM("Kubernetes:1.16.15"),
mockClient.MakeFakeVirtualMachineScaleSetVM(fmt.Sprintf("Kubernetes:%s", defaultVersion)),
mockClient.MakeFakeVirtualMachineScaleSetVM(fmt.Sprintf("Kubernetes:%s", mockK8sVersionOneLessThanDefault)),
mockClient.MakeFakeVirtualMachineScaleSetVM(fmt.Sprintf("Kubernetes:%s", mockK8sVersionOneLessThanDefault)),
mockClient.MakeFakeVirtualMachineScaleSetVM(fmt.Sprintf("Kubernetes:%s", defaultVersion)),
}
}
uc.Force = false
Expand Down Expand Up @@ -362,17 +369,17 @@ var _ = Describe("Upgrade Kubernetes cluster tests", func() {
It("Should use kubernetes api to get node versions for VMSS when latest model is not applied", func() {
trueVar := true
falseVar := false
vmWithoutLatestModelApplied := mockClient.MakeFakeVirtualMachineScaleSetVMWithGivenName("Kubernetes:1.16.15", "vmWithoutLatestModelApplied!")
vmWithoutLatestModelApplied := mockClient.MakeFakeVirtualMachineScaleSetVMWithGivenName(fmt.Sprintf("Kubernetes:%s", defaultVersion), "vmWithoutLatestModelApplied!")
vmWithoutLatestModelApplied.VirtualMachineScaleSetVMProperties.LatestModelApplied = &falseVar
vmWithLatestModelApplied := mockClient.MakeFakeVirtualMachineScaleSetVMWithGivenName("Kubernetes:1.16.15", "vmWithLatestModelApplied")
vmWithLatestModelApplied := mockClient.MakeFakeVirtualMachineScaleSetVMWithGivenName(fmt.Sprintf("Kubernetes:%s", defaultVersion), "vmWithLatestModelApplied")
vmWithLatestModelApplied.VirtualMachineScaleSetVMProperties.LatestModelApplied = &trueVar

mockClient.MockKubernetesClient.GetNodeFunc = func(name string) (*v1.Node, error) {
node := &v1.Node{}
node.Status.NodeInfo.KubeletVersion = "v1.18.12"
node.Status.NodeInfo.KubeletVersion = "v" + mockK8sVersionOneLessThanDefault
node.Status = v1.NodeStatus{}
node.Status.NodeInfo = v1.NodeSystemInfo{
KubeletVersion: "v1.18.12",
KubeletVersion: "v" + mockK8sVersionOneLessThanDefault,
}

return node, nil
Expand Down Expand Up @@ -443,7 +450,7 @@ var _ = Describe("Upgrade Kubernetes cluster tests", func() {

BeforeEach(func() {
mockClient = armhelpers.MockAKSEngineClient{}
cs = api.CreateMockContainerService("testcluster", "1.17.5", 3, 3, false)
cs = api.CreateMockContainerService("testcluster", "", 3, 3, false)
uc = UpgradeCluster{
Translator: &i18n.Translator{},
Logger: log.NewEntry(log.New()),
Expand Down Expand Up @@ -485,10 +492,10 @@ var _ = Describe("Upgrade Kubernetes cluster tests", func() {
It("Should mark scale sets as windows correctly.", func() {
mockClient.FakeListVirtualMachineScaleSetVMsResult = func() []compute.VirtualMachineScaleSetVM {
return []compute.VirtualMachineScaleSetVM{
mockClient.MakeFakeVirtualMachineScaleSetVM("Kubernetes:1.17.5"),
mockClient.MakeFakeVirtualMachineScaleSetVM("Kubernetes:1.17.4"),
mockClient.MakeFakeVirtualMachineScaleSetVM("Kubernetes:1.17.2"),
mockClient.MakeFakeVirtualMachineScaleSetVM("Kubernetes:1.17.5"),
mockClient.MakeFakeVirtualMachineScaleSetVM(fmt.Sprintf("Kubernetes:%s", defaultVersion)),
mockClient.MakeFakeVirtualMachineScaleSetVM(fmt.Sprintf("Kubernetes:%s", mockK8sVersionOneLessThanDefault)),
mockClient.MakeFakeVirtualMachineScaleSetVM(fmt.Sprintf("Kubernetes:%s", mockK8sVersionOneLessThanDefault)),
mockClient.MakeFakeVirtualMachineScaleSetVM(fmt.Sprintf("Kubernetes:%s", defaultVersion)),
}
}
uc.Force = false
Expand Down Expand Up @@ -652,7 +659,7 @@ var _ = Describe("Upgrade Kubernetes cluster tests", func() {
})

It("Should leave platform fault domain count nil for VMSS", func() {
cs := api.CreateMockContainerService("testcluster", "1.16.1", 3, 2, false)
cs := api.CreateMockContainerService("testcluster", "", 3, 2, false)
cs.Properties.MasterProfile.AvailabilityProfile = api.AvailabilitySet
cs.Properties.AgentPoolProfiles[0].AvailabilityProfile = api.VirtualMachineScaleSets
cs.Properties.AgentPoolProfiles[0].StorageProfile = "ManagedDisks"
Expand Down Expand Up @@ -682,16 +689,16 @@ var _ = Describe("Upgrade Kubernetes cluster tests", func() {
//masters
mockClient.FakeListVirtualMachineResult = func() []compute.VirtualMachine {
return []compute.VirtualMachine{
mockClient.MakeFakeVirtualMachine("one", "Kubernetes:1.15.1"),
mockClient.MakeFakeVirtualMachine("two", "Kubernetes:1.15.1"),
mockClient.MakeFakeVirtualMachine("three", "Kubernetes:1.15.1"),
mockClient.MakeFakeVirtualMachine("one", fmt.Sprintf("Kubernetes:%s", mockK8sVersionOneLessThanDefault)),
mockClient.MakeFakeVirtualMachine("two", fmt.Sprintf("Kubernetes:%s", mockK8sVersionOneLessThanDefault)),
mockClient.MakeFakeVirtualMachine("three", fmt.Sprintf("Kubernetes:%s", mockK8sVersionOneLessThanDefault)),
}
}
//agents
mockClient.FakeListVirtualMachineScaleSetVMsResult = func() []compute.VirtualMachineScaleSetVM {
return []compute.VirtualMachineScaleSetVM{
mockClient.MakeFakeVirtualMachineScaleSetVM("Kubernetes:1.15.1"),
mockClient.MakeFakeVirtualMachineScaleSetVM("Kubernetes:1.15.1"),
mockClient.MakeFakeVirtualMachineScaleSetVM(fmt.Sprintf("Kubernetes:%s", mockK8sVersionOneLessThanDefault)),
mockClient.MakeFakeVirtualMachineScaleSetVM(fmt.Sprintf("Kubernetes:%s", mockK8sVersionOneLessThanDefault)),
}
}
uc.Client = &mockClient
Expand Down Expand Up @@ -824,7 +831,7 @@ var _ = Describe("Upgrade Kubernetes cluster tests", func() {
})

It("Should pause cluster-autoscaler during upgrade operation", func() {
cs := api.CreateMockContainerService("testcluster", "1.17.5", 3, 2, false)
cs := api.CreateMockContainerService("testcluster", "", 3, 2, false)
enabled := true
addon := api.KubernetesAddon{
Name: "cluster-autoscaler",
Expand Down Expand Up @@ -869,7 +876,7 @@ var _ = Describe("Upgrade Kubernetes cluster tests", func() {
})

It("Should not pause cluster-autoscaler if only control plane is upgraded", func() {
cs := api.CreateMockContainerService("testcluster", "1.17.5", 3, 2, false)
cs := api.CreateMockContainerService("testcluster", "", 3, 2, false)
enabled := true
addon := api.KubernetesAddon{
Name: "cluster-autoscaler",
Expand Down

0 comments on commit 2676144

Please sign in to comment.