Skip to content

Commit 4ef8339

Browse files
committed
Update to gophercloud 2.8
testhelper returns now a fake-server that needs to be passed around. In turn, we can drop copied code for updating the service.
1 parent 99b6c64 commit 4ef8339

10 files changed

Lines changed: 48 additions & 68 deletions

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module github.com/cobaltcore-dev/openstack-hypervisor-operator
66
go 1.24.7
77

88
require (
9-
github.com/gophercloud/gophercloud/v2 v2.7.0
10-
github.com/gophercloud/utils/v2 v2.0.0-20250711132455-9770683b100a
9+
github.com/gophercloud/gophercloud/v2 v2.8.0
10+
github.com/gophercloud/utils/v2 v2.0.0-20250930154317-576cdf6142a7
1111
github.com/onsi/ginkgo/v2 v2.26.0
1212
github.com/onsi/gomega v1.38.2
1313
k8s.io/api v0.34.1

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ github.com/google/pprof v0.0.0-20250403155104-27863c87afa6 h1:BHT72Gu3keYf3ZEu2J
7575
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
7676
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
7777
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
78-
github.com/gophercloud/gophercloud/v2 v2.7.0 h1:o0m4kgVcPgHlcXiWAjoVxGd8QCmvM5VU+YM71pFbn0E=
79-
github.com/gophercloud/gophercloud/v2 v2.7.0/go.mod h1:Ki/ILhYZr/5EPebrPL9Ej+tUg4lqx71/YH2JWVeU+Qk=
80-
github.com/gophercloud/utils/v2 v2.0.0-20250711132455-9770683b100a h1:erVLycqmezd0+eukgQ4xgLxGsByDKvqJxLXVc35tUYI=
81-
github.com/gophercloud/utils/v2 v2.0.0-20250711132455-9770683b100a/go.mod h1:1mckc18GQSFLRhDy2BjPGkkpbrjxY5iwX/oxpdTE2kw=
78+
github.com/gophercloud/gophercloud/v2 v2.8.0 h1:of2+8tT6+FbEYHfYC8GBu8TXJNsXYSNm9KuvpX7Neqo=
79+
github.com/gophercloud/gophercloud/v2 v2.8.0/go.mod h1:Ki/ILhYZr/5EPebrPL9Ej+tUg4lqx71/YH2JWVeU+Qk=
80+
github.com/gophercloud/utils/v2 v2.0.0-20250930154317-576cdf6142a7 h1:y0/17y47lq1dD97qEqbbvjsMF8PzLmqmD839aOePRuc=
81+
github.com/gophercloud/utils/v2 v2.0.0-20250930154317-576cdf6142a7/go.mod h1:dVCIqYUB0Q8JDbMZaReU6BkAQAS9j3l3Kyc7GuSIztU=
8282
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 h1:5ZPtiqj0JL5oKWmcsq4VMaAW5ukBEgSGXEN89zeH1Jo=
8383
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3/go.mod h1:ndYquD05frm2vACXE1nsccT4oJzjhw2arTS2cpUD1PI=
8484
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=

internal/controller/aggregates_controller_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ import (
3535
)
3636

3737
var _ = Describe("AggregatesController", func() {
38-
var tc *AggregatesController
38+
var (
39+
tc *AggregatesController
40+
fakeServer testhelper.FakeServer
41+
)
3942
const EOF = "EOF"
4043
const AggregateListBodyEmpty = `
4144
{
@@ -101,19 +104,19 @@ var _ = Describe("AggregatesController", func() {
101104

102105
BeforeEach(func(ctx context.Context) {
103106
By("Setting up the OpenStack http mock server")
104-
testhelper.SetupHTTP()
107+
fakeServer = testhelper.SetupHTTP()
105108

106109
By("Creating the AggregatesController")
107110
tc = &AggregatesController{
108111
Client: k8sClient,
109112
Scheme: k8sClient.Scheme(),
110-
computeClient: client.ServiceClient(),
113+
computeClient: client.ServiceClient(fakeServer),
111114
}
112115
})
113116

114117
AfterEach(func() {
115118
By("Tearing down the OpenStack http mock server")
116-
testhelper.TeardownHTTP()
119+
fakeServer.Teardown()
117120

118121
By("Deleting the Hypervisor resource")
119122
hypervisor := &kvmv1.Hypervisor{}
@@ -139,14 +142,14 @@ var _ = Describe("AggregatesController", func() {
139142
Expect(k8sClient.Create(ctx, hypervisor)).To(Succeed())
140143

141144
// Mock resourceproviders.GetAggregates
142-
testhelper.Mux.HandleFunc("GET /os-aggregates", func(w http.ResponseWriter, r *http.Request) {
145+
fakeServer.Mux.HandleFunc("GET /os-aggregates", func(w http.ResponseWriter, r *http.Request) {
143146
w.Header().Add("Content-Type", "application/json")
144147
w.WriteHeader(http.StatusOK)
145148

146149
_, err := fmt.Fprint(w, AggregateListBodyEmpty)
147150
Expect(err).NotTo(HaveOccurred())
148151
})
149-
testhelper.Mux.HandleFunc("POST /os-aggregates", func(w http.ResponseWriter, r *http.Request) {
152+
fakeServer.Mux.HandleFunc("POST /os-aggregates", func(w http.ResponseWriter, r *http.Request) {
150153
w.Header().Add("Content-Type", "application/json")
151154
w.WriteHeader(http.StatusOK)
152155

@@ -155,7 +158,7 @@ var _ = Describe("AggregatesController", func() {
155158
})
156159

157160
// Mock resourceproviders.UpdateAggregates
158-
testhelper.Mux.HandleFunc("POST /os-aggregates/42/action", func(w http.ResponseWriter, r *http.Request) {
161+
fakeServer.Mux.HandleFunc("POST /os-aggregates/42/action", func(w http.ResponseWriter, r *http.Request) {
159162
// parse request
160163
Expect(r.Header.Get("Content-Type")).To(Equal("application/json"))
161164
expectedBody := `{"add_host":{"host":"hv-test"}}`
@@ -204,7 +207,7 @@ var _ = Describe("AggregatesController", func() {
204207
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
205208

206209
// Mock resourceproviders.GetAggregates
207-
testhelper.Mux.HandleFunc("GET /os-aggregates", func(w http.ResponseWriter, r *http.Request) {
210+
fakeServer.Mux.HandleFunc("GET /os-aggregates", func(w http.ResponseWriter, r *http.Request) {
208211
w.Header().Add("Content-Type", "application/json")
209212
w.WriteHeader(http.StatusOK)
210213

@@ -227,8 +230,8 @@ var _ = Describe("AggregatesController", func() {
227230
_, err = fmt.Fprint(w, AggregateRemoveHostBody)
228231
Expect(err).NotTo(HaveOccurred())
229232
}
230-
testhelper.Mux.HandleFunc("POST /os-aggregates/100001/action", expectRemoveHostFromAggregate)
231-
testhelper.Mux.HandleFunc("POST /os-aggregates/99/action", expectRemoveHostFromAggregate)
233+
fakeServer.Mux.HandleFunc("POST /os-aggregates/100001/action", expectRemoveHostFromAggregate)
234+
fakeServer.Mux.HandleFunc("POST /os-aggregates/99/action", expectRemoveHostFromAggregate)
232235
})
233236

234237
It("should update Aggregates and set status condition when Aggregates differ", func() {

internal/controller/decomission_controller_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package controller
2020
import (
2121
"context"
2222

23-
"github.com/gophercloud/gophercloud/v2/testhelper"
2423
. "github.com/onsi/ginkgo/v2"
2524
. "github.com/onsi/gomega"
2625
corev1 "k8s.io/api/core/v1"
@@ -88,9 +87,6 @@ var _ = Describe("Decommission Controller", func() {
8887

8988
It("should successfully reconcile the resource", func(ctx context.Context) {
9089
By("ConditionType the created resource")
91-
testhelper.SetupHTTP()
92-
defer testhelper.TeardownHTTP()
93-
9490
req := ctrl.Request{
9591
NamespacedName: types.NamespacedName{Name: nodeName},
9692
}

internal/controller/eviction_controller_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ var _ = Describe("Eviction Controller", func() {
9595
}
9696
reconcileRequest = reconcile.Request{NamespacedName: typeNamespacedName}
9797
controllerReconciler *EvictionReconciler
98+
fakeServer testhelper.FakeServer
9899
)
99100

100101
ctx := context.Background()
101102

102103
BeforeEach(func() {
103104
By("Setting up the OpenStack http mock server")
104-
testhelper.SetupHTTP()
105+
fakeServer = testhelper.SetupHTTP()
105106
})
106107

107108
AfterEach(func() {
@@ -121,7 +122,7 @@ var _ = Describe("Eviction Controller", func() {
121122
}
122123

123124
By("Tearing down the OpenStack http mock server")
124-
testhelper.TeardownHTTP()
125+
fakeServer.Teardown()
125126
controllerReconciler = nil
126127
hv := &kvmv1.Hypervisor{
127128
ObjectMeta: metav1.ObjectMeta{
@@ -211,14 +212,14 @@ var _ = Describe("Eviction Controller", func() {
211212
controllerReconciler = &EvictionReconciler{
212213
Client: k8sClient,
213214
Scheme: k8sClient.Scheme(),
214-
computeClient: client.ServiceClient(),
215+
computeClient: client.ServiceClient(fakeServer),
215216
rand: rand.New(rand.NewSource(42)),
216217
}
217218
})
218219

219220
When("hypervisor is not found in openstack", func() {
220221
BeforeEach(func() {
221-
testhelper.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
222+
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
222223
w.Header().Add("Content-Type", "application/json")
223224
w.WriteHeader(http.StatusOK)
224225
Expect(fmt.Fprintf(w, `{"hypervisors": []}`)).ToNot(BeNil())
@@ -249,12 +250,12 @@ var _ = Describe("Eviction Controller", func() {
249250
})
250251
When("enabled hypervisor has no servers", func() {
251252
BeforeEach(func() {
252-
testhelper.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
253+
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
253254
w.Header().Add("Content-Type", "application/json")
254255
w.WriteHeader(http.StatusOK)
255256
Expect(fmt.Fprintf(w, HypervisorWithServers, serviceId, "", hypervisorName)).ToNot(BeNil())
256257
})
257-
testhelper.Mux.HandleFunc("PUT /os-services/test-id", func(w http.ResponseWriter, r *http.Request) {
258+
fakeServer.Mux.HandleFunc("PUT /os-services/test-id", func(w http.ResponseWriter, r *http.Request) {
258259
w.Header().Add("Content-Type", "application/json")
259260
w.WriteHeader(http.StatusOK)
260261
Expect(fmt.Fprintf(w, `{"service": {"id": "%v", "status": "disabled"}}`, serviceId)).ToNot(BeNil())
@@ -346,12 +347,12 @@ var _ = Describe("Eviction Controller", func() {
346347
})
347348
When("disabled hypervisor has no servers", func() {
348349
BeforeEach(func() {
349-
testhelper.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
350+
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
350351
w.Header().Add("Content-Type", "application/json")
351352
w.WriteHeader(http.StatusOK)
352353
Expect(fmt.Fprintf(w, HypervisorWithServers, serviceId, "some reason", hypervisorName)).ToNot(BeNil())
353354
})
354-
testhelper.Mux.HandleFunc("PUT /os-services/test-id", func(w http.ResponseWriter, r *http.Request) {
355+
fakeServer.Mux.HandleFunc("PUT /os-services/test-id", func(w http.ResponseWriter, r *http.Request) {
355356
w.WriteHeader(http.StatusOK)
356357
Expect(fmt.Fprintf(w, `{"service": {"id": "%v", "status": "disabled"}}`, serviceId)).ToNot(BeNil())
357358
})

internal/controller/node_eviction_label_controller_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var _ = Describe("Node Eviction Label Controller", func() {
4545
var (
4646
nodeReconciler *NodeEvictionLabelReconciler
4747
req = ctrl.Request{NamespacedName: types.NamespacedName{Name: nodeName}}
48+
fakeServer testhelper.FakeServer
4849
)
4950

5051
Context("When reconciling a node", func() {
@@ -61,8 +62,8 @@ var _ = Describe("Node Eviction Label Controller", func() {
6162
}
6263

6364
BeforeEach(func() {
64-
testhelper.SetupHTTP()
65-
Expect(os.Setenv("KVM_HA_SERVICE_URL", testhelper.Endpoint())).To(Succeed())
65+
fakeServer = testhelper.SetupHTTP()
66+
Expect(os.Setenv("KVM_HA_SERVICE_URL", fakeServer.Endpoint())).To(Succeed())
6667
nodeReconciler = &NodeEvictionLabelReconciler{
6768
Client: k8sClient,
6869
Scheme: k8sClient.Scheme(),
@@ -120,7 +121,7 @@ var _ = Describe("Node Eviction Label Controller", func() {
120121
node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}}
121122
By("Cleanup the specific node")
122123
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, node))).To(Succeed())
123-
testhelper.TeardownHTTP()
124+
fakeServer.Teardown()
124125
})
125126

126127
It("should successfully reconcile the resource", func() {

internal/controller/onboarding_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (r *OnboardingController) initialOnboarding(ctx context.Context, hv *kvmv1.
206206
Status: services.ServiceEnabled,
207207
ForcedDown: &falseVal,
208208
}
209-
if result := openstack.UpdateService(ctx, r.computeClient, hv.Status.ServiceID, opts); result.Err != nil {
209+
if result := services.Update(ctx, r.computeClient, hv.Status.ServiceID, opts); result.Err != nil {
210210
return result.Err
211211
}
212212

internal/controller/onboarding_controller_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package controller
2020
import (
2121
"context"
2222

23-
"github.com/gophercloud/gophercloud/v2/testhelper"
2423
. "github.com/onsi/ginkgo/v2"
2524
. "github.com/onsi/gomega"
2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -32,7 +31,9 @@ import (
3231
)
3332

3433
var _ = Describe("Onboarding Controller", func() {
35-
var onboardingReconciler *OnboardingController
34+
var (
35+
onboardingReconciler *OnboardingController
36+
)
3637

3738
Context("When reconciling a hypervisor", func() {
3839
const hypervisorName = "some-test"
@@ -66,15 +67,9 @@ var _ = Describe("Onboarding Controller", func() {
6667
Spec: kvmv1.HypervisorSpec{},
6768
}
6869
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
69-
70-
By("Setting up the OpenStack http mock server")
71-
testhelper.SetupHTTP()
7270
})
7371

7472
AfterEach(func() {
75-
By("Clean up the OpenStack http mock server")
76-
testhelper.TeardownHTTP()
77-
7873
hv := &kvmv1.Hypervisor{ObjectMeta: metav1.ObjectMeta{Name: hypervisorName}}
7974
By("Cleanup the specific hypervisor CRO")
8075
Expect(client.IgnoreAlreadyExists(k8sClient.Delete(ctx, hv))).To(Succeed())

internal/controller/traits_controller_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ import (
3535
)
3636

3737
var _ = Describe("TraitsController", func() {
38-
var tc *TraitsController
38+
var (
39+
tc *TraitsController
40+
fakeServer testhelper.FakeServer
41+
)
42+
3943
const TraitsBody = `
4044
{
4145
"resource_provider_generation": 1,
@@ -58,13 +62,13 @@ var _ = Describe("TraitsController", func() {
5862

5963
BeforeEach(func(ctx context.Context) {
6064
By("Setting up the OpenStack http mock server")
61-
testhelper.SetupHTTP()
65+
fakeServer = testhelper.SetupHTTP()
6266

6367
By("Creating the TraitsController")
6468
tc = &TraitsController{
6569
Client: k8sClient,
6670
Scheme: k8sClient.Scheme(),
67-
serviceClient: client.ServiceClient(),
71+
serviceClient: client.ServiceClient(fakeServer),
6872
}
6973

7074
By("Creating a Hypervisor resource")
@@ -91,29 +95,29 @@ var _ = Describe("TraitsController", func() {
9195
})
9296

9397
AfterEach(func() {
94-
By("Tearing down the OpenStack http mock server")
95-
testhelper.TeardownHTTP()
96-
9798
By("Deleting the Hypervisor resource")
9899
hypervisor := &kvmv1.Hypervisor{}
99100
Expect(tc.Client.Get(ctx, types.NamespacedName{Name: "hv-test", Namespace: "default"}, hypervisor)).To(Succeed())
100101
Expect(tc.Client.Delete(ctx, hypervisor)).To(Succeed())
102+
103+
By("Tearing down the OpenStack http mock server")
104+
fakeServer.Teardown()
101105
})
102106

103107
// Tests
104108

105109
Context("Reconcile", func() {
106110
BeforeEach(func() {
107111
// Mock resourceproviders.GetTraits
108-
testhelper.Mux.HandleFunc("GET /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
112+
fakeServer.Mux.HandleFunc("GET /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
109113
w.Header().Add("Content-Type", "application/json")
110114
w.WriteHeader(http.StatusOK)
111115

112116
_, err := fmt.Fprint(w, TraitsBody)
113117
Expect(err).NotTo(HaveOccurred())
114118
})
115119
// Mock resourceproviders.UpdateTraits
116-
testhelper.Mux.HandleFunc("PUT /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
120+
fakeServer.Mux.HandleFunc("PUT /resource_providers/1234/traits", func(w http.ResponseWriter, r *http.Request) {
117121
// parse request
118122
Expect(r.Method).To(Equal("PUT"))
119123
Expect(r.Header.Get("Content-Type")).To(Equal("application/json"))

internal/openstack/services.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ limitations under the License.
2525
package openstack
2626

2727
import (
28-
"context"
29-
3028
"github.com/gophercloud/gophercloud/v2"
3129
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/services"
3230
)
@@ -47,21 +45,3 @@ type UpdateServiceOpts struct {
4745
func (opts UpdateServiceOpts) ToServiceUpdateMap() (map[string]any, error) {
4846
return gophercloud.BuildRequestBody(opts, "")
4947
}
50-
51-
func updateServiceURL(c *gophercloud.ServiceClient, id string) string {
52-
return c.ServiceURL("os-services", id)
53-
}
54-
55-
// UpdateService requests that various attributes of the indicated service be changed.
56-
func UpdateService(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpdateServiceOpts) (r services.UpdateResult) {
57-
b, err := opts.ToServiceUpdateMap()
58-
if err != nil {
59-
r.Err = err
60-
return
61-
}
62-
resp, err := client.Put(ctx, updateServiceURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
63-
OkCodes: []int{200},
64-
})
65-
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
66-
return
67-
}

0 commit comments

Comments
 (0)