Skip to content

Commit

Permalink
Re-Adding Grafana Proxy test
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Jan 3, 2024
1 parent 82385c9 commit c01f7f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
18 changes: 18 additions & 0 deletions internal/service/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions internal/service/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
Expand All @@ -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.
Expand Down

0 comments on commit c01f7f2

Please sign in to comment.