-
Notifications
You must be signed in to change notification settings - Fork 2
Add check config integration test #316
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
RiSKeD
wants to merge
3
commits into
main
Choose a base branch
from
test/check-config-integration-test
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.
+104
−4
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,103 @@ | ||
| // Copyright 2025 Blindspot Software | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| // Package dutagent_integration contains integration tests for the dutagent binary. | ||
| package dutagent_integration | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestMain(m *testing.M) { | ||
| bin, err := buildDutagent() | ||
| if err != nil { | ||
| os.Stderr.WriteString("failed to build dutagent: " + err.Error() + "\n") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| os.Setenv("DUTAGENT_TEST_BINARY", bin) | ||
|
|
||
| code := m.Run() | ||
| os.Remove(bin) | ||
| os.Exit(code) | ||
| } | ||
|
|
||
| func dutagentBinary(t *testing.T) string { | ||
| t.Helper() | ||
|
|
||
| bin := os.Getenv("DUTAGENT_TEST_BINARY") | ||
| if bin == "" { | ||
| t.Fatal("DUTAGENT_TEST_BINARY not set — was TestMain skipped?") | ||
| } | ||
|
|
||
| return bin | ||
| } | ||
|
|
||
| func buildDutagent() (string, error) { | ||
| bin, err := os.CreateTemp("", "dutagent-test-*") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| bin.Close() | ||
|
|
||
| cmd := exec.Command("go", "build", "-o", bin.Name(), "../../../cmds/dutagent") | ||
| cmd.Stdout = os.Stderr | ||
| cmd.Stderr = os.Stderr | ||
|
|
||
| if err := cmd.Run(); err != nil { | ||
|
RiSKeD marked this conversation as resolved.
|
||
| return "", err | ||
| } | ||
|
|
||
| return bin.Name(), nil | ||
|
RiSKeD marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func exampleConfigs(t *testing.T) []string { | ||
| t.Helper() | ||
|
|
||
| // Resolve relative to this source file so the test works regardless of | ||
| // working directory (e.g. go test ./...). | ||
| _, thisFile, _, _ := runtime.Caller(0) | ||
| root := filepath.Join(filepath.Dir(thisFile), "..", "..", "..") | ||
|
|
||
| configs, err := filepath.Glob(filepath.Join(root, "pkg", "module", "*", "*-example-cfg.yml")) | ||
| if err != nil { | ||
| t.Fatalf("glob failed: %v", err) | ||
| } | ||
|
|
||
| if len(configs) == 0 { | ||
| t.Fatal("no example config files found — check the glob pattern") | ||
| } | ||
|
|
||
| return configs | ||
| } | ||
|
|
||
| // TestCheckConfigExampleFiles runs dutagent --check-config against every example | ||
| // config file shipped with the module packages and asserts: | ||
| // - exit code 0 | ||
| // - "Configuration is valid" in output | ||
| func TestCheckConfigExampleFiles(t *testing.T) { | ||
| bin := dutagentBinary(t) | ||
|
|
||
| for _, cfgPath := range exampleConfigs(t) { | ||
| t.Run(filepath.Base(filepath.Dir(cfgPath)), func(t *testing.T) { | ||
| cmd := exec.CommandContext(t.Context(), bin, "--check-config", "-c", cfgPath) | ||
| out, err := cmd.CombinedOutput() | ||
|
|
||
| if err != nil { | ||
| t.Errorf("unexpected exit: %v\n%s", err, out) | ||
| return | ||
| } | ||
|
|
||
| if !bytes.Contains(out, []byte("Configuration is valid")) { | ||
| t.Errorf("expected 'Configuration is valid' in output, got:\n%s", out) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.