From c01f7f23969f9633eb1f73f956fa1d6c4fd37763 Mon Sep 17 00:00:00 2001 From: Samir Faci Date: Wed, 3 Jan 2024 09:21:59 -0600 Subject: [PATCH] Re-Adding Grafana Proxy test --- internal/service/common_test.go | 18 ++++++++++++++++++ internal/service/login.go | 14 +++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/internal/service/common_test.go b/internal/service/common_test.go index 6916da55..ecae0cb3 100644 --- a/internal/service/common_test.go +++ b/internal/service/common_test.go @@ -5,9 +5,27 @@ import ( "github.com/gosimple/slug" "github.com/stretchr/testify/assert" "os" + "strings" "testing" ) +func TestRelativePathLogin(t *testing.T) { + cwd, err := os.Getwd() + assert.Nil(t, err) + if strings.Contains(cwd, "service") { + os.Chdir("../..") + } + os.Setenv("GDG_CONTEXTS__TESTING__URL", "http://localhost:3000/grafana/") + config.InitConfig("config/testing.yml", "'") + defer os.Unsetenv("GDG_CONTEXTS__TESTING__URL") + + svc := NewApiService("dummy") + _, cfg := svc.(*DashNGoImpl).getNewClient() + assert.Equal(t, cfg.Host, "localhost:3000") + assert.Equal(t, cfg.BasePath, "/grafana/api") + +} + // Validates the paths for the various entity types using the common // code used to create folders and generate paths. func TestSlug(t *testing.T) { diff --git a/internal/service/login.go b/internal/service/login.go index 333ce6d7..7527c698 100644 --- a/internal/service/login.go +++ b/internal/service/login.go @@ -39,7 +39,7 @@ func ignoreSSL(transportConfig *client.TransportConfig) { type NewClientOpts func(transportConfig *client.TransportConfig) -func (s *DashNGoImpl) getNewClient(opts ...NewClientOpts) *client.GrafanaHTTPAPI { +func (s *DashNGoImpl) getNewClient(opts ...NewClientOpts) (*client.GrafanaHTTPAPI, *client.TransportConfig) { var err error u, err := url.Parse(s.grafanaConf.URL) if err != nil { @@ -62,21 +62,24 @@ func (s *DashNGoImpl) getNewClient(opts ...NewClientOpts) *client.GrafanaHTTPAPI }) } for _, opt := range opts { - opt(httpConfig) + if opt != nil { + opt(httpConfig) + } } if config.Config().IgnoreSSL() { ignoreSSL(httpConfig) } - return client.NewHTTPClientWithConfig(strfmt.Default, httpConfig) + return client.NewHTTPClientWithConfig(strfmt.Default, httpConfig), httpConfig } // GetClient Returns a new defaultClient given token precedence over Basic Auth func (s *DashNGoImpl) GetClient() *client.GrafanaHTTPAPI { if s.grafanaConf.APIToken != "" { - return s.getNewClient(func(clientCfg *client.TransportConfig) { + grafanaClient, _ := s.getNewClient(func(clientCfg *client.TransportConfig) { clientCfg.APIKey = s.grafanaConf.APIToken }) + return grafanaClient } else { return s.GetBasicAuthClient() } @@ -92,9 +95,10 @@ func (s *DashNGoImpl) GetAdminClient() *client.GrafanaHTTPAPI { // GetBasicAuthClient returns a basic auth grafana API Client func (s *DashNGoImpl) GetBasicAuthClient() *client.GrafanaHTTPAPI { - return s.getNewClient(func(clientCfg *client.TransportConfig) { + grafanaClient, _ := s.getNewClient(func(clientCfg *client.TransportConfig) { clientCfg.BasicAuth = url.UserPassword(s.grafanaConf.UserName, s.grafanaConf.Password) }) + return grafanaClient } // ignoreSSLErrors when called replaces the default http legacyClient to ignore invalid SSL issues.