mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Tests: Batch of GoConvey to Testify conversions (#27008)
This commit is contained in:
@@ -3,28 +3,27 @@ package api
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHTTPServer(t *testing.T) {
|
func TestHTTPServer_MetricsBasicAuth(t *testing.T) {
|
||||||
Convey("Given a HTTPServer", t, func() {
|
ts := &HTTPServer{
|
||||||
ts := &HTTPServer{
|
Cfg: setting.NewCfg(),
|
||||||
Cfg: setting.NewCfg(),
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Convey("Given that basic auth on the metrics endpoint is enabled", func() {
|
t.Run("enabled", func(t *testing.T) {
|
||||||
ts.Cfg.MetricsEndpointBasicAuthUsername = "foo"
|
ts.Cfg.MetricsEndpointBasicAuthUsername = "foo"
|
||||||
ts.Cfg.MetricsEndpointBasicAuthPassword = "bar"
|
ts.Cfg.MetricsEndpointBasicAuthPassword = "bar"
|
||||||
|
|
||||||
So(ts.metricsEndpointBasicAuthEnabled(), ShouldBeTrue)
|
assert.True(t, ts.metricsEndpointBasicAuthEnabled())
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Given that basic auth on the metrics endpoint is disabled", func() {
|
t.Run("disabled", func(t *testing.T) {
|
||||||
ts.Cfg.MetricsEndpointBasicAuthUsername = ""
|
ts.Cfg.MetricsEndpointBasicAuthUsername = ""
|
||||||
ts.Cfg.MetricsEndpointBasicAuthPassword = ""
|
ts.Cfg.MetricsEndpointBasicAuthPassword = ""
|
||||||
|
|
||||||
So(ts.metricsEndpointBasicAuthEnabled(), ShouldBeFalse)
|
assert.False(t, ts.metricsEndpointBasicAuthEnabled())
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,9 +12,13 @@ import (
|
|||||||
|
|
||||||
var ls_getPlugins func(path string) []models.InstalledPlugin = services.GetLocalPlugins
|
var ls_getPlugins func(path string) []models.InstalledPlugin = services.GetLocalPlugins
|
||||||
|
|
||||||
|
var (
|
||||||
|
errMissingPathFlag = errors.New("missing path flag")
|
||||||
|
errNotDirectory = errors.New("plugin path is not a directory")
|
||||||
|
)
|
||||||
var validateLsCommand = func(pluginDir string) error {
|
var validateLsCommand = func(pluginDir string) error {
|
||||||
if pluginDir == "" {
|
if pluginDir == "" {
|
||||||
return errors.New("missing path flag")
|
return errMissingPathFlag
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("plugindir: " + pluginDir + "\n")
|
logger.Debug("plugindir: " + pluginDir + "\n")
|
||||||
@@ -24,7 +28,7 @@ var validateLsCommand = func(pluginDir string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !pluginDirInfo.IsDir() {
|
if !pluginDirInfo.IsDir() {
|
||||||
return errors.New("plugin path is not a directory")
|
return errNotDirectory
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -4,67 +4,81 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/commandstest"
|
"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/commandstest"
|
||||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
|
"github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMissingPath(t *testing.T) {
|
func TestMissingPath(t *testing.T) {
|
||||||
var org = validateLsCommand
|
tests := []struct {
|
||||||
|
description string
|
||||||
|
cliContext map[string]string
|
||||||
|
ioHelper *commandstest.FakeIoUtil
|
||||||
|
error error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
description: "missing path flag",
|
||||||
|
cliContext: make(map[string]string),
|
||||||
|
ioHelper: &commandstest.FakeIoUtil{},
|
||||||
|
error: errMissingPathFlag,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "not a directory",
|
||||||
|
cliContext: map[string]string{"pluginsDir": "/var/lib/grafana/plugins/notadir.txt"},
|
||||||
|
ioHelper: &commandstest.FakeIoUtil{FakeIsDirectory: false},
|
||||||
|
error: errNotDirectory,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
Convey("ls command", t, func() {
|
for _, tc := range tests {
|
||||||
validateLsCommand = org
|
t.Run(tc.description, func(t *testing.T) {
|
||||||
|
origIoHelper := services.IoHelper
|
||||||
|
services.IoHelper = tc.ioHelper
|
||||||
|
t.Cleanup(func() {
|
||||||
|
services.IoHelper = origIoHelper
|
||||||
|
})
|
||||||
|
|
||||||
|
c, err := commandstest.NewCliContext(tc.cliContext)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("Missing path flag", func() {
|
|
||||||
cmd := Command{}
|
cmd := Command{}
|
||||||
c, err := commandstest.NewCliContext(map[string]string{})
|
err = cmd.lsCommand(c)
|
||||||
So(err, ShouldBeNil)
|
assert.Equal(t, tc.error, err)
|
||||||
services.IoHelper = &commandstest.FakeIoUtil{}
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Convey("should return error", func() {
|
func TestValidateLsCommand_override(t *testing.T) {
|
||||||
err := cmd.lsCommand(c)
|
expected := errors.New("dummy error")
|
||||||
So(err, ShouldBeError, "missing path flag")
|
t.Run("override validateLsCommand", func(t *testing.T) {
|
||||||
})
|
var org = validateLsCommand
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
validateLsCommand = org
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Path is not a directory", func() {
|
c, err := commandstest.NewCliContext(map[string]string{"path": "/var/lib/grafana/plugins"})
|
||||||
c, err := commandstest.NewCliContext(map[string]string{"path": "/var/lib/grafana/plugins"})
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
|
||||||
services.IoHelper = &commandstest.FakeIoUtil{
|
|
||||||
FakeIsDirectory: false,
|
|
||||||
}
|
|
||||||
cmd := Command{}
|
|
||||||
|
|
||||||
Convey("should return error", func() {
|
validateLsCommand = func(pluginDir string) error {
|
||||||
err := cmd.lsCommand(c)
|
return expected
|
||||||
So(err, ShouldNotBeNil)
|
}
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("can override validateLsCommand", func() {
|
cmd := Command{}
|
||||||
c, err := commandstest.NewCliContext(map[string]string{"path": "/var/lib/grafana/plugins"})
|
err = cmd.lsCommand(c)
|
||||||
So(err, ShouldBeNil)
|
assert.Error(t, err)
|
||||||
|
assert.Equal(t, expected, err, "can override validateLsCommand")
|
||||||
|
})
|
||||||
|
|
||||||
validateLsCommand = func(pluginDir string) error {
|
// meta-test for test cleanup of global variable
|
||||||
return errors.New("dummy error")
|
t.Run("validateLsCommand reset after test", func(t *testing.T) {
|
||||||
}
|
c, err := commandstest.NewCliContext(map[string]string{"path": "/var/lib/grafana/plugins"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("should return error", func() {
|
cmd := Command{}
|
||||||
cmd := Command{}
|
err = cmd.lsCommand(c)
|
||||||
err := cmd.lsCommand(c)
|
assert.NotEqual(t, err, expected, "validateLsCommand is reset")
|
||||||
So(err.Error(), ShouldEqual, "dummy error")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Validate that validateLsCommand is reset", func() {
|
|
||||||
c, err := commandstest.NewCliContext(map[string]string{"path": "/var/lib/grafana/plugins"})
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
cmd := Command{}
|
|
||||||
|
|
||||||
Convey("should return error", func() {
|
|
||||||
err := cmd.lsCommand(c)
|
|
||||||
So(err.Error(), ShouldNotEqual, "dummy error")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,25 +3,23 @@ package apikeygen
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestApiKeyGen(t *testing.T) {
|
func TestApiKeyGen(t *testing.T) {
|
||||||
Convey("When generating new api key", t, func() {
|
result, err := New(12, "Cool key")
|
||||||
result, err := New(12, "Cool key")
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
So(result.ClientSecret, ShouldNotBeEmpty)
|
assert.NotEmpty(t, result.ClientSecret)
|
||||||
So(result.HashedKey, ShouldNotBeEmpty)
|
assert.NotEmpty(t, result.HashedKey)
|
||||||
|
|
||||||
Convey("can decode key", func() {
|
keyInfo, err := Decode(result.ClientSecret)
|
||||||
keyInfo, err := Decode(result.ClientSecret)
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
keyHashed, err := util.EncodePassword(keyInfo.Key, keyInfo.Name)
|
keyHashed, err := util.EncodePassword(keyInfo.Key, keyInfo.Name)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
So(keyHashed, ShouldEqual, result.HashedKey)
|
assert.Equal(t, result.HashedKey, keyHashed)
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ package dashdiffs
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDiff(t *testing.T) {
|
func TestDiff(t *testing.T) {
|
||||||
@@ -60,71 +62,69 @@ func TestDiff(t *testing.T) {
|
|||||||
}`
|
}`
|
||||||
)
|
)
|
||||||
|
|
||||||
Convey("Testing dashboard diffs", t, func() {
|
// Compute the diff between the two JSON objects
|
||||||
// Compute the diff between the two JSON objects
|
baseData, err := simplejson.NewJson([]byte(leftJSON))
|
||||||
baseData, err := simplejson.NewJson([]byte(leftJSON))
|
require.NoError(t, err)
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
newData, err := simplejson.NewJson([]byte(rightJSON))
|
newData, err := simplejson.NewJson([]byte(rightJSON))
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
left, jsonDiff, err := getDiff(baseData, newData)
|
left, jsonDiff, err := getDiff(baseData, newData)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
Convey("The JSONFormatter should produce the expected JSON tokens", func() {
|
t.Run("JSONFormatter produces expected JSON tokens", func(t *testing.T) {
|
||||||
f := NewJSONFormatter(left)
|
f := NewJSONFormatter(left)
|
||||||
_, err := f.Format(jsonDiff)
|
_, err := f.Format(jsonDiff)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Total up the change types. If the number of different change
|
// Total up the change types. If the number of different change
|
||||||
// types is correct, it means that the diff is producing correct
|
// types is correct, it means that the diff is producing correct
|
||||||
// output to the template rendered.
|
// output to the template rendered.
|
||||||
changeCounts := make(map[ChangeType]int)
|
changeCounts := make(map[ChangeType]int)
|
||||||
for _, line := range f.Lines {
|
for _, line := range f.Lines {
|
||||||
changeCounts[line.Change]++
|
changeCounts[line.Change]++
|
||||||
|
}
|
||||||
|
|
||||||
|
// The expectedChangeCounts here were determined by manually
|
||||||
|
// looking at the JSON
|
||||||
|
expectedChangeCounts := map[ChangeType]int{
|
||||||
|
ChangeNil: 12,
|
||||||
|
ChangeAdded: 2,
|
||||||
|
ChangeDeleted: 1,
|
||||||
|
ChangeOld: 5,
|
||||||
|
ChangeNew: 5,
|
||||||
|
ChangeUnchanged: 5,
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, expectedChangeCounts, changeCounts)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("BasicFormatter produces expected BasicBlocks", func(t *testing.T) {
|
||||||
|
f := NewBasicFormatter(left)
|
||||||
|
_, err := f.Format(jsonDiff)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
bd := &BasicDiff{}
|
||||||
|
blocks := bd.Basic(f.jsonDiff.Lines)
|
||||||
|
|
||||||
|
changeCounts := make(map[ChangeType]int)
|
||||||
|
for _, block := range blocks {
|
||||||
|
for _, change := range block.Changes {
|
||||||
|
changeCounts[change.Change]++
|
||||||
}
|
}
|
||||||
|
|
||||||
// The expectedChangeCounts here were determined by manually
|
for _, summary := range block.Summaries {
|
||||||
// looking at the JSON
|
changeCounts[summary.Change]++
|
||||||
expectedChangeCounts := map[ChangeType]int{
|
|
||||||
ChangeNil: 12,
|
|
||||||
ChangeAdded: 2,
|
|
||||||
ChangeDeleted: 1,
|
|
||||||
ChangeOld: 5,
|
|
||||||
ChangeNew: 5,
|
|
||||||
ChangeUnchanged: 5,
|
|
||||||
}
|
|
||||||
So(changeCounts, ShouldResemble, expectedChangeCounts)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("The BasicFormatter should produce the expected BasicBlocks", func() {
|
|
||||||
f := NewBasicFormatter(left)
|
|
||||||
_, err := f.Format(jsonDiff)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
bd := &BasicDiff{}
|
|
||||||
blocks := bd.Basic(f.jsonDiff.Lines)
|
|
||||||
|
|
||||||
changeCounts := make(map[ChangeType]int)
|
|
||||||
for _, block := range blocks {
|
|
||||||
for _, change := range block.Changes {
|
|
||||||
changeCounts[change.Change]++
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, summary := range block.Summaries {
|
|
||||||
changeCounts[summary.Change]++
|
|
||||||
}
|
|
||||||
|
|
||||||
changeCounts[block.Change]++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedChangeCounts := map[ChangeType]int{
|
changeCounts[block.Change]++
|
||||||
ChangeNil: 3,
|
}
|
||||||
ChangeAdded: 2,
|
|
||||||
ChangeDeleted: 1,
|
expectedChangeCounts := map[ChangeType]int{
|
||||||
ChangeOld: 3,
|
ChangeNil: 3,
|
||||||
}
|
ChangeAdded: 2,
|
||||||
So(changeCounts, ShouldResemble, expectedChangeCounts)
|
ChangeDeleted: 1,
|
||||||
})
|
ChangeOld: 3,
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, expectedChangeCounts, changeCounts)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (w *FileLogWriter) WriteLine(line string) error {
|
func (w *FileLogWriter) WriteLine(line string) error {
|
||||||
@@ -17,30 +19,29 @@ func (w *FileLogWriter) WriteLine(line string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLogFile(t *testing.T) {
|
func TestLogFile(t *testing.T) {
|
||||||
Convey("When logging to file", t, func() {
|
fileLogWrite := NewFileWriter()
|
||||||
fileLogWrite := NewFileWriter()
|
require.NotNil(t, fileLogWrite)
|
||||||
So(fileLogWrite, ShouldNotBeNil)
|
|
||||||
|
|
||||||
fileLogWrite.Filename = "grafana_test.log"
|
|
||||||
err := fileLogWrite.Init()
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
Convey("Log file is empty", func() {
|
|
||||||
So(fileLogWrite.maxlines_curlines, ShouldEqual, 0)
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Logging should add lines", func() {
|
|
||||||
err := fileLogWrite.WriteLine("test1\n")
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
err = fileLogWrite.WriteLine("test2\n")
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
err = fileLogWrite.WriteLine("test3\n")
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(fileLogWrite.maxlines_curlines, ShouldEqual, 3)
|
|
||||||
})
|
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
fileLogWrite.Close()
|
fileLogWrite.Close()
|
||||||
err = os.Remove(fileLogWrite.Filename)
|
err := os.Remove(fileLogWrite.Filename)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
fileLogWrite.Filename = "grafana_test.log"
|
||||||
|
err := fileLogWrite.Init()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Zero(t, fileLogWrite.maxlines_curlines)
|
||||||
|
|
||||||
|
t.Run("adding lines", func(t *testing.T) {
|
||||||
|
err := fileLogWrite.WriteLine("test1\n")
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = fileLogWrite.WriteLine("test2\n")
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = fileLogWrite.WriteLine("test3\n")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, 3, fileLogWrite.maxlines_curlines)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,16 +3,14 @@ package log
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/inconshreveable/log15"
|
"github.com/inconshreveable/log15"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type FakeLogger struct {
|
type FakeLogger struct {
|
||||||
debug string
|
m map[string]string
|
||||||
info string
|
|
||||||
warn string
|
|
||||||
err string
|
|
||||||
crit string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeLogger) New(ctx ...interface{}) log15.Logger {
|
func (f *FakeLogger) New(ctx ...interface{}) log15.Logger {
|
||||||
@@ -20,23 +18,23 @@ func (f *FakeLogger) New(ctx ...interface{}) log15.Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeLogger) Debug(msg string, ctx ...interface{}) {
|
func (f *FakeLogger) Debug(msg string, ctx ...interface{}) {
|
||||||
f.debug = msg
|
f.m["debug"] = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeLogger) Info(msg string, ctx ...interface{}) {
|
func (f *FakeLogger) Info(msg string, ctx ...interface{}) {
|
||||||
f.info = msg
|
f.m["info"] = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeLogger) Warn(msg string, ctx ...interface{}) {
|
func (f *FakeLogger) Warn(msg string, ctx ...interface{}) {
|
||||||
f.warn = msg
|
f.m["warn"] = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeLogger) Error(msg string, ctx ...interface{}) {
|
func (f *FakeLogger) Error(msg string, ctx ...interface{}) {
|
||||||
f.err = msg
|
f.m["err"] = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeLogger) Crit(msg string, ctx ...interface{}) {
|
func (f *FakeLogger) Crit(msg string, ctx ...interface{}) {
|
||||||
f.crit = msg
|
f.m["crit"] = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeLogger) GetHandler() log15.Handler {
|
func (f *FakeLogger) GetHandler() log15.Handler {
|
||||||
@@ -45,72 +43,80 @@ func (f *FakeLogger) GetHandler() log15.Handler {
|
|||||||
|
|
||||||
func (f *FakeLogger) SetHandler(l log15.Handler) {}
|
func (f *FakeLogger) SetHandler(l log15.Handler) {}
|
||||||
|
|
||||||
func TestLogWriter(t *testing.T) {
|
func TestLogWriter_level(t *testing.T) {
|
||||||
Convey("When writing to a LogWriter", t, func() {
|
tests := []struct {
|
||||||
Convey("Should write using the correct level [crit]", func() {
|
description string
|
||||||
fake := &FakeLogger{}
|
logger string
|
||||||
|
prefix string
|
||||||
|
level Lvl
|
||||||
|
input []byte
|
||||||
|
expectedConsumed int
|
||||||
|
expectedOutput string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
description: "level crit",
|
||||||
|
logger: "crit",
|
||||||
|
input: []byte("crit"),
|
||||||
|
level: LvlCrit,
|
||||||
|
expectedConsumed: 4,
|
||||||
|
expectedOutput: "crit",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "level error",
|
||||||
|
logger: "err",
|
||||||
|
input: []byte("error"),
|
||||||
|
level: LvlError,
|
||||||
|
expectedConsumed: 5,
|
||||||
|
expectedOutput: "error",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "level warn",
|
||||||
|
logger: "warn",
|
||||||
|
input: []byte("warn"),
|
||||||
|
level: LvlWarn,
|
||||||
|
expectedConsumed: 4,
|
||||||
|
expectedOutput: "warn",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "level info",
|
||||||
|
logger: "info",
|
||||||
|
input: []byte("info"),
|
||||||
|
level: LvlInfo,
|
||||||
|
expectedConsumed: 4,
|
||||||
|
expectedOutput: "info",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "level debug",
|
||||||
|
logger: "debug",
|
||||||
|
input: []byte("debug"),
|
||||||
|
level: LvlDebug,
|
||||||
|
expectedConsumed: 5,
|
||||||
|
expectedOutput: "debug",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "prefix",
|
||||||
|
logger: "debug",
|
||||||
|
input: []byte("debug"),
|
||||||
|
prefix: "prefix",
|
||||||
|
level: LvlDebug,
|
||||||
|
expectedConsumed: 5,
|
||||||
|
expectedOutput: "prefixdebug",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
crit := NewLogWriter(fake, LvlCrit, "")
|
for _, tc := range tests {
|
||||||
n, err := crit.Write([]byte("crit"))
|
tc := tc // to avoid timing issues
|
||||||
|
|
||||||
So(n, ShouldEqual, 4)
|
t.Run(tc.description, func(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
t.Parallel()
|
||||||
So(fake.crit, ShouldEqual, "crit")
|
fake := &FakeLogger{m: map[string]string{}}
|
||||||
|
|
||||||
|
w := NewLogWriter(fake, tc.level, tc.prefix)
|
||||||
|
n, err := w.Write(tc.input)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, tc.expectedConsumed, n)
|
||||||
|
assert.Equal(t, tc.expectedOutput, fake.m[tc.logger])
|
||||||
})
|
})
|
||||||
|
}
|
||||||
Convey("Should write using the correct level [error]", func() {
|
|
||||||
fake := &FakeLogger{}
|
|
||||||
|
|
||||||
crit := NewLogWriter(fake, LvlError, "")
|
|
||||||
n, err := crit.Write([]byte("error"))
|
|
||||||
|
|
||||||
So(n, ShouldEqual, 5)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(fake.err, ShouldEqual, "error")
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Should write using the correct level [warn]", func() {
|
|
||||||
fake := &FakeLogger{}
|
|
||||||
|
|
||||||
crit := NewLogWriter(fake, LvlWarn, "")
|
|
||||||
n, err := crit.Write([]byte("warn"))
|
|
||||||
|
|
||||||
So(n, ShouldEqual, 4)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(fake.warn, ShouldEqual, "warn")
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Should write using the correct level [info]", func() {
|
|
||||||
fake := &FakeLogger{}
|
|
||||||
|
|
||||||
crit := NewLogWriter(fake, LvlInfo, "")
|
|
||||||
n, err := crit.Write([]byte("info"))
|
|
||||||
|
|
||||||
So(n, ShouldEqual, 4)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(fake.info, ShouldEqual, "info")
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Should write using the correct level [debug]", func() {
|
|
||||||
fake := &FakeLogger{}
|
|
||||||
|
|
||||||
crit := NewLogWriter(fake, LvlDebug, "")
|
|
||||||
n, err := crit.Write([]byte("debug"))
|
|
||||||
|
|
||||||
So(n, ShouldEqual, 5)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(fake.debug, ShouldEqual, "debug")
|
|
||||||
})
|
|
||||||
|
|
||||||
Convey("Should prefix the output with the prefix", func() {
|
|
||||||
fake := &FakeLogger{}
|
|
||||||
|
|
||||||
crit := NewLogWriter(fake, LvlDebug, "prefix")
|
|
||||||
n, err := crit.Write([]byte("debug"))
|
|
||||||
|
|
||||||
So(n, ShouldEqual, 5) // n is how much of input consumed
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(fake.debug, ShouldEqual, "prefixdebug")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func createTestableServerLock(t *testing.T) *ServerLockService {
|
func createTestableServerLock(t *testing.T) *ServerLockService {
|
||||||
@@ -21,35 +23,36 @@ func createTestableServerLock(t *testing.T) *ServerLockService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestServerLock(t *testing.T) {
|
func TestServerLock(t *testing.T) {
|
||||||
Convey("Server lock", t, func() {
|
sl := createTestableServerLock(t)
|
||||||
sl := createTestableServerLock(t)
|
operationUID := "test-operation"
|
||||||
operationUID := "test-operation"
|
|
||||||
|
|
||||||
first, err := sl.getOrCreate(context.Background(), operationUID)
|
first, err := sl.getOrCreate(context.Background(), operationUID)
|
||||||
So(err, ShouldBeNil)
|
require.NoError(t, err)
|
||||||
|
|
||||||
lastExecution := first.LastExecution
|
t.Run("trying to create three new row locks", func(t *testing.T) {
|
||||||
Convey("trying to create three new row locks", func() {
|
expectedLastExecution := first.LastExecution
|
||||||
for i := 0; i < 3; i++ {
|
var latest *serverLock
|
||||||
first, err = sl.getOrCreate(context.Background(), operationUID)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
So(first.OperationUid, ShouldEqual, operationUID)
|
|
||||||
So(first.Id, ShouldEqual, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
Convey("Should not create new since lock already exist", func() {
|
for i := 0; i < 3; i++ {
|
||||||
So(lastExecution, ShouldEqual, first.LastExecution)
|
latest, err = sl.getOrCreate(context.Background(), operationUID)
|
||||||
})
|
require.NoError(t, err)
|
||||||
})
|
assert.Equal(t, operationUID, first.OperationUid)
|
||||||
|
assert.Equal(t, int64(1), first.Id)
|
||||||
|
}
|
||||||
|
|
||||||
Convey("Should be able to create lock on first row", func() {
|
assert.Equal(t,
|
||||||
gotLock, err := sl.acquireLock(context.Background(), first)
|
expectedLastExecution,
|
||||||
So(err, ShouldBeNil)
|
latest.LastExecution,
|
||||||
So(gotLock, ShouldBeTrue)
|
"latest execution should not have changed")
|
||||||
|
})
|
||||||
|
|
||||||
gotLock, err = sl.acquireLock(context.Background(), first)
|
t.Run("create lock on first row", func(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
gotLock, err := sl.acquireLock(context.Background(), first)
|
||||||
So(gotLock, ShouldBeFalse)
|
require.NoError(t, err)
|
||||||
})
|
assert.True(t, gotLock)
|
||||||
|
|
||||||
|
gotLock, err = sl.acquireLock(context.Background(), first)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.False(t, gotLock)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user