diff --git a/agent/installer/installer_test.go b/agent/installer/installer_test.go index c54a1bc3a..50b78d549 100644 --- a/agent/installer/installer_test.go +++ b/agent/installer/installer_test.go @@ -101,6 +101,15 @@ 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 error", func() { + os := "a" + k8s := "a" + _, _, err := PreviewChanges(os, k8s) + Expect(err).Should((HaveOccurred())) + Expect(err).Should(Equal(ErrOsK8sNotSupported)) + }) + }) }) func NewPreviewInstaller(os string, ob algo.OutputBuilder) *installer { diff --git a/agent/installer/internal/algo/algo_test.go b/agent/installer/internal/algo/algo_test.go index 0fcac6cb8..2953b1367 100644 --- a/agent/installer/internal/algo/algo_test.go +++ b/agent/installer/internal/algo/algo_test.go @@ -43,4 +43,25 @@ var _ = Describe("Installer Algo Tests", func() { Expect(outputBuilderCounter.LogCalledCnt).Should(Equal(stepsNum)) }) }) + Context("When error occurs during installation", func() { + var ( + mockUbuntu MockUbuntuWithError + ) + BeforeEach(func() { + mockUbuntu = MockUbuntuWithError{} + mockUbuntu.errorOnStep = 5 + + installer = &BaseK8sInstaller{ + K8sStepProvider: &mockUbuntu} + }) + It("Should rollback all applied steps", func() { + err := installer.Install() + Expect(err).Should((HaveOccurred())) + Expect(len(mockUbuntu.doSteps)).Should(Equal(mockUbuntu.errorOnStep)) + Expect(len(mockUbuntu.undoSteps)).Should(Equal(mockUbuntu.errorOnStep)) + for i, sz := 0, len(mockUbuntu.doSteps); i < sz; i++ { + Expect(mockUbuntu.doSteps[i]).Should(Equal(mockUbuntu.undoSteps[sz-i-1])) + } + }) + }) }) diff --git a/agent/installer/internal/algo/mock_ubuntu_with_error.go b/agent/installer/internal/algo/mock_ubuntu_with_error.go new file mode 100644 index 000000000..7311b0e95 --- /dev/null +++ b/agent/installer/internal/algo/mock_ubuntu_with_error.go @@ -0,0 +1,93 @@ +// Copyright 2021 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package algo + +import "errors" + +type mockStep struct { + Step + Desc string + Err error + *BaseK8sInstaller + *MockUbuntuWithError +} + +func (s *mockStep) do() error { + s.doSteps = append(s.doSteps, s.Desc) + return s.Err +} + +func (s *mockStep) undo() error { + s.undoSteps = append(s.undoSteps, s.Desc) + return s.Err +} + +type MockUbuntuWithError struct { + BaseK8sInstaller + errorOnStep int + curStep int + doSteps []string + undoSteps []string +} + +func (u *MockUbuntuWithError) getEmptyStep(desc string, bki *BaseK8sInstaller) Step { + u.curStep++ + var err error + if u.curStep == u.errorOnStep { + err = errors.New("error") + } + return &mockStep{ + BaseK8sInstaller: bki, + Desc: desc, + Err: err, + MockUbuntuWithError: u} +} + +func (u *MockUbuntuWithError) swapStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("SWAP", bki) +} + +func (u *MockUbuntuWithError) firewallStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("FIREWALL", bki) +} + +func (u *MockUbuntuWithError) kernelModsLoadStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("KERNEL MODULES", bki) +} + +func (u *MockUbuntuWithError) unattendedUpdStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("AUTO OS UPGRADES", bki) +} + +func (u *MockUbuntuWithError) osWideCfgUpdateStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("OS CONFIGURATION", bki) +} + +func (u *MockUbuntuWithError) criToolsStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("cri-tools.deb", bki) +} + +func (u *MockUbuntuWithError) criKubernetesStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("kubernetes-cni.deb", bki) +} + +func (u *MockUbuntuWithError) kubectlStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("kubectl.deb", bki) +} + +func (u *MockUbuntuWithError) kubeadmStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("kubeadm.deb", bki) +} + +func (u *MockUbuntuWithError) kubeletStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("kubelet.deb", bki) +} + +func (u *MockUbuntuWithError) containerdStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("CONTAINERD", bki) +} + +func (u *MockUbuntuWithError) containerdDaemonStep(bki *BaseK8sInstaller) Step { + return u.getEmptyStep("CONTAINERD SERVICE", bki) +}