forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_command_runner_test.go
215 lines (194 loc) · 6.33 KB
/
apply_command_runner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package events_test
import (
"errors"
"testing"
"github.com/google/go-github/v50/github"
. "github.com/petergtz/pegomock"
"github.com/runatlantis/atlantis/server/core/db"
"github.com/runatlantis/atlantis/server/core/locking"
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/models/testdata"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/metrics"
. "github.com/runatlantis/atlantis/testing"
)
func TestApplyCommandRunner_IsLocked(t *testing.T) {
RegisterMockTestingT(t)
cases := []struct {
Description string
ApplyLocked bool
ApplyLockError error
ExpComment string
}{
{
Description: "When global apply lock is present IsDisabled returns true",
ApplyLocked: true,
ApplyLockError: nil,
ExpComment: "**Error:** Running `atlantis apply` is disabled.",
},
{
Description: "When no global apply lock is present and DisableApply flag is false IsDisabled returns false",
ApplyLocked: false,
ApplyLockError: nil,
ExpComment: "Ran Apply for 0 projects:",
},
{
Description: "If ApplyLockChecker returns an error IsDisabled return value of DisableApply flag",
ApplyLockError: errors.New("error"),
ApplyLocked: false,
ExpComment: "Ran Apply for 0 projects:",
},
}
for _, c := range cases {
t.Run(c.Description, func(t *testing.T) {
vcsClient := setup(t)
scopeNull, _, _ := metrics.NewLoggingScope(logger, "atlantis")
pull := &github.PullRequest{
State: github.String("open"),
}
modelPull := models.PullRequest{BaseRepo: testdata.GithubRepo, State: models.OpenPullState, Num: testdata.Pull.Num}
When(githubGetter.GetPullRequest(testdata.GithubRepo, testdata.Pull.Num)).ThenReturn(pull, nil)
When(eventParsing.ParseGithubPull(pull)).ThenReturn(modelPull, modelPull.BaseRepo, testdata.GithubRepo, nil)
ctx := &command.Context{
User: testdata.User,
Log: logging.NewNoopLogger(t),
Scope: scopeNull,
Pull: modelPull,
HeadRepo: testdata.GithubRepo,
Trigger: command.CommentTrigger,
}
When(applyLockChecker.CheckApplyLock()).ThenReturn(locking.ApplyCommandLock{Locked: c.ApplyLocked}, c.ApplyLockError)
applyCommandRunner.Run(ctx, &events.CommentCommand{Name: command.Apply})
vcsClient.VerifyWasCalledOnce().CreateComment(testdata.GithubRepo, modelPull.Num, c.ExpComment, "apply")
})
}
}
func TestApplyCommandRunner_IsSilenced(t *testing.T) {
RegisterMockTestingT(t)
cases := []struct {
Description string
Matched bool
Targeted bool
VCSStatusSilence bool
PrevApplyStored bool // stores a 1/1 passing apply in the backend
ExpVCSStatusSet bool
ExpVCSStatusTotal int
ExpVCSStatusSucc int
ExpSilenced bool
}{
{
Description: "When applying, don't comment but set the 0/0 VCS status",
ExpVCSStatusSet: true,
ExpSilenced: true,
},
{
Description: "When applying with any previous apply's, don't comment but set the 0/0 VCS status",
PrevApplyStored: true,
ExpVCSStatusSet: true,
ExpSilenced: true,
},
{
Description: "When applying with unmatched target, don't comment but set the 0/0 VCS status",
Targeted: true,
ExpVCSStatusSet: true,
ExpSilenced: true,
},
{
Description: "When applying with unmatched target and any previous apply's, don't comment and maintain VCS status",
Targeted: true,
PrevApplyStored: true,
ExpVCSStatusSet: true,
ExpSilenced: true,
ExpVCSStatusSucc: 1,
ExpVCSStatusTotal: 1,
},
{
Description: "When applying with silenced VCS status, don't do anything",
VCSStatusSilence: true,
ExpVCSStatusSet: false,
ExpSilenced: true,
},
{
Description: "When applying with matching projects, comment as usual",
Matched: true,
ExpVCSStatusSet: true,
ExpSilenced: false,
ExpVCSStatusSucc: 1,
ExpVCSStatusTotal: 1,
},
}
for _, c := range cases {
t.Run(c.Description, func(t *testing.T) {
// create an empty DB
tmp := t.TempDir()
db, err := db.New(tmp)
Ok(t, err)
vcsClient := setup(t, func(tc *TestConfig) {
tc.SilenceNoProjects = true
tc.silenceVCSStatusNoProjects = c.VCSStatusSilence
tc.backend = db
})
scopeNull, _, _ := metrics.NewLoggingScope(logger, "atlantis")
modelPull := models.PullRequest{BaseRepo: testdata.GithubRepo, State: models.OpenPullState, Num: testdata.Pull.Num}
cmd := &events.CommentCommand{Name: command.Apply}
if c.Targeted {
cmd.RepoRelDir = "mydir"
}
ctx := &command.Context{
User: testdata.User,
Log: logging.NewNoopLogger(t),
Scope: scopeNull,
Pull: modelPull,
HeadRepo: testdata.GithubRepo,
Trigger: command.CommentTrigger,
}
if c.PrevApplyStored {
_, err = db.UpdatePullWithResults(modelPull, []command.ProjectResult{
{
Command: command.Apply,
RepoRelDir: "prevdir",
Workspace: "default",
},
})
Ok(t, err)
}
When(projectCommandBuilder.BuildApplyCommands(ctx, cmd)).Then(func(args []Param) ReturnValues {
if c.Matched {
return ReturnValues{[]command.ProjectContext{{
CommandName: command.Apply,
ProjectPlanStatus: models.PlannedPlanStatus,
}}, nil}
}
return ReturnValues{[]command.ProjectContext{}, nil}
})
applyCommandRunner.Run(ctx, cmd)
timesComment := 1
if c.ExpSilenced {
timesComment = 0
}
vcsClient.VerifyWasCalled(Times(timesComment)).CreateComment(AnyRepo(), AnyInt(), AnyString(), AnyString())
if c.ExpVCSStatusSet {
commitUpdater.VerifyWasCalledOnce().UpdateCombinedCount(
matchers.AnyModelsRepo(),
matchers.AnyModelsPullRequest(),
matchers.EqModelsCommitStatus(models.SuccessCommitStatus),
matchers.EqCommandName(command.Apply),
EqInt(c.ExpVCSStatusSucc),
EqInt(c.ExpVCSStatusTotal),
)
} else {
commitUpdater.VerifyWasCalled(Never()).UpdateCombinedCount(
matchers.AnyModelsRepo(),
matchers.AnyModelsPullRequest(),
matchers.AnyModelsCommitStatus(),
matchers.EqCommandName(command.Apply),
AnyInt(),
AnyInt(),
)
}
})
}
}