Logging: Unify logging fakes (#48822)

This commit is contained in:
Emil Tullstedt
2022-05-06 17:44:22 +02:00
committed by GitHub
parent 3ffe447c80
commit 51ff2b8c58
9 changed files with 99 additions and 147 deletions
+4 -19
View File
@@ -13,6 +13,9 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
@@ -28,8 +31,6 @@ import (
"github.com/grafana/grafana/pkg/services/secrets/fakes"
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func fakeSetIndexViewData(t *testing.T) {
@@ -65,22 +66,6 @@ func getBody(resp *httptest.ResponseRecorder) (string, error) {
return string(responseData), nil
}
type FakeLogger struct {
log.Logger
}
func (fl *FakeLogger) Debug(testMessage string, ctx ...interface{}) {
}
func (fl *FakeLogger) Info(testMessage string, ctx ...interface{}) {
}
func (fl *FakeLogger) Warn(testMessage string, ctx ...interface{}) {
}
func (fl *FakeLogger) Error(testMessage string, ctx ...interface{}) {
}
type redirectCase struct {
desc string
url string
@@ -332,7 +317,7 @@ func TestLoginPostRedirect(t *testing.T) {
fakeViewIndex(t)
sc := setupScenarioContext(t, "/login")
hs := &HTTPServer{
log: &FakeLogger{},
log: log.NewNopLogger(),
Cfg: setting.NewCfg(),
HooksService: &hooks.HooksService{},
License: &licensing.OSSLicensingService{},
+13 -21
View File
@@ -11,6 +11,8 @@ import (
"path/filepath"
"testing"
"github.com/grafana/grafana/pkg/infra/log/logtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -55,7 +57,7 @@ func Test_GetPluginAssets(t *testing.T) {
pluginID: p,
},
}
l := &logger{}
l := &logtest.Fake{}
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, requestedFile)
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*", service, l,
@@ -64,7 +66,7 @@ func Test_GetPluginAssets(t *testing.T) {
require.Equal(t, 200, sc.resp.Code)
assert.Equal(t, expectedBody, sc.resp.Body.String())
assert.Empty(t, l.warnings)
assert.Zero(t, l.WarnLogs.Calls)
})
})
@@ -80,7 +82,7 @@ func Test_GetPluginAssets(t *testing.T) {
pluginID: p,
},
}
l := &logger{}
l := &logtest.Fake{}
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, tmpFileInParentDir.Name())
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*", service, l,
@@ -103,7 +105,7 @@ func Test_GetPluginAssets(t *testing.T) {
pluginID: p,
},
}
l := &logger{}
l := &logtest.Fake{}
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, requestedFile)
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*", service, l,
@@ -112,7 +114,7 @@ func Test_GetPluginAssets(t *testing.T) {
require.Equal(t, 200, sc.resp.Code)
assert.Equal(t, expectedBody, sc.resp.Body.String())
assert.Empty(t, l.warnings)
assert.Zero(t, l.WarnLogs.Calls)
})
})
@@ -128,7 +130,7 @@ func Test_GetPluginAssets(t *testing.T) {
pluginID: p,
},
}
l := &logger{}
l := &logtest.Fake{}
requestedFile := "nonExistent"
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, requestedFile)
@@ -141,7 +143,7 @@ func Test_GetPluginAssets(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 404, sc.resp.Code)
assert.Equal(t, "Plugin file not found", respJson["message"])
assert.Empty(t, l.warnings)
assert.Zero(t, l.WarnLogs.Calls)
})
})
@@ -149,7 +151,7 @@ func Test_GetPluginAssets(t *testing.T) {
service := &fakePluginStore{
plugins: map[string]plugins.PluginDTO{},
}
l := &logger{}
l := &logtest.Fake{}
requestedFile := "nonExistent"
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, requestedFile)
@@ -162,7 +164,7 @@ func Test_GetPluginAssets(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 404, sc.resp.Code)
assert.Equal(t, "Plugin not found", respJson["message"])
assert.Empty(t, l.warnings)
assert.Zero(t, l.WarnLogs.Calls)
})
})
@@ -174,7 +176,7 @@ func Test_GetPluginAssets(t *testing.T) {
},
},
}
l := &logger{}
l := &logtest.Fake{}
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, requestedFile)
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*", service, l,
@@ -183,7 +185,7 @@ func Test_GetPluginAssets(t *testing.T) {
require.Equal(t, 200, sc.resp.Code)
assert.Equal(t, expectedBody, sc.resp.Body.String())
assert.Empty(t, l.warnings)
assert.Zero(t, l.WarnLogs.Calls)
})
})
}
@@ -235,16 +237,6 @@ func pluginAssetScenario(t *testing.T, desc string, url string, urlPattern strin
})
}
type logger struct {
log.Logger
warnings []string
}
func (l *logger) Warn(msg string, ctx ...interface{}) {
l.warnings = append(l.warnings, msg)
}
type fakePluginClient struct {
plugins.Client
+12 -21
View File
@@ -8,7 +8,11 @@ import (
"strings"
"testing"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/log/logtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
pref "github.com/grafana/grafana/pkg/services/preference"
@@ -17,21 +21,8 @@ import (
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/web"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testLogger struct {
log.Logger
warnCalled bool
warnMessage string
}
func (stub *testLogger) Warn(testMessage string, ctx ...interface{}) {
stub.warnCalled = true
stub.warnMessage = testMessage
}
func TestTeamAPIEndpoint(t *testing.T) {
t.Run("Given two teams", func(t *testing.T) {
hs := setupSimpleHTTPServer(nil)
@@ -123,11 +114,11 @@ func TestTeamAPIEndpoint(t *testing.T) {
require.NoError(t, err)
t.Run("with no real signed in user", func(t *testing.T) {
stub := &testLogger{}
logger := &logtest.Fake{}
c := &models.ReqContext{
Context: &web.Context{Req: req},
SignedInUser: &models.SignedInUser{},
Logger: stub,
Logger: logger,
}
c.OrgRole = models.ROLE_EDITOR
c.Req.Body = mockRequestBody(models.CreateTeamCommand{Name: teamName})
@@ -135,23 +126,23 @@ func TestTeamAPIEndpoint(t *testing.T) {
r := hs.CreateTeam(c)
assert.Equal(t, 200, r.Status())
assert.True(t, stub.warnCalled)
assert.Equal(t, stub.warnMessage, "Could not add creator to team because is not a real user")
assert.NotZero(t, logger.WarnLogs.Calls)
assert.Equal(t, "Could not add creator to team because is not a real user", logger.WarnLogs.Message)
})
t.Run("with real signed in user", func(t *testing.T) {
stub := &testLogger{}
logger := &logtest.Fake{}
c := &models.ReqContext{
Context: &web.Context{Req: req},
SignedInUser: &models.SignedInUser{UserId: 42},
Logger: stub,
Logger: logger,
}
c.OrgRole = models.ROLE_EDITOR
c.Req.Body = mockRequestBody(models.CreateTeamCommand{Name: teamName})
c.Req.Header.Add("Content-Type", "application/json")
r := hs.CreateTeam(c)
assert.Equal(t, 200, r.Status())
assert.False(t, stub.warnCalled)
assert.Zero(t, logger.WarnLogs.Calls)
})
})
}