-
Notifications
You must be signed in to change notification settings - Fork 328
Add --group flag to override group name for stdin input #1163
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
NoNickeD
wants to merge
3
commits into
open-policy-agent:master
Choose a base branch
from
NoNickeD:add-group-flag
base: master
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 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
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,155 @@ | ||
| package runner | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestTestRunner_FileNameOverrideFlag(t *testing.T) { | ||
| // Create a temporary directory for test files | ||
| tmpDir := t.TempDir() | ||
|
|
||
| // Create a test policy file | ||
| policyDir := filepath.Join(tmpDir, "policy") | ||
| if err := os.Mkdir(policyDir, 0755); err != nil { | ||
| t.Fatalf("Failed to create policy directory: %v", err) | ||
| } | ||
|
|
||
| policyContent := `package main | ||
|
|
||
| deny contains msg if { | ||
| input.kind == "Deployment" | ||
| input.metadata.name == "test" | ||
| msg := "test deployment found" | ||
| } | ||
| ` | ||
| policyFile := filepath.Join(policyDir, "test.rego") | ||
| if err := os.WriteFile(policyFile, []byte(policyContent), 0644); err != nil { | ||
| t.Fatalf("Failed to write policy file: %v", err) | ||
| } | ||
|
|
||
| // Test cases | ||
| tests := []struct { | ||
| name string | ||
| fileNameOverride string | ||
| expectedFileName string | ||
| }{ | ||
| { | ||
| name: "with file-name-override", | ||
| fileNameOverride: "my-custom-file.yaml", | ||
| expectedFileName: "my-custom-file.yaml", | ||
| }, | ||
| { | ||
| name: "without file-name-override", | ||
| fileNameOverride: "", | ||
| expectedFileName: "-", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Create TestRunner with FileNameOverride set | ||
| runner := TestRunner{ | ||
| Policy: []string{policyDir}, | ||
| Namespace: []string{"main"}, | ||
| FileNameOverride: tc.fileNameOverride, | ||
| RegoVersion: "v1", | ||
| } | ||
|
|
||
| // Create stdin input by using "-" as the file path | ||
| fileList := []string{"-"} | ||
|
|
||
| // Mock stdin with test data | ||
| oldStdin := os.Stdin | ||
| r, w, _ := os.Pipe() | ||
| os.Stdin = r | ||
| testInput := `{ | ||
| "apiVersion": "apps/v1", | ||
| "kind": "Deployment", | ||
| "metadata": { | ||
| "name": "test" | ||
| } | ||
| }` | ||
| w.Write([]byte(testInput)) | ||
| w.Close() | ||
| defer func() { os.Stdin = oldStdin }() | ||
|
|
||
| // Run the test | ||
| ctx := t.Context() | ||
| results, err := runner.Run(ctx, fileList) | ||
| if err != nil { | ||
| t.Fatalf("Run failed: %v", err) | ||
| } | ||
|
|
||
| // Verify results | ||
| if len(results) == 0 { | ||
| t.Fatal("Expected at least one result") | ||
| } | ||
|
|
||
| // Check that the filename was properly overridden | ||
| if results[0].FileName != tc.expectedFileName { | ||
| t.Errorf("Expected filename to be '%s', got '%s'", tc.expectedFileName, results[0].FileName) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTestRunner_FileNameOverrideOnlyAffectsStdin(t *testing.T) { | ||
| // Create a temporary directory for test files | ||
| tmpDir := t.TempDir() | ||
|
|
||
| // Create a test policy file | ||
| policyDir := filepath.Join(tmpDir, "policy") | ||
| if err := os.Mkdir(policyDir, 0755); err != nil { | ||
| t.Fatalf("Failed to create policy directory: %v", err) | ||
| } | ||
|
|
||
| policyContent := `package main | ||
|
|
||
| deny contains msg if { | ||
| input.kind == "Deployment" | ||
| msg := "deployment found" | ||
| } | ||
| ` | ||
| policyFile := filepath.Join(policyDir, "test.rego") | ||
| if err := os.WriteFile(policyFile, []byte(policyContent), 0644); err != nil { | ||
| t.Fatalf("Failed to write policy file: %v", err) | ||
| } | ||
|
|
||
| // Create a test config file | ||
| configContent := `apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: test-file | ||
| ` | ||
| configFile := filepath.Join(tmpDir, "deployment.yaml") | ||
| if err := os.WriteFile(configFile, []byte(configContent), 0644); err != nil { | ||
| t.Fatalf("Failed to write config file: %v", err) | ||
| } | ||
|
|
||
| // Create TestRunner with FileNameOverride set | ||
| runner := TestRunner{ | ||
| Policy: []string{policyDir}, | ||
| Namespace: []string{"main"}, | ||
| FileNameOverride: "overridden-name.yaml", | ||
| RegoVersion: "v1", | ||
| } | ||
|
|
||
| // Run with a regular file (not stdin) | ||
| ctx := t.Context() | ||
| results, err := runner.Run(ctx, []string{configFile}) | ||
| if err != nil { | ||
| t.Fatalf("Run failed: %v", err) | ||
| } | ||
|
|
||
| // Verify results | ||
| if len(results) == 0 { | ||
| t.Fatal("Expected at least one result") | ||
| } | ||
|
|
||
| // Check that the regular file name was NOT overridden | ||
| if results[0].FileName != configFile { | ||
| t.Errorf("Expected filename to remain '%s', got '%s'", configFile, results[0].FileName) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.