Skip to content

Commit 5e496d2

Browse files
authored
chore(tests): deprecate gomega (#2317)
* chore(tests): deprecate gomega in several suites * chore(fetchers): deprecate gomega * chore(go.mod): deprecate gomega * chore(fetchers): simplify tests
1 parent 4fd4bc9 commit 5e496d2

File tree

7 files changed

+83
-72
lines changed

7 files changed

+83
-72
lines changed

api/v1beta1/suite_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"testing"
2222

2323
. "github.com/onsi/ginkgo/v2"
24-
. "github.com/onsi/gomega"
2524
"github.com/stretchr/testify/require"
2625
"k8s.io/client-go/kubernetes/scheme"
2726
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -48,8 +47,6 @@ func TestAPIs(t *testing.T) {
4847
t.Skip("-short was passed, skipping CRDs")
4948
}
5049

51-
RegisterFailHandler(Fail)
52-
5350
RunSpecs(t, "CRDs Suite")
5451
}
5552

controllers/content/fetchers/suite_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1414

1515
. "github.com/onsi/ginkgo/v2"
16-
. "github.com/onsi/gomega"
1716
)
1817

1918
var (
@@ -26,8 +25,6 @@ func TestAPIs(t *testing.T) {
2625
t.Skip("-short was passed, skipping Fetchers")
2726
}
2827

29-
RegisterFailHandler(Fail)
30-
3128
RunSpecs(t, "Fetchers Suite")
3229
}
3330

controllers/content/fetchers/url_fetcher_test.go

Lines changed: 83 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package fetchers
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
7+
"net/http/httptest"
68

7-
"github.com/onsi/gomega/ghttp"
89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
1011
v1 "k8s.io/api/core/v1"
@@ -16,32 +17,92 @@ import (
1617
. "github.com/onsi/ginkgo/v2"
1718
)
1819

