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 Jun 13, 2024
1 parent 32221a9 commit 334361d
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion tests/blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"testing"
"time"

"github.com/coreos/ignition/v2/config"
"github.com/coreos/ignition/v2/internal/exec/util"
"github.com/coreos/ignition/v2/tests/register"
"github.com/coreos/ignition/v2/tests/servers"
"github.com/coreos/ignition/v2/tests/types"
Expand Down Expand Up @@ -259,13 +261,20 @@ 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() {
for _, luks := range renderedConfig.Storage.Luks {
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 +356,19 @@ 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 {
deviceExists, err := util.PathExists(fmt.Sprintf("/dev/mapper/%s", deviceName))
if err != nil {
return fmt.Errorf("failed to check if device exists at %s: %v", deviceName, err)
}
if deviceExists {
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
}

0 comments on commit 334361d

Please sign in to comment.