From 16d6714269fa85d26a7b6e44eb3dfddaf98f3fb6 Mon Sep 17 00:00:00 2001 From: Adam Piasecki Date: Wed, 18 Oct 2023 12:48:41 +0100 Subject: [PATCH] tests/ignition: Modify qemufailure to include bootfs test See: https://github.com/coreos/coreos-assembler/issues/2953 --- mantle/kola/tests/ignition/qemufailure.go | 81 ++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/mantle/kola/tests/ignition/qemufailure.go b/mantle/kola/tests/ignition/qemufailure.go index 681f2295a2..ced2320ce7 100644 --- a/mantle/kola/tests/ignition/qemufailure.go +++ b/mantle/kola/tests/ignition/qemufailure.go @@ -17,6 +17,8 @@ package ignition import ( "context" "fmt" + "os" + "os/exec" "time" "github.com/pkg/errors" @@ -32,19 +34,30 @@ func init() { register.RegisterTest(®ister.Test{ Name: "coreos.ignition.failure", Description: "Verify ignition will fail with unsupported action.", - Run: runIgnitionFailure, + Run: runIgnitionTestGroup, ClusterSize: 0, Platforms: []string{"qemu"}, Tags: []string{"ignition"}, }) } +func runIgnitionTestGroup(c cluster.TestCluster) { + c.Run("ignition", runIgnitionFailure) + c.Run("bootfs", runBootfsFailure) +} + func runIgnitionFailure(c cluster.TestCluster) { if err := ignitionFailure(c); err != nil { c.Fatal(err.Error()) } } +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. @@ -101,3 +114,69 @@ func ignitionFailure(c cluster.TestCluster) error { return nil } } + +func bootfsFailure(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. + failConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings) + if err != nil { + return errors.Wrapf(err, "creating empty config") + } + cmd := exec.Command("/bin/bash", "-c", fmt.Sprintf(`set -euo pipefail; + truncate -s 1G fakeboot + mkfs.ext4 -L boot fakeboot + `)) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + c.Fatal(err) + } + // failConfig.AddFile("/notwritable.txt", "Hello world", 0644) + + builder := platform.NewQemuBuilder() + defer builder.Close() + builder.SetConfig(failConfig) + 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 + } +}