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 7, 2023
1 parent 9b0e7e8 commit 572d6f9
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions mantle/kola/tests/ignition/qemufailure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package ignition
import (
"context"
"fmt"
"os"
"os/exec"
"time"

"github.com/pkg/errors"
Expand All @@ -37,6 +39,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 +54,12 @@ func runIgnitionFailure(c cluster.TestCluster) {
}
}

func runBootfsFailure(c cluster.TestCluster) {
if err := bootfsFailure(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 +116,87 @@ func ignitionFailure(c cluster.TestCluster) error {
return nil
}
}

// Attach a block device which has a pre-existing boot filesystem on it
func bootfsFailure(c cluster.TestCluster) error {
builder := platform.NewQemuBuilder()
defer builder.Close()
// get current path and create tmp dir
fakeBoot := "fakeboot"
tempPath, err := builder.TempFile(fakeBoot)
if err != nil {
return err
}
pathName := tempPath.Name()
fmt.Printf("Temp path: %v\n", pathName)

createBoot, err := os.Create(pathName)
if err != nil {
return err
}

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

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

err = builder.AddDisk(&platform.Disk{
BackingFile: kola.QEMUOptions.DiskImage,
BackingFormat: "raw",
})
if err != nil {
return err
}
err = builder.AddBootDisk(&platform.Disk{
BackingFile: kola.QEMUOptions.DiskImage,
})

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() {
err := inst.WaitAll(ctx)
if err == nil {
err = fmt.Errorf("bootfs unexpectedly succeeded")
} else if err == platform.ErrInitramfsEmergency {
// The expected case
err = nil
} else {
err = errors.Wrapf(err, "expected initramfs emergency.target error")
}
errchan <- err
}()

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 572d6f9

Please sign in to comment.