diff --git a/tests/recovery/recovery_test.go b/tests/recovery/recovery_test.go index d62fa959414..335dcacc484 100644 --- a/tests/recovery/recovery_test.go +++ b/tests/recovery/recovery_test.go @@ -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() @@ -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)) }) @@ -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") @@ -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() @@ -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)) }) diff --git a/tests/smoke/smoke_test.go b/tests/smoke/smoke_test.go index 5d6ad383471..7401539fbef 100644 --- a/tests/smoke/smoke_test.go +++ b/tests/smoke/smoke_test.go @@ -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") @@ -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)) }) diff --git a/tests/sut/sut.go b/tests/sut/sut.go index d185fa02135..2353ea8f0df 100644 --- a/tests/sut/sut.go +++ b/tests/sut/sut.go @@ -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 @@ -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)) } @@ -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 { @@ -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 { @@ -226,7 +232,6 @@ func (s SUT) GatherLog(logPath string) { defer scpClient.Close() defer f.Close() - err = scpClient.CopyFromRemote(f, logPath) if err != nil { @@ -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 @@ -270,4 +274,4 @@ func DialWithDeadline(network string, addr string, config *ssh.ClientConfig, tim } }() return ssh.NewClient(c, chans, reqs), nil -} \ No newline at end of file +}