19-
var _ = Describe("Fetching dashboards from URL", func() {
20+
const (
21+
basicAuthUsername = "root"
22+
basicAuthPassword = "secret"
23+
)
24+
25+
func getCredentials(secretName string) (*v1.Secret, *v1beta1.GrafanaContentURLAuthorization) {
26+
GinkgoHelper()
27+
28+
const (
29+
usernameKey = "USERNAME"
30+
passwordKey = "PASSWORD"
31+
)
32+
33+
secret := &v1.Secret{
34+
ObjectMeta: metav1.ObjectMeta{
35+
Name: secretName,
36+
Namespace: "default",
37+
},
38+
StringData: map[string]string{
39+
usernameKey: basicAuthUsername,
40+
passwordKey: basicAuthPassword,
41+
},
42+
}
43+
44+
urlAuthorization := &v1beta1.GrafanaContentURLAuthorization{
45+
BasicAuth: &v1beta1.GrafanaContentURLBasicAuth{
46+
Username: &v1.SecretKeySelector{
47+
LocalObjectReference: v1.LocalObjectReference{
48+
Name: secretName,
49+
},
50+
Key: usernameKey,
51+
Optional: nil,
52+
},
53+
Password: &v1.SecretKeySelector{
54+
LocalObjectReference: v1.LocalObjectReference{
55+
Name: secretName,
56+
},
57+
Key: passwordKey,
58+
Optional: nil,
59+
},
60+
},
61+
}
62+
63+
return secret, urlAuthorization
64+
}
65+
66+
var _ = Describe("URL fetcher", Ordered, func() {
2067
t := GinkgoT()
2168

2269
want := []byte(`{"dummyField": "dummyData"}`)
2370
wantCompressed, err := cache.Gzip(want)
2471

2572
require.NoError(t, err)
2673

27-
var server *ghttp.Server
74+
publicEndpoint := "/public"
75+
privateEndpoint := "/private"
2876

29-
BeforeEach(func() {
30-
server = ghttp.NewServer()
77+
mux := http.NewServeMux()
78+
mux.HandleFunc(publicEndpoint, func(w http.ResponseWriter, req *http.Request) {
79+
w.Header().Set("Content-Type", "application/json")
80+
fmt.Fprint(w, string(want))
81+
})
82+
mux.HandleFunc(privateEndpoint, func(w http.ResponseWriter, req *http.Request) {
83+
username, password, ok := req.BasicAuth()
84+
if !ok || username != basicAuthUsername || password != basicAuthPassword {
85+
http.Error(w, "Unauthorized", http.StatusUnauthorized)
86+
return
87+
}
88+
89+
w.Header().Set("Content-Type", "application/json")
90+
fmt.Fprint(w, string(want))
3191
})
3292

33-
When("using no authentication", func() {
34-
BeforeEach(func() {
35-
server.AppendHandlers(ghttp.CombineHandlers(
36-
ghttp.RespondWith(http.StatusOK, want),
37-
))
38-
})
93+
ts := httptest.NewServer(mux)
94+
AfterAll(func() {
95+
ts.Close()
96+
})
3997

40-
It("fetches the correct url", func() {
98+
When("no authentication", func() {
99+
url := ts.URL + publicEndpoint
100+
101+
It("successfully fetches the dashboard", func() {
41102
dashboard := &v1beta1.GrafanaDashboard{
42103
Spec: v1beta1.GrafanaDashboardSpec{
43104
GrafanaContentSpec: v1beta1.GrafanaContentSpec{
44-
URL: server.URL(),
105+
URL: url,
45106
},
46107
},
47108
Status: v1beta1.GrafanaDashboardStatus{},
@@ -52,65 +113,31 @@ var _ = Describe("Fetching dashboards from URL", func() {
52113

53114
assert.Equal(t, want, got)
54115
assert.Equal(t, wantCompressed, dashboard.Status.ContentCache)
55-
assert.Equal(t, server.URL(), dashboard.Status.ContentURL)
116+
assert.Equal(t, url, dashboard.Status.ContentURL)
56117
assert.NotZero(t, dashboard.Status.ContentTimestamp.Time)
57118
})
58119
})
59120

60121
When("using authentication", func() {
61-
basicAuthUsername := "admin"
62-
basicAuthPassword := "admin"
63-
64-
BeforeEach(func() {
65-
server.AppendHandlers(ghttp.CombineHandlers(
66-
ghttp.VerifyBasicAuth(basicAuthUsername, basicAuthPassword),
67-
ghttp.RespondWith(http.StatusOK, want),
68-
))
69-
})
122+
url := ts.URL + privateEndpoint
123+
124+
It("successfully fetches the dashboard", func() {
125+
credentialsSecret, urlAuthorization := getCredentials("credentials")
70126

71-
It("fetches the correct url", func() {
72127
dashboard := &v1beta1.GrafanaDashboard{
73128
ObjectMeta: metav1.ObjectMeta{
74-
Name: "test",
129+
Name: "url-basic-auth",
75130
Namespace: "default",
76131
},
77132
Spec: v1beta1.GrafanaDashboardSpec{
78133
GrafanaContentSpec: v1beta1.GrafanaContentSpec{
79-
URL: server.URL(),
80-
URLAuthorization: &v1beta1.GrafanaContentURLAuthorization{
81-
BasicAuth: &v1beta1.GrafanaContentURLBasicAuth{
82-
Username: &v1.SecretKeySelector{
83-
LocalObjectReference: v1.LocalObjectReference{
84-
Name: "credentials",
85-
},
86-
Key: "USERNAME",
87-
Optional: nil,
88-
},
89-
Password: &v1.SecretKeySelector{
90-
LocalObjectReference: v1.LocalObjectReference{
91-
Name: "credentials",
92-
},
93-
Key: "PASSWORD",
94-
Optional: nil,
95-
},
96-
},
97-
},
134+
URL: url,
135+
URLAuthorization: urlAuthorization,
98136
},
99137
},
100138
Status: v1beta1.GrafanaDashboardStatus{},
101139
}
102140

103-
credentialsSecret := &v1.Secret{
104-
ObjectMeta: metav1.ObjectMeta{
105-
Name: "credentials",
106-
Namespace: "default",
107-
},
108-
StringData: map[string]string{
109-
"USERNAME": "admin",
110-
"PASSWORD": "admin",
111-
},
112-
}
113-
114141
err = k8sClient.Create(context.Background(), credentialsSecret)
115142
require.NoError(t, err)
116143

@@ -119,7 +146,7 @@ var _ = Describe("Fetching dashboards from URL", func() {
119146

120147
assert.Equal(t, want, got)
121148
assert.Equal(t, wantCompressed, dashboard.Status.ContentCache)
122-
assert.Equal(t, server.URL(), dashboard.Status.ContentURL)
149+
assert.Equal(t, url, dashboard.Status.ContentURL)
123150
assert.NotZero(t, dashboard.Status.ContentTimestamp.Time)
124151
})
125152
})

controllers/content/suite_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/grafana/grafana-operator/v5/api/v1beta1"
2323
. "github.com/onsi/ginkgo/v2"
24-
. "github.com/onsi/gomega"
2524
"github.com/stretchr/testify/require"
2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2726
"k8s.io/apimachinery/pkg/runtime"
@@ -86,8 +85,6 @@ func TestAPIs(t *testing.T) {
8685
t.Skip("-short was passed, skipping Content")
8786
}
8887

89-
RegisterFailHandler(Fail)
90-
9188
RunSpecs(t, "Content Suite")
9289
}
9390

controllers/reconcilers/grafana/suite_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"testing"
2222

2323
. "github.com/onsi/ginkgo/v2"
24-
. "github.com/onsi/gomega"
2524
routev1 "github.com/openshift/api/route/v1"
2625
"github.com/stretchr/testify/require"
2726
"k8s.io/client-go/kubernetes/scheme"
@@ -48,8 +47,6 @@ func TestAPIs(t *testing.T) {
4847
t.Skip("-short was passed, skipping Reconcilers")
4948
}
5049

51-
RegisterFailHandler(Fail)
52-
5350
RunSpecs(t, "Reconcilers Suite")
5451
}
5552

controllers/suite_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"github.com/stretchr/testify/require"
4040

4141
. "github.com/onsi/ginkgo/v2"
42-
. "github.com/onsi/gomega"
4342
"github.com/testcontainers/testcontainers-go"
4443
"github.com/testcontainers/testcontainers-go/wait"
4544
//+kubebuilder:scaffold:imports
@@ -69,8 +68,6 @@ func TestAPIs(t *testing.T) {
6968
t.Skip("-short was passed, skipping Controllers")
7069
}
7170

72-
RegisterFailHandler(Fail)
73-
7471
RunSpecs(t, "Controllers Suite")
7572
}
7673

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ require (
1515
github.com/grafana/grafana-openapi-client-go v0.0.0-20251118002049-9bc70ad4530f
1616
github.com/grafana/grafana-plugin-sdk-go v0.281.0
1717
github.com/onsi/ginkgo/v2 v2.27.2
18-
github.com/onsi/gomega v1.38.2
1918
github.com/openshift/api v0.0.0-20251021211107-8c9accafe91d
2019
github.com/prometheus/client_golang v1.23.2
2120
github.com/spyzhov/ajson v0.9.6

0 commit comments

Comments
 (0)