mirror of
https://github.com/netbirdio/netbird.git
synced 2026-09-23 07:09:08 +02:00
Tests that need root or mutate host state (nftables/iptables/DNS, TUN/WireGuard interfaces, routes, eBPF, SSH/service install) are now gated behind a //go:build privileged tag. The default `go test ./client/...` runs as a non-root user with no sudo and leaves host networking untouched; mixed files were split so pure-logic tests stay in the default suite. A self-hosting ory/dockertest/v4 harness (client/testutil/privileged) runs the privileged suite inside a --privileged --cap-add=NET_ADMIN container via `make test-privileged`; a DOCKER_CI=true guard skips the spawn when already inside the container. Added `make test-unit` for the host-safe run.
70 lines
1.3 KiB
Go
70 lines
1.3 KiB
Go
//go:build linux && !android
|
|
|
|
package systemops
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestEntryExists(t *testing.T) {
|
|
tempDir := t.TempDir()
|
|
tempFilePath := fmt.Sprintf("%s/rt_tables", tempDir)
|
|
|
|
content := []string{
|
|
"1000 reserved",
|
|
fmt.Sprintf("%d %s", NetbirdVPNTableID, NetbirdVPNTableName),
|
|
"9999 other_table",
|
|
}
|
|
require.NoError(t, os.WriteFile(tempFilePath, []byte(strings.Join(content, "\n")), 0644))
|
|
|
|
file, err := os.Open(tempFilePath)
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
assert.NoError(t, file.Close())
|
|
}()
|
|
|
|
tests := []struct {
|
|
name string
|
|
id int
|
|
shouldExist bool
|
|
err error
|
|
}{
|
|
{
|
|
name: "ExistsWithNetbirdPrefix",
|
|
id: 7120,
|
|
shouldExist: true,
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "ExistsWithDifferentName",
|
|
id: 1000,
|
|
shouldExist: true,
|
|
err: ErrTableIDExists,
|
|
},
|
|
{
|
|
name: "DoesNotExist",
|
|
id: 1234,
|
|
shouldExist: false,
|
|
err: nil,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
exists, err := entryExists(file, tc.id)
|
|
if tc.err != nil {
|
|
assert.ErrorIs(t, err, tc.err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
assert.Equal(t, tc.shouldExist, exists)
|
|
})
|
|
}
|
|
}
|