Skip to content

Commit

Permalink
Adapt tests to new grub config
Browse files Browse the repository at this point in the history
This commit refactors the tests to make use of `grub2-editenv` utility
to configure the default grub boot entry.

Signed-off-by: David Cassany <[email protected]>
  • Loading branch information
davidcassany committed Jul 7, 2021
1 parent 6cb479c commit 407149f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 69 deletions.
50 changes: 16 additions & 34 deletions tests/recovery/recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ var _ = Describe("cOS Recovery upgrade tests", func() {
s.EventuallyConnects()
})

AfterEach(func() {
if CurrentGinkgoTestDescription().Failed == false {
s.Reset()
}
})

Context("upgrading COS_ACTIVE from the recovery partition", func() {
AfterEach(func() {
if CurrentGinkgoTestDescription().Failed == false {
s.Reset()
}
})
It("upgrades to the latest", func() {
currentName := s.GetOSRelease("NAME")

By("booting into recovery to check the OS version")
err := s.ChangeBoot(sut.Recovery)
err := s.ChangeBootOnce(sut.Recovery)
Expect(err).ToNot(HaveOccurred())

s.Reboot()
Expand All @@ -42,14 +41,13 @@ var _ = Describe("cOS Recovery upgrade tests", func() {
Expect(currentName).To(Equal(recoveryName))
}

By("upgrade with CURRENT=active.img")
out, err := s.Command("CURRENT=active.img cos-upgrade")
Expect(err).ToNot(HaveOccurred())
Expect(out).Should(ContainSubstring("Upgrade done, now you might want to reboot"))
Expect(out).Should(ContainSubstring("Upgrading system"))

err = s.ChangeBoot(sut.Active)
Expect(err).ToNot(HaveOccurred())

By("Reboot to upgraded active")
s.Reboot()
ExpectWithOffset(1, s.BootFrom()).To(Equal(sut.Active))
})
Expand Down Expand Up @@ -85,11 +83,8 @@ var _ = Describe("cOS Recovery upgrade tests", func() {
})
})

// After this tests the VM is no longer in its initial state!!
Context("upgrading recovery", func() {
AfterEach(func() {
s.Reset()
})

When("using specific images", func() {
It("upgrades to a specific image and reset back to the installed version", func() {
version := s.GetOSRelease("VERSION")
Expand All @@ -100,7 +95,7 @@ var _ = Describe("cOS Recovery upgrade tests", func() {
Expect(out).Should(ContainSubstring("Upgrading recovery partition"))

By("booting into recovery to check the OS version")
err = s.ChangeBoot(sut.Recovery)
err = s.ChangeBootOnce(sut.Recovery)
Expect(err).ToNot(HaveOccurred())

s.Reboot()
Expand All @@ -111,39 +106,26 @@ var _ = Describe("cOS Recovery upgrade tests", func() {
Expect(out).ToNot(Equal(version))
Expect(out).To(Equal("0.5.3\n"))

By("setting back to active and rebooting")
err = s.ChangeBoot(sut.Active)
Expect(err).ToNot(HaveOccurred())

By("rebooting back to active")
s.Reboot()
ExpectWithOffset(1, s.BootFrom()).To(Equal(sut.Active))
})
})

When("using upgrade channel", func() {
// TODO: This test cannot be enabled until we have in master a published version of cOS >=0.5.3
It("upgrades to latest image", func() {
By("upgrading recovery and reboot")
By("upgrading recovery")
out, err := s.Command("cos-upgrade --no-verify --recovery")
Expect(err).ToNot(HaveOccurred())
Expect(out).Should(ContainSubstring("Upgrade done, now you might want to reboot"))
Expect(out).Should(ContainSubstring("Upgrading recovery partition"))

err = s.ChangeBoot(sut.Recovery)
By("Reboot to upgraded recovery")
err = s.ChangeBootOnce(sut.Recovery)
Expect(err).ToNot(HaveOccurred())

s.Reboot()

By("checking recovery version")
out, err = s.Command("source /etc/os-release && echo $VERSION")
Expect(err).ToNot(HaveOccurred())
Expect(out).ToNot(Equal(""))
Expect(out).ToNot(Equal("0.5.1\n"))

By("switch back to active and reboot")
err = s.ChangeBoot(sut.Active)
Expect(err).ToNot(HaveOccurred())

ExpectWithOffset(1, s.BootFrom()).To(Equal(sut.Recovery))
By("rebooting back to active")
s.Reboot()
ExpectWithOffset(1, s.BootFrom()).To(Equal(sut.Active))
})
Expand Down
5 changes: 2 additions & 3 deletions tests/smoke/smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var _ = Describe("cOS Smoke tests", func() {

Context("After install", func() {
It("can boot into passive", func() {
err := s.ChangeBoot(sut.Passive)
err := s.ChangeBootOnce(sut.Passive)
Expect(err).ToNot(HaveOccurred())

By("rebooting into passive")
Expand All @@ -25,8 +25,7 @@ var _ = Describe("cOS Smoke tests", func() {
_, err = s.Command("cat /run/cos/recovery_mode")
Expect(err).To(HaveOccurred())

By("switching back to active")
s.ChangeBoot(sut.Active)
By("reboot back to active")
s.Reboot()
Expect(s.BootFrom()).To(Equal(sut.Active))
})
Expand Down
68 changes: 36 additions & 32 deletions tests/sut/sut.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,15 @@ import (
"strings"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
ssh "golang.org/x/crypto/ssh"
)

const (
grubSwap = `dev=$(blkid -L COS_STATE); \
mount -o rw,remount $dev && \
mount $dev /boot/grub2 && \
sed -i 's/set default=.*/set default=%s/' /boot/grub2/grub2/grub.cfg && \
sync`

grubSwapRecovery = `
dev=$(blkid -L COS_STATE); mkdir /run/state; \
mount $dev /run/state && \
sed -i 's/set default=.*/set default=%s/' /run/state/grub2/grub.cfg
`
grubSwapOnce = "grub2-editenv /oem/grubenv set next_entry=%s"
grubSwap = "grub2-editenv /oem/grubenv set saved_entry=%s"

Passive = 0
Active = iota
Expand Down Expand Up @@ -75,35 +67,49 @@ func (s *SUT) ChangeBoot(b int) error {
bootEntry = "recovery"
}

if s.BootFrom() == Recovery {
_, err := s.command(fmt.Sprintf(grubSwapRecovery, bootEntry), false)
Expect(err).ToNot(HaveOccurred())
} else {
_, err := s.command(fmt.Sprintf(grubSwap, bootEntry), false)
Expect(err).ToNot(HaveOccurred())
_, err := s.command(fmt.Sprintf(grubSwap, bootEntry), false)
Expect(err).ToNot(HaveOccurred())

return nil
}

func (s *SUT) ChangeBootOnce(b int) error {

var bootEntry string

switch b {
case Active:
bootEntry = "cos"
case Passive:
bootEntry = "fallback"
case Recovery:
bootEntry = "recovery"
}

_, err := s.command(fmt.Sprintf(grubSwapOnce, bootEntry), false)
Expect(err).ToNot(HaveOccurred())

return nil
}

// Reset runs reboots cOS into Recovery and runs cos-reset.
// It will boot back the system from the Active partition afterwards
func (s *SUT) Reset() {
err := s.ChangeBoot(Recovery)
Expect(err).ToNot(HaveOccurred())

s.Reboot()
if s.BootFrom() != Recovery {
By("Reboot to recovery before reset")
err := s.ChangeBootOnce(Recovery)
Expect(err).ToNot(HaveOccurred())
s.Reboot()
Expect(s.BootFrom()).To(Equal(Recovery))
}

Expect(s.BootFrom()).To(Equal(Recovery))
By("Running cos-reset")
out, err := s.command("cos-reset", false)
Expect(err).ToNot(HaveOccurred())
Expect(out).Should(ContainSubstring("Installing"))

err = s.ChangeBoot(Active)
Expect(err).ToNot(HaveOccurred())

By("Reboot to active after cos-reset")
s.Reboot()

ExpectWithOffset(1, s.BootFrom()).To(Equal(Active))
}

Expand All @@ -129,7 +135,7 @@ func (s *SUT) SquashFSRecovery() bool {
out, err := s.command("cat /proc/cmdline", false)
ExpectWithOffset(1, err).ToNot(HaveOccurred())

return strings.Contains(out,"rd.live.squashimg")
return strings.Contains(out, "rd.live.squashimg")
}

func (s *SUT) GetOSRelease(ss string) string {
Expand Down Expand Up @@ -204,10 +210,10 @@ func (s *SUT) connectToHost(timeout bool) (*ssh.Client, error) {
}

// GatherLog will try to scp the given log from the machine to a local file
func (s SUT) GatherLog(logPath string) {
func (s SUT) GatherLog(logPath string) {
fmt.Printf("Trying to get file: %s\n", logPath)
clientConfig, _ := auth.PasswordKey(s.Username, s.Password, ssh.InsecureIgnoreHostKey())
scpClient := scp.NewClientWithTimeout(s.Host, &clientConfig, 10 * time.Second)
scpClient := scp.NewClientWithTimeout(s.Host, &clientConfig, 10*time.Second)

err := scpClient.Connect()
if err != nil {
Expand All @@ -226,7 +232,6 @@ func (s SUT) GatherLog(logPath string) {
defer scpClient.Close()
defer f.Close()


err = scpClient.CopyFromRemote(f, logPath)

if err != nil {
Expand All @@ -237,7 +242,6 @@ func (s SUT) GatherLog(logPath string) {
_ = os.Chmod(fmt.Sprintf("logs/%s", baseName), 0666)
fmt.Printf("File %s copied!\n", baseName)


}

// DialWithDeadline Dials SSH with a deadline to avoid Read timeouts
Expand Down Expand Up @@ -270,4 +274,4 @@ func DialWithDeadline(network string, addr string, config *ssh.ClientConfig, tim
}
}()
return ssh.NewClient(c, chans, reqs), nil
}
}

0 comments on commit 407149f

Please sign in to comment.