Skip to content

Commit e2eacdc

Browse files
authored
fix: Return correct status when using custom policy (#5156)
Signed-off-by: bakuljajan <[email protected]>
1 parent c864248 commit e2eacdc

File tree

2 files changed

+76
-74
lines changed

2 files changed

+76
-74
lines changed

server/core/runtime/run_step_runner.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ func (r *RunStepRunner) Run(
9595
err = fmt.Errorf("%s: running %q in %q: \n%s", err, command, path, output)
9696
if !ctx.CustomPolicyCheck {
9797
ctx.Log.Debug("error: %s", err)
98-
return "", err
98+
} else {
99+
ctx.Log.Debug("Treating custom policy tool error exit code as a policy failure. Error output: %s", err)
99100
}
100-
ctx.Log.Debug("Treating custom policy tool error exit code as a policy failure. Error output: %s", err)
101+
return "", err
101102
}
102103

103104
switch postProcessOutput {

server/core/runtime/run_step_runner_test.go

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -100,89 +100,90 @@ func TestRunStepRunner_Run(t *testing.T) {
100100
ExpOut: "args=-target=resource1,-target=resource2\n",
101101
},
102102
}
103+
for _, customPolicyCheck := range []bool{false, true} {
104+
for _, c := range cases {
105+
var projVersion *version.Version
106+
var err error
103107

104-
for _, c := range cases {
108+
projVersion, err = version.NewVersion("v0.11.0")
105109

106-
var projVersion *version.Version
107-
var err error
108-
109-
projVersion, err = version.NewVersion("v0.11.0")
110+
if c.Version != "" {
111+
projVersion, err = version.NewVersion(c.Version)
112+
Ok(t, err)
113+
}
110114

111-
if c.Version != "" {
112-
projVersion, err = version.NewVersion(c.Version)
113115
Ok(t, err)
114-
}
115116

116-
Ok(t, err)
117-
118-
projTFDistribution := "terraform"
119-
if c.Distribution != "" {
120-
projTFDistribution = c.Distribution
121-
}
117+
projTFDistribution := "terraform"
118+
if c.Distribution != "" {
119+
projTFDistribution = c.Distribution
120+
}
122121

123-
defaultVersion, _ := version.NewVersion("0.8")
122+
defaultVersion, _ := version.NewVersion("0.8")
124123

125-
RegisterMockTestingT(t)
126-
terraform := tfclientmocks.NewMockClient()
127-
defaultDistribution := tf.NewDistributionTerraformWithDownloader(mocks.NewMockDownloader())
128-
When(terraform.EnsureVersion(Any[logging.SimpleLogging](), Any[tf.Distribution](), Any[*version.Version]())).
129-
ThenReturn(nil)
124+
RegisterMockTestingT(t)
125+
terraform := tfclientmocks.NewMockClient()
126+
defaultDistribution := tf.NewDistributionTerraformWithDownloader(mocks.NewMockDownloader())
127+
When(terraform.EnsureVersion(Any[logging.SimpleLogging](), Any[tf.Distribution](), Any[*version.Version]())).
128+
ThenReturn(nil)
130129

131-
logger := logging.NewNoopLogger(t)
132-
projectCmdOutputHandler := jobmocks.NewMockProjectCommandOutputHandler()
133-
tmpDir := t.TempDir()
130+
logger := logging.NewNoopLogger(t)
131+
projectCmdOutputHandler := jobmocks.NewMockProjectCommandOutputHandler()
132+
tmpDir := t.TempDir()
134133

135-
r := runtime.RunStepRunner{
136-
TerraformExecutor: terraform,
137-
DefaultTFDistribution: defaultDistribution,
138-
DefaultTFVersion: defaultVersion,
139-
TerraformBinDir: "/bin/dir",
140-
ProjectCmdOutputHandler: projectCmdOutputHandler,
141-
}
142-
t.Run(c.Command, func(t *testing.T) {
143-
ctx := command.ProjectContext{
144-
BaseRepo: models.Repo{
145-
Name: "basename",
146-
Owner: "baseowner",
147-
},
148-
HeadRepo: models.Repo{
149-
Name: "headname",
150-
Owner: "headowner",
151-
},
152-
Pull: models.PullRequest{
153-
Num: 2,
154-
URL: "https://github.com/runatlantis/atlantis/pull/2",
155-
HeadBranch: "add-feat",
156-
HeadCommit: "12345abcdef",
157-
BaseBranch: "main",
158-
Author: "acme",
159-
},
160-
User: models.User{
161-
Username: "acme-user",
162-
},
163-
Log: logger,
164-
Workspace: "myworkspace",
165-
RepoRelDir: "mydir",
166-
TerraformDistribution: &projTFDistribution,
167-
TerraformVersion: projVersion,
168-
ProjectName: c.ProjectName,
169-
EscapedCommentArgs: []string{"-target=resource1", "-target=resource2"},
170-
}
171-
out, err := r.Run(ctx, nil, c.Command, tmpDir, map[string]string{"test": "var"}, true, valid.PostProcessRunOutputShow)
172-
if c.ExpErr != "" {
173-
ErrContains(t, c.ExpErr, err)
174-
return
134+
r := runtime.RunStepRunner{
135+
TerraformExecutor: terraform,
136+
DefaultTFDistribution: defaultDistribution,
137+
DefaultTFVersion: defaultVersion,
138+
TerraformBinDir: "/bin/dir",
139+
ProjectCmdOutputHandler: projectCmdOutputHandler,
175140
}
176-
Ok(t, err)
177-
// Replace $DIR in the exp with the actual temp dir. We do this
178-
// here because when constructing the cases we don't yet know the
179-
// temp dir.
180-
expOut := strings.Replace(c.ExpOut, "$DIR", tmpDir, -1)
181-
Equals(t, expOut, out)
141+
t.Run(fmt.Sprintf("%s_CustomPolicyCheck=%v", c.Command, customPolicyCheck), func(t *testing.T) {
142+
ctx := command.ProjectContext{
143+
BaseRepo: models.Repo{
144+
Name: "basename",
145+
Owner: "baseowner",
146+
},
147+
HeadRepo: models.Repo{
148+
Name: "headname",
149+
Owner: "headowner",
150+
},
151+
Pull: models.PullRequest{
152+
Num: 2,
153+
URL: "https://github.com/runatlantis/atlantis/pull/2",
154+
HeadBranch: "add-feat",
155+
HeadCommit: "12345abcdef",
156+
BaseBranch: "main",
157+
Author: "acme",
158+
},
159+
User: models.User{
160+
Username: "acme-user",
161+
},
162+
Log: logger,
163+
Workspace: "myworkspace",
164+
RepoRelDir: "mydir",
165+
TerraformDistribution: &projTFDistribution,
166+
TerraformVersion: projVersion,
167+
ProjectName: c.ProjectName,
168+
EscapedCommentArgs: []string{"-target=resource1", "-target=resource2"},
169+
CustomPolicyCheck: customPolicyCheck,
170+
}
171+
out, err := r.Run(ctx, nil, c.Command, tmpDir, map[string]string{"test": "var"}, true, valid.PostProcessRunOutputShow)
172+
if c.ExpErr != "" {
173+
ErrContains(t, c.ExpErr, err)
174+
return
175+
}
176+
Ok(t, err)
177+
// Replace $DIR in the exp with the actual temp dir. We do this
178+
// here because when constructing the cases we don't yet know the
179+
// temp dir.
180+
expOut := strings.Replace(c.ExpOut, "$DIR", tmpDir, -1)
181+
Equals(t, expOut, out)
182182

183-
terraform.VerifyWasCalledOnce().EnsureVersion(Eq(logger), NotEq(defaultDistribution), Eq(projVersion))
184-
terraform.VerifyWasCalled(Never()).EnsureVersion(Eq(logger), Eq(defaultDistribution), Eq(defaultVersion))
183+
terraform.VerifyWasCalledOnce().EnsureVersion(Eq(logger), NotEq(defaultDistribution), Eq(projVersion))
184+
terraform.VerifyWasCalled(Never()).EnsureVersion(Eq(logger), Eq(defaultDistribution), Eq(defaultVersion))
185185

186-
})
186+
})
187+
}
187188
}
188189
}

0 commit comments

Comments
 (0)