Skip to content

Commit

Permalink
tests/ignition: Use ignition config to create a second bootfs
Browse files Browse the repository at this point in the history
Co-authored-by: JB Trystram <[email protected]>
  • Loading branch information
c4rt0 and jbtrystram committed Nov 14, 2023
1 parent 370113e commit bd33b17
Showing 1 changed file with 148 additions and 8 deletions.
156 changes: 148 additions & 8 deletions mantle/kola/tests/ignition/qemufailure.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ import (
"github.com/coreos/coreos-assembler/mantle/kola/register"
"github.com/coreos/coreos-assembler/mantle/platform"
"github.com/coreos/coreos-assembler/mantle/platform/conf"
"github.com/coreos/ignition/v2/config/v3_2/types"
"github.com/coreos/coreos-assembler/mantle/util"
)

var console bool

func init() {
register.RegisterTest(&register.Test{
Name: "coreos.ignition.failure",
Expand All @@ -45,7 +49,14 @@ func init() {
ClusterSize: 0,
Description: "Verify boot fails if there are pre-existing boot filesystems.",
Platforms: []string{"qemu"},
Run: runBootfsFailure,
Run: runDualBootfsFailure,
})
register.RegisterTest(&register.Test{
Name: "coreos.unique.ignition.failure",
ClusterSize: 0,
Description: "Verify boot fails if there are pre-existing boot filesystems created with ignition.",
Platforms: []string{"qemu"},
Run: runDualBootfsIgnitionFailure,
})
}

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

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

func runDualBootfsIgnitionFailure(c cluster.TestCluster) {
if err := dualBootfsIgnitionFailure(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 All @@ -70,12 +87,7 @@ func ignitionFailure(c cluster.TestCluster) error {
}
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,
})
builder := platform.NewQemuBuilder() // platform.Manhole()
if err != nil {
return err
}
Expand Down Expand Up @@ -163,6 +175,134 @@ func dualBootfsFailure(c cluster.TestCluster) error {
builder.MemoryMiB = 1024
builder.Firmware = kola.QEMUOptions.Firmware
inst, err := builder.Exec()
if err != nil {
return err
} // platform.Manhole()
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
}
}

// Use ignition config to create a second bootfs
func dualBootfsIgnitionFailure(c cluster.TestCluster) error {
builder := platform.NewQemuBuilder()
// inserting log file for troubleshooting
// file located in coreos-assembler directory
builder.InheritConsole = console
if !console {
builder.ConsoleFile = "console.txt"
}
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

Check failure on line 251 in mantle/kola/tests/ignition/qemufailure.go

View workflow job for this annotation

GitHub Actions / golangci-lint

const `oneGB` is unused (unused)
if err != nil {
return err
}

// 1 - create and mount an empty drive to the VM - done
// 2 - produce an ignition file that format "a" disk with a"boot" label. done
// 3 - boot the VM with the ignition file - done
// 4 - observe the failure - done

failConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings)
if err != nil {
return errors.Wrapf(err, "creating empty config")
}

// Craft an ingniton file that format a partition
formaterConfig := types.Config {
Ignition: types.Ignition{
Version: "3.2.0",
},
Storage: types.Storage {
Filesystems: []types.Filesystem{
{
Device: "/dev/disk/by-id/virtio-extra-boot",
Label: util.StrToPtr("bootF"),
Format: util.StrToPtr("ext-4"),
},
},
},
}

failConfig.MergeV32(formaterConfig)

if !failConfig.ValidConfig() {
return errors.Wrapf(err, "invalid formater config")
}

builder.SetConfig(failConfig)
err = builder.AddBootDisk(&platform.Disk{
BackingFile: kola.QEMUOptions.DiskImage,
})
if err != nil {
return err
}
err = builder.AddDisk(&platform.Disk{
BackingFile: fakeBootFile.Name(),
BackingFormat: "raw",
DeviceOpts: []string{"serial=extra-boot"},
})
if err != nil {
return err
}
builder.MemoryMiB = 1024
builder.Firmware = kola.QEMUOptions.Firmware
inst, err := builder.Exec()
if err != nil {
return err
}
Expand Down

0 comments on commit bd33b17

Please sign in to comment.