From d29ed31b4fcbf6885a30d05a6297d88ad4dd3b73 Mon Sep 17 00:00:00 2001 From: Brett Galkowski Date: Wed, 4 Oct 2023 14:02:29 -0700 Subject: [PATCH] Refactoring ConftestOutput attribute to PolicyOutput --- runatlantis.io/docs/policy-checking.md | 2 +- server/core/runtime/policy/conftest_client.go | 8 ++-- .../runtime/policy/conftest_client_test.go | 10 ++--- server/events/markdown_renderer_test.go | 42 +++++++++---------- server/events/models/models.go | 14 +++---- server/events/models/models_test.go | 16 +++---- server/events/project_command_runner.go | 2 +- server/events/templates/policy_check.tmpl | 2 +- 8 files changed, 48 insertions(+), 48 deletions(-) diff --git a/runatlantis.io/docs/policy-checking.md b/runatlantis.io/docs/policy-checking.md index 2530c5969f..c996ef7ee0 100644 --- a/runatlantis.io/docs/policy-checking.md +++ b/runatlantis.io/docs/policy-checking.md @@ -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 diff --git a/server/core/runtime/policy/conftest_client.go b/server/core/runtime/policy/conftest_client.go index 3c97bffb0e..9b0f1fa2f8 100644 --- a/server/core/runtime/policy/conftest_client.go +++ b/server/core/runtime/policy/conftest_client.go @@ -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, }) } diff --git a/server/core/runtime/policy/conftest_client_test.go b/server/core/runtime/policy/conftest_client_test.go index 171882c257..143b6e8dbc 100644 --- a/server/core/runtime/policy/conftest_client_test.go +++ b/server/core/runtime/policy/conftest_client_test.go @@ -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"} @@ -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"} @@ -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"} @@ -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 - - 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 - - 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"} @@ -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 - - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions","Passed":false,"ReqApprovals":0,"CurApprovals":0},{"PolicySetName":"policy2","ConftestOutput":"FAIL - - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions","Passed":false,"ReqApprovals":0,"CurApprovals":0}]` + expectedResult := `[{"PolicySetName":"policy1","PolicyOutput":"FAIL - - failure\n1 tests, 0 passed, 0 warnings, 1 failure, 0 exceptions","Passed":false,"ReqApprovals":0,"CurApprovals":0},{"PolicySetName":"policy2","PolicyOutput":"FAIL - - 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"} diff --git a/server/events/markdown_renderer_test.go b/server/events/markdown_renderer_test.go index 548ce03cdb..e5c5b3e03e 100644 --- a/server/events/markdown_renderer_test.go +++ b/server/events/markdown_renderer_test.go @@ -267,7 +267,7 @@ $$$ { PolicySetName: "policy1", // strings.Repeat require to get wrapped result - ConftestOutput: `FAIL - - main - WARNING: Null Resource creation is prohibited. + PolicyOutput: `FAIL - - main - WARNING: Null Resource creation is prohibited. 2 tests, 1 passed, 0 warnings, 1 failure, 0 exceptions`, Passed: false, @@ -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", @@ -335,7 +335,7 @@ $$$ { PolicySetName: "policy1", // strings.Repeat require to get wrapped result - ConftestOutput: strings.Repeat("line\n", 13) + `FAIL - - main - WARNING: Null Resource creation is prohibited. + PolicyOutput: strings.Repeat("line\n", 13) + `FAIL - - main - WARNING: Null Resource creation is prohibited. 2 tests, 1 passed, 0 warnings, 1 failure, 0 exceptions`, Passed: false, @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", diff --git a/server/events/models/models.go b/server/events/models/models.go index bdc821c285..549e06a6af 100644 --- a/server/events/models/models.go +++ b/server/events/models/models.go @@ -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. @@ -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) } } @@ -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) } } diff --git a/server/events/models/models_test.go b/server/events/models/models_test.go index 126d89c60a..c05016d2af 100644 --- a/server/events/models/models_test.go +++ b/server/events/models/models_test.go @@ -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", @@ -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 diff --git a/server/events/project_command_runner.go b/server/events/project_command_runner.go index 1ee11c59b6..40504e5304 100644 --- a/server/events/project_command_runner.go +++ b/server/events/project_command_runner.go @@ -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, "") } } diff --git a/server/events/templates/policy_check.tmpl b/server/events/templates/policy_check.tmpl index dcabbea963..69ab18cc8d 100644 --- a/server/events/templates/policy_check.tmpl +++ b/server/events/templates/policy_check.tmpl @@ -3,7 +3,7 @@ {{ range $ps, $policy_sets }} #### Policy Set: `{{ $ps.PolicySetName }}` ```diff -{{ $ps.ConftestOutput }} +{{ $ps.PolicyOutput }} ``` {{ end }} {{ end }}