@@ -57,6 +57,25 @@ var _ = Describe("HypervisorServiceController", func() {
5757 }`
5858 )
5959
60+ mockServiceUpdate := func (expectedBody string ) {
61+ // Mock services.Update
62+ fakeServer .Mux .HandleFunc ("PUT /os-services/1234" , func (w http.ResponseWriter , r * http.Request ) {
63+ // parse request
64+ Expect (r .Method ).To (Equal ("PUT" ))
65+ Expect (r .Header .Get ("Content-Type" )).To (Equal ("application/json" ))
66+
67+ // verify request body
68+ body := make ([]byte , r .ContentLength )
69+ _ , err := r .Body .Read (body )
70+ Expect (err == nil || err .Error () == "EOF" ).To (BeTrue ())
71+ Expect (string (body )).To (MatchJSON (expectedBody ))
72+
73+ w .WriteHeader (http .StatusOK )
74+ _ , err = fmt .Fprint (w , ServiceEnabledResponse )
75+ Expect (err ).NotTo (HaveOccurred ())
76+ })
77+ }
78+
6079 // Setup and teardown
6180 BeforeEach (func (ctx context.Context ) {
6281 By ("Setting up the OpenStack http mock server" )
@@ -115,24 +134,8 @@ var _ = Describe("HypervisorServiceController", func() {
115134 Expect (tc .Client .Get (ctx , hypervisorName , hypervisor )).To (Succeed ())
116135 hypervisor .Spec .Maintenance = ""
117136 Expect (tc .Client .Update (ctx , hypervisor )).To (Succeed ())
118- // Mock services.Update
119- fakeServer .Mux .HandleFunc ("PUT /os-services/1234" , func (w http.ResponseWriter , r * http.Request ) {
120- // parse request
121- Expect (r .Method ).To (Equal ("PUT" ))
122- Expect (r .Header .Get ("Content-Type" )).To (Equal ("application/json" ))
123-
124- // verify request body
125- expectedBody := `{"status": "enabled"}`
126- body := make ([]byte , r .ContentLength )
127- _ , err := r .Body .Read (body )
128- Expect (err == nil || err .Error () == "EOF" ).To (BeTrue ())
129- Expect (string (body )).To (MatchJSON (expectedBody ))
130-
131- w .WriteHeader (http .StatusOK )
132- _ , err = fmt .Fprint (w , ServiceEnabledResponse )
133- Expect (err ).NotTo (HaveOccurred ())
134- })
135-
137+ expectedBody := `{"status": "enabled"}`
138+ mockServiceUpdate (expectedBody )
136139 req := ctrl.Request {NamespacedName : hypervisorName }
137140 _ , err := tc .Reconcile (ctx , req )
138141 Expect (err ).NotTo (HaveOccurred ())
@@ -153,24 +156,8 @@ var _ = Describe("HypervisorServiceController", func() {
153156 Expect (tc .Client .Get (ctx , hypervisorName , hypervisor )).To (Succeed ())
154157 hypervisor .Spec .Maintenance = mode
155158 Expect (tc .Client .Update (ctx , hypervisor )).To (Succeed ())
156- // Mock services.Update
157- fakeServer .Mux .HandleFunc ("PUT /os-services/1234" , func (w http.ResponseWriter , r * http.Request ) {
158- // parse request
159- Expect (r .Method ).To (Equal ("PUT" ))
160- Expect (r .Header .Get ("Content-Type" )).To (Equal ("application/json" ))
161-
162- // verify request body
163- expectedBody := fmt .Sprintf (`{"disabled_reason": "Hypervisor CRD: spec.maintenance=%v", "status": "disabled"}` , mode )
164- body := make ([]byte , r .ContentLength )
165- _ , err := r .Body .Read (body )
166- Expect (err == nil || err .Error () == "EOF" ).To (BeTrue ())
167- Expect (string (body )).To (MatchJSON (expectedBody ))
168-
169- w .WriteHeader (http .StatusOK )
170- _ , err = fmt .Fprint (w , ServiceEnabledResponse )
171- Expect (err ).NotTo (HaveOccurred ())
172- })
173-
159+ expectedBody := fmt .Sprintf (`{"disabled_reason": "Hypervisor CRD: spec.maintenance=%v", "status": "disabled"}` , mode )
160+ mockServiceUpdate (expectedBody )
174161 req := ctrl.Request {NamespacedName : hypervisorName }
175162 _ , err := tc .Reconcile (ctx , req )
176163 Expect (err ).NotTo (HaveOccurred ())
0 commit comments