/
scheduler_test.go
106 lines (96 loc) · 1.99 KB
/
scheduler_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
package scheduler
import (
"fmt"
"hash/fnv"
"os"
"testing"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/store"
uuid "github.com/satori/go.uuid"
)
func TestScheduleJobsByPriority(t *testing.T) {
gaia.Cfg = &gaia.Config{}
storeInstance := store.NewStore()
gaia.Cfg.DataPath = "data"
gaia.Cfg.Bolt.Path = "test.db"
gaia.Cfg.Bolt.Mode = 0600
// Create test folder
err := os.MkdirAll(gaia.Cfg.DataPath, 0700)
if err != nil {
fmt.Printf("cannot create data folder: %s\n", err.Error())
t.Fatal(err)
}
if err = storeInstance.Init(); err != nil {
t.Fatal(err)
}
p, r := prepareTestData()
s := NewScheduler(storeInstance)
s.scheduleJobsByPriority(r, p)
// Iterate jobs
for _, job := range r.Jobs {
if job.Status != gaia.JobSuccess {
t.Fatalf("job status should be success but was %s", string(job.Status))
} else {
t.Logf("Job %s has been executed...", job.Title)
}
}
// cleanup
err = os.Remove("data/test.db")
if err != nil {
t.Fatal(err)
}
err = os.Remove("data")
if err != nil {
t.Fatal(err)
}
}
func prepareTestData() (pipeline *gaia.Pipeline, pipelineRun *gaia.PipelineRun) {
job1 := gaia.Job{
ID: hash("Job1"),
Title: "Job1",
Priority: 0,
Status: gaia.JobSuccess,
}
job2 := gaia.Job{
ID: hash("Job2"),
Title: "Job2",
Priority: 10,
Status: gaia.JobSuccess,
}
job3 := gaia.Job{
ID: hash("Job3"),
Title: "Job3",
Priority: 20,
Status: gaia.JobSuccess,
}
job4 := gaia.Job{
ID: hash("Job4"),
Title: "Job4",
Priority: 20,
Status: gaia.JobSuccess,
}
pipeline = &gaia.Pipeline{
ID: 1,
Name: "Test Pipeline",
Type: gaia.GOLANG,
}
pipelineRun = &gaia.PipelineRun{
ID: 1,
PipelineID: 1,
Status: gaia.RunNotScheduled,
UniqueID: uuid.Must(uuid.NewV4()).String(),
Jobs: []gaia.Job{
job1,
job2,
job3,
job4,
},
}
return
}
// hash hashes the given string.
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}