summaryrefslogtreecommitdiff
path: root/integrationtests/helpers_test.go
blob: 6ef7ba72a6eedc89882f3e21ded84ef9bbc5253c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package integrationtests

import (
	"os"
	"path/filepath"
	"testing"
)

const (
	iorBinaryDefault      = "../ior"
	workloadBinaryDefault = "../ioworkload"
	defaultDuration       = 10
	parallelEnvVar        = "IOR_INTEGRATION_PARALLEL"
)

func newTestHarness(t *testing.T) TestHarness {
	t.Helper()
	if os.Geteuid() != 0 {
		t.Skip("requires root for BPF")
	}

	return TestHarness{
		IorBinary:      absPath(t, iorBinaryDefault),
		WorkloadBinary: absPath(t, workloadBinaryDefault),
		OutputDir:      t.TempDir(),
	}
}

func absPath(t *testing.T, rel string) string {
	t.Helper()
	p, err := filepath.Abs(rel)
	if err != nil {
		t.Fatalf("resolve path %s: %v", rel, err)
	}
	return p
}

// writeScript creates an executable shell script in dir and returns its path.
func writeScript(t *testing.T, dir, name, content string) string {
	t.Helper()
	path := filepath.Join(dir, name)
	if err := os.WriteFile(path, []byte("#!/bin/sh\n"+content+"\n"), 0o755); err != nil {
		t.Fatalf("write script %s: %v", name, err)
	}
	return path
}

func runScenario(t *testing.T, scenario string, expected []ExpectedEvent) {
	t.Helper()
	runScenarioResult(t, scenario, expected)
}

func runScenarioResult(t *testing.T, scenario string, expected []ExpectedEvent) (TestResult, int) {
	t.Helper()
	enableParallelIfRequested(t)
	h := newTestHarness(t)
	result, pid, err := h.Run(scenario, defaultDuration)
	if err != nil {
		t.Fatalf("run scenario %s: %v", scenario, err)
	}

	AssertNoUnexpectedPID(t, result, pid)
	AssertNoUnexpectedComm(t, result, "ioworkload")
	AssertEventsPresent(t, result, expected)
	return result, pid
}

func enableParallelIfRequested(t *testing.T) {
	t.Helper()
	if os.Getenv(parallelEnvVar) == "1" {
		t.Parallel()
	}
}