Skip to content

Commit

Permalink
mantle/kola: refactor tests to use new VerifyError func
Browse files Browse the repository at this point in the history
  • Loading branch information
c4rt0 committed Nov 20, 2023
1 parent 14b0143 commit 9b89a62
Showing 1 changed file with 74 additions and 139 deletions.
213 changes: 74 additions & 139 deletions mantle/kola/tests/ignition/qemufailure.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ 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"
"github.com/coreos/ignition/v2/config/v3_2/types"
)

var console bool

func init() {
register.RegisterTest(&register.Test{
Name: "coreos.ignition.failure",
Expand Down Expand Up @@ -78,40 +76,42 @@ func runDualBootfsIgnitionFailure(c cluster.TestCluster) {
}
}

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.
failConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings)
if err != nil {
return errors.Wrapf(err, "creating empty config")
}
failConfig.AddFile("/notwritable.txt", "Hello world", 0644)

builder := platform.NewQemuBuilder() // platform.Manhole()
if err != nil {
return err
}
builder.MemoryMiB = 1024
builder.Firmware = kola.QEMUOptions.Firmware
// take string and grep for it in console logs
func VerifyError(builder *platform.QemuBuilder, tempLogFile string, searchString string) error {
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("Ignition unexpectedly succeeded")
} else if err == platform.ErrInitramfsEmergency {
// The expected case
err = nil
resultingError := inst.WaitAll(ctx)

if resultingError == nil {
resultingError = fmt.Errorf("Ignition unexpectedly succeeded")

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

View workflow job for this annotation

GitHub Actions / golangci-lint

ineffectual assignment to resultingError (ineffassign)
} else if resultingError == platform.ErrInitramfsEmergency {
// Expectred initramfs failure, checking the console file to insure
// that coreos.ignition.failure failed
b, err := os.ReadFile(tempLogFile)
if err != nil {
resultingError = err

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

View workflow job for this annotation

GitHub Actions / golangci-lint

ineffectual assignment to resultingError (ineffassign)
}
textExists, err := regexp.Match(searchString, b)
if err != nil {
resultingError = err

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

View workflow job for this annotation

GitHub Actions / golangci-lint

ineffectual assignment to resultingError (ineffassign)
}
if textExists {
// The expected case
resultingError = nil
} else {
resultingError = errors.Wrapf(err, "expected coreos.ignition.failure to fail")
}
} else {
err = errors.Wrapf(err, "expected initramfs emergency.target error")
resultingError = errors.Wrapf(err, "expected initramfs emergency.target error")
}
errchan <- err
}()
Expand All @@ -130,15 +130,44 @@ func ignitionFailure(c cluster.TestCluster) error {
}
}

// Verify that there is only one boot filesystem attached to the device
func dualBootfsFailure(c cluster.TestCluster) error {
builder := platform.NewQemuBuilder()
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.
failConfig, err := conf.EmptyIgnition().Render(conf.FailWarnings)
if err != nil {
return errors.Wrapf(err, "creating empty config")
}
failConfig.AddFile("/notwritable.txt", "Hello world", 0644)

consoleFile, err := builder.TempFile("console.log")
builder := platform.NewQemuBuilder() // platform.Manhole()

// Create a temporary log file
tempLogFile := platform.CreateTempLogFile(builder)

defer builder.Close()
builder.SetConfig(failConfig)
err = builder.AddBootDisk(&platform.Disk{
BackingFile: kola.QEMUOptions.DiskImage,
})
if err != nil {
return err
}
builder.ConsoleFile = consoleFile.Name()

builder.MemoryMiB = 1024
builder.Firmware = kola.QEMUOptions.Firmware

searchString := "/notwritable.txt"
if VerifyError(builder, tempLogFile, searchString) != nil {
return err
}
return nil
}

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

// get current path and create tmp dir
fakeBootFile, err := builder.TempFile("fakeBoot")
Expand Down Expand Up @@ -174,85 +203,35 @@ 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
searchString := "Error: System has 2 devices with a filesystem labeled 'boot'"
if VerifyError(builder, tempLogFile, searchString) != nil {
return err
}
return nil
}

// Use ignition config to create a second bootfs
// 1 - produce an ignition file that format a disk with a"boot" label.
// 2 - boot the VM with the ignition file and an extra disk.
// 3 - observe the failure
// 3 - observe the failure
func dualBootfsIgnitionFailure(c cluster.TestCluster) error {
builder := platform.NewQemuBuilder()
// inserting log file for troubleshooting
// file located in coreos-assembler directory

// consoleFile, err := builder.TempFile("console.log")
// if err != nil {
// return err
// }
//builder.ConsoleFile = consoleFile.Name()
builder.ConsoleFile = "console.txt"
// Create a temporary log file
tempLogFile := platform.CreateTempLogFile(builder)

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 {
// Craft an ingniton file that formats a partition
formaterConfig := types.Config{
Ignition: types.Ignition{
Version: "3.2.0",
},
Storage: types.Storage {
Storage: types.Storage{
Filesystems: []types.Filesystem{
{
Device: "/dev/disk/by-id/virtio-extra-boot",
Expand Down Expand Up @@ -281,54 +260,10 @@ func dualBootfsIgnitionFailure(c cluster.TestCluster) error {

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
searchString := "Error: System has 2 devices with a filesystem labeled 'boot'"
if VerifyError(builder, tempLogFile, searchString) != nil {
return err
}
return nil
}

0 comments on commit 9b89a62

Please sign in to comment.