-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
74 lines (62 loc) · 1.91 KB
/
context_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
package main
import (
"os"
"path/filepath"
"strconv"
"testing"
"time"
)
func TestContext(t *testing.T) {
tmpdir, err := os.MkdirTemp(os.TempDir(), "lurch-test-workdir")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmpdir)
conf := LoadConfig([]string{"-t", tmpdir})
c := NewContext(conf)
NewWebSocketService(c)
p1 := c.OpenProject("project-1")
if err = os.MkdirAll(p1.dir, 0755); err != nil {
panic(err)
}
os.WriteFile(filepath.Join(p1.dir, "script.sh"), []byte("#!/bin/sh\n\necho Project-1 run\nsleep 1"), 0755)
p2 := c.OpenProject("project-2")
if err = os.MkdirAll(p2.dir, 0755); err != nil {
panic(err)
}
os.WriteFile(filepath.Join(p2.dir, "script.sh"), []byte("#!/bin/sh\n\nmkdir target\ntouch target/data\necho Project-2 run\nsleep 60"), 0755)
for i := 0; i < 12; i++ {
p2.NewJob()
}
if projects, err := c.ListProjects(); err != nil {
panic(err)
} else if len(projects) != 2 {
t.Fatalf("TestContext: 2 project were expected, but found %d", len(projects))
}
c.StartJob(p1, map[string]string{"key1": "val1", "_key2": "val2"})
time.Sleep(3 * time.Second)
if jobs, err := c.ListJobs(p1); err != nil {
panic(err)
} else if s := jobs[0].Status(); s != Finished {
t.Fatalf("TestContext: job ends with unexpected status %s", s.String())
}
if job := c.OpenJob(p1, "1"); err != nil {
panic(err)
} else if s := job.Status(); s != Finished {
t.Fatalf("TestContext: job ends with unexpected status %s", s.String())
}
c.StartJob(p2, nil)
time.Sleep(2 * time.Second)
j := c.OpenJob(p2, strconv.Itoa(p2.LastCount()))
if !c.IsBeingBuilt(j) {
t.Fatalf("TestContext: job #1 for project-2 should be still running")
}
c.Interrupt(j)
time.Sleep(2 * time.Second)
if c.IsBeingBuilt(j) {
t.Fatalf("TestContext: job #1 for project-2 should not be running")
}
if s := j.Status(); s != Stopped {
t.Fatalf("TestContext: job #1 for project-2 has status %s instead of stopped", s.String())
}
}