Skip to content

Commit

Permalink
fix: use ginkgo for test suite
Browse files Browse the repository at this point in the history
This correctly uses envtest to support kubernetes api interactions
  • Loading branch information
theSuess committed Sep 2, 2024
1 parent f4d60a6 commit 4b4d590
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 89 deletions.
3 changes: 2 additions & 1 deletion controllers/fetchers/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package fetchers

import (
grafanav1beta1 "github.com/grafana/grafana-operator/v5/api/v1beta1"
"testing"

grafanav1beta1 "github.com/grafana/grafana-operator/v5/api/v1beta1"

"github.com/onsi/ginkgo"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
Expand Down
179 changes: 91 additions & 88 deletions controllers/fetchers/url_fetcher_test.go
<
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,110 @@ package fetchers

import (
"context"
"net/http"

"github.com/onsi/gomega/ghttp"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net/http"
"net/http/httptest"
"testing"

"github.com/grafana/grafana-operator/v5/api/v1beta1"
"github.com/stretchr/testify/assert"
)

func TestFetchDashboardFromUrl(t *testing.T) {
dashboardJSON := []byte(`{"dummyField": "dummyData"}`)
compressedJSON, err := v1beta1.Gzip(dashboardJSON)
assert.Nil(t, err, "Failed to compress a dashboard")

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, err := w.Write(dashboardJSON)
assert.NoError(t, err)
}))
defer ts.Close()

dashboard := &v1beta1.GrafanaDashboard{
Spec: v1beta1.GrafanaDashboardSpec{
Url: ts.URL,
},
Status: v1beta1.GrafanaDashboardStatus{},
}

fetchedDashboard, err := FetchDashboardFromUrl(context.Background(), dashboard, k8sClient, nil)
assert.Nil(t, err)
assert.Equal(t, dashboardJSON, fetchedDashboard, "Fetched dashboard doesn't match the original")

assert.False(t, dashboard.Status.ContentTimestamp.Time.IsZero(), "ContentTimestamp should have been updated")
assert.Equal(t, compressedJSON, dashboard.Status.ContentCache, "ContentCache should have been updated")
assert.Equal(t, ts.URL, dashboard.Status.ContentUrl, "ContentUrl should have been updated")
}
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestFetchDashboardFromUrlBasicAuth(t *testing.T) {
var _ = Describe("Fetching dashboards from URL", func() {
dashboardJSON := []byte(`{"dummyField": "dummyData"}`)
compressedJSON, err := v1beta1.Gzip(dashboardJSON)
assert.Nil(t, err, "Failed to compress a dashboard")
Expect(err).NotTo(HaveOccurred())

basicAuthUsername := "admin"
basicAuthPassword := "admin"
var server *ghttp.Server

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
assert.True(t, ok)
assert.Equal(t, username, basicAuthUsername)
assert.Equal(t, password, basicAuthPassword)
BeforeEach(func() {
server = ghttp.NewServer()
})

w.Header().Set("Content-Type", "application/json")
_, err := w.Write(dashboardJSON)
assert.NoError(t, err)
}))
defer ts.Close()
When("using no authentication", func() {
BeforeEach(func() {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.RespondWith(http.StatusOK, dashboardJSON),
))
})

dashboard := &v1beta1.GrafanaDashboard{
Spec: v1beta1.GrafanaDashboardSpec{
Url: ts.URL,
UrlAuthorization: &v1beta1.GrafanaDashboardUrlAuthorization{
BasicAuth: &v1beta1.GrafanaDashboardUrlBasicAuth{
Username: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: "credentials",
},
Key: basicAuthUsername,
Optional: nil,
},
Password: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: "credentials",
It("fetches the correct url", func() {
dashboard := &v1beta1.GrafanaDashboard{
Spec: v1beta1.GrafanaDashboardSpec{
Url: server.URL(),
},
Status: v1beta1.GrafanaDashboardStatus{},
}

fetchedDashboard, err := FetchDashboardFromUrl(context.Background(), dashboard, k8sClient, nil)
Expect(err).NotTo(HaveOccurred())
Expect(fetchedDashboard).To(Equal(fetchedDashboard))
Expect(dashboard.Status.ContentTimestamp.Time.IsZero()).To(BeFalse())
Expect(dashboard.Status.ContentCache).To(Equal(compressedJSON))
Expect(dashboard.Status.ContentUrl).To(Equal(server.URL()))
})
})
When("using authentication", func() {
basicAuthUsername := "admin"
basicAuthPassword := "admin"
BeforeEach(func() {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyBasicAuth(basicAuthUsername, basicAuthPassword),
ghttp.RespondWith(http.StatusOK, dashboardJSON),
))
})

It("fetches the correct url", func() {
dashboard := &v1beta1.GrafanaDashboard{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
},
Spec: v1beta1.GrafanaDashboardSpec{
Url: server.URL(),
UrlAuthorization: &v1beta1.GrafanaDashboardUrlAuthorization{
BasicAuth: &v1beta1.GrafanaDashboardUrlBasicAuth{
Username: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: "credentials",
},
Key: "USERNAME",
Optional: nil,
},
Password: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: "credentials",
},
Key: "PASSWORD",
Optional: nil,
},
},
Key: basicAuthPassword,
Optional: nil,
},
},
},
},
Status: v1beta1.GrafanaDashboardStatus{},
}
Status: v1beta1.GrafanaDashboardStatus{},
}

credentialsSecret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "credentials",
},
StringData: map[string]string{
"USERNAME": "admin",
"PASSWORD": "admin",
},
}

err = k8sClient.Create(context.Background(), credentialsSecret)
assert.NoError(t, err)

fetchedDashboard, err := FetchDashboardFromUrl(context.Background(), dashboard, k8sClient, nil)
assert.Nil(t, err)
assert.Equal(t, dashboardJSON, fetchedDashboard, "Fetched dashboard doesn't match the original")

assert.False(t, dashboard.Status.ContentTimestamp.Time.IsZero(), "ContentTimestamp should have been updated")
assert.Equal(t, compressedJSON, dashboard.Status.ContentCache, "ContentCache should have been updated")
assert.Equal(t, ts.URL, dashboard.Status.ContentUrl, "ContentUrl should have been updated")
}
credentialsSecret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "credentials",
Namespace: "default",
},
StringData: map[string]string{
"USERNAME": "admin",
"PASSWORD": "admin",
},
}
err = k8sClient.Create(context.Background(), credentialsSecret)
Expect(err).NotTo(HaveOccurred())
fetchedDashboard, err := FetchDashboardFromUrl(context.Background(), dashboard, k8sClient, nil)
Expect(err).NotTo(HaveOccurred())
Expect(fetchedDashboard).To(Equal(fetchedDashboard))