Skip to content

Commit

Permalink
add noop logger to tests that need it
Browse files Browse the repository at this point in the history
  • Loading branch information
jranson committed Aug 16, 2024
1 parent 903e29c commit e6a928f
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 140 deletions.
5 changes: 4 additions & 1 deletion pkg/backends/alb/response_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ import (

"github.com/trickstercache/trickster/v2/pkg/backends/alb/pool"
"github.com/trickstercache/trickster/v2/pkg/backends/healthcheck"
"github.com/trickstercache/trickster/v2/pkg/observability/logging"
"github.com/trickstercache/trickster/v2/pkg/proxy/request"
"github.com/trickstercache/trickster/v2/pkg/proxy/response/merge"
tu "github.com/trickstercache/trickster/v2/pkg/testutil"
)

var testLogger = logging.NoopLogger()

func testMergeFunc(w http.ResponseWriter, r *http.Request, rgs merge.ResponseGates) {

}

func TestHandleResponseMerge(t *testing.T) {

r, _ := http.NewRequest("GET", "http://trickstercache.org/", nil)
rsc := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc.ResponseMergeFunc = testMergeFunc
rsc.IsMergeMember = true
r = request.SetResources(r, rsc)
Expand Down
19 changes: 11 additions & 8 deletions pkg/backends/healthcheck/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import (
"testing"

ho "github.com/trickstercache/trickster/v2/pkg/backends/healthcheck/options"
"github.com/trickstercache/trickster/v2/pkg/observability/logging"
)

var testLogger = logging.NoopLogger()

func TestNew(t *testing.T) {

hc := New()
Expand Down Expand Up @@ -58,23 +61,23 @@ func TestRegister(t *testing.T) {
hc := New().(*healthChecker)
o := ho.New()
o.IntervalMS = 500
_, err := hc.Register("test", "test", o, http.DefaultClient, nil)
_, err := hc.Register("test", "test", o, http.DefaultClient, testLogger)
if err != nil {
t.Error(err)
}
target := hc.targets["test"]
target.Start()
target.Stop()
_, err = hc.Register("test", "test", o, http.DefaultClient, nil)
_, err = hc.Register("test", "test", o, http.DefaultClient, testLogger)
if err != nil {
t.Error(err)
}
o.Body = "test-body"
_, err = hc.Register("test", "test", o, http.DefaultClient, nil)
_, err = hc.Register("test", "test", o, http.DefaultClient, testLogger)
if err != nil {
t.Error(err)
}
_, err = hc.Register("test", "test", nil, http.DefaultClient, nil)
_, err = hc.Register("test", "test", nil, http.DefaultClient, testLogger)
if err != ho.ErrNoOptionsProvided {
t.Errorf("expected %v got %v", ho.ErrNoOptionsProvided, err)
}
Expand All @@ -84,7 +87,7 @@ func TestUnregister(t *testing.T) {
hc := New().(*healthChecker)
o := ho.New()
o.IntervalMS = 500
_, err := hc.Register("test", "test", o, http.DefaultClient, nil)
_, err := hc.Register("test", "test", o, http.DefaultClient, testLogger)
if err != nil {
t.Error(err)
}
Expand All @@ -99,7 +102,7 @@ func TestStatus(t *testing.T) {
hc := New().(*healthChecker)
o := ho.New()
o.IntervalMS = 500
_, err := hc.Register("test", "test", o, http.DefaultClient, nil)
_, err := hc.Register("test", "test", o, http.DefaultClient, testLogger)
if err != nil {
t.Error(err)
}
Expand All @@ -125,7 +128,7 @@ func TestStatuses(t *testing.T) {
hc := New().(*healthChecker)
o := ho.New()
o.IntervalMS = 500
_, err := hc.Register("test", "test", o, http.DefaultClient, nil)
_, err := hc.Register("test", "test", o, http.DefaultClient, testLogger)
if err != nil {
t.Error(err)
}
Expand All @@ -144,7 +147,7 @@ func TestHealthCheckerProbe(t *testing.T) {
ts := newTestServer(200, "OK", map[string]string{})
r, _ := http.NewRequest("GET", ts.URL+"/", nil)

_, err := hc.Register("test", "test", o, http.DefaultClient, nil)
_, err := hc.Register("test", "test", o, http.DefaultClient, testLogger)
if err != nil {
t.Error(err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/backends/healthcheck/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func TestProbe(t *testing.T) {
baseRequest: r,
httpClient: ts.Client(),
ec: []int{200},
logger: testLogger,
}
target.probe()
if target.successConsecutiveCnt.Load() != 1 {
Expand Down
29 changes: 19 additions & 10 deletions pkg/backends/prometheus/model/alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ import (
"strconv"
"testing"

"github.com/trickstercache/trickster/v2/pkg/observability/logging"
"github.com/trickstercache/trickster/v2/pkg/proxy/request"
"github.com/trickstercache/trickster/v2/pkg/proxy/response/merge"
)

var testLogger = logging.NoopLogger()
var testResources = request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)

func TestCalculateHash(t *testing.T) {

a := &WFAlert{
Expand Down Expand Up @@ -81,6 +85,11 @@ func TestMerge(t *testing.T) {

}

func newTestReq() *http.Request {
r, _ := http.NewRequest(http.MethodGet, "/", nil)
return r
}

func TestMergeAndWriteAlerts(t *testing.T) {

var nilRG *merge.ResponseGate
Expand All @@ -91,22 +100,22 @@ func TestMergeAndWriteAlerts(t *testing.T) {
expCode int
}{
{
nil,
newTestReq(),
nil,
http.StatusBadGateway,
},
{
nil,
newTestReq(),
merge.ResponseGates{nilRG},
http.StatusBadGateway,
},
{
nil,
newTestReq(),
testResponseGates1(),
http.StatusOK,
},
{
nil,
newTestReq(),
testResponseGates2(),
http.StatusBadRequest,
},
Expand All @@ -115,7 +124,7 @@ func TestMergeAndWriteAlerts(t *testing.T) {
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
w := httptest.NewRecorder()
MergeAndWriteAlerts(w, test.r, test.rgs)
MergeAndWriteAlerts(w, request.SetResources(test.r, testResources), test.rgs)
if w.Code != test.expCode {
t.Errorf("expected %d got %d", test.expCode, w.Code)
}
Expand All @@ -130,7 +139,7 @@ func testResponseGates1() merge.ResponseGates {
{"state":"test","labels":{},"annotations":{},"value":"x","activeAt":"y"}
]}}`)
closer1 := io.NopCloser(bytes.NewReader(b1))
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc1.Response = &http.Response{
Body: closer1,
StatusCode: 200,
Expand All @@ -144,7 +153,7 @@ func testResponseGates1() merge.ResponseGates {

b2bad := []byte(`{"stat`)
closer2 := io.NopCloser(bytes.NewReader(b2bad))
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc2.Response = &http.Response{
Body: closer2,
StatusCode: 200,
Expand All @@ -158,7 +167,7 @@ func testResponseGates1() merge.ResponseGates {

b3 := []byte(`{"status":"success","data":{"alerts":[]}}`)
closer3 := io.NopCloser(bytes.NewReader(b3))
rsc3 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc3 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc3.Response = &http.Response{
Body: closer3,
StatusCode: 200,
Expand All @@ -178,7 +187,7 @@ func testResponseGates2() merge.ResponseGates {

b1 := []byte(`{"status":"error","data":{"alerts":[]}}`)
closer1 := io.NopCloser(bytes.NewReader(b1))
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc1.Response = &http.Response{
Body: closer1,
StatusCode: 400,
Expand All @@ -192,7 +201,7 @@ func testResponseGates2() merge.ResponseGates {

b2 := []byte(`{"status":"error","data":{"alerts":[]}}`)
closer2 := io.NopCloser(bytes.NewReader(b1))
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc2.Response = &http.Response{
Body: closer2,
StatusCode: 400,
Expand Down
10 changes: 5 additions & 5 deletions pkg/backends/prometheus/model/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func testResponseGates3() merge.ResponseGates {

b1 := []byte(`{"status":"success","data":["test", "trickster"]}`)
closer1 := io.NopCloser(bytes.NewReader(b1))
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc1.Response = &http.Response{
Body: closer1,
StatusCode: 200,
Expand All @@ -135,7 +135,7 @@ func testResponseGates3() merge.ResponseGates {

b2bad := []byte(`{"stat`)
closer2 := io.NopCloser(bytes.NewReader(b2bad))
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc2.Response = &http.Response{
Body: closer2,
StatusCode: 200,
Expand All @@ -149,7 +149,7 @@ func testResponseGates3() merge.ResponseGates {

b3 := []byte(`{"status":"success","data":["test2", "trickster2"]}`)
closer3 := io.NopCloser(bytes.NewReader(b3))
rsc3 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc3 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc3.Response = &http.Response{
Body: closer3,
StatusCode: 200,
Expand All @@ -169,7 +169,7 @@ func testResponseGates4() merge.ResponseGates {

b1 := []byte(`{"status":"error"`)
closer1 := io.NopCloser(bytes.NewReader(b1))
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc1.Response = &http.Response{
Body: closer1,
StatusCode: 400,
Expand All @@ -183,7 +183,7 @@ func testResponseGates4() merge.ResponseGates {

b2 := []byte(`{"status":"error","data":["should", "not", "append"]`)
closer2 := io.NopCloser(bytes.NewReader(b1))
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc2.Response = &http.Response{
Body: closer2,
StatusCode: 400,
Expand Down
10 changes: 5 additions & 5 deletions pkg/backends/prometheus/model/series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func testResponseGates5() merge.ResponseGates {

b1 := []byte(`{"status":"success","data":[{"__name__":"test1","instance":"i1","job":"trickster"}]}`)
closer1 := io.NopCloser(bytes.NewReader(b1))
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc1.Response = &http.Response{
Body: closer1,
StatusCode: 200,
Expand All @@ -191,7 +191,7 @@ func testResponseGates5() merge.ResponseGates {

b2bad := []byte(`{"stat`)
closer2 := io.NopCloser(bytes.NewReader(b2bad))
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc2.Response = &http.Response{
Body: closer2,
StatusCode: 200,
Expand All @@ -205,7 +205,7 @@ func testResponseGates5() merge.ResponseGates {

b3 := []byte(`{"status":"success","data":[{"__name__":"test1","instance":"i2","job":"trickster"}]}`)
closer3 := io.NopCloser(bytes.NewReader(b3))
rsc3 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc3 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc3.Response = &http.Response{
Body: closer3,
StatusCode: 200,
Expand All @@ -225,7 +225,7 @@ func testResponseGates6() merge.ResponseGates {

b1 := []byte(`{"status":"error"`)
closer1 := io.NopCloser(bytes.NewReader(b1))
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc1.Response = &http.Response{
Body: closer1,
StatusCode: 400,
Expand All @@ -239,7 +239,7 @@ func testResponseGates6() merge.ResponseGates {

b2 := []byte(`{"status":"error","data":[{"__name__":"should","instance":"not","job":"append"}]}`)
closer2 := io.NopCloser(bytes.NewReader(b1))
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc2.Response = &http.Response{
Body: closer2,
StatusCode: 400,
Expand Down
2 changes: 1 addition & 1 deletion pkg/backends/prometheus/model/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func MergeAndWriteVector(w http.ResponseWriter, r *http.Request, rgs merge.Respo
ds, ok := t2.(*dataset.DataSet)
if !ok {
rg.Resources.Logger.Error("vector unmarshaling error",
logging.Pairs{"provider": "prometheus", "detail": err.Error()})
logging.Pairs{"provider": "prometheus"})
continue
}
ts = ds
Expand Down
10 changes: 5 additions & 5 deletions pkg/backends/prometheus/model/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func testResponseGates7() merge.ResponseGates {

b1 := []byte(testVector)
closer1 := io.NopCloser(bytes.NewReader(b1))
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc1.Response = &http.Response{
Body: closer1,
StatusCode: 200,
Expand All @@ -78,7 +78,7 @@ func testResponseGates7() merge.ResponseGates {

b2bad := []byte(`{"stat`)
closer2 := io.NopCloser(bytes.NewReader(b2bad))
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc2.Response = &http.Response{
Body: closer2,
StatusCode: 200,
Expand All @@ -92,7 +92,7 @@ func testResponseGates7() merge.ResponseGates {

b3 := []byte(testVector2)
closer3 := io.NopCloser(bytes.NewReader(b3))
rsc3 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc3 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc3.Response = &http.Response{
Body: closer3,
StatusCode: 200,
Expand All @@ -114,7 +114,7 @@ func testResponseGates8() merge.ResponseGates {

b1 := []byte(`{"status":"error","data":{}`)
closer1 := io.NopCloser(bytes.NewReader(b1))
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc1 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc1.Response = &http.Response{
Body: closer1,
StatusCode: 400,
Expand All @@ -128,7 +128,7 @@ func testResponseGates8() merge.ResponseGates {

b2 := []byte(`{"status":"error","data":{}`)
closer2 := io.NopCloser(bytes.NewReader(b1))
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, nil)
rsc2 := request.NewResources(nil, nil, nil, nil, nil, nil, testLogger)
rsc2.Response = &http.Response{
Body: closer2,
StatusCode: 400,
Expand Down
7 changes: 2 additions & 5 deletions pkg/backends/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/trickstercache/trickster/v2/pkg/backends"
bo "github.com/trickstercache/trickster/v2/pkg/backends/options"
"github.com/trickstercache/trickster/v2/pkg/backends/prometheus/model"
po "github.com/trickstercache/trickster/v2/pkg/backends/prometheus/options"
cr "github.com/trickstercache/trickster/v2/pkg/cache/registration"
"github.com/trickstercache/trickster/v2/pkg/config"
Expand All @@ -37,8 +36,6 @@ import (
"github.com/trickstercache/trickster/v2/pkg/timeseries"
)

var testModeler = model.NewModeler()

func TestPrometheusClientInterfacing(t *testing.T) {

// this test ensures the client will properly conform to the
Expand Down Expand Up @@ -143,7 +140,7 @@ func TestParseTimeRangeQuery(t *testing.T) {
req := &http.Request{URL: u}
o := bo.New()
o.Prometheus = &po.Options{Labels: map[string]string{"test": "trickster"}}
rsc := request.NewResources(o, nil, nil, nil, nil, nil, nil)
rsc := request.NewResources(o, nil, nil, nil, nil, nil, testLogger)
req = request.SetResources(req, rsc)

client := &Client{}
Expand Down Expand Up @@ -378,7 +375,7 @@ func TestParseVectorQuery(t *testing.T) {

o := bo.New()
o.Prometheus = &po.Options{Labels: map[string]string{"test": "trickster"}}
rsc := request.NewResources(o, nil, nil, nil, nil, nil, nil)
rsc := request.NewResources(o, nil, nil, nil, nil, nil, testLogger)
req = request.SetResources(req, rsc)

rounder := time.Second * 15
Expand Down
Loading

0 comments on commit e6a928f

Please sign in to comment.