forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dummy_nats_job_supervisor_test.go
150 lines (130 loc) · 4.81 KB
/
dummy_nats_job_supervisor_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
package jobsupervisor_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
boshalert "github.com/cloudfoundry/bosh-agent/agent/alert"
boshhandler "github.com/cloudfoundry/bosh-agent/handler"
. "github.com/cloudfoundry/bosh-agent/jobsupervisor"
fakembus "github.com/cloudfoundry/bosh-agent/mbus/fakes"
)
var _ = Describe("dummyNatsJobSupervisor", func() {
var (
dummyNats JobSupervisor
handler *fakembus.FakeHandler
)
BeforeEach(func() {
handler = &fakembus.FakeHandler{}
dummyNats = NewDummyNatsJobSupervisor(handler)
})
Describe("MonitorJobFailures", func() {
It("monitors job status", func() {
err := dummyNats.MonitorJobFailures(func(boshalert.MonitAlert) error { return nil })
Expect(err).NotTo(HaveOccurred())
Expect(handler.RegisteredAdditionalFunc).ToNot(BeNil())
})
})
Describe("Status", func() {
BeforeEach(func() {
err := dummyNats.MonitorJobFailures(func(boshalert.MonitAlert) error { return nil })
Expect(err).NotTo(HaveOccurred())
})
It("returns the received status", func() {
statusMessage := boshhandler.NewRequest("", "set_dummy_status", []byte(`{"status":"failing"}`), 0)
handler.RegisteredAdditionalFunc(statusMessage)
Expect(dummyNats.Status()).To(Equal("failing"))
})
It("returns running as a default value", func() {
Expect(dummyNats.Status()).To(Equal("running"))
})
It("does not change the status given other messages", func() {
statusMessage := boshhandler.NewRequest("", "some_other_message", []byte(`{"status":"failing"}`), 0)
handler.RegisteredAdditionalFunc(statusMessage)
Expect(dummyNats.Status()).To(Equal("running"))
})
})
Describe("Start", func() {
BeforeEach(func() {
err := dummyNats.MonitorJobFailures(func(boshalert.MonitAlert) error { return nil })
Expect(err).NotTo(HaveOccurred())
})
Context("When set_task_fail flag is sent in messagae", func() {
It("raises an error", func() {
statusMessage := boshhandler.NewRequest("", "set_task_fail", []byte(`{"status":"fail_task"}`), 0)
handler.RegisteredAdditionalFunc(statusMessage)
err := dummyNats.Start()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-task-fail-error"))
})
})
Context("when set_task_fail flag is not sent in message", func() {
It("does not raise an error", func() {
statusMessage := boshhandler.NewRequest("", "set_task_fail", []byte(`{"status":"something_else"}`), 0)
handler.RegisteredAdditionalFunc(statusMessage)
err := dummyNats.Start()
Expect(err).ToNot(HaveOccurred())
})
})
})
Describe("Stop", func() {
It("changes status to 'stopped'", func() {
err := dummyNats.Stop()
Expect(err).ToNot(HaveOccurred())
Expect(dummyNats.Status()).To(Equal("stopped"))
})
Context("when a job is failing", func() {
BeforeEach(func() {
err := dummyNats.MonitorJobFailures(func(boshalert.MonitAlert) error { return nil })
Expect(err).NotTo(HaveOccurred())
})
Context("with 'fail_task'", func() {
It("does not change status", func() {
statusMessage := boshhandler.NewRequest("", "set_task_fail", []byte(`{"status":"fail_task"}`), 0)
handler.RegisteredAdditionalFunc(statusMessage)
err := dummyNats.Stop()
Expect(err).ToNot(HaveOccurred())
Expect(dummyNats.Status()).To(Equal("fail_task"))
})
})
Context("with 'failing'", func() {
It("does not change status", func() {
statusMessage := boshhandler.NewRequest("", "set_dummy_status", []byte(`{"status":"failing"}`), 0)
handler.RegisteredAdditionalFunc(statusMessage)
err := dummyNats.Stop()
Expect(err).ToNot(HaveOccurred())
Expect(dummyNats.Status()).To(Equal("failing"))
})
})
})
})
Describe("StopAndWait", func() {
It("changes status to 'stopped'", func() {
err := dummyNats.StopAndWait()
Expect(err).ToNot(HaveOccurred())
Expect(dummyNats.Status()).To(Equal("stopped"))
})
Context("when a job is failing", func() {
BeforeEach(func() {
err := dummyNats.MonitorJobFailures(func(boshalert.MonitAlert) error { return nil })
Expect(err).NotTo(HaveOccurred())
})
Context("with 'fail_task'", func() {
It("does not change status", func() {
statusMessage := boshhandler.NewRequest("", "set_task_fail", []byte(`{"status":"fail_task"}`), 0)
handler.RegisteredAdditionalFunc(statusMessage)
err := dummyNats.StopAndWait()
Expect(err).ToNot(HaveOccurred())
Expect(dummyNats.Status()).To(Equal("fail_task"))
})
})
Context("with 'failing'", func() {
It("does not change status", func() {
statusMessage := boshhandler.NewRequest("", "set_dummy_status", []byte(`{"status":"failing"}`), 0)
handler.RegisteredAdditionalFunc(statusMessage)
err := dummyNats.StopAndWait()
Expect(err).ToNot(HaveOccurred())
Expect(dummyNats.Status()).To(Equal("failing"))
})
})
})
})
})