-
Notifications
You must be signed in to change notification settings - Fork 563
/
Copy pathruns_test.go
161 lines (140 loc) · 4.74 KB
/
runs_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
package main
import (
"fmt"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/utils"
github2 "github.com/diggerhq/digger/libs/ci/github"
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
"github.com/diggerhq/digger/libs/spec"
"github.com/stretchr/testify/assert"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
"os"
"strings"
"testing"
)
func init() {
log.SetOutput(os.Stdout)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
type MockCiBackend struct {
}
func (m MockCiBackend) TriggerWorkflow(spec spec.Spec, runName string, vcsToken string) error {
return nil
}
func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) {
log.Println("setup suite")
// database file name
dbName := "database_tasks_test.db"
// remove old database
e := os.Remove(dbName)
if e != nil {
if !strings.Contains(e.Error(), "no such file or directory") {
log.Fatal(e)
}
}
// open and create a new database
gdb, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{})
if err != nil {
log.Fatal(err)
}
// migrate tables
err = gdb.AutoMigrate(&models.Policy{}, &models.Organisation{}, &models.Repo{}, &models.Project{}, &models.Token{},
&models.User{}, &models.ProjectRun{}, &models.GithubAppInstallation{}, &models.GithubAppConnection{}, &models.GithubAppInstallationLink{},
&models.GithubDiggerJobLink{}, &models.DiggerJob{}, &models.DiggerJobParentLink{}, &models.DiggerRun{}, &models.DiggerRunQueueItem{})
if err != nil {
log.Fatal(err)
}
database := &models.Database{GormDB: gdb}
models.DB = database
// create an org
orgTenantId := "11111111-1111-1111-1111-111111111111"
externalSource := "test"
orgName := "testOrg"
org, err := database.CreateOrganisation(orgName, externalSource, orgTenantId)
if err != nil {
log.Fatal(err)
}
// create digger repo
repoName := "test repo"
repo, err := database.CreateRepo(repoName, "", "", "", "", org, "")
if err != nil {
log.Fatal(err)
}
// create test project
projectName := "test project"
_, err = database.CreateProject(projectName, org, repo, false, false)
if err != nil {
log.Fatal(err)
}
models.DB = database
// Return a function to teardown the test
return func(tb testing.TB) {
log.Println("teardown suite")
err = os.Remove(dbName)
if err != nil {
log.Fatal(err)
}
}, database
}
func TestThatRunQueueItemMovesFromQueuedToPlanningAfterPickup(t *testing.T) {
// TODO: Fix the failing tests and unskip
t.Skip()
teardownSuite, _ := setupSuite(t)
defer teardownSuite(t)
type params struct {
BatchStatus orchestrator_scheduler.DiggerBatchStatus
InitialStatus models.DiggerRunStatus
NextExpectedStatus models.DiggerRunStatus
}
testParameters := []params{
{
BatchStatus: orchestrator_scheduler.BatchJobSucceeded,
InitialStatus: models.RunQueued,
NextExpectedStatus: models.RunPlanning,
},
// TODO: Uncomment when functionality working
//{
// BatchStatus: orchestrator_scheduler.BatchJobFailed,
// InitialStatus: models.RunPlanning,
// NextExpectedStatus: models.RunFailed,
//},
{
BatchStatus: orchestrator_scheduler.BatchJobSucceeded,
InitialStatus: models.RunPlanning,
NextExpectedStatus: models.RunPendingApproval,
},
{
BatchStatus: orchestrator_scheduler.BatchJobSucceeded,
InitialStatus: models.RunApproved,
NextExpectedStatus: models.RunApplying,
},
// TODO: uncomment when functional
//{
// BatchStatus: orchestrator_scheduler.BatchJobFailed,
// InitialStatus: models.RunApplying,
// NextExpectedStatus: models.RunFailed,
//},
{
BatchStatus: orchestrator_scheduler.BatchJobSucceeded,
InitialStatus: models.RunApplying,
NextExpectedStatus: models.RunSucceeded,
},
}
for i, testParam := range testParameters {
ciService := github2.MockCiService{}
batch, _ := models.DB.CreateDiggerBatch(models.DiggerVCSGithub, 123, "", "", "", 22, "", "", "", nil, 0, "", false)
project, _ := models.DB.CreateProject(fmt.Sprintf("test%v", i), nil, nil, false, false)
planStage, _ := models.DB.CreateDiggerRunStage(batch.ID.String())
applyStage, _ := models.DB.CreateDiggerRunStage(batch.ID.String())
diggerRun, _ := models.DB.CreateDiggerRun("", 1, testParam.InitialStatus, "sha", "", 123, 1, project.Name, "apply", &planStage.ID, &applyStage.ID)
queueItem, _ := models.DB.CreateDiggerRunQueueItem(project.ID, diggerRun.ID)
batch.Status = testParam.BatchStatus
models.DB.UpdateDiggerBatch(batch)
queueItem, _ = models.DB.GetDiggerRunQueueItem(queueItem.ID)
RunQueuesStateMachine(queueItem, ciService, utils.DiggerGithubClientMockProvider{})
diggerRunRefreshed, _ := models.DB.GetDiggerRun(diggerRun.ID)
assert.Equal(t, testParam.NextExpectedStatus, diggerRunRefreshed.Status)
}
}