Files
grafana/pkg/tsdb/cloudwatch/namespaces_test.go
T
Nathan Vērzemnieks 560be84dc8 [release-12.0.5] Backport aws-sdk-go-v2 update and subsequent fixes (#111243)
* CloudWatch: Backport aws-sdk-go-v2 update from external plugin (#107136)

(cherry picked from a18ea34688)

* Datasources: Update grafana-aws-sdk for new sigv4 middleware and aws-sdk-go v1 removal (#107522)

(cherry picked from commit 66d9a33cc9)

* CloudWatch: Fix proxy transport issue (#107807)

(cherry picked from c3eeb1fcd9)

* CloudWatch: Fix http client handling + assume role bug (#107893)

(cherry picked from commit f34a9fc0c2)

* CloudWatch: Use default region when query region is unset (#109089)

(cherry picked from commit 5f4097a159)

* CloudWatch: Fix handling region for legacy alerts (#109217)

(cherry picked from commit 2bf9aea8ef)

* Update go.mod owners

---------

Co-authored-by: Isabella Siu <Isabella.siu@grafana.com>
2025-09-19 12:46:40 +02:00

69 lines
2.6 KiB
Go

package cloudwatch
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/services"
)
func Test_Namespaces_Route(t *testing.T) {
t.Run("calls GetHardCodedNamespaces", func(t *testing.T) {
origGetHardCodedNamespaces := services.GetHardCodedNamespaces
t.Cleanup(func() {
services.GetHardCodedNamespaces = origGetHardCodedNamespaces
})
haveBeenCalled := false
services.GetHardCodedNamespaces = func() []resources.ResourceResponse[string] {
haveBeenCalled = true
return []resources.ResourceResponse[string]{}
}
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/namespaces", nil)
ds := newTestDatasource()
handler := http.HandlerFunc(ds.resourceRequestMiddleware(ds.NamespacesHandler))
handler.ServeHTTP(rr, req)
assert.True(t, haveBeenCalled)
})
t.Run("returns merges hardcoded namespaces and custom namespaces", func(t *testing.T) {
origGetHardCodedNamespaces := services.GetHardCodedNamespaces
t.Cleanup(func() {
services.GetHardCodedNamespaces = origGetHardCodedNamespaces
})
services.GetHardCodedNamespaces = func() []resources.ResourceResponse[string] {
return []resources.ResourceResponse[string]{{Value: "AWS/EC2"}, {Value: "AWS/ELB"}}
}
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/namespaces", nil)
ds := newTestDatasource(func(ds *DataSource) {
ds.Settings.Namespace = "customNamespace1,customNamespace2"
})
handler := http.HandlerFunc(ds.resourceRequestMiddleware(ds.NamespacesHandler))
handler.ServeHTTP(rr, req)
assert.JSONEq(t, `[{"value":"AWS/EC2"}, {"value":"AWS/ELB"}, {"value":"customNamespace1"}, {"value":"customNamespace2"}]`, rr.Body.String())
})
t.Run("sorts result", func(t *testing.T) {
origGetHardCodedNamespaces := services.GetHardCodedNamespaces
t.Cleanup(func() {
services.GetHardCodedNamespaces = origGetHardCodedNamespaces
})
services.GetHardCodedNamespaces = func() []resources.ResourceResponse[string] {
return []resources.ResourceResponse[string]{{Value: "AWS/XYZ"}, {Value: "AWS/ELB"}}
}
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/namespaces", nil)
ds := newTestDatasource(func(ds *DataSource) {
ds.Settings.Namespace = "DCustomNamespace1,ACustomNamespace2"
})
handler := http.HandlerFunc(ds.resourceRequestMiddleware(ds.NamespacesHandler))
handler.ServeHTTP(rr, req)
assert.JSONEq(t, `[{"value":"ACustomNamespace2"}, {"value":"AWS/ELB"}, {"value":"AWS/XYZ"}, {"value":"DCustomNamespace1"}]`, rr.Body.String())
})
}