-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[client] Categorize privileged tests behind a build tag and run them in Docker #6425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pappz
wants to merge
8
commits into
main
Choose a base branch
from
feature/privileged-test-tag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ddc4904
[client] categorize root/system-mutating tests behind a privileged bu…
pappz 9d21a2c
[client] add PRIV_RUN/PRIV_PKGS filters to the privileged test harness
pappz 07f3c75
[client] fix unused-helper lint after the privileged test split
pappz a89a5c5
[client] fix privileged test CI failures and run the harness on macOS
pappz 31beb96
Update client/internal/engine_privileged_test.go
pappz e926f39
Update client/internal/routemanager/systemops/systemops_linux_test.go
pappz 28bd68c
Update client/internal/routemanager/systemops/systemops_windows_test.go
pappz 0339328
Update client/server/server_privileged_test.go
pappz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| //go:build privileged | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/kardianos/service" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const ( | ||
| serviceStartTimeout = 10 * time.Second | ||
| serviceStopTimeout = 5 * time.Second | ||
| statusPollInterval = 500 * time.Millisecond | ||
| ) | ||
|
|
||
| // waitForServiceStatus waits for service to reach expected status with timeout | ||
| func waitForServiceStatus(expectedStatus service.Status, timeout time.Duration) (bool, error) { | ||
| cfg, err := newSVCConfig() | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| ctxSvc, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| s, err := newSVC(newProgram(ctxSvc, cancel), cfg) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| ctx, timeoutCancel := context.WithTimeout(context.Background(), timeout) | ||
| defer timeoutCancel() | ||
|
|
||
| ticker := time.NewTicker(statusPollInterval) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return false, fmt.Errorf("timeout waiting for service status %v", expectedStatus) | ||
| case <-ticker.C: | ||
| status, err := s.Status() | ||
| if err != nil { | ||
| // Continue polling on transient errors | ||
| continue | ||
| } | ||
| if status == expectedStatus { | ||
| return true, nil | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestServiceLifecycle tests the complete service lifecycle | ||
| func TestServiceLifecycle(t *testing.T) { | ||
| // TODO: Add support for Windows and macOS | ||
| if runtime.GOOS != "linux" && runtime.GOOS != "freebsd" { | ||
| t.Skipf("Skipping service lifecycle test on unsupported OS: %s", runtime.GOOS) | ||
| } | ||
|
|
||
| if os.Getenv("CONTAINER") == "true" { | ||
| t.Skip("Skipping service lifecycle test in container environment") | ||
| } | ||
|
|
||
| originalServiceName := serviceName | ||
| serviceName = "netbirdtest" + fmt.Sprintf("%d", time.Now().Unix()) | ||
| defer func() { | ||
| serviceName = originalServiceName | ||
| }() | ||
|
|
||
| tempDir := t.TempDir() | ||
| configPath = fmt.Sprintf("%s/netbird-test-config.json", tempDir) | ||
| logLevel = "info" | ||
| daemonAddr = fmt.Sprintf("unix://%s/netbird-test.sock", tempDir) | ||
|
|
||
| // Ensure cleanup even if a subtest fails and Stop/Uninstall subtests don't run. | ||
| t.Cleanup(func() { | ||
| cfg, err := newSVCConfig() | ||
| if err != nil { | ||
| t.Errorf("cleanup: create service config: %v", err) | ||
| return | ||
| } | ||
| ctxSvc, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| s, err := newSVC(newProgram(ctxSvc, cancel), cfg) | ||
| if err != nil { | ||
| t.Errorf("cleanup: create service: %v", err) | ||
| return | ||
| } | ||
|
|
||
| // If the subtests already cleaned up, there's nothing to do. | ||
| if _, err := s.Status(); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if err := s.Stop(); err != nil { | ||
| t.Errorf("cleanup: stop service: %v", err) | ||
| } | ||
| if err := s.Uninstall(); err != nil { | ||
| t.Errorf("cleanup: uninstall service: %v", err) | ||
| } | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| t.Run("Install", func(t *testing.T) { | ||
| installCmd.SetContext(ctx) | ||
| err := installCmd.RunE(installCmd, []string{}) | ||
| require.NoError(t, err) | ||
|
|
||
| cfg, err := newSVCConfig() | ||
| require.NoError(t, err) | ||
|
|
||
| ctxSvc, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| s, err := newSVC(newProgram(ctxSvc, cancel), cfg) | ||
| require.NoError(t, err) | ||
|
|
||
| status, err := s.Status() | ||
| assert.NoError(t, err) | ||
| assert.NotEqual(t, service.StatusUnknown, status) | ||
| }) | ||
|
|
||
| t.Run("Start", func(t *testing.T) { | ||
| startCmd.SetContext(ctx) | ||
| err := startCmd.RunE(startCmd, []string{}) | ||
| require.NoError(t, err) | ||
|
|
||
| running, err := waitForServiceStatus(service.StatusRunning, serviceStartTimeout) | ||
| require.NoError(t, err) | ||
| assert.True(t, running) | ||
| }) | ||
|
|
||
| t.Run("Restart", func(t *testing.T) { | ||
| restartCmd.SetContext(ctx) | ||
| err := restartCmd.RunE(restartCmd, []string{}) | ||
| require.NoError(t, err) | ||
|
|
||
| running, err := waitForServiceStatus(service.StatusRunning, serviceStartTimeout) | ||
| require.NoError(t, err) | ||
| assert.True(t, running) | ||
| }) | ||
|
|
||
| t.Run("Reconfigure", func(t *testing.T) { | ||
| originalLogLevel := logLevel | ||
| logLevel = "debug" | ||
| defer func() { | ||
| logLevel = originalLogLevel | ||
| }() | ||
|
|
||
| reconfigureCmd.SetContext(ctx) | ||
| err := reconfigureCmd.RunE(reconfigureCmd, []string{}) | ||
| require.NoError(t, err) | ||
|
|
||
| running, err := waitForServiceStatus(service.StatusRunning, serviceStartTimeout) | ||
| require.NoError(t, err) | ||
| assert.True(t, running) | ||
| }) | ||
|
|
||
| t.Run("Stop", func(t *testing.T) { | ||
| stopCmd.SetContext(ctx) | ||
| err := stopCmd.RunE(stopCmd, []string{}) | ||
| require.NoError(t, err) | ||
|
|
||
| stopped, err := waitForServiceStatus(service.StatusStopped, serviceStopTimeout) | ||
| require.NoError(t, err) | ||
| assert.True(t, stopped) | ||
| }) | ||
|
|
||
| t.Run("Uninstall", func(t *testing.T) { | ||
| uninstallCmd.SetContext(ctx) | ||
| err := uninstallCmd.RunE(uninstallCmd, []string{}) | ||
| require.NoError(t, err) | ||
|
|
||
| cfg, err := newSVCConfig() | ||
| require.NoError(t, err) | ||
|
|
||
| ctxSvc, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| s, err := newSVC(newProgram(ctxSvc, cancel), cfg) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = s.Status() | ||
| assert.Error(t, err) | ||
| }) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restore mutated global variables in test cleanup.
The test modifies package-level globals
configPath,logLevel, anddaemonAddrbut doesn't restore them after the test completes. If other tests in this package rely on these defaults, they could be affected by this test's mutations.Apply the same save-and-restore pattern used for
serviceName(lines 73-77).🔧 Proposed fix to restore globals
📝 Committable suggestion
🤖 Prompt for AI Agents