mirror of
https://github.com/grafana/grafana.git
synced 2025-08-06 20:59:35 +08:00
Refactoring, worked on middleware unit tests, and began thinking about api unit tests, #1921
This commit is contained in:
35
pkg/api/api_test.go
Normal file
35
pkg/api/api_test.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHttpApi(t *testing.T) {
|
||||||
|
|
||||||
|
// Convey("Given the grafana api", t, func() {
|
||||||
|
// ConveyApiScenario("Can sign up", func(c apiTestContext) {
|
||||||
|
// c.PostJson()
|
||||||
|
// So(c.Resp, ShouldEqualJsonApiResponse, "User created and logged in")
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// m := macaron.New()
|
||||||
|
// m.Use(middleware.GetContextHandler())
|
||||||
|
// m.Use(middleware.Sessioner(&session.Options{}))
|
||||||
|
// Register(m)
|
||||||
|
//
|
||||||
|
// var context *middleware.Context
|
||||||
|
// m.Get("/", func(c *middleware.Context) {
|
||||||
|
// context = c
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// resp := httptest.NewRecorder()
|
||||||
|
// req, err := http.NewRequest("GET", "/", nil)
|
||||||
|
// So(err, ShouldBeNil)
|
||||||
|
//
|
||||||
|
// m.ServeHTTP(resp, req)
|
||||||
|
//
|
||||||
|
// Convey("should red 200", func() {
|
||||||
|
// So(resp.Code, ShouldEqual, 200)
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
}
|
@ -10,31 +10,49 @@ import (
|
|||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type scenarioContext struct {
|
||||||
|
m *macaron.Macaron
|
||||||
|
context *Context
|
||||||
|
resp *httptest.ResponseRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sc *scenarioContext) PerformGet(url string) {
|
||||||
|
req, err := http.NewRequest("GET", "/", nil)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
sc.m.ServeHTTP(sc.resp, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
type scenarioFunc func(c *scenarioContext)
|
||||||
|
|
||||||
|
func middlewareScenario(desc string, fn scenarioFunc) {
|
||||||
|
sc := &scenarioContext{}
|
||||||
|
|
||||||
|
sc.m = macaron.New()
|
||||||
|
sc.m.Use(GetContextHandler())
|
||||||
|
// mock out gc goroutine
|
||||||
|
startSessionGC = func() {}
|
||||||
|
sc.m.Use(Sessioner(&session.Options{}))
|
||||||
|
|
||||||
|
sc.m.Get("/", func(c *Context) {
|
||||||
|
sc.context = c
|
||||||
|
})
|
||||||
|
|
||||||
|
sc.resp = httptest.NewRecorder()
|
||||||
|
fn(sc)
|
||||||
|
}
|
||||||
|
|
||||||
func TestMiddlewareContext(t *testing.T) {
|
func TestMiddlewareContext(t *testing.T) {
|
||||||
|
|
||||||
Convey("Given grafana context", t, func() {
|
Convey("Given grafana context", t, func() {
|
||||||
m := macaron.New()
|
middlewareScenario("middleware should add context to injector", func(sc *scenarioContext) {
|
||||||
m.Use(GetContextHandler())
|
sc.PerformGet("/")
|
||||||
m.Use(Sessioner(&session.Options{}))
|
So(sc.context, ShouldNotBeNil)
|
||||||
|
|
||||||
var context *Context
|
|
||||||
|
|
||||||
m.Get("/", func(c *Context) {
|
|
||||||
context = c
|
|
||||||
})
|
})
|
||||||
|
|
||||||
resp := httptest.NewRecorder()
|
middlewareScenario("Default middleware should allow get request", func(sc *scenarioContext) {
|
||||||
req, err := http.NewRequest("GET", "/", nil)
|
sc.PerformGet("/")
|
||||||
So(err, ShouldBeNil)
|
So(sc.resp.Code, ShouldEqual, 200)
|
||||||
|
|
||||||
m.ServeHTTP(resp, req)
|
|
||||||
|
|
||||||
Convey("Should be able to get grafana context in handlers", func() {
|
|
||||||
So(context, ShouldNotBeNil)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("should return 200", func() {
|
|
||||||
So(resp.Code, ShouldEqual, 200)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,13 @@ const (
|
|||||||
|
|
||||||
var sessionManager *session.Manager
|
var sessionManager *session.Manager
|
||||||
var sessionOptions *session.Options
|
var sessionOptions *session.Options
|
||||||
|
var startSessionGC func()
|
||||||
|
|
||||||
func startSessionGC() {
|
func init() {
|
||||||
sessionManager.GC()
|
startSessionGC = func() {
|
||||||
time.AfterFunc(time.Duration(sessionOptions.Gclifetime)*time.Second, startSessionGC)
|
sessionManager.GC()
|
||||||
|
time.AfterFunc(time.Duration(sessionOptions.Gclifetime)*time.Second, startSessionGC)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareOptions(opt *session.Options) *session.Options {
|
func prepareOptions(opt *session.Options) *session.Options {
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
package migrations
|
package migrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/grafana/grafana/pkg/log"
|
|
||||||
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
|
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
|
||||||
|
|
||||||
@ -16,7 +13,7 @@ import (
|
|||||||
var indexTypes = []string{"Unknown", "INDEX", "UNIQUE INDEX"}
|
var indexTypes = []string{"Unknown", "INDEX", "UNIQUE INDEX"}
|
||||||
|
|
||||||
func TestMigrations(t *testing.T) {
|
func TestMigrations(t *testing.T) {
|
||||||
log.NewLogger(0, "console", `{"level": 0}`)
|
//log.NewLogger(0, "console", `{"level": 0}`)
|
||||||
|
|
||||||
testDBs := []sqlutil.TestDB{
|
testDBs := []sqlutil.TestDB{
|
||||||
sqlutil.TestDB_Sqlite3,
|
sqlutil.TestDB_Sqlite3,
|
||||||
@ -31,30 +28,30 @@ func TestMigrations(t *testing.T) {
|
|||||||
sqlutil.CleanDB(x)
|
sqlutil.CleanDB(x)
|
||||||
|
|
||||||
mg := NewMigrator(x)
|
mg := NewMigrator(x)
|
||||||
mg.LogLevel = log.DEBUG
|
//mg.LogLevel = log.DEBUG
|
||||||
AddMigrations(mg)
|
AddMigrations(mg)
|
||||||
|
|
||||||
err = mg.Start()
|
err = mg.Start()
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
tables, err := x.DBMetas()
|
// tables, err := x.DBMetas()
|
||||||
So(err, ShouldBeNil)
|
// So(err, ShouldBeNil)
|
||||||
|
//
|
||||||
fmt.Printf("\nDB Schema after migration: table count: %v\n", len(tables))
|
// fmt.Printf("\nDB Schema after migration: table count: %v\n", len(tables))
|
||||||
|
//
|
||||||
for _, table := range tables {
|
// for _, table := range tables {
|
||||||
fmt.Printf("\nTable: %v \n", table.Name)
|
// fmt.Printf("\nTable: %v \n", table.Name)
|
||||||
for _, column := range table.Columns() {
|
// for _, column := range table.Columns() {
|
||||||
fmt.Printf("\t %v \n", column.String(x.Dialect()))
|
// fmt.Printf("\t %v \n", column.String(x.Dialect()))
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
if len(table.Indexes) > 0 {
|
// if len(table.Indexes) > 0 {
|
||||||
fmt.Printf("\n\tIndexes:\n")
|
// fmt.Printf("\n\tIndexes:\n")
|
||||||
for _, index := range table.Indexes {
|
// for _, index := range table.Indexes {
|
||||||
fmt.Printf("\t %v (%v) %v \n", index.Name, strings.Join(index.Cols, ","), indexTypes[index.Type])
|
// fmt.Printf("\t %v (%v) %v \n", index.Name, strings.Join(index.Cols, ","), indexTypes[index.Type])
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestAccountDataAccess(t *testing.T) {
|
func TestAccountDataAccess(t *testing.T) {
|
||||||
|
|
||||||
Convey("Testing Account DB Access", t, func() {
|
Convey("Testing Account DB Access", t, func() {
|
||||||
InitTestDB(t)
|
InitTestDB(t)
|
||||||
|
|
||||||
|
@ -122,7 +122,7 @@ type CommandLineArgs struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
IsWindows = runtime.GOOS == "windows"
|
IsWindows = runtime.GOOS == "windows"
|
||||||
log.NewLogger(0, "console", `{"level": 0}`)
|
//log.NewLogger(0, "console", `{"level": 0}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAppUrlAndSubUrl(section *ini.Section) (string, string) {
|
func parseAppUrlAndSubUrl(section *ini.Section) (string, string) {
|
||||||
|
Reference in New Issue
Block a user