bazel: migrate all integration tests (and retire CMakeLists.txt)

This commit is contained in:
Malte Poll 2023-12-05 11:19:18 +01:00
parent a87fd7607f
commit e113253262
7 changed files with 142 additions and 33 deletions

View File

@ -29,23 +29,6 @@ jobs:
with: with:
ref: ${{ !github.event.pull_request.head.repo.fork && github.head_ref || '' }} ref: ${{ !github.event.pull_request.head.repo.fork && github.head_ref || '' }}
- name: Setup Go environment
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: "1.21.5"
cache: true
- name: Install Dependencies
run: sudo apt-get update && sudo apt-get install -y libcryptsetup12 libcryptsetup-dev
- name: Create and populate build folder
run: mkdir build && cd build && cmake ..
# Runs all test targets starting with "integration-"
- name: CMake-based Integration Tests
working-directory: build
run: ctest -R integration-
- name: Setup bazel - name: Setup bazel
uses: ./.github/actions/setup_bazel_nix uses: ./.github/actions/setup_bazel_nix
with: with:
@ -55,4 +38,4 @@ jobs:
- name: Integration Tests - name: Integration Tests
env: env:
TMPDIR: ${{ runner.temp }} TMPDIR: ${{ runner.temp }}
run: bazel test //... --test_output=errors --config=nostamp --config=integration-only --remote_download_minimal run: sudo -E "PATH=$PATH" bazel test //... --config=nostamp --remote_download_minimal --config=integration --spawn_strategy=standalone

View File

@ -1,8 +0,0 @@
cmake_minimum_required(VERSION 3.11)
project(constellation LANGUAGES C)
enable_testing()
# TODO(malt3): Remove this once every integration test is migrated to Bazel
add_test(NAME integration-csi COMMAND bash -c "go test -tags integration -c ./test/ && sudo ./test.test -test.v" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/csi)
add_test(NAME integration-dm COMMAND bash -c "go test -tags integration -c ./test/ && sudo ./test.test -test.v" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/disk-mapper/internal)

View File

@ -76,6 +76,21 @@ nixpkgs_package(
repository = "@nixpkgs", repository = "@nixpkgs",
) )
nixpkgs_package(
name = "util-linux",
repository = "@nixpkgs",
)
nixpkgs_package(
name = "coreutils",
repository = "@nixpkgs",
)
nixpkgs_package(
name = "e2fsprogs",
repository = "@nixpkgs",
)
load("//nix/cc:nixpkgs_cc_libraries.bzl", "nixpkgs_cc_library_deps") load("//nix/cc:nixpkgs_cc_libraries.bzl", "nixpkgs_cc_library_deps")
nixpkgs_cc_library_deps() nixpkgs_cc_library_deps()

View File

@ -3,19 +3,51 @@ load("//bazel/go:go_test.bzl", "go_test")
go_test( go_test(
name = "test_test", name = "test_test",
srcs = ["mount_integration_test.go"], srcs = ["mount_integration_test.go"],
count = 1,
# tool dependencies come from the test code itself (dd, rm, cp)
# and from github.com/kubernetes/mount-utils/mount_linux.go
data = [
"@coreutils//:bin/cp",
"@coreutils//:bin/dd",
"@coreutils//:bin/rm",
"@e2fsprogs//:bin/fsck.ext4",
"@e2fsprogs//:bin/mkfs.ext4",
"@util-linux//:bin/blkid",
"@util-linux//:bin/fsck",
"@util-linux//:bin/mount",
"@util-linux//:bin/umount",
],
env = {
"BLKID": "$(rlocationpath @util-linux//:bin/blkid)",
"CP": "$(rlocationpath @coreutils//:bin/cp)",
"DD": "$(rlocationpath @coreutils//:bin/dd)",
"FSCK": "$(rlocationpath @util-linux//:bin/fsck)",
"FSCK_EXT4": "$(rlocationpath @e2fsprogs//:bin/fsck.ext4)",
"MKFS_EXT4": "$(rlocationpath @e2fsprogs//:bin/mkfs.ext4)",
"MOUNT": "$(rlocationpath @util-linux//:bin/mount)",
"RM": "$(rlocationpath @coreutils//:bin/rm)",
"UMOUNT": "$(rlocationpath @util-linux//:bin/umount)",
},
# keep # keep
tags = ["manual"], tags = [
"integration",
"local",
"no-sandbox",
],
target_compatible_with = ["@platforms//os:linux"],
deps = select({ deps = select({
"@io_bazel_rules_go//go/platform:android": [ "@io_bazel_rules_go//go/platform:android": [
"//csi/cryptmapper", "//csi/cryptmapper",
"@com_github_stretchr_testify//assert", "@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require", "@com_github_stretchr_testify//require",
"@io_bazel_rules_go//go/runfiles:go_default_library",
"@org_uber_go_goleak//:goleak", "@org_uber_go_goleak//:goleak",
], ],
"@io_bazel_rules_go//go/platform:linux": [ "@io_bazel_rules_go//go/platform:linux": [
"//csi/cryptmapper", "//csi/cryptmapper",
"@com_github_stretchr_testify//assert", "@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require", "@com_github_stretchr_testify//require",
"@io_bazel_rules_go//go/runfiles:go_default_library",
"@org_uber_go_goleak//:goleak", "@org_uber_go_goleak//:goleak",
], ],
"//conditions:default": [], "//conditions:default": [],

View File

@ -13,9 +13,13 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strings"
"sync" "sync"
"syscall"
"testing" "testing"
"github.com/bazelbuild/rules_go/go/runfiles"
"github.com/edgelesssys/constellation/v2/csi/cryptmapper" "github.com/edgelesssys/constellation/v2/csi/cryptmapper"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -27,14 +31,40 @@ const (
deviceName string = "testDeviceName" deviceName string = "testDeviceName"
) )
var toolsEnvs []string = []string{"CP", "DD", "RM", "FSCK_EXT4", "MKFS_EXT4", "BLKID", "FSCK", "MOUNT", "UMOUNT"}
// addToolsToPATH is used to update the PATH to contain necessary tool binaries for
// coreutils, util-linux and ext4.
func addToolsToPATH() error {
path := ":" + os.Getenv("PATH") + ":"
for _, tool := range toolsEnvs {
toolPath := os.Getenv(tool)
if toolPath == "" {
continue
}
toolPath, err := runfiles.Rlocation(toolPath)
if err != nil {
return err
}
pathComponent := filepath.Dir(toolPath)
if strings.Contains(path, ":"+pathComponent+":") {
continue
}
path = ":" + pathComponent + path
}
path = strings.Trim(path, ":")
os.Setenv("PATH", path)
return nil
}
func setup(devicePath string) { func setup(devicePath string) {
if err := exec.Command("/bin/dd", "if=/dev/zero", fmt.Sprintf("of=%s", devicePath), "bs=64M", "count=1").Run(); err != nil { if err := exec.Command("dd", "if=/dev/zero", fmt.Sprintf("of=%s", devicePath), "bs=64M", "count=1").Run(); err != nil {
panic(err) panic(err)
} }
} }
func teardown(devicePath string) { func teardown(devicePath string) {
if err := exec.Command("/bin/rm", "-f", devicePath).Run(); err != nil { if err := exec.Command("rm", "-f", devicePath).Run(); err != nil {
panic(err) panic(err)
} }
} }
@ -44,16 +74,22 @@ func cp(source, target string) error {
} }
func resize(devicePath string) { func resize(devicePath string) {
if err := exec.Command("/bin/dd", "if=/dev/zero", fmt.Sprintf("of=%s", devicePath), "bs=32M", "count=1", "oflag=append", "conv=notrunc").Run(); err != nil { if err := exec.Command("dd", "if=/dev/zero", fmt.Sprintf("of=%s", devicePath), "bs=32M", "count=1", "oflag=append", "conv=notrunc").Run(); err != nil {
panic(err) panic(err)
} }
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
// try to become root (best effort)
_ = syscall.Setuid(0)
if os.Getuid() != 0 { if os.Getuid() != 0 {
fmt.Printf("This test suite requires root privileges, as libcryptsetup uses the kernel's device mapper.\n") fmt.Printf("This test suite requires root privileges, as libcryptsetup uses the kernel's device mapper.\n")
os.Exit(1) os.Exit(1)
} }
if err := addToolsToPATH(); err != nil {
fmt.Printf("Failed to add tools to PATH: %v\n", err)
os.Exit(1)
}
goleak.VerifyTestMain(m) goleak.VerifyTestMain(m)

View File

@ -6,8 +6,21 @@ go_test(
"benchmark_test.go", "benchmark_test.go",
"integration_test.go", "integration_test.go",
], ],
data = [
"@coreutils//:bin/dd",
"@coreutils//:bin/rm",
],
env = {
"DD": "$(rlocationpath @coreutils//:bin/dd)",
"RM": "$(rlocationpath @coreutils//:bin/rm)",
},
# keep # keep
tags = ["manual"], tags = [
"integration",
"local",
"no-sandbox",
],
target_compatible_with = ["@platforms//os:linux"],
deps = select({ deps = select({
"@io_bazel_rules_go//go/platform:android": [ "@io_bazel_rules_go//go/platform:android": [
"//disk-mapper/internal/diskencryption", "//disk-mapper/internal/diskencryption",
@ -16,6 +29,7 @@ go_test(
"@com_github_martinjungblut_go_cryptsetup//:go-cryptsetup", "@com_github_martinjungblut_go_cryptsetup//:go-cryptsetup",
"@com_github_stretchr_testify//assert", "@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require", "@com_github_stretchr_testify//require",
"@io_bazel_rules_go//go/runfiles:go_default_library",
"@org_uber_go_goleak//:goleak", "@org_uber_go_goleak//:goleak",
"@org_uber_go_zap//zapcore", "@org_uber_go_zap//zapcore",
], ],
@ -26,6 +40,7 @@ go_test(
"@com_github_martinjungblut_go_cryptsetup//:go-cryptsetup", "@com_github_martinjungblut_go_cryptsetup//:go-cryptsetup",
"@com_github_stretchr_testify//assert", "@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require", "@com_github_stretchr_testify//require",
"@io_bazel_rules_go//go/runfiles:go_default_library",
"@org_uber_go_goleak//:goleak", "@org_uber_go_goleak//:goleak",
"@org_uber_go_zap//zapcore", "@org_uber_go_zap//zapcore",
], ],

View File

@ -14,8 +14,12 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strings"
"syscall"
"testing" "testing"
"github.com/bazelbuild/rules_go/go/runfiles"
"github.com/edgelesssys/constellation/v2/disk-mapper/internal/diskencryption" "github.com/edgelesssys/constellation/v2/disk-mapper/internal/diskencryption"
ccryptsetup "github.com/edgelesssys/constellation/v2/internal/cryptsetup" ccryptsetup "github.com/edgelesssys/constellation/v2/internal/cryptsetup"
"github.com/edgelesssys/constellation/v2/internal/logger" "github.com/edgelesssys/constellation/v2/internal/logger"
@ -32,21 +36,53 @@ const (
var diskPath = flag.String("disk", "", "Path to the disk to use for the benchmark") var diskPath = flag.String("disk", "", "Path to the disk to use for the benchmark")
var toolsEnvs []string = []string{"DD", "RM"}
// addToolsToPATH is used to update the PATH to contain necessary tool binaries for
// coreutils.
func addToolsToPATH() error {
path := ":" + os.Getenv("PATH") + ":"
for _, tool := range toolsEnvs {
toolPath := os.Getenv(tool)
if toolPath == "" {
continue
}
toolPath, err := runfiles.Rlocation(toolPath)
if err != nil {
return err
}
pathComponent := filepath.Dir(toolPath)
if strings.Contains(path, ":"+pathComponent+":") {
continue
}
path = ":" + pathComponent + path
}
path = strings.Trim(path, ":")
os.Setenv("PATH", path)
return nil
}
func setup(sizeGB int) error { func setup(sizeGB int) error {
return exec.Command("/bin/dd", "if=/dev/random", fmt.Sprintf("of=%s", devicePath), "bs=1G", fmt.Sprintf("count=%d", sizeGB)).Run() return exec.Command("dd", "if=/dev/random", fmt.Sprintf("of=%s", devicePath), "bs=1G", fmt.Sprintf("count=%d", sizeGB)).Run()
} }
func teardown() error { func teardown() error {
return exec.Command("/bin/rm", "-f", devicePath).Run() return exec.Command("rm", "-f", devicePath).Run()
} }
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
flag.Parse() flag.Parse()
// try to become root (best effort)
_ = syscall.Setuid(0)
if os.Getuid() != 0 { if os.Getuid() != 0 {
fmt.Printf("This test suite requires root privileges, as libcrypsetup uses the kernel's device mapper.\n") fmt.Printf("This test suite requires root privileges, as libcrypsetup uses the kernel's device mapper.\n")
os.Exit(1) os.Exit(1)
} }
if err := addToolsToPATH(); err != nil {
fmt.Printf("Failed to add tools to PATH: %v\n", err)
os.Exit(1)
}
goleak.VerifyTestMain(m, goleak.VerifyTestMain(m,
// https://github.com/census-instrumentation/opencensus-go/issues/1262 // https://github.com/census-instrumentation/opencensus-go/issues/1262