Skip to content

Commit

Permalink
Improve testing
Browse files Browse the repository at this point in the history
Signed-off-by: David Cassany <[email protected]>
  • Loading branch information
davidcassany committed Nov 27, 2023
1 parent ca68440 commit ea0c6ef
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 91 deletions.
19 changes: 18 additions & 1 deletion pkg/action/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ var _ = Describe("Install action tests", func() {
mounter = v1mock.NewErrorMounter()
client = &v1mock.FakeHTTPClient{}
memLog = &bytes.Buffer{}
bootloader = &v1mock.FakeBootloader{}
logger = v1.NewBufferLogger(memLog)
logger.SetLevel(v1.DebugLevel())
extractor = v1mock.NewFakeImageExtractor(logger)
Expand Down Expand Up @@ -103,6 +102,8 @@ var _ = Describe("Install action tests", func() {
_, err = fs.Create(device)
Expect(err).ShouldNot(HaveOccurred())

bootloader = &v1mock.FakeBootloader{}

partNum := 0
partedOut := printOutput
cmdFail = ""
Expand Down Expand Up @@ -279,6 +280,22 @@ var _ = Describe("Install action tests", func() {
Expect(client.WasGetCalledWith("http://my.config.org")).To(BeTrue())
})

It("Fails setting the persistent grub variables", func() {
spec.Target = device
bootloader.ErrorSetPersistentVariables = true
err = installer.Run()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("setting persistent variables"))
})

It("Fails setting the default grub entry", func() {
spec.Target = device
bootloader.ErrorSetDefaultEntry = true
err = installer.Run()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("setting default entry"))
})

It("Fails if disk doesn't exist", Label("disk"), func() {
spec.Target = "nonexistingdisk"
Expect(installer.Run()).NotTo(BeNil())
Expand Down
17 changes: 14 additions & 3 deletions pkg/action/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ var _ = Describe("Reset action tests", func() {
mounter = v1mock.NewErrorMounter()
client = &v1mock.FakeHTTPClient{}
memLog = &bytes.Buffer{}
bootloader = &v1mock.FakeBootloader{}
logger = v1.NewBufferLogger(memLog)
extractor = v1mock.NewFakeImageExtractor(logger)
var err error
Expand Down Expand Up @@ -84,15 +83,15 @@ var _ = Describe("Reset action tests", func() {
var cmdFail, bootedFrom string
var err error
BeforeEach(func() {

Expect(err).ShouldNot(HaveOccurred())
cmdFail = ""
recoveryImg := filepath.Join(constants.RunningStateDir, "cOS", constants.RecoveryImgFile)
err = utils.MkdirAll(fs, filepath.Dir(recoveryImg), constants.DirPerm)
Expect(err).To(BeNil())
_, err = fs.Create(recoveryImg)
Expect(err).To(BeNil())

bootloader = &v1mock.FakeBootloader{}

mainDisk := block.Disk{
Name: "device",
Partitions: []*block.Partition{
Expand Down Expand Up @@ -189,6 +188,18 @@ var _ = Describe("Reset action tests", func() {
It("Successfully resets from a channel package", Label("channel"), func() {
Expect(reset.Run()).To(BeNil())
})
It("Fails setting the persistent grub variables", func() {
bootloader.ErrorSetPersistentVariables = true
err = reset.Run()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("setting persistent variables"))
})
It("Fails setting the default grub entry", func() {
bootloader.ErrorSetDefaultEntry = true
err = reset.Run()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("setting default entry"))
})
It("Fails installing grub", func() {
bootloader.ErrorInstall = true
Expect(reset.Run()).NotTo(BeNil())
Expand Down
103 changes: 16 additions & 87 deletions pkg/action/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var _ = Describe("Runtime Actions", func() {
var cleanup func()
var memLog *bytes.Buffer
var ghwTest v1mock.GhwMock
var bootloader *v1mock.FakeBootloader

BeforeEach(func() {
runner = v1mock.NewFakeRunner()
Expand All @@ -59,6 +60,7 @@ var _ = Describe("Runtime Actions", func() {
client = &v1mock.FakeHTTPClient{}
memLog = &bytes.Buffer{}
logger = v1.NewBufferLogger(memLog)
bootloader = &v1mock.FakeBootloader{}
extractor = v1mock.NewFakeImageExtractor(logger)
var err error
fs, cleanup, err = vfst.NewTestFS(map[string]interface{}{})
Expand Down Expand Up @@ -209,6 +211,20 @@ var _ = Describe("Runtime Actions", func() {
// Make sure is a cloud init error!
Expect(err.Error()).To(ContainSubstring("cloud init"))
})
It("Fails setting the grub labels", func() {
bootloader.ErrorSetPersistentVariables = true
upgrade = action.NewUpgradeAction(config, spec, action.WithUpgradeBootloader(bootloader))
err := upgrade.Run()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("setting persistent variables"))
})
It("Fails setting the grub default entry", func() {
bootloader.ErrorSetDefaultEntry = true
upgrade = action.NewUpgradeAction(config, spec, action.WithUpgradeBootloader(bootloader))
err := upgrade.Run()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("setting default entry"))
})
It("Successfully upgrades from docker image with custom labels", Label("docker"), func() {
// Create installState with previous install state
statePath := filepath.Join(constants.RunningStateDir, constants.InstallStateFile)
Expand Down Expand Up @@ -469,40 +485,7 @@ var _ = Describe("Runtime Actions", func() {
Expect(err).To(HaveOccurred())

})
It("Successfully upgrades from channel upgrade", Label("channel"), func() {
upgrade = action.NewUpgradeAction(config, spec)
err := upgrade.Run()
Expect(err).ToNot(HaveOccurred())

// Check that the rebrand worked with our os-release value
Expect(memLog).To(ContainSubstring("default_menu_entry=TESTOS"))

// Not much that we can create here as the dir copy was done on the real os, but we do the rest of the ops on a mem one
// This should be the new image
// Should probably do well in mounting the image and checking contents to make sure everything worked
info, err := fs.Stat(activeImg)
Expect(err).ToNot(HaveOccurred())
// Image size should not be empty
Expect(info.Size()).To(BeNumerically("==", int64(spec.Active.Size*1024*1024)))
Expect(info.IsDir()).To(BeFalse())

// Should have backed up active to passive
info, err = fs.Stat(passiveImg)
Expect(err).ToNot(HaveOccurred())
// Should be an really small image as it should only contain our text
// As this was generated by us at the start test and moved by the upgrade from active.iomg
Expect(info.Size()).To(BeNumerically(">", 0))
Expect(info.Size()).To(BeNumerically("<", int64(spec.Active.Size*1024*1024)))
f, _ := fs.ReadFile(passiveImg)
// This should be a backup so it should read active
Expect(f).To(ContainSubstring("active"))

// Expect transition image to be gone
_, err = fs.Stat(spec.Active.File)
Expect(err).To(HaveOccurred())
})
It("Successfully upgrades with cosign", Pending, Label("channel", "cosign"), func() {})
It("Successfully upgrades with mtree", Pending, Label("channel", "mtree"), func() {})
It("Successfully upgrades with strict", Pending, Label("channel", "strict"), func() {})
})
Describe(fmt.Sprintf("Booting from %s", constants.PassiveLabel), Label("passive_label"), func() {
Expand Down Expand Up @@ -680,33 +663,6 @@ var _ = Describe("Runtime Actions", func() {
Expect(err).To(HaveOccurred())

})
It("Successfully upgrades recovery from channel upgrade", Label("channel"), func() {
// This should be the old image
info, err := fs.Stat(recoveryImg)
Expect(err).ToNot(HaveOccurred())
// Image size should be empty
Expect(info.Size()).To(BeNumerically(">", 0))
Expect(info.IsDir()).To(BeFalse())
f, _ := fs.ReadFile(recoveryImg)
Expect(f).To(ContainSubstring("recovery"))

upgrade = action.NewUpgradeAction(config, spec)
err = upgrade.Run()
Expect(err).ToNot(HaveOccurred())

// This should be the new image
info, err = fs.Stat(recoveryImg)
Expect(err).ToNot(HaveOccurred())
// Image size should be empty
Expect(info.Size()).To(BeNumerically("==", 0))
Expect(info.IsDir()).To(BeFalse())
f, _ = fs.ReadFile(recoveryImg)
Expect(f).ToNot(ContainSubstring("recovery"))

// Transition squash should not exist
info, err = fs.Stat(spec.Recovery.File)
Expect(err).To(HaveOccurred())
})
})
Describe("Not using squashfs", Label("non-squashfs"), func() {
var err error
Expand Down Expand Up @@ -798,33 +754,6 @@ var _ = Describe("Runtime Actions", func() {
Images[constants.RecoveryImgName].Source.String()).
To(Equal(spec.Recovery.Source.String()))
})
It("Successfully upgrades recovery from channel upgrade", Label("channel"), func() {
// This should be the old image
info, err := fs.Stat(recoveryImg)
Expect(err).ToNot(HaveOccurred())
// Image size should not be empty
Expect(info.Size()).To(BeNumerically(">", 0))
Expect(info.Size()).To(BeNumerically("<", int64(spec.Recovery.Size*1024*1024)))
Expect(info.IsDir()).To(BeFalse())
f, _ := fs.ReadFile(recoveryImg)
Expect(f).To(ContainSubstring("recovery"))

upgrade = action.NewUpgradeAction(config, spec)
err = upgrade.Run()
Expect(err).ToNot(HaveOccurred())

// Should have created recovery image
info, err = fs.Stat(recoveryImg)
Expect(err).ToNot(HaveOccurred())
// Should have default image size
Expect(info.Size()).To(BeNumerically("==", int64(spec.Recovery.Size*1024*1024)))

// Expect the rest of the images to not be there
for _, img := range []string{activeImg, passiveImg} {
_, err := fs.Stat(img)
Expect(err).To(HaveOccurred())
}
})
})
})
})
Expand Down

0 comments on commit ea0c6ef

Please sign in to comment.