Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
b68634fb
Unverified
Commit
b68634fb
authored
Aug 14, 2020
by
Emil Tullstedt
Committed by
GitHub
Aug 14, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests: Batch of GoConvey to Testify conversions (#27008)
parent
7ad1e366
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
294 additions
and
269 deletions
+294
-269
pkg/api/http_server_test.go
+15
-16
pkg/cmd/grafana-cli/commands/ls_command.go
+6
-2
pkg/cmd/grafana-cli/commands/ls_command_test.go
+59
-45
pkg/components/apikeygen/apikeygen_test.go
+12
-14
pkg/components/dashdiffs/formatter_test.go
+59
-59
pkg/infra/log/file_test.go
+25
-24
pkg/infra/log/log_writer_test.go
+84
-78
pkg/infra/serverlock/serverlock_test.go
+34
-31
No files found.
pkg/api/http_server_test.go
View file @
b68634fb
...
@@ -3,28 +3,27 @@ package api
...
@@ -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
())
})
})
})
}
}
pkg/cmd/grafana-cli/commands/ls_command.go
View file @
b68634fb
...
@@ -12,9 +12,13 @@ import (
...
@@ -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
err
ors
.
New
(
"missing path flag"
)
return
err
MissingPathFlag
}
}
logger
.
Debug
(
"plugindir: "
+
pluginDir
+
"
\n
"
)
logger
.
Debug
(
"plugindir: "
+
pluginDir
+
"
\n
"
)
...
@@ -24,7 +28,7 @@ var validateLsCommand = func(pluginDir string) error {
...
@@ -24,7 +28,7 @@ var validateLsCommand = func(pluginDir string) error {
}
}
if
!
pluginDirInfo
.
IsDir
()
{
if
!
pluginDirInfo
.
IsDir
()
{
return
err
ors
.
New
(
"plugin path is not a directory"
)
return
err
NotDirectory
}
}
return
nil
return
nil
...
...
pkg/cmd/grafana-cli/commands/ls_command_test.go
View file @
b68634fb
...
@@ -4,67 +4,81 @@ import (
...
@@ -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
})
Convey
(
"Missing path flag"
,
func
()
{
c
,
err
:=
commandstest
.
NewCliContext
(
tc
.
cliContext
)
cmd
:=
Command
{}
require
.
NoError
(
t
,
err
)
c
,
err
:=
commandstest
.
NewCliContext
(
map
[
string
]
string
{})
So
(
err
,
ShouldBeNil
)
services
.
IoHelper
=
&
commandstest
.
FakeIoUtil
{}
Convey
(
"should return error"
,
func
()
{
cmd
:=
Command
{}
err
:=
cmd
.
lsCommand
(
c
)
err
=
cmd
.
lsCommand
(
c
)
So
(
err
,
ShouldBeError
,
"missing path flag"
)
assert
.
Equal
(
t
,
tc
.
error
,
err
)
})
})
})
}
}
Convey
(
"Path is not a directory"
,
func
()
{
func
TestValidateLsCommand_override
(
t
*
testing
.
T
)
{
c
,
err
:=
commandstest
.
NewCliContext
(
map
[
string
]
string
{
"path"
:
"/var/lib/grafana/plugins"
})
expected
:=
errors
.
New
(
"dummy error"
)
So
(
err
,
ShouldBeNil
)
t
.
Run
(
"override validateLsCommand"
,
func
(
t
*
testing
.
T
)
{
services
.
IoHelper
=
&
commandstest
.
FakeIoUtil
{
var
org
=
validateLsCommand
FakeIsDirectory
:
false
,
}
cmd
:=
Command
{}
Convey
(
"should return error"
,
func
()
{
t
.
Cleanup
(
func
()
{
err
:=
cmd
.
lsCommand
(
c
)
validateLsCommand
=
org
So
(
err
,
ShouldNotBeNil
)
})
})
})
Convey
(
"can override validateLsCommand"
,
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
)
validateLsCommand
=
func
(
pluginDir
string
)
error
{
validateLsCommand
=
func
(
pluginDir
string
)
error
{
return
errors
.
New
(
"dummy error"
)
return
expected
}
}
Convey
(
"should return error"
,
func
()
{
cmd
:=
Command
{}
cmd
:=
Command
{}
err
=
cmd
.
lsCommand
(
c
)
err
:=
cmd
.
lsCommand
(
c
)
assert
.
Error
(
t
,
err
)
So
(
err
.
Error
(),
ShouldEqual
,
"dummy error"
)
assert
.
Equal
(
t
,
expected
,
err
,
"can override validateLsCommand"
)
})
})
})
Convey
(
"Validate that validateLsCommand is reset"
,
func
()
{
// meta-test for test cleanup of global variable
c
,
err
:=
commandstest
.
NewCliContext
(
map
[
string
]
string
{
"path"
:
"/var/lib/grafana/plugins"
})
t
.
Run
(
"validateLsCommand reset after test"
,
func
(
t
*
testing
.
T
)
{
So
(
err
,
ShouldBeNil
)
c
,
err
:=
commandstest
.
NewCliContext
(
map
[
string
]
string
{
"path"
:
"/var/lib/grafana/plugins"
}
)
cmd
:=
Command
{}
require
.
NoError
(
t
,
err
)
Convey
(
"should return error"
,
func
()
{
cmd
:=
Command
{}
err
:=
cmd
.
lsCommand
(
c
)
err
=
cmd
.
lsCommand
(
c
)
So
(
err
.
Error
(),
ShouldNotEqual
,
"dummy error"
)
assert
.
NotEqual
(
t
,
err
,
expected
,
"validateLsCommand is reset"
)
})
})
})
})
}
}
pkg/components/apikeygen/apikeygen_test.go
View file @
b68634fb
...
@@ -3,25 +3,23 @@ package apikeygen
...
@@ -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
,
ShouldNotBeEmpt
y
)
assert
.
NotEmpty
(
t
,
result
.
HashedKe
y
)
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
)
})
})
}
}
pkg/components/dashdiffs/formatter_test.go
View file @
b68634fb
...
@@ -3,8 +3,10 @@ package dashdiffs
...
@@ -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) {
...
@@ -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
))
require
.
NoError
(
t
,
err
)
So
(
err
,
ShouldBeNil
)
left
,
jsonDiff
,
err
:=
getDiff
(
baseData
,
newData
)
require
.
NoError
(
t
,
err
)
t
.
Run
(
"JSONFormatter produces expected JSON tokens"
,
func
(
t
*
testing
.
T
)
{
f
:=
NewJSONFormatter
(
left
)
_
,
err
:=
f
.
Format
(
jsonDiff
)
require
.
NoError
(
t
,
err
)
// Total up the change types. If the number of different change
// types is correct, it means that the diff is producing correct
// output to the template rendered.
changeCounts
:=
make
(
map
[
ChangeType
]
int
)
for
_
,
line
:=
range
f
.
Lines
{
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
)
})
left
,
jsonDiff
,
err
:=
getDiff
(
baseData
,
newData
)
t
.
Run
(
"BasicFormatter produces expected BasicBlocks"
,
func
(
t
*
testing
.
T
)
{
So
(
err
,
ShouldBeNil
)
f
:=
NewBasicFormatter
(
left
)
_
,
err
:=
f
.
Format
(
jsonDiff
)
require
.
NoError
(
t
,
err
)
Convey
(
"The JSONFormatter should produce the expected JSON tokens"
,
func
()
{
bd
:=
&
BasicDiff
{}
f
:=
NewJSONFormatter
(
left
)
blocks
:=
bd
.
Basic
(
f
.
jsonDiff
.
Lines
)
_
,
err
:=
f
.
Format
(
jsonDiff
)
So
(
err
,
ShouldBeNil
)
// Total up the change types. If the number of different change
changeCounts
:=
make
(
map
[
ChangeType
]
int
)
// types is correct, it means that the diff is producing correct
for
_
,
block
:=
range
blocks
{
// output to the template rendered.
for
_
,
change
:=
range
block
.
Changes
{
changeCounts
:=
make
(
map
[
ChangeType
]
int
)
changeCounts
[
change
.
Change
]
++
for
_
,
line
:=
range
f
.
Lines
{
changeCounts
[
line
.
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
()
{
changeCounts
[
block
.
Change
]
++
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
{
expectedChangeCounts
:=
map
[
ChangeType
]
int
{
changeCounts
[
summary
.
Change
]
++
ChangeNil
:
3
,
}
ChangeAdded
:
2
,
ChangeDeleted
:
1
,
changeCounts
[
block
.
Change
]
++
ChangeOld
:
3
,
}
}
assert
.
EqualValues
(
t
,
expectedChangeCounts
,
changeCounts
)
expectedChangeCounts
:=
map
[
ChangeType
]
int
{
ChangeNil
:
3
,
ChangeAdded
:
2
,
ChangeDeleted
:
1
,
ChangeOld
:
3
,
}
So
(
changeCounts
,
ShouldResemble
,
expectedChangeCounts
)
})
})
})
}
}
pkg/infra/log/file_test.go
View file @
b68634fb
...
@@ -4,7 +4,9 @@ import (
...
@@ -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 {
...
@@ -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
)
})
})
}
}
pkg/infra/log/log_writer_test.go
View file @
b68634fb
...
@@ -3,16 +3,14 @@ package log
...
@@ -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 {
...
@@ -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 {
...
@@ -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
crit
:=
NewLogWriter
(
fake
,
LvlCrit
,
""
)
level
Lvl
n
,
err
:=
crit
.
Write
([]
byte
(
"crit"
))
input
[]
byte
expectedConsumed
int
So
(
n
,
ShouldEqual
,
4
)
expectedOutput
string
So
(
err
,
ShouldBeNil
)
}{
So
(
fake
.
crit
,
ShouldEqual
,
"crit"
)
{
})
description
:
"level crit"
,
logger
:
"crit"
,
Convey
(
"Should write using the correct level [error]"
,
func
()
{
input
:
[]
byte
(
"crit"
),
fake
:=
&
FakeLogger
{}
level
:
LvlCrit
,
expectedConsumed
:
4
,
crit
:=
NewLogWriter
(
fake
,
LvlError
,
""
)
expectedOutput
:
"crit"
,
n
,
err
:=
crit
.
Write
([]
byte
(
"error"
))
},
{
So
(
n
,
ShouldEqual
,
5
)
description
:
"level error"
,
So
(
err
,
ShouldBeNil
)
logger
:
"err"
,
So
(
fake
.
err
,
ShouldEqual
,
"error"
)
input
:
[]
byte
(
"error"
),
})
level
:
LvlError
,
expectedConsumed
:
5
,
Convey
(
"Should write using the correct level [warn]"
,
func
()
{
expectedOutput
:
"error"
,
fake
:=
&
FakeLogger
{}
},
{
crit
:=
NewLogWriter
(
fake
,
LvlWarn
,
""
)
description
:
"level warn"
,
n
,
err
:=
crit
.
Write
([]
byte
(
"warn"
))
logger
:
"warn"
,
input
:
[]
byte
(
"warn"
),
So
(
n
,
ShouldEqual
,
4
)
level
:
LvlWarn
,
So
(
err
,
ShouldBeNil
)
expectedConsumed
:
4
,
So
(
fake
.
warn
,
ShouldEqual
,
"warn"
)
expectedOutput
:
"warn"
,
})
},
{
Convey
(
"Should write using the correct level [info]"
,
func
()
{
description
:
"level info"
,
fake
:=
&
FakeLogger
{}
logger
:
"info"
,
input
:
[]
byte
(
"info"
),
crit
:=
NewLogWriter
(
fake
,
LvlInfo
,
""
)
level
:
LvlInfo
,
n
,
err
:=
crit
.
Write
([]
byte
(
"info"
))
expectedConsumed
:
4
,
expectedOutput
:
"info"
,
So
(
n
,
ShouldEqual
,
4
)
},
So
(
err
,
ShouldBeNil
)
{
So
(
fake
.
info
,
ShouldEqual
,
"info"
)
description
:
"level debug"
,
})
logger
:
"debug"
,
input
:
[]
byte
(
"debug"
),
Convey
(
"Should write using the correct level [debug]"
,
func
()
{
level
:
LvlDebug
,
fake
:=
&
FakeLogger
{}
expectedConsumed
:
5
,
expectedOutput
:
"debug"
,
crit
:=
NewLogWriter
(
fake
,
LvlDebug
,
""
)
},
n
,
err
:=
crit
.
Write
([]
byte
(
"debug"
))
{
description
:
"prefix"
,
So
(
n
,
ShouldEqual
,
5
)
logger
:
"debug"
,
So
(
err
,
ShouldBeNil
)
input
:
[]
byte
(
"debug"
),
So
(
fake
.
debug
,
ShouldEqual
,
"debug"
)
prefix
:
"prefix"
,
})
level
:
LvlDebug
,
expectedConsumed
:
5
,
Convey
(
"Should prefix the output with the prefix"
,
func
()
{
expectedOutput
:
"prefixdebug"
,
fake
:=
&
FakeLogger
{}
},
}
crit
:=
NewLogWriter
(
fake
,
LvlDebug
,
"prefix"
)
n
,
err
:=
crit
.
Write
([]
byte
(
"debug"
))
for
_
,
tc
:=
range
tests
{
tc
:=
tc
// to avoid timing issues
So
(
n
,
ShouldEqual
,
5
)
// n is how much of input consumed
So
(
err
,
ShouldBeNil
)
t
.
Run
(
tc
.
description
,
func
(
t
*
testing
.
T
)
{
So
(
fake
.
debug
,
ShouldEqual
,
"prefixdebug"
)
t
.
Parallel
()
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
])
})
})
}
)
}
}
}
pkg/infra/serverlock/serverlock_test.go
View file @
b68634fb
...
@@ -4,9 +4,11 @@ import (
...
@@ -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 {
...
@@ -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
)
require
.
NoError
(
t
,
err
)
So
(
err
,
ShouldBeNil
)
t
.
Run
(
"trying to create three new row locks"
,
func
(
t
*
testing
.
T
)
{
lastExecution
:=
first
.
LastExecution
expectedLastExecution
:=
first
.
LastExecution
Convey
(
"trying to create three new row locks"
,
func
()
{
var
latest
*
serverLock
for
i
:=
0
;
i
<
3
;
i
++
{
first
,
err
=
sl
.
getOrCreate
(
context
.
Background
(),
operationUID
)
for
i
:=
0
;
i
<
3
;
i
++
{
So
(
err
,
ShouldBeNil
)
latest
,
err
=
sl
.
getOrCreate
(
context
.
Background
(),
operationUID
)
So
(
first
.
OperationUid
,
ShouldEqual
,
operationUID
)
require
.
NoError
(
t
,
err
)
So
(
first
.
Id
,
ShouldEqual
,
1
)
assert
.
Equal
(
t
,
operationUID
,
first
.
OperationUid
)
}
assert
.
Equal
(
t
,
int64
(
1
),
first
.
Id
)
}
Convey
(
"Should not create new since lock already exist"
,
func
()
{
So
(
lastExecution
,
ShouldEqual
,
first
.
LastExecution
)
assert
.
Equal
(
t
,
})
expectedLastExecution
,
})
latest
.
LastExecution
,
"latest execution should not have changed"
)
Convey
(
"Should be able to create lock on first row"
,
func
()
{
})
gotLock
,
err
:=
sl
.
acquireLock
(
context
.
Background
(),
first
)
So
(
err
,
ShouldBeNil
)
t
.
Run
(
"create lock on first row"
,
func
(
t
*
testing
.
T
)
{
So
(
gotLock
,
ShouldBeTrue
)
gotLock
,
err
:=
sl
.
acquireLock
(
context
.
Background
(),
first
)
require
.
NoError
(
t
,
err
)
gotLock
,
err
=
sl
.
acquireLock
(
context
.
Background
(),
first
)
assert
.
True
(
t
,
gotLock
)
So
(
err
,
ShouldBeNil
)
So
(
gotLock
,
ShouldBeFalse
)
gotLock
,
err
=
sl
.
acquireLock
(
context
.
Background
(),
first
)
})
require
.
NoError
(
t
,
err
)
assert
.
False
(
t
,
gotLock
)
})
})
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment