Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional tests for installer and algo installer #200

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions agent/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,18 @@ func getSupportedRegistryDescription() registry {
func PreviewChanges(os, k8sVer string) (install, uninstall string, err error) {
stepPreviewer := stringPrinter{msgFmt: "# %s"}
reg := getSupportedRegistry(&bundleDownloader{}, &stepPreviewer)
installer := reg.GetInstaller(os, k8sVer).(algo.Installer)
err = installer.Install()
installer := reg.GetInstaller(os, k8sVer)
if installer == nil {
return
georgievaVMW marked this conversation as resolved.
Show resolved Hide resolved
}
algoInstaller := installer.(algo.Installer)
err = algoInstaller.Install()
if err != nil {
return
}
install = stepPreviewer.String()
stepPreviewer.steps = nil
err = installer.Uninstall()
err = algoInstaller.Uninstall()
if err != nil {
return
}
Expand Down
10 changes: 10 additions & 0 deletions agent/installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ var _ = Describe("Byohost Installer Tests", func() {
Expect(uninstall).ShouldNot(ContainSubstring("Installing"))
})
})
Context("When PreviewChanges is called for non-supported os and k8s", func() {
It("Should return empty result", func() {
georgievaVMW marked this conversation as resolved.
Show resolved Hide resolved
os := "a"
k8s := "a"
install, uninstall, err := PreviewChanges(os, k8s)
Expect(err).ShouldNot((HaveOccurred()))
Expect(install).Should(Equal(""))
Expect(uninstall).Should(Equal(""))
})
})
})

func NewPreviewInstaller(os string, ob algo.OutputBuilder) *installer {
Expand Down
37 changes: 37 additions & 0 deletions agent/installer/internal/algo/algo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package algo

import (
"log"
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -43,4 +46,38 @@ var _ = Describe("Installer Algo Tests", func() {
Expect(outputBuilderCounter.LogCalledCnt).Should(Equal(stepsNum))
})
})
Context("When error occurs during installation", func() {
var (
err error
mockUbuntu MockUbuntuWithError
)
const (
stepWhenError = 5
georgievaVMW marked this conversation as resolved.
Show resolved Hide resolved
obcExpectedTotal = stepWhenError*4 + 2
)
BeforeEach(func() {
mockUbuntu = MockUbuntuWithError{}
mockUbuntu.OutputBuilder = &outputBuilderCounter
mockUbuntu.BundlePath, err = os.MkdirTemp("", "algoErrTest")
if err != nil {
log.Fatal(err)
}

installer = &BaseK8sInstaller{
BundlePath: mockUbuntu.BundlePath,
K8sStepProvider: &mockUbuntu,
OutputBuilder: &outputBuilderCounter}
georgievaVMW marked this conversation as resolved.
Show resolved Hide resolved
})
AfterEach(func() {
err = os.RemoveAll(mockUbuntu.BundlePath)
georgievaVMW marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatal(err)
}
})
It("Should rollback all applied steps", func() {
err = installer.Install()
Expect(err).Should((HaveOccurred()))
Expect(outputBuilderCounter.LogCalledCnt).Should(Equal(obcExpectedTotal))
georgievaVMW marked this conversation as resolved.
Show resolved Hide resolved
})
})
})
69 changes: 69 additions & 0 deletions agent/installer/internal/algo/mock_ubuntu_with_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package algo

type MockUbuntuWithError struct {
BaseK8sInstaller
}

func getEmptyStep(desc string, bki *BaseK8sInstaller) Step {
georgievaVMW marked this conversation as resolved.
Show resolved Hide resolved
return &ShellStep{
BaseK8sInstaller: bki,
Desc: desc,
DoCmd: `:`,
UndoCmd: `:`}
}

func (u *MockUbuntuWithError) swapStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("SWAP", bki)
}

func (u *MockUbuntuWithError) firewallStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("FIREWALL", bki)
}

func (u *MockUbuntuWithError) kernelModsLoadStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("KERNEL MODULES", bki)
}

func (u *MockUbuntuWithError) unattendedUpdStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("AUTO OS UPGRADES", bki)
}

func (u *MockUbuntuWithError) osWideCfgUpdateStep(bki *BaseK8sInstaller) Step {
// Here DoCmd should return error when executed
return &ShellStep{
BaseK8sInstaller: bki,
Desc: "OS CONFIGURATION",
DoCmd: `a`,
UndoCmd: `:`}
}

func (u *MockUbuntuWithError) criToolsStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("cri-tools.deb", bki)
}

func (u *MockUbuntuWithError) criKubernetesStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("kubernetes-cni.deb", bki)
}

func (u *MockUbuntuWithError) kubectlStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("kubectl.deb", bki)
}

func (u *MockUbuntuWithError) kubeadmStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("kubeadm.deb", bki)
}

func (u *MockUbuntuWithError) kubeletStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("kubelet.deb", bki)
}

func (u *MockUbuntuWithError) containerdStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("CONTAINERD", bki)
}

func (u *MockUbuntuWithError) containerdDaemonStep(bki *BaseK8sInstaller) Step {
return getEmptyStep("CONTAINERD SERVICE", bki)
}