2022-10-26 08:54:23 -05:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2023-05-24 03:19:34 -05:00
|
|
|
"context"
|
2022-10-26 08:54:23 -05:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2022-11-02 09:14:02 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-10-26 08:54:23 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
2022-11-28 05:39:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
|
2022-10-26 08:54:23 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/services"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Namespaces_Route(t *testing.T) {
|
|
|
|
customNamespaces := ""
|
2023-05-24 03:19:34 -05:00
|
|
|
factoryFunc := func(_ context.Context, pluginCtx backend.PluginContext, region string) (reqCtx models.RequestContext, err error) {
|
2022-10-26 08:54:23 -05:00
|
|
|
return models.RequestContext{
|
2022-11-22 08:59:11 -06:00
|
|
|
Settings: models.CloudWatchSettings{
|
2022-10-26 08:54:23 -05:00
|
|
|
Namespace: customNamespaces,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("calls GetHardCodedNamespaces", func(t *testing.T) {
|
|
|
|
origGetHardCodedNamespaces := services.GetHardCodedNamespaces
|
|
|
|
t.Cleanup(func() {
|
|
|
|
services.GetHardCodedNamespaces = origGetHardCodedNamespaces
|
|
|
|
})
|
|
|
|
haveBeenCalled := false
|
2022-11-28 05:39:12 -06:00
|
|
|
services.GetHardCodedNamespaces = func() []resources.ResourceResponse[string] {
|
2022-10-26 08:54:23 -05:00
|
|
|
haveBeenCalled = true
|
2022-11-28 05:39:12 -06:00
|
|
|
return []resources.ResourceResponse[string]{}
|
2022-10-26 08:54:23 -05:00
|
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/namespaces", nil)
|
2022-11-02 09:14:02 -05:00
|
|
|
handler := http.HandlerFunc(ResourceRequestMiddleware(NamespacesHandler, logger, factoryFunc))
|
2022-10-26 08:54:23 -05:00
|
|
|
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
|
|
|
|
})
|
2022-11-28 05:39:12 -06:00
|
|
|
services.GetHardCodedNamespaces = func() []resources.ResourceResponse[string] {
|
|
|
|
return []resources.ResourceResponse[string]{{Value: "AWS/EC2"}, {Value: "AWS/ELB"}}
|
2022-10-26 08:54:23 -05:00
|
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/namespaces", nil)
|
|
|
|
customNamespaces = "customNamespace1,customNamespace2"
|
2022-11-02 09:14:02 -05:00
|
|
|
handler := http.HandlerFunc(ResourceRequestMiddleware(NamespacesHandler, logger, factoryFunc))
|
2022-10-26 08:54:23 -05:00
|
|
|
handler.ServeHTTP(rr, req)
|
2022-11-28 05:39:12 -06:00
|
|
|
assert.JSONEq(t, `[{"value":"AWS/EC2"}, {"value":"AWS/ELB"}, {"value":"customNamespace1"}, {"value":"customNamespace2"}]`, rr.Body.String())
|
2022-10-26 08:54:23 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sorts result", func(t *testing.T) {
|
|
|
|
origGetHardCodedNamespaces := services.GetHardCodedNamespaces
|
|
|
|
t.Cleanup(func() {
|
|
|
|
services.GetHardCodedNamespaces = origGetHardCodedNamespaces
|
|
|
|
})
|
2022-11-28 05:39:12 -06:00
|
|
|
services.GetHardCodedNamespaces = func() []resources.ResourceResponse[string] {
|
|
|
|
return []resources.ResourceResponse[string]{{Value: "AWS/XYZ"}, {Value: "AWS/ELB"}}
|
2022-10-26 08:54:23 -05:00
|
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/namespaces", nil)
|
|
|
|
customNamespaces = "DCustomNamespace1,ACustomNamespace2"
|
2022-11-02 09:14:02 -05:00
|
|
|
handler := http.HandlerFunc(ResourceRequestMiddleware(NamespacesHandler, logger, factoryFunc))
|
2022-10-26 08:54:23 -05:00
|
|
|
handler.ServeHTTP(rr, req)
|
2022-11-28 05:39:12 -06:00
|
|
|
assert.JSONEq(t, `[{"value":"ACustomNamespace2"}, {"value":"AWS/ELB"}, {"value":"AWS/XYZ"}, {"value":"DCustomNamespace1"}]`, rr.Body.String())
|
2022-10-26 08:54:23 -05:00
|
|
|
})
|
|
|
|
}
|