summaryrefslogtreecommitdiff
path: root/tests/e2e/test_helpers.go
blob: 93e851c15d3e3be69e5112e3b353bed509abf8d5 (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
package e2e

import (
	"fmt"
	"os"
	"strings"
	"testing"
)

// GetBinaryPath resolves the path to the wg-wrap binary.
// It prioritizes the WG_WRAP_BIN environment variable.
func GetBinaryPath() (string, error) {
	path := os.Getenv("WG_WRAP_BIN")
	if path == "" {
		return "", fmt.Errorf("WG_WRAP_BIN environment variable not set")
	}

	if _, err := os.Stat(path); err != nil {
		return "", fmt.Errorf("binary not found at path %s: %w", path, err)
	}

	return path, nil
}

// EnsureBinary returns the path to the wg-wrap binary or skips the test if it's not available.
func EnsureBinary(t *testing.T) string {
	t.Helper()
	bin, err := GetBinaryPath()
	if err != nil {
		t.Skipf("skipping test: %v", err)
	}
	return bin
}

// SetEnvOverrides returns a new slice of environment variables with the provided overrides applied.
// It ensures that overriding an existing variable replaces it rather than appending it.
func SetEnvOverrides(overrides map[string]string) []string {
	env := os.Environ()
	newEnv := make([]string, 0, len(env)+len(overrides))

	for _, e := range env {
		matched := false
		for k := range overrides {
			if strings.HasPrefix(e, k+"=") {
				matched = true
				break
			}
		}
		if !matched {
			newEnv = append(newEnv, e)
		}
	}

	for k, v := range overrides {
		newEnv = append(newEnv, fmt.Sprintf("%s=%s", k, v))
	}

	return newEnv
}