chore: make chronograf unit tests pass
parent
93ca5c9f76
commit
d2ece11acb
|
@ -15,12 +15,12 @@ var _ chronograf.SourcesStore = &SourcesStore{}
|
|||
// SourcesBucket is the bolt bucket used to store source information
|
||||
var SourcesBucket = []byte("Sources")
|
||||
|
||||
var defaultSource = &chronograf.Source{
|
||||
ID: 0,
|
||||
var DefaultSource = &chronograf.Source{
|
||||
ID: 9223372036854775807, // Make largest int64 so that there won't be collisions
|
||||
Name: "autogen",
|
||||
Type: "influx",
|
||||
URL: "http://localhost:9999",
|
||||
Default: true,
|
||||
Default: false,
|
||||
}
|
||||
|
||||
// SourcesStore is a bolt implementation to store time-series source information.
|
||||
|
@ -29,14 +29,16 @@ type SourcesStore struct {
|
|||
}
|
||||
|
||||
func (s *SourcesStore) Migrate(ctx context.Context) error {
|
||||
if err := s.Put(ctx, defaultSource); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sources, err := s.All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(sources) == 0 {
|
||||
if err := s.Put(ctx, DefaultSource); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
defaultOrg, err := s.client.OrganizationsStore.DefaultOrganization(ctx)
|
||||
if err != nil {
|
||||
|
@ -159,11 +161,6 @@ func (s *SourcesStore) Put(ctx context.Context, src *chronograf.Source) error {
|
|||
}
|
||||
|
||||
func (s *SourcesStore) put(ctx context.Context, src *chronograf.Source, tx *bolt.Tx) error {
|
||||
if src.Default {
|
||||
if err := s.resetDefaultSource(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
b := tx.Bucket(SourcesBucket)
|
||||
|
||||
if v, err := internal.MarshalSource(*src); err != nil {
|
||||
|
|
|
@ -146,8 +146,8 @@ func TestSourceStore(t *testing.T) {
|
|||
|
||||
if bsrcs, err := s.All(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(bsrcs) != 2 {
|
||||
t.Fatalf("After delete All returned incorrect number of srcs; got %d, expected %d", len(bsrcs), 2)
|
||||
} else if len(bsrcs) != 3 {
|
||||
t.Fatalf("After delete All returned incorrect number of srcs; got %d, expected %d", len(bsrcs), 3)
|
||||
} else if !reflect.DeepEqual(bsrcs[0], srcs[1]) {
|
||||
t.Fatalf("After delete All returned incorrect source; got %v, expected %v", bsrcs[0], srcs[1])
|
||||
}
|
||||
|
@ -159,6 +159,9 @@ func TestSourceStore(t *testing.T) {
|
|||
if err := s.Delete(ctx, srcs[2]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Delete(ctx, *bolt.DefaultSource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Try to add one source as a non-default and ensure that it becomes a
|
||||
// default
|
||||
|
|
|
@ -8,9 +8,9 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/influxdata/platform/chronograf"
|
||||
"github.com/influxdata/platform/chronograf/mocks"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func TestService_Annotations(t *testing.T) {
|
||||
|
@ -168,8 +168,9 @@ func TestService_Annotations(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.r = tt.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.r = tt.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
|
|
@ -5,9 +5,9 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/influxdata/platform/chronograf"
|
||||
idgen "github.com/influxdata/platform/chronograf/id"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -273,7 +273,7 @@ func (s *Service) DashboardCellID(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
boards := newDashboardResponse(dash)
|
||||
cid := httprouter.GetParamFromContext(ctx, "cid")
|
||||
cid := httprouter.ParamsFromContext(ctx).ByName("cid")
|
||||
for _, cell := range boards.Cells {
|
||||
if cell.ID == cid {
|
||||
encodeJSON(w, http.StatusOK, cell, s.Logger)
|
||||
|
@ -298,7 +298,7 @@ func (s *Service) RemoveDashboardCell(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
cid := httprouter.GetParamFromContext(ctx, "cid")
|
||||
cid := httprouter.ParamsFromContext(ctx).ByName("cid")
|
||||
cellid := -1
|
||||
for i, cell := range dash.Cells {
|
||||
if cell.ID == cid {
|
||||
|
@ -335,7 +335,7 @@ func (s *Service) ReplaceDashboardCell(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
cid := httprouter.GetParamFromContext(ctx, "cid")
|
||||
cid := httprouter.ParamsFromContext(ctx).ByName("cid")
|
||||
cellid := -1
|
||||
for i, cell := range dash.Cells {
|
||||
if cell.ID == cid {
|
||||
|
|
|
@ -12,10 +12,10 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/influxdata/platform/chronograf"
|
||||
"github.com/influxdata/platform/chronograf/mocks"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func Test_Cells_CorrectAxis(t *testing.T) {
|
||||
|
@ -220,7 +220,11 @@ func Test_Service_DashboardCells(t *testing.T) {
|
|||
Value: v,
|
||||
})
|
||||
}
|
||||
ctx = httprouter.WithParams(ctx, params)
|
||||
ctx = context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
params,
|
||||
)
|
||||
|
||||
// setup response recorder and request
|
||||
rr := httptest.NewRecorder()
|
||||
|
@ -674,6 +678,19 @@ func TestService_ReplaceDashboardCell(t *testing.T) {
|
|||
"id": tt.ID,
|
||||
"cid": tt.CID,
|
||||
})
|
||||
tt.r = tt.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
Value: tt.ID,
|
||||
},
|
||||
{
|
||||
Key: "cid",
|
||||
Value: tt.CID,
|
||||
},
|
||||
}))
|
||||
s.ReplaceDashboardCell(tt.w, tt.r)
|
||||
got := tt.w.Body.String()
|
||||
if got != tt.want {
|
||||
|
|
|
@ -8,8 +8,8 @@ import (
|
|||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/influxdata/platform/chronograf"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -221,7 +221,7 @@ func (h *Service) DropDatabase(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
db := httprouter.GetParamFromContext(ctx, "db")
|
||||
db := httprouter.ParamsFromContext(ctx).ByName("db")
|
||||
|
||||
dropErr := dbsvc.DropDB(ctx, db)
|
||||
if dropErr != nil {
|
||||
|
@ -255,7 +255,7 @@ func (h *Service) RetentionPolicies(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
db := httprouter.GetParamFromContext(ctx, "db")
|
||||
db := httprouter.ParamsFromContext(ctx).ByName("db")
|
||||
res, err := h.allRPs(ctx, dbsvc, srcID, db)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Unable to connect get RPs %d: %v", srcID, err)
|
||||
|
@ -319,7 +319,7 @@ func (h *Service) NewRetentionPolicy(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
db := httprouter.GetParamFromContext(ctx, "db")
|
||||
db := httprouter.ParamsFromContext(ctx).ByName("db")
|
||||
rp, err := dbsvc.CreateRP(ctx, db, postedRP)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, err.Error(), h.Logger)
|
||||
|
@ -369,8 +369,9 @@ func (h *Service) UpdateRetentionPolicy(w http.ResponseWriter, r *http.Request)
|
|||
return
|
||||
}
|
||||
|
||||
db := httprouter.GetParamFromContext(ctx, "db")
|
||||
rp := httprouter.GetParamFromContext(ctx, "rp")
|
||||
params := httprouter.ParamsFromContext(ctx)
|
||||
db := params.ByName("db")
|
||||
rp := params.ByName("rp")
|
||||
p, err := dbsvc.UpdateRP(ctx, db, rp, postedRP)
|
||||
|
||||
if err != nil {
|
||||
|
@ -412,8 +413,9 @@ func (s *Service) DropRetentionPolicy(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
db := httprouter.GetParamFromContext(ctx, "db")
|
||||
rp := httprouter.GetParamFromContext(ctx, "rp")
|
||||
params := httprouter.ParamsFromContext(ctx)
|
||||
db := params.ByName("db")
|
||||
rp := params.ByName("rp")
|
||||
dropErr := dbsvc.DropRP(ctx, db, rp)
|
||||
if dropErr != nil {
|
||||
Error(w, http.StatusBadRequest, dropErr.Error(), s.Logger)
|
||||
|
@ -452,7 +454,7 @@ func (h *Service) Measurements(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
db := httprouter.GetParamFromContext(ctx, "db")
|
||||
db := httprouter.ParamsFromContext(ctx).ByName("db")
|
||||
measurements, err := dbsvc.GetMeasurements(ctx, db, limit, offset)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Unable to get measurements %d: %v", srcID, err)
|
||||
|
|
|
@ -8,10 +8,10 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/influxdata/platform/chronograf"
|
||||
"github.com/influxdata/platform/chronograf/log"
|
||||
"github.com/influxdata/platform/chronograf/mocks"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func TestService_GetDatabases(t *testing.T) {
|
||||
|
@ -562,8 +562,9 @@ func TestService_Measurements(t *testing.T) {
|
|||
"http://any.url",
|
||||
nil,
|
||||
)
|
||||
r = r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
r = r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
|
||||
"github.com/influxdata/platform/chronograf/mocks"
|
||||
|
||||
|
@ -83,15 +83,15 @@ func TestService_Influx(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
Value: tt.ID,
|
||||
},
|
||||
},
|
||||
))
|
||||
}))
|
||||
h := &Service{
|
||||
Store: &mocks.Store{
|
||||
SourcesStore: tt.fields.SourcesStore,
|
||||
|
|
|
@ -8,10 +8,10 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/influxdata/platform/chronograf"
|
||||
"github.com/influxdata/platform/chronograf/log"
|
||||
"github.com/influxdata/platform/chronograf/mocks"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func TestService_Permissions(t *testing.T) {
|
||||
|
@ -80,8 +80,9 @@ func TestService_Permissions(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
|
|
@ -7,9 +7,9 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/influxdata/platform/chronograf"
|
||||
"github.com/influxdata/platform/chronograf/mocks"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func TestService_Queries(t *testing.T) {
|
||||
|
@ -87,8 +87,9 @@ func TestService_Queries(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.r = tt.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.r = tt.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
|
|
@ -10,11 +10,11 @@ import (
|
|||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/bouk/httprouter"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/influxdata/platform/chronograf"
|
||||
"github.com/influxdata/platform/chronograf/log"
|
||||
"github.com/influxdata/platform/chronograf/mocks"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func Test_ValidSourceRequest(t *testing.T) {
|
||||
|
@ -449,8 +449,9 @@ func TestService_SourcesID(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
@ -553,8 +554,9 @@ func TestService_UpdateSource(t *testing.T) {
|
|||
}))
|
||||
defer ts.Close()
|
||||
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
@ -857,8 +859,9 @@ func TestService_NewSourceUser(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
@ -1027,8 +1030,9 @@ func TestService_SourceUsers(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
@ -1194,8 +1198,9 @@ func TestService_SourceUserID(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
@ -1291,8 +1296,9 @@ func TestService_RemoveSourceUser(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
@ -1471,17 +1477,14 @@ func TestService_UpdateSourceUser(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
Value: tt.ID,
|
||||
},
|
||||
{
|
||||
Key: "uid",
|
||||
Value: tt.UID,
|
||||
},
|
||||
}))
|
||||
h := &Service{
|
||||
Store: &mocks.Store{
|
||||
|
@ -1715,8 +1718,9 @@ func TestService_NewSourceRole(t *testing.T) {
|
|||
TimeSeriesClient: tt.fields.TimeSeries,
|
||||
Logger: tt.fields.Logger,
|
||||
}
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
|
@ -1831,17 +1835,14 @@ func TestService_UpdateSourceRole(t *testing.T) {
|
|||
Logger: tt.fields.Logger,
|
||||
}
|
||||
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
Value: tt.ID,
|
||||
},
|
||||
{
|
||||
Key: "rid",
|
||||
Value: tt.RoleID,
|
||||
},
|
||||
}))
|
||||
|
||||
h.UpdateSourceRole(tt.args.w, tt.args.r)
|
||||
|
@ -1956,17 +1957,14 @@ func TestService_SourceRoleID(t *testing.T) {
|
|||
Logger: tt.fields.Logger,
|
||||
}
|
||||
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
Value: tt.ID,
|
||||
},
|
||||
{
|
||||
Key: "rid",
|
||||
Value: tt.RoleID,
|
||||
},
|
||||
}))
|
||||
|
||||
h.SourceRoleID(tt.args.w, tt.args.r)
|
||||
|
@ -2054,17 +2052,14 @@ func TestService_RemoveSourceRole(t *testing.T) {
|
|||
Logger: tt.fields.Logger,
|
||||
}
|
||||
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
Value: tt.ID,
|
||||
},
|
||||
{
|
||||
Key: "rid",
|
||||
Value: tt.RoleID,
|
||||
},
|
||||
}))
|
||||
|
||||
h.RemoveSourceRole(tt.args.w, tt.args.r)
|
||||
|
@ -2168,17 +2163,14 @@ func TestService_SourceRoles(t *testing.T) {
|
|||
Logger: tt.fields.Logger,
|
||||
}
|
||||
|
||||
tt.args.r = tt.args.r.WithContext(httprouter.WithParams(
|
||||
context.Background(),
|
||||
tt.args.r = tt.args.r.WithContext(context.WithValue(
|
||||
context.TODO(),
|
||||
httprouter.ParamsKey,
|
||||
httprouter.Params{
|
||||
{
|
||||
Key: "id",
|
||||
Value: tt.ID,
|
||||
},
|
||||
{
|
||||
Key: "rid",
|
||||
Value: tt.RoleID,
|
||||
},
|
||||
}))
|
||||
|
||||
h.SourceRoles(tt.args.w, tt.args.r)
|
||||
|
|
Loading…
Reference in New Issue