Skip to content

Commit

Permalink
Refactoring ConftestOutput attribute to PolicyOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
bgalkows committed Oct 6, 2023
1 parent d3bc4e3 commit d29ed31
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion runatlantis.io/docs/policy-checking.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ When the policy check workflow runs, a file is created in the working directory
[
{
"PolicySetName": "policy1",
"ConftestOutput": "",
"PolicyOutput": "",
"Passed": false,
"ReqApprovals": 1,
"CurApprovals": 0
Expand Down
8 changes: 4 additions & 4 deletions server/core/runtime/policy/conftest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ func (c *ConfTestExecutorWorkflow) Run(ctx command.ProjectContext, executablePat
}

policySetResults = append(policySetResults, models.PolicySetResult{
PolicySetName: policySet.Name,
ConftestOutput: cmdOutput,
Passed: passed,
ReqApprovals: policySet.ApproveCount,
PolicySetName: policySet.Name,
PolicyOutput: cmdOutput,
Passed: passed,
ReqApprovals: policySet.ApproveCount,
})
}

Expand Down
10 changes: 5 additions & 5 deletions server/core/runtime/policy/conftest_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestRun(t *testing.T) {
var extraArgs []string

expectedOutput := "Success"
expectedResult := `[{"PolicySetName":"policy1","ConftestOutput":"Success","Passed":true,"ReqApprovals":0,"CurApprovals":0},{"PolicySetName":"policy2","ConftestOutput":"Success","Passed":true,"ReqApprovals":0,"CurApprovals":0}]`
expectedResult := `[{"PolicySetName":"policy1","PolicyOutput":"Success","Passed":true,"ReqApprovals":0,"CurApprovals":0},{"PolicySetName":"policy2","PolicyOutput":"Success","Passed":true,"ReqApprovals":0,"CurApprovals":0}]`

expectedArgsPolicy1 := []string{executablePath, "test", "-p", localPolicySetPath1, filepath.Join(workdir, "testproj-default.json"), "--no-color"}
expectedArgsPolicy2 := []string{executablePath, "test", "-p", localPolicySetPath2, filepath.Join(workdir, "testproj-default.json"), "--no-color"}
Expand All @@ -216,7 +216,7 @@ func TestRun(t *testing.T) {
extraArgs := []string{"--all-namespaces"}

expectedOutput := "Success"
expectedResult := `[{"PolicySetName":"policy1","ConftestOutput":"","Passed":true,"ReqApprovals":0,"CurApprovals":0},{"PolicySetName":"policy2","ConftestOutput":"","Passed":true,"ReqApprovals":0,"CurApprovals":0}]`
expectedResult := `[{"PolicySetName":"policy1","PolicyOutput":"","Passed":true,"ReqApprovals":0,"CurApprovals":0},{"PolicySetName":"policy2","PolicyOutput":"","Passed":true,"ReqApprovals":0,"CurApprovals":0}]`

expectedArgsPolicy1 := []string{executablePath, "test", "-p", localPolicySetPath1, filepath.Join(workdir, "testproj-default.json"), "--no-color"}
expectedArgsPolicy2 := []string{executablePath, "test", "-p", localPolicySetPath2, filepath.Join(workdir, "testproj-default.json"), "--no-color"}
Expand All @@ -241,7 +241,7 @@ func TestRun(t *testing.T) {
var extraArgs []string

expectedOutput := "Success"
expectedResult := `[{"PolicySetName":"policy1","ConftestOutput":"Success","Passed":true,"ReqApprovals":0,"CurApprovals":0}]`
expectedResult := `[{"PolicySetName":"policy1","PolicyOutput":"Success","Passed":true,"ReqApprovals":0,"CurApprovals":0}]`

expectedArgsPolicy1 := []string{executablePath, "test", "-p", localPolicySetPath1, filepath.Join(workdir, "testproj-default.json"), "--no-color"}
expectedArgsPolicy2 := []string{executablePath, "test", "-p", localPolicySetPath2, filepath.Join(workdir, "testproj-default.json"), "--no-color"}
Expand Down Expand Up @@ -284,7 +284,7 @@ func TestRun(t *testing.T) {

expectedOutputPolicy1 := fmt.Sprintf("FAIL - %s - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions", filepath.Join(workdir, "testproj-default.json"))
expectedOutputPolicy2 := "Success"
expectedResult := `[{"PolicySetName":"policy1","ConftestOutput":"FAIL - <redacted plan file> - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions","Passed":false,"ReqApprovals":0,"CurApprovals":0},{"PolicySetName":"policy2","ConftestOutput":"Success","Passed":true,"ReqApprovals":0,"CurApprovals":0}]`
expectedResult := `[{"PolicySetName":"policy1","PolicyOutput":"FAIL - <redacted plan file> - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions","Passed":false,"ReqApprovals":0,"CurApprovals":0},{"PolicySetName":"policy2","PolicyOutput":"Success","Passed":true,"ReqApprovals":0,"CurApprovals":0}]`

expectedArgsPolicy1 := []string{executablePath, "test", "-p", localPolicySetPath1, filepath.Join(workdir, "testproj-default.json"), "--no-color"}
expectedArgsPolicy2 := []string{executablePath, "test", "-p", localPolicySetPath2, filepath.Join(workdir, "testproj-default.json"), "--no-color"}
Expand All @@ -306,7 +306,7 @@ func TestRun(t *testing.T) {
var extraArgs []string

expectedOutput := fmt.Sprintf("FAIL - %s - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions", filepath.Join(workdir, "testproj-default.json"))
expectedResult := `[{"PolicySetName":"policy1","ConftestOutput":"FAIL - <redacted plan file> - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions","Passed":false,"ReqApprovals":0,"CurApprovals":0},{"PolicySetName":"policy2","ConftestOutput":"FAIL - <redacted plan file> - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions","Passed":false,"ReqApprovals":0,"CurApprovals":0}]`
expectedResult := `[{"PolicySetName":"policy1","PolicyOutput":"FAIL - <redacted plan file> - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions","Passed":false,"ReqApprovals":0,"CurApprovals":0},{"PolicySetName":"policy2","PolicyOutput":"FAIL - <redacted plan file> - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions","Passed":false,"ReqApprovals":0,"CurApprovals":0}]`

expectedArgsPolicy1 := []string{executablePath, "test", "-p", localPolicySetPath1, filepath.Join(workdir, "testproj-default.json"), "--no-color"}
expectedArgsPolicy2 := []string{executablePath, "test", "-p", localPolicySetPath2, filepath.Join(workdir, "testproj-default.json"), "--no-color"}
Expand Down
42 changes: 21 additions & 21 deletions server/events/markdown_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ $$$
{
PolicySetName: "policy1",
// strings.Repeat require to get wrapped result
ConftestOutput: `FAIL - <redacted plan file> - main - WARNING: Null Resource creation is prohibited.
PolicyOutput: `FAIL - <redacted plan file> - main - WARNING: Null Resource creation is prohibited.
2 tests, 1 passed, 0 warnings, 1 failure, 0 exceptions`,
Passed: false,
Expand All @@ -276,9 +276,9 @@ $$$
{
PolicySetName: "policy2",
// strings.Repeat require to get wrapped result
ConftestOutput: "2 tests, 2 passed, 0 warnings, 0 failure, 0 exceptions",
Passed: true,
ReqApprovals: 1,
PolicyOutput: "2 tests, 2 passed, 0 warnings, 0 failure, 0 exceptions",
Passed: true,
ReqApprovals: 1,
},
},
LockURL: "lock-url",
Expand Down Expand Up @@ -335,7 +335,7 @@ $$$
{
PolicySetName: "policy1",
// strings.Repeat require to get wrapped result
ConftestOutput: strings.Repeat("line\n", 13) + `FAIL - <redacted plan file> - main - WARNING: Null Resource creation is prohibited.
PolicyOutput: strings.Repeat("line\n", 13) + `FAIL - <redacted plan file> - main - WARNING: Null Resource creation is prohibited.
2 tests, 1 passed, 0 warnings, 1 failure, 0 exceptions`,
Passed: false,
Expand Down Expand Up @@ -565,9 +565,9 @@ $$$
PolicyCheckResults: &models.PolicyCheckResults{
PolicySetResults: []models.PolicySetResult{
models.PolicySetResult{
PolicySetName: "policy1",
ConftestOutput: "4 tests, 4 passed, 0 warnings, 0 failures, 0 exceptions",
Passed: true,
PolicySetName: "policy1",
PolicyOutput: "4 tests, 4 passed, 0 warnings, 0 failures, 0 exceptions",
Passed: true,
},
},
LockURL: "lock-url",
Expand All @@ -582,9 +582,9 @@ $$$
PolicyCheckResults: &models.PolicyCheckResults{
PolicySetResults: []models.PolicySetResult{
models.PolicySetResult{
PolicySetName: "policy1",
ConftestOutput: "4 tests, 4 passed, 0 warnings, 0 failures, 0 exceptions",
Passed: true,
PolicySetName: "policy1",
PolicyOutput: "4 tests, 4 passed, 0 warnings, 0 failures, 0 exceptions",
Passed: true,
},
}, LockURL: "lock-url2",
ApplyCmd: "atlantis apply -d path2 -w workspace",
Expand Down Expand Up @@ -778,9 +778,9 @@ $$$
PolicyCheckResults: &models.PolicyCheckResults{
PolicySetResults: []models.PolicySetResult{
models.PolicySetResult{
PolicySetName: "policy1",
ConftestOutput: "4 tests, 4 passed, 0 warnings, 0 failures, 0 exceptions",
Passed: true,
PolicySetName: "policy1",
PolicyOutput: "4 tests, 4 passed, 0 warnings, 0 failures, 0 exceptions",
Passed: true,
},
}, LockURL: "lock-url",
ApplyCmd: "atlantis apply -d path -w workspace",
Expand All @@ -794,10 +794,10 @@ $$$
PolicyCheckResults: &models.PolicyCheckResults{
PolicySetResults: []models.PolicySetResult{
models.PolicySetResult{
PolicySetName: "policy1",
ConftestOutput: "4 tests, 2 passed, 0 warnings, 2 failures, 0 exceptions",
Passed: false,
ReqApprovals: 1,
PolicySetName: "policy1",
PolicyOutput: "4 tests, 2 passed, 0 warnings, 2 failures, 0 exceptions",
Passed: false,
ReqApprovals: 1,
},
}, LockURL: "lock-url",
ApplyCmd: "atlantis apply -d path -w workspace",
Expand Down Expand Up @@ -1317,9 +1317,9 @@ func TestRenderCustomPolicyCheckTemplate_DisableApplyAll(t *testing.T) {
PolicyCheckResults: &models.PolicyCheckResults{
PolicySetResults: []models.PolicySetResult{
models.PolicySetResult{
PolicySetName: "policy1",
ConftestOutput: "4 tests, 4 passed, 0 warnings, 0 failures, 0 exceptions",
Passed: true,
PolicySetName: "policy1",
PolicyOutput: "4 tests, 4 passed, 0 warnings, 0 failures, 0 exceptions",
Passed: true,
},
}, LockURL: "lock-url",
ApplyCmd: "atlantis apply -d path -w workspace",
Expand Down
14 changes: 7 additions & 7 deletions server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,11 @@ type PlanSuccess struct {
}

type PolicySetResult struct {
PolicySetName string
ConftestOutput string
Passed bool
ReqApprovals int
CurApprovals int
PolicySetName string
PolicyOutput string
Passed bool
ReqApprovals int
CurApprovals int
}

// PolicySetApproval tracks the number of approvals a given policy set has.
Expand Down Expand Up @@ -472,7 +472,7 @@ func (p *PolicyCheckResults) CombinedOutput() string {
combinedOutput := ""
for _, psResult := range p.PolicySetResults {
// accounting for json output from conftest.
for _, psResultLine := range strings.Split(psResult.ConftestOutput, "\\n") {
for _, psResultLine := range strings.Split(psResult.PolicyOutput, "\\n") {
combinedOutput = fmt.Sprintf("%s\n%s", combinedOutput, psResultLine)
}
}
Expand All @@ -484,7 +484,7 @@ func (p *PolicyCheckResults) Summary() string {
note := ""
for _, policySetResult := range p.PolicySetResults {
r := regexp.MustCompile(`\d+ tests?, \d+ passed, \d+ warnings?, \d+ failures?, \d+ exceptions?(, \d skipped)?`)
if match := r.FindString(policySetResult.ConftestOutput); match != "" {
if match := r.FindString(policySetResult.PolicyOutput); match != "" {
note = fmt.Sprintf("%s\npolicy set: %s: %s", note, policySetResult.PolicySetName, match)
}
}
Expand Down
16 changes: 8 additions & 8 deletions server/events/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ func TestPolicyCheckResults_Summary(t *testing.T) {
description: "test single format with single policy set",
policysetResults: []models.PolicySetResult{
{
PolicySetName: "policy1",
ConftestOutput: "20 tests, 19 passed, 2 warnings, 0 failures, 0 exceptions",
PolicySetName: "policy1",
PolicyOutput: "20 tests, 19 passed, 2 warnings, 0 failures, 0 exceptions",
},
},
exp: "policy set: policy1: 20 tests, 19 passed, 2 warnings, 0 failures, 0 exceptions",
Expand All @@ -443,16 +443,16 @@ func TestPolicyCheckResults_Summary(t *testing.T) {
description: "test multiple formats with multiple policy sets",
policysetResults: []models.PolicySetResult{
{
PolicySetName: "policy1",
ConftestOutput: "20 tests, 19 passed, 2 warnings, 0 failures, 0 exceptions",
PolicySetName: "policy1",
PolicyOutput: "20 tests, 19 passed, 2 warnings, 0 failures, 0 exceptions",
},
{
PolicySetName: "policy2",
ConftestOutput: "3 tests, 0 passed, 1 warning, 1 failure, 0 exceptions, 1 skipped",
PolicySetName: "policy2",
PolicyOutput: "3 tests, 0 passed, 1 warning, 1 failure, 0 exceptions, 1 skipped",
},
{
PolicySetName: "policy3",
ConftestOutput: "1 test, 0 passed, 1 warning, 1 failure, 1 exception",
PolicySetName: "policy3",
PolicyOutput: "1 test, 0 passed, 1 warning, 1 failure, 1 exception",
},
},
exp: `policy set: policy1: 20 tests, 19 passed, 2 warnings, 0 failures, 0 exceptions
Expand Down
2 changes: 1 addition & 1 deletion server/events/project_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func (p *DefaultProjectCommandRunner) doPolicyCheck(ctx command.ProjectContext)
} else {
// Using a policy tool other than Conftest, manually building result struct
passed := !strings.Contains(strings.ToLower(output), "fail")
policySetResults = append(policySetResults, models.PolicySetResult{PolicySetName: "Custom", ConftestOutput: output, Passed: passed, ReqApprovals: 1, CurApprovals: 0})
policySetResults = append(policySetResults, models.PolicySetResult{PolicySetName: "Custom", PolicyOutput: output, Passed: passed, ReqApprovals: 1, CurApprovals: 0})
preConftestOutput = append(preConftestOutput, "")
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/events/templates/policy_check.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{ range $ps, $policy_sets }}
#### Policy Set: `{{ $ps.PolicySetName }}`
```diff
{{ $ps.ConftestOutput }}
{{ $ps.PolicyOutput }}
```
{{ end }}
{{ end }}

0 comments on commit d29ed31

Please sign in to comment.