Skip to content

Commit 4acddf8

Browse files
committed
refactor loki client mock
1 parent f7d7292 commit 4acddf8

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

pkg/handler/loki.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/sirupsen/logrus"
1313

14+
"github.com/netobserv/network-observability-console-plugin/pkg/handler/lokiclientmock"
1415
"github.com/netobserv/network-observability-console-plugin/pkg/httpclient"
1516
"github.com/netobserv/network-observability-console-plugin/pkg/loki"
1617
"github.com/netobserv/network-observability-console-plugin/pkg/metrics"
@@ -30,8 +31,14 @@ func newLokiClient(cfg *loki.Config) httpclient.Caller {
3031
lokiOrgIDHeader: {cfg.TenantID},
3132
}
3233
}
34+
35+
if cfg.UseMocks {
36+
hlog.Debug("Mocking Loki Client")
37+
return new(lokiclientmock.LokiClientMock)
38+
}
39+
3340
// TODO: loki with auth
34-
return httpclient.NewHTTPClient(cfg.Timeout, headers, cfg.SkipTLS, cfg.UseMocks)
41+
return httpclient.NewHTTPClient(cfg.Timeout, headers, cfg.SkipTLS)
3542
}
3643

3744
/* loki query will fail if spaces or quotes are not encoded

pkg/httpclient/httpclienttest/http_client_json_mock.go renamed to pkg/handler/lokiclientmock/loki_client_mock.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package httpclienttest
1+
package lokiclientmock
22

33
import (
44
"io/ioutil"
55
"strings"
66
)
77

8-
type HTTPClientJSONMock struct {
8+
type LokiClientMock struct {
99
}
1010

11-
func (o *HTTPClientJSONMock) Get(url string) ([]byte, int, error) {
11+
func (o *LokiClientMock) Get(url string) ([]byte, int, error) {
1212
var path string
1313

1414
isLabel := strings.Contains(url, "/label/")

pkg/httpclient/http_client.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88
"time"
99

10-
"github.com/netobserv/network-observability-console-plugin/pkg/httpclient/httpclienttest"
1110
"github.com/sirupsen/logrus"
1211
)
1312

@@ -23,7 +22,7 @@ type httpClient struct {
2322

2423
var slog = logrus.WithField("module", "server")
2524

26-
func NewHTTPClient(timeout time.Duration, overrideHeaders map[string][]string, skipTLS bool, useMocks bool) Caller {
25+
func NewHTTPClient(timeout time.Duration, overrideHeaders map[string][]string, skipTLS bool) Caller {
2726
transport := &http.Transport{
2827
DialContext: (&net.Dialer{Timeout: timeout}).DialContext,
2928
IdleConnTimeout: timeout,
@@ -35,11 +34,6 @@ func NewHTTPClient(timeout time.Duration, overrideHeaders map[string][]string, s
3534
slog.Warn("skipping TLS checks. SSL certificate verification is now disabled !")
3635
}
3736

38-
if useMocks {
39-
slog.Debug("Mocking HTTP Client")
40-
return new(httpclienttest.HTTPClientJSONMock)
41-
}
42-
4337
return &httpClient{
4438
client: http.Client{Transport: transport, Timeout: timeout},
4539
headers: overrideHeaders,

0 commit comments

Comments
 (0)