-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_test.go
138 lines (109 loc) · 2.37 KB
/
worker_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
package main
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/mock"
)
func TestDeployToGKE(t *testing.T) {
event := Event{
BuildID: "er45y76u",
Repository: "git@github.com:royge/build2gke.git",
Branch: "main",
Command: "make echo",
}
ctx := context.Background()
t.Run("Success", func(t *testing.T) {
if err := deployToGKE(ctx, &event); err != nil {
t.Errorf("unable to deploy to GKE: %v", err)
}
})
t.Run("Error", func(t *testing.T) {
event.Command = "make unknown"
if err := deployToGKE(ctx, &event); err == nil {
t.Error("expected error, but got nil")
}
})
}
func TestWorker_WatchForDeployment(t *testing.T) {
ctx := context.Background()
receiver := new(receiverMock)
receiver.On(
"Receive",
mock.Anything,
mock.AnythingOfType("func(context.Context, *pubsub.Message)"),
).Return(nil)
exit := make(chan interface{})
worker := Worker{
Receiver: receiver,
Exit: exit,
}
go func(t *testing.T) {
t.Helper()
_, err := worker.watchForDeployment(ctx, exit)
if err != nil {
t.Fatalf("failure to run the runner: %v", err)
}
exit <- true
}(t)
<-exit
}
func TestWorker_DoDeployment(t *testing.T) {
var err error
done := make(chan interface{})
jobs := make(chan Event)
doneJobs := make(<-chan Event)
ctx := context.Background()
worker := Worker{
Actions: []ActionFunc{
func(ctx context.Context, event *Event) error {
t.Log("[INFO] job: ", event)
return nil
},
},
}
go func(t *testing.T) {
t.Helper()
doneJobs, err = worker.doDeployment(ctx, done, jobs)
if err != nil {
t.Errorf("unable to do deployment: %v", err)
}
for job := range doneJobs {
t.Log("[INFO] done job:", job)
if job.Status != Success {
t.Errorf(
"want job status to be %v, got %v",
Success,
job.Status,
)
}
}
}(t)
go func(t *testing.T) {
t.Helper()
jobs <- Event{
BuildID: "123456",
Status: Pending,
}
t.Log("[INFO] waiting for 250 milliseconds...")
time.Sleep(250 * time.Millisecond)
jobs <- Event{
BuildID: "123457",
Status: Pending,
}
t.Log("[INFO] waiting for 2 seconds...")
time.Sleep(2 * time.Second)
jobs <- Event{
BuildID: "123458",
Status: Pending,
}
t.Log("[INFO] waiting for 3 seconds...")
time.Sleep(3 * time.Second)
jobs <- Event{
BuildID: "123459",
Status: Pending,
}
done <- true
}(t)
<-done
}