Skip to content

Commit

Permalink
tests/ignition: Modify qemufailure to include bootfs test
Browse files Browse the repository at this point in the history
See: #2953
  • Loading branch information
c4rt0 committed Nov 10, 2023
1 parent 9b0e7e8 commit 370113e
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions mantle/kola/tests/ignition/qemufailure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package ignition
import (
"context"
"fmt"
"os"
"os/exec"
"regexp"
"time"

"github.com/pkg/errors"
Expand All @@ -37,6 +40,13 @@ func init() {
Platforms: []string{"qemu"},
Tags: []string{"ignition"},
})
register.RegisterTest(&register.Test{
Name: "coreos.unique.boot.failure",
ClusterSize: 0,
Description: "Verify boot fails if there are pre-existing boot filesystems.",
Platforms: []string{"qemu"},
Run: runBootfsFailure,
})
}

func runIgnitionFailure(c cluster.TestCluster) {
Expand All @@ -45,6 +55,12 @@ func runIgnitionFailure(c cluster.TestCluster) {
}
}

func runBootfsFailure(c cluster.TestCluster) {
if err := dualBootfsFailure(c); err != nil {
c.Fatal(err.Error())
}
}

func ignitionFailure(c cluster.TestCluster) error {
// We can't create files in / due to the immutable bit OSTree creates, so
// this is a convenient way to test Ignition failure.
Expand Down Expand Up @@ -101,3 +117,99 @@ func ignitionFailure(c cluster.TestCluster) error {
return nil
}
}

// Verify that there is only one boot filesystem attached to the device
func dualBootfsFailure(c cluster.TestCluster) error {
builder := platform.NewQemuBuilder()

consoleFile, err := builder.TempFile("console.log")
if err != nil {
return err
}
builder.ConsoleFile = consoleFile.Name()

// get current path and create tmp dir
fakeBootFile, err := builder.TempFile("fakeBoot")
if err != nil {
return err
}

// Truncate the file to 1 gigabyte
const oneGB = 1 << 30
err = fakeBootFile.Truncate(oneGB)
if err != nil {
return err
}

cmd := exec.Command("mkfs.ext4", "-L", "boot", fakeBootFile.Name())
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
c.Fatal(err)
}

err = builder.AddBootDisk(&platform.Disk{
BackingFile: kola.QEMUOptions.DiskImage,
})
if err != nil {
return err
}
err = builder.AddDisk(&platform.Disk{
BackingFile: fakeBootFile.Name(),
BackingFormat: "raw",
})
if err != nil {
return err
}
builder.MemoryMiB = 1024
builder.Firmware = kola.QEMUOptions.Firmware
inst, err := builder.Exec()
if err != nil {
return err
}
defer inst.Destroy()

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()

errchan := make(chan error)
go func() {
resultingError := inst.WaitAll(ctx)

if resultingError == nil {
resultingError = fmt.Errorf("bootfs unexpectedly succeeded")
} else if resultingError == platform.ErrInitramfsEmergency {
// Expectred initramfs failure, checking the console file to insure
// that coreos-unique-boot.service failed
b, err := os.ReadFile(builder.ConsoleFile)
if err != nil {
panic(err)
}
isExist, err := regexp.Match("Error: System has 2 devices with a filesystem labeled 'boot'", b)
if err != nil {
panic(err)
}
if isExist {
// The expected case
resultingError = nil
} else {
resultingError = errors.Wrapf(err, "expected coreos-unique-boot.service to fail")
}
} else {
resultingError = errors.Wrapf(err, "expected initramfs emergency.target error")
}
errchan <- resultingError
}()

select {
case <-ctx.Done():
if err := inst.Kill(); err != nil {
return errors.Wrapf(err, "failed to kill the vm instance")
}
return errors.Wrapf(ctx.Err(), "timed out waiting for initramfs error")
case err := <-errchan:
if err != nil {
return err
}
return nil
}
}

0 comments on commit 370113e

Please sign in to comment.