Skip to content

Commit ed1682f

Browse files
committed
More work on some tests. Need to find a way to test pipeline execution
1 parent 3c17fc6 commit ed1682f

File tree

4 files changed

+87
-73
lines changed

4 files changed

+87
-73
lines changed

scheduler/scheduler.go

+21-42
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func (s *Scheduler) work() {
7878

7979
// Mark the scheduled run as running
8080
r.Status = gaia.RunRunning
81+
r.RunDate = time.Now()
8182

8283
// Update entry in store
8384
err := s.storeService.PipelinePutRun(&r)
@@ -105,8 +106,25 @@ func (s *Scheduler) work() {
105106
continue
106107
}
107108

108-
// Start pipeline run process
109-
s.executePipeline(pipeline, &r)
109+
// Get all jobs
110+
r.Jobs, err = s.getPipelineJobs(pipeline)
111+
if err != nil {
112+
gaia.Cfg.Logger.Error("cannot get pipeline jobs before execution", "error", err.Error())
113+
114+
// Update store
115+
r.Status = gaia.RunFailed
116+
s.storeService.PipelinePutRun(&r)
117+
continue
118+
}
119+
120+
// Check if this pipeline has jobs declared
121+
if len(r.Jobs) == 0 {
122+
continue
123+
}
124+
125+
// Schedule jobs and execute them.
126+
// Also update the run in the store.
127+
s.scheduleJobsByPriority(&r, pipeline)
110128
}
111129
}
112130

@@ -176,33 +194,6 @@ func (s *Scheduler) SchedulePipeline(p *gaia.Pipeline) (*gaia.PipelineRun, error
176194
return &run, s.storeService.PipelinePutRun(&run)
177195
}
178196

179-
// executePipeline executes the given pipeline and updates it status periodically.
180-
func (s *Scheduler) executePipeline(p *gaia.Pipeline, r *gaia.PipelineRun) {
181-
// Set start time
182-
r.RunDate = time.Now()
183-
184-
// Get all jobs
185-
var err error
186-
r.Jobs, err = s.getPipelineJobs(p)
187-
if err != nil {
188-
gaia.Cfg.Logger.Error("cannot get pipeline jobs before execution", "error", err.Error())
189-
190-
// Update store
191-
r.Status = gaia.RunFailed
192-
s.storeService.PipelinePutRun(r)
193-
return
194-
}
195-
196-
// Check if this pipeline has jobs declared
197-
if len(r.Jobs) == 0 {
198-
return
199-
}
200-
201-
// Schedule jobs and execute them.
202-
// Also update the run in the store.
203-
s.scheduleJobsByPriority(r, p)
204-
}
205-
206197
// executeJob executes a single job.
207198
// This method is blocking.
208199
func executeJob(job *gaia.Job, p *gaia.Pipeline, wg *sync.WaitGroup, triggerSave chan bool) {
@@ -211,13 +202,6 @@ func executeJob(job *gaia.Job, p *gaia.Pipeline, wg *sync.WaitGroup, triggerSave
211202
triggerSave <- true
212203
}()
213204

214-
// In testmode we do not test this.
215-
// TODO: Bad testing. Fix this asap!
216-
if gaia.Cfg.TestMode {
217-
job.Status = gaia.JobSuccess
218-
return
219-
}
220-
221205
// Set Job to running
222206
job.Status = gaia.JobRunning
223207

@@ -276,7 +260,7 @@ func (s *Scheduler) scheduleJobsByPriority(r *gaia.PipelineRun, p *gaia.Pipeline
276260
var wg sync.WaitGroup
277261
triggerSave := make(chan bool)
278262
for id, job := range r.Jobs {
279-
if job.Priority == lowestPrio {
263+
if job.Priority == lowestPrio && job.Status == gaia.JobWaitingExec {
280264
// Increase wait group by one
281265
wg.Add(1)
282266

@@ -320,11 +304,6 @@ func (s *Scheduler) scheduleJobsByPriority(r *gaia.PipelineRun, p *gaia.Pipeline
320304
// getJobResultsAndStore
321305
func (s *Scheduler) getJobResultsAndStore(triggerSave chan bool, r *gaia.PipelineRun) {
322306
for _ = range triggerSave {
323-
// TODO: Bad testing. Fix this asap!
324-
if gaia.Cfg.TestMode {
325-
continue
326-
}
327-
328307
// Store update
329308
s.storeService.PipelinePutRun(r)
330309
}

scheduler/scheduler_test.go

+48-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package scheduler
22

33
import (
4+
"fmt"
45
"hash/fnv"
6+
"os"
57
"testing"
68

79
"github.com/gaia-pipeline/gaia"
@@ -11,39 +13,77 @@ import (
1113

1214
func TestScheduleJobsByPriority(t *testing.T) {
1315
gaia.Cfg = &gaia.Config{}
14-
gaia.Cfg.TestMode = true
16+
storeInstance := store.NewStore()
17+
gaia.Cfg.DataPath = "data"
18+
gaia.Cfg.Bolt.Path = "test.db"
19+
gaia.Cfg.Bolt.Mode = 0600
1520

21+
// Create test folder
22+
err := os.MkdirAll(gaia.Cfg.DataPath, 0700)
23+
if err != nil {
24+
fmt.Printf("cannot create data folder: %s\n", err.Error())
25+
t.Fatal(err)
26+
}
27+
28+
if err = storeInstance.Init(); err != nil {
29+
t.Fatal(err)
30+
}
31+
p, r := prepareTestData()
32+
s := NewScheduler(storeInstance)
33+
s.scheduleJobsByPriority(r, p)
34+
35+
// Iterate jobs
36+
for _, job := range r.Jobs {
37+
if job.Status != gaia.JobSuccess {
38+
t.Fatalf("job status should be success but was %s", string(job.Status))
39+
} else {
40+
t.Logf("Job %s has been executed...", job.Title)
41+
}
42+
}
43+
44+
// cleanup
45+
err = os.Remove("data/test.db")
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
err = os.Remove("data")
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
}
54+
55+
func prepareTestData() (pipeline *gaia.Pipeline, pipelineRun *gaia.PipelineRun) {
1656
job1 := gaia.Job{
1757
ID: hash("Job1"),
1858
Title: "Job1",
1959
Priority: 0,
20-
Status: gaia.JobWaitingExec,
60+
Status: gaia.JobSuccess,
2161
}
2262
job2 := gaia.Job{
2363
ID: hash("Job2"),
2464
Title: "Job2",
2565
Priority: 10,
26-
Status: gaia.JobWaitingExec,
66+
Status: gaia.JobSuccess,
2767
}
2868
job3 := gaia.Job{
2969
ID: hash("Job3"),
3070
Title: "Job3",
3171
Priority: 20,
32-
Status: gaia.JobWaitingExec,
72+
Status: gaia.JobSuccess,
3373
}
3474
job4 := gaia.Job{
3575
ID: hash("Job4"),
3676
Title: "Job4",
3777
Priority: 20,
38-
Status: gaia.JobWaitingExec,
78+
Status: gaia.JobSuccess,
3979
}
4080

41-
p := &gaia.Pipeline{
81+
pipeline = &gaia.Pipeline{
4282
ID: 1,
4383
Name: "Test Pipeline",
4484
Type: gaia.GOLANG,
4585
}
46-
r := &gaia.PipelineRun{
86+
pipelineRun = &gaia.PipelineRun{
4787
ID: 1,
4888
PipelineID: 1,
4989
Status: gaia.RunNotScheduled,
@@ -55,18 +95,7 @@ func TestScheduleJobsByPriority(t *testing.T) {
5595
job4,
5696
},
5797
}
58-
59-
s := NewScheduler(store.NewStore())
60-
s.scheduleJobsByPriority(r, p)
61-
62-
// Iterate jobs
63-
for _, job := range r.Jobs {
64-
if job.Status != gaia.JobSuccess {
65-
t.Fatalf("job status should be success but was %s", string(job.Status))
66-
} else {
67-
t.Logf("Job %s has been executed...", job.Title)
68-
}
69-
}
98+
return
7099
}
71100

72101
// hash hashes the given string.

store/store.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ func (s *Store) Init() error {
5454
s.db = db
5555

5656
// Setup database
57-
return setupDatabase(s)
57+
return s.setupDatabase()
5858
}
5959

6060
// setupDatabase create all buckets in the db.
6161
// Additionally, it makes sure that the admin user exists.
62-
func setupDatabase(s *Store) error {
62+
func (s *Store) setupDatabase() error {
6363
// Create bucket if not exists function
6464
var bucketName []byte
6565
c := func(tx *bolt.Tx) error {

store/store_test.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@ import (
99
)
1010

1111
var store *Store
12-
var config *gaia.Config
1312

1413
func TestMain(m *testing.M) {
1514
store = NewStore()
16-
config = &gaia.Config{}
17-
config.DataPath = "data"
18-
config.Bolt.Path = "test.db"
19-
config.Bolt.Mode = 0600
15+
gaia.Cfg = &gaia.Config{}
16+
gaia.Cfg.DataPath = "data"
17+
gaia.Cfg.Bolt.Path = "test.db"
18+
gaia.Cfg.Bolt.Mode = 0600
19+
20+
// Create test folder
21+
err := os.MkdirAll(gaia.Cfg.DataPath, 0700)
22+
if err != nil {
23+
fmt.Printf("cannot create data folder: %s\n", err.Error())
24+
os.Exit(1)
25+
}
2026

2127
r := m.Run()
2228

2329
// cleanup
24-
err := os.Remove("data")
30+
err = os.Remove("data")
2531
if err != nil {
2632
fmt.Printf("cannot remove data folder: %s\n", err.Error())
2733
r = 1
@@ -30,7 +36,7 @@ func TestMain(m *testing.M) {
3036
}
3137

3238
func TestInit(t *testing.T) {
33-
err := store.Init(config)
39+
err := store.Init()
3440
if err != nil {
3541
t.Fatal(err)
3642
}
@@ -43,7 +49,7 @@ func TestInit(t *testing.T) {
4349
}
4450

4551
func TestUserGet(t *testing.T) {
46-
err := store.Init(config)
52+
err := store.Init()
4753
if err != nil {
4854
t.Fatal(err)
4955
}
@@ -81,7 +87,7 @@ func TestUserGet(t *testing.T) {
8187
}
8288

8389
func TestUserPut(t *testing.T) {
84-
err := store.Init(config)
90+
err := store.Init()
8591
if err != nil {
8692
t.Fatal(err)
8793
}
@@ -103,7 +109,7 @@ func TestUserPut(t *testing.T) {
103109
}
104110

105111
func TestUserAuth(t *testing.T) {
106-
err := store.Init(config)
112+
err := store.Init()
107113
if err != nil {
108114
t.Fatal(err)
109115
}

0 commit comments

Comments
 (0)