mirror of
https://git.vectorsigma.ru/public/atlantis.git
synced 2026-07-29 14:28:22 +00:00
* Add pre and post workflow hook status * fix missing mocks * fix pre/post workflow hooks tests * add cmd output to file * fix typo * add cmd output to file on pre workflow hooks * Add hooks output to web UI * Remove unnecessary dependencies * Clean the code and make tests work again * Make lint happy * Small fixes, create dedicated UUID generator and docs * Add missing commentary * Use matchers instead of mocking for tests and fix e2e tests * Change 'OUTPUT_FILE' to 'OUTPUT_STATUS_FILE' * Reduce SendWorkflowHook calls on hooks * Apply suggestions from code review Co-authored-by: PePe Amengual <jose.amengual@gmail.com> Co-authored-by: nitrocode <7775707+nitrocode@users.noreply.github.com>
185 lines
4.6 KiB
Go
185 lines
4.6 KiB
Go
package events
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/runatlantis/atlantis/server/events/command"
|
|
"github.com/runatlantis/atlantis/server/events/models"
|
|
. "github.com/runatlantis/atlantis/testing"
|
|
)
|
|
|
|
func TestApplyUpdateCommitStatus(t *testing.T) {
|
|
cases := map[string]struct {
|
|
cmd command.Name
|
|
pullStatus models.PullStatus
|
|
expStatus models.CommitStatus
|
|
expNumSuccess int
|
|
expNumTotal int
|
|
}{
|
|
"apply, one pending": {
|
|
cmd: command.Apply,
|
|
pullStatus: models.PullStatus{
|
|
Projects: []models.ProjectStatus{
|
|
{
|
|
Status: models.PlannedPlanStatus,
|
|
},
|
|
{
|
|
Status: models.AppliedPlanStatus,
|
|
},
|
|
},
|
|
},
|
|
expStatus: models.PendingCommitStatus,
|
|
expNumSuccess: 1,
|
|
expNumTotal: 2,
|
|
},
|
|
"apply, all successful": {
|
|
cmd: command.Apply,
|
|
pullStatus: models.PullStatus{
|
|
Projects: []models.ProjectStatus{
|
|
{
|
|
Status: models.AppliedPlanStatus,
|
|
},
|
|
{
|
|
Status: models.AppliedPlanStatus,
|
|
},
|
|
},
|
|
},
|
|
expStatus: models.SuccessCommitStatus,
|
|
expNumSuccess: 2,
|
|
expNumTotal: 2,
|
|
},
|
|
"apply, one errored, one pending": {
|
|
cmd: command.Apply,
|
|
pullStatus: models.PullStatus{
|
|
Projects: []models.ProjectStatus{
|
|
{
|
|
Status: models.AppliedPlanStatus,
|
|
},
|
|
{
|
|
Status: models.ErroredApplyStatus,
|
|
},
|
|
{
|
|
Status: models.PlannedPlanStatus,
|
|
},
|
|
},
|
|
},
|
|
expStatus: models.FailedCommitStatus,
|
|
expNumSuccess: 1,
|
|
expNumTotal: 3,
|
|
},
|
|
}
|
|
|
|
for name, c := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
csu := &MockCSU{}
|
|
cr := &ApplyCommandRunner{
|
|
commitStatusUpdater: csu,
|
|
}
|
|
cr.updateCommitStatus(&command.Context{}, c.pullStatus)
|
|
Equals(t, models.Repo{}, csu.CalledRepo)
|
|
Equals(t, models.PullRequest{}, csu.CalledPull)
|
|
Equals(t, c.expStatus, csu.CalledStatus)
|
|
Equals(t, c.cmd, csu.CalledCommand)
|
|
Equals(t, c.expNumSuccess, csu.CalledNumSuccess)
|
|
Equals(t, c.expNumTotal, csu.CalledNumTotal)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPlanUpdateCommitStatus(t *testing.T) {
|
|
cases := map[string]struct {
|
|
cmd command.Name
|
|
pullStatus models.PullStatus
|
|
expStatus models.CommitStatus
|
|
expNumSuccess int
|
|
expNumTotal int
|
|
}{
|
|
"single plan success": {
|
|
cmd: command.Plan,
|
|
pullStatus: models.PullStatus{
|
|
Projects: []models.ProjectStatus{
|
|
{
|
|
Status: models.PlannedPlanStatus,
|
|
},
|
|
},
|
|
},
|
|
expStatus: models.SuccessCommitStatus,
|
|
expNumSuccess: 1,
|
|
expNumTotal: 1,
|
|
},
|
|
"one plan error, other errors": {
|
|
cmd: command.Plan,
|
|
pullStatus: models.PullStatus{
|
|
Projects: []models.ProjectStatus{
|
|
{
|
|
Status: models.ErroredPlanStatus,
|
|
},
|
|
{
|
|
Status: models.PlannedPlanStatus,
|
|
},
|
|
{
|
|
Status: models.AppliedPlanStatus,
|
|
},
|
|
{
|
|
Status: models.ErroredApplyStatus,
|
|
},
|
|
},
|
|
},
|
|
expStatus: models.FailedCommitStatus,
|
|
expNumSuccess: 3,
|
|
expNumTotal: 4,
|
|
},
|
|
}
|
|
|
|
for name, c := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
csu := &MockCSU{}
|
|
cr := &PlanCommandRunner{
|
|
commitStatusUpdater: csu,
|
|
}
|
|
cr.updateCommitStatus(&command.Context{}, c.pullStatus)
|
|
Equals(t, models.Repo{}, csu.CalledRepo)
|
|
Equals(t, models.PullRequest{}, csu.CalledPull)
|
|
Equals(t, c.expStatus, csu.CalledStatus)
|
|
Equals(t, c.cmd, csu.CalledCommand)
|
|
Equals(t, c.expNumSuccess, csu.CalledNumSuccess)
|
|
Equals(t, c.expNumTotal, csu.CalledNumTotal)
|
|
})
|
|
}
|
|
}
|
|
|
|
type MockCSU struct {
|
|
CalledRepo models.Repo
|
|
CalledPull models.PullRequest
|
|
CalledStatus models.CommitStatus
|
|
CalledCommand command.Name
|
|
CalledNumSuccess int
|
|
CalledNumTotal int
|
|
}
|
|
|
|
func (m *MockCSU) UpdateCombinedCount(repo models.Repo, pull models.PullRequest, status models.CommitStatus, command command.Name, numSuccess int, numTotal int) error {
|
|
m.CalledRepo = repo
|
|
m.CalledPull = pull
|
|
m.CalledStatus = status
|
|
m.CalledCommand = command
|
|
m.CalledNumSuccess = numSuccess
|
|
m.CalledNumTotal = numTotal
|
|
return nil
|
|
}
|
|
|
|
func (m *MockCSU) UpdateCombined(repo models.Repo, pull models.PullRequest, status models.CommitStatus, command command.Name) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockCSU) UpdateProject(ctx command.ProjectContext, cmdName command.Name, status models.CommitStatus, url string) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockCSU) UpdatePreWorkflowHook(pull models.PullRequest, status models.CommitStatus, hookDescription string, runtimeDescription string, url string) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockCSU) UpdatePostWorkflowHook(pull models.PullRequest, status models.CommitStatus, hookDescription string, runtimeDescription string, url string) error {
|
|
return nil
|
|
}
|