Skip to content

Commit

Permalink
blackbox_test: add support for luks tests
Browse files Browse the repository at this point in the history
Until now, the blackbox tests would not correctly cleanup after
running a test with a luks device. Now Luks devices can be tested
using blackbox test framework.
  • Loading branch information
prestist committed May 28, 2024
1 parent 32221a9 commit c419c64
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
23 changes: 22 additions & 1 deletion tests/blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
Expand Down Expand Up @@ -150,6 +151,7 @@ func outer(t *testing.T, test types.Test, negativeTests bool) error {
if err != nil {
return err
}

for i, disk := range test.In {
// Set image file path
disk.ImageFile = filepath.Join(tmpDirectory, fmt.Sprintf("hd%d", i))
Expand Down Expand Up @@ -259,13 +261,23 @@ func outer(t *testing.T, test types.Test, negativeTests bool) error {
// If we're not expecting the config to be bad, make sure it passes
// validation.
if !test.ConfigShouldBeBad {
_, rpt, err := config.Parse([]byte(test.Config))
renderedConfig, rpt, err := config.Parse([]byte(test.Config))
if rpt.IsFatal() {
return fmt.Errorf("test has bad config: %s", rpt.String())
}
if err != nil {
return fmt.Errorf("error parsing config: %v", err)
}
defer func() {
if test.ConfigVersion != "3.1.0" && test.ConfigVersion != "3.0.0" && test.Config != "" {
for _, luks := range renderedConfig.Storage.Luks {
t.Log(luks.Name)
if err := removeLuksDevice(luks.Name); err != nil {
t.Error(fmt.Errorf("failed to remove existing LUKS device %s: %v", luks.Name, err))
}
}
}
}()
}

// Ignition config
Expand Down Expand Up @@ -347,3 +359,12 @@ func outer(t *testing.T, test types.Test, negativeTests bool) error {
return fmt.Errorf("Expected failure and ignition succeeded")
}
}

// Remove a LUKS device
func removeLuksDevice(deviceName string) error {
cmd := exec.Command("sudo", "cryptsetup", "luksClose", deviceName)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to remove LUKS device %s: %v", deviceName, err)
}
return nil
}
9 changes: 9 additions & 0 deletions tests/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func (test *Test) ReplaceAllUUIDVars() error {
UUIDmap := make(map[string]string)

test.Config, err = replaceUUIDVars(test.Config, UUIDmap)
test.replaceUUIDVars()
if err != nil {
return err
}
Expand Down Expand Up @@ -259,6 +260,14 @@ func replaceUUIDVars(str string, UUIDmap map[string]string) (string, error) {
return finalStr, nil
}

// Replaces any $uuid vars with a new uuid.
func (t *Test) replaceUUIDVars() {
for strings.Contains(t.Config, "$UUID") {
newUUID := uuid.New()
t.Config = strings.Replace(t.Config, "$UUID", newUUID.String(), 1)
}
}

// Format: $uuid<num> where the uuid variable (uuid<num>) is the key
// value is the UUID for this uuid variable
func getUUID(key string, UUIDmap map[string]string) string {
Expand Down

0 comments on commit c419c64

Please sign in to comment.