mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
automagic sql db migrations (#461)
* adds migrations closes #57 migrations only run if the database is not brand new. brand new databases will contain all the right fields when CREATE TABLE is called, this is for readability mostly more than efficiency (do not want to have to go through all of the database migrations to ascertain what columns a table has). upon startup of a new database, the migrations will be analyzed and the highest version set, so that future migrations will be run. this should also avoid running through all the migrations, which could bork db's easily enough (if the user just exits from impatience, say). otherwise, all migrations that a db has not yet seen will be run against it upon startup, this should be seamless to the user whether they had a db that had 0 migrations run on it before or N. this means users will not have to explicitly run any migrations on their dbs nor see any errors when we upgrade the db (so long as things go well). if migrations do not go so well, users will have to manually repair dbs (this is the intention of the `migrate` library and it seems sane), this should be rare, and I'm unsure myself how best to resolve not having gone through this myself, I would assume it will require running down migrations and then manually updating the migration field; in any case, docs once one of us has to go through this. migrations are written to files and checked into version control, and then use go-bindata to generate those files into go code and compiled in to be consumed by the migrate library (so that we don't have to put migration files on any servers) -- this is also in vcs. this seems to work ok. I don't like having to use the separate go-bindata tool but it wasn't really hard to install and then go generate takes care of the args. adding migrations should be relatively rare anyway, but tried to make it pretty painless. 1 migration to add created_at to the route is done here as an example of how to do migrations, as well as testing these things ;) -- `created_at` will be `0001-01-01T00:00:00.000Z` for any existing routes after a user runs this version. could spend the extra time adding 'today's date to any outstanding records, but that's not really accurate, the main thing is nobody will have to nuke their db with the migrations in place & we don't have any prod clusters really to worry about. all future routes will correctly have `created_at` set, and plan to add other timestamps but wanted to keep this patch as small as possible so only did routes.created_at. there are tests that a spankin new db will work as expected as well as a db after running all down & up migrations works. the latter tests only run on mysql and postgres, since sqlite3 does not like ALTER TABLE DROP COLUMN; up migrations will need to be tested manually for sqlite3 only, but in theory if they are simple and work on postgres and mysql, there is a good likelihood of success; the new migration from this patch works on sqlite3 fine. for now, we need to use `github.com/rdallman/migrate` to move forward, as getting integrated into upstream is proving difficult due to `github.com/go-sql-driver/mysql` being broken on master (yay dependencies). Fortunately for us, we vendor a version of the `mysql` bindings that actually works, thus, we are capable of using the `mattes/migrate` library with success due to that. this also will require go1.9 to use the new `database/sql.Conn` type, CI has been updated accordingly. some doc fixes too from testing.. and of course updated all deps. anyway, whew. this should let us add fields to the db without busting everybody's dbs. open to feedback on better ways, but this was overall pretty simple despite futzing with mysql. * add migrate pkg to deps, update deps use rdallman/migrate until we resolve in mattes land * add README in migrations package * add ref to mattes lib
This commit is contained in:
@@ -25,8 +25,13 @@ func setLogBuffer() *bytes.Buffer {
|
||||
return &buf
|
||||
}
|
||||
|
||||
func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
func Test(t *testing.T, dsf func(t *testing.T) models.Datastore) {
|
||||
buf := setLogBuffer()
|
||||
defer func() {
|
||||
if t.Failed() {
|
||||
t.Log(buf.String())
|
||||
}
|
||||
}()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -39,17 +44,16 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
call.Path = testRoute.Path
|
||||
|
||||
t.Run("call-insert", func(t *testing.T) {
|
||||
ds := dsf()
|
||||
ds := dsf(t)
|
||||
call.ID = id.New().String()
|
||||
err := ds.InsertCall(ctx, call)
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("call-get", func(t *testing.T) {
|
||||
ds := dsf()
|
||||
ds := dsf(t)
|
||||
call.ID = id.New().String()
|
||||
ds.InsertCall(ctx, call)
|
||||
newCall, err := ds.GetCall(ctx, call.AppName, call.ID)
|
||||
@@ -57,13 +61,12 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
t.Fatalf("Test GetCall(ctx, call.ID): unexpected error `%v`", err)
|
||||
}
|
||||
if call.ID != newCall.ID {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCall(ctx, call.ID): unexpected error `%v`", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("calls-get", func(t *testing.T) {
|
||||
ds := dsf()
|
||||
ds := dsf(t)
|
||||
filter := &models.CallFilter{AppName: call.AppName, Path: call.Path, PerPage: 100}
|
||||
call.ID = id.New().String()
|
||||
call.CreatedAt = strfmt.DateTime(time.Now())
|
||||
@@ -76,7 +79,6 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected error `%v`", err)
|
||||
}
|
||||
if len(calls) != 1 {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected length `%v`", len(calls))
|
||||
}
|
||||
|
||||
@@ -102,7 +104,6 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected error `%v`", err)
|
||||
}
|
||||
if len(calls) != 3 {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected length `%v`", len(calls))
|
||||
}
|
||||
|
||||
@@ -113,10 +114,8 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected error `%v`", err)
|
||||
}
|
||||
if len(calls) != 1 {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected length `%v`", len(calls))
|
||||
} else if calls[0].ID != c3.ID {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls: call ids not in expected order: %v %v", calls[0].ID, c3.ID)
|
||||
}
|
||||
|
||||
@@ -127,13 +126,10 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected error `%v`", err)
|
||||
}
|
||||
if len(calls) != 2 {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected length `%v`", len(calls))
|
||||
} else if calls[0].ID != c2.ID {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls: call ids not in expected order: %v %v", calls[0].ID, c2.ID)
|
||||
} else if calls[1].ID != call.ID {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls: call ids not in expected order: %v %v", calls[1].ID, call.ID)
|
||||
}
|
||||
|
||||
@@ -143,7 +139,6 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected error `%v`", err)
|
||||
}
|
||||
if len(calls) != 0 {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected length `%v`", len(calls))
|
||||
}
|
||||
|
||||
@@ -152,7 +147,6 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected error `%v`", err)
|
||||
}
|
||||
if len(calls) != 0 {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected length `%v`", len(calls))
|
||||
}
|
||||
|
||||
@@ -167,42 +161,35 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected error `%v`", err)
|
||||
}
|
||||
if len(calls) != 1 {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls(ctx, filter): unexpected length `%v`", len(calls))
|
||||
} else if calls[0].ID != c2.ID {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetCalls: call id not expected %s vs %s", calls[0].ID, c2.ID)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("apps", func(t *testing.T) {
|
||||
ds := dsf()
|
||||
ds := dsf(t)
|
||||
// Testing insert app
|
||||
_, err := ds.InsertApp(ctx, nil)
|
||||
if err != models.ErrDatastoreEmptyApp {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertApp(nil): expected error `%v`, but it was `%v`", models.ErrDatastoreEmptyApp, err)
|
||||
}
|
||||
|
||||
_, err = ds.InsertApp(ctx, &models.App{})
|
||||
if err != models.ErrDatastoreEmptyAppName {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertApp(&{}): expected error `%v`, but it was `%v`", models.ErrDatastoreEmptyAppName, err)
|
||||
}
|
||||
|
||||
inserted, err := ds.InsertApp(ctx, testApp)
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertApp: error when storing new app: %s", err)
|
||||
}
|
||||
if !reflect.DeepEqual(*inserted, *testApp) {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertApp: expected to insert:\n%v\nbut got:\n%v", testApp, inserted)
|
||||
}
|
||||
|
||||
_, err = ds.InsertApp(ctx, testApp)
|
||||
if err != models.ErrAppsAlreadyExists {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertApp duplicated: expected error `%v`, but it was `%v`", models.ErrAppsAlreadyExists, err)
|
||||
}
|
||||
|
||||
@@ -211,12 +198,10 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
updated, err := ds.UpdateApp(ctx,
|
||||
&models.App{Name: testApp.Name, Config: map[string]string{"TEST": "1"}})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateApp: error when updating app: %v", err)
|
||||
}
|
||||
expected := &models.App{Name: testApp.Name, Config: map[string]string{"TEST": "1"}}
|
||||
if !reflect.DeepEqual(*updated, *expected) {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateApp: expected updated `%v` but got `%v`", expected, updated)
|
||||
}
|
||||
|
||||
@@ -224,12 +209,10 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
updated, err = ds.UpdateApp(ctx,
|
||||
&models.App{Name: testApp.Name, Config: map[string]string{"OTHER": "TEST"}})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateApp: error when updating app: %v", err)
|
||||
}
|
||||
expected = &models.App{Name: testApp.Name, Config: map[string]string{"TEST": "1", "OTHER": "TEST"}}
|
||||
if !reflect.DeepEqual(*updated, *expected) {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateApp: expected updated `%v` but got `%v`", expected, updated)
|
||||
}
|
||||
|
||||
@@ -237,12 +220,10 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
updated, err = ds.UpdateApp(ctx,
|
||||
&models.App{Name: testApp.Name, Config: map[string]string{"TEST": ""}})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateApp: error when updating app: %v", err)
|
||||
}
|
||||
expected = &models.App{Name: testApp.Name, Config: map[string]string{"OTHER": "TEST"}}
|
||||
if !reflect.DeepEqual(*updated, *expected) {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateApp: expected updated `%v` but got `%v`", expected, updated)
|
||||
}
|
||||
}
|
||||
@@ -250,31 +231,26 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
// Testing get app
|
||||
_, err = ds.GetApp(ctx, "")
|
||||
if err != models.ErrDatastoreEmptyAppName {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApp: expected error to be %v, but it was %s", models.ErrDatastoreEmptyAppName, err)
|
||||
}
|
||||
|
||||
app, err := ds.GetApp(ctx, testApp.Name)
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApp: error: %s", err)
|
||||
}
|
||||
if app.Name != testApp.Name {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApp: expected `app.Name` to be `%s` but it was `%s`", app.Name, testApp.Name)
|
||||
}
|
||||
|
||||
// Testing list apps
|
||||
apps, err := ds.GetApps(ctx, &models.AppFilter{PerPage: 100})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApps: unexpected error %v", err)
|
||||
}
|
||||
if len(apps) == 0 {
|
||||
t.Fatal("Test GetApps: expected result count to be greater than 0")
|
||||
}
|
||||
if apps[0].Name != testApp.Name {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApps: expected `app.Name` to be `%s` but it was `%s`", app.Name, testApp.Name)
|
||||
}
|
||||
|
||||
@@ -292,28 +268,23 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
|
||||
apps, err = ds.GetApps(ctx, &models.AppFilter{PerPage: 1})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApps: error: %s", err)
|
||||
}
|
||||
if len(apps) != 1 {
|
||||
t.Fatalf("Test GetApps: expected result count to be 1 but got %d", len(apps))
|
||||
} else if apps[0].Name != testApp.Name {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApps: expected `app.Name` to be `%s` but it was `%s`", testApp.Name, apps[0].Name)
|
||||
}
|
||||
|
||||
apps, err = ds.GetApps(ctx, &models.AppFilter{PerPage: 100, Cursor: apps[0].Name})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApps: error: %s", err)
|
||||
}
|
||||
if len(apps) != 2 {
|
||||
t.Fatalf("Test GetApps: expected result count to be 2 but got %d", len(apps))
|
||||
} else if apps[0].Name != a2.Name {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApps: expected `app.Name` to be `%s` but it was `%s`", a2.Name, apps[0].Name)
|
||||
} else if apps[1].Name != a3.Name {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApps: expected `app.Name` to be `%s` but it was `%s`", a3.Name, apps[1].Name)
|
||||
}
|
||||
|
||||
@@ -326,20 +297,17 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
|
||||
apps, err = ds.GetApps(ctx, &models.AppFilter{PerPage: 100})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApps: error: %s", err)
|
||||
}
|
||||
if len(apps) != 4 {
|
||||
t.Fatalf("Test GetApps: expected result count to be 4 but got %d", len(apps))
|
||||
} else if apps[0].Name != a4.Name {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApps: expected `app.Name` to be `%s` but it was `%s`", a4.Name, apps[0].Name)
|
||||
}
|
||||
|
||||
// TODO fix up prefix stuff
|
||||
//apps, err = ds.GetApps(ctx, &models.AppFilter{Name: "Tes"})
|
||||
//if err != nil {
|
||||
//t.Log(buf.String())
|
||||
//t.Fatalf("Test GetApps(filter): unexpected error %v", err)
|
||||
//}
|
||||
//if len(apps) != 3 {
|
||||
@@ -349,22 +317,18 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
// Testing app delete
|
||||
err = ds.RemoveApp(ctx, "")
|
||||
if err != models.ErrDatastoreEmptyAppName {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test RemoveApp: expected error `%v`, but it was `%v`", models.ErrDatastoreEmptyAppName, err)
|
||||
}
|
||||
|
||||
err = ds.RemoveApp(ctx, testApp.Name)
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test RemoveApp: error: %s", err)
|
||||
}
|
||||
app, err = ds.GetApp(ctx, testApp.Name)
|
||||
if err != models.ErrAppsNotFound {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetApp(removed): expected error `%v`, but it was `%v`", models.ErrAppsNotFound, err)
|
||||
}
|
||||
if app != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatal("Test RemoveApp: failed to remove the app")
|
||||
}
|
||||
|
||||
@@ -376,17 +340,15 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
},
|
||||
})
|
||||
if err != models.ErrAppsNotFound {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateApp(inexistent): expected error `%v`, but it was `%v`", models.ErrAppsNotFound, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("routes", func(t *testing.T) {
|
||||
ds := dsf()
|
||||
ds := dsf(t)
|
||||
// Insert app again to test routes
|
||||
_, err := ds.InsertApp(ctx, testApp)
|
||||
if err != nil && err != models.ErrAppsAlreadyExists {
|
||||
t.Log(buf.String())
|
||||
t.Fatal("Test InsertRoute Prep: failed to insert app: ", err)
|
||||
}
|
||||
|
||||
@@ -394,25 +356,21 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
{
|
||||
_, err = ds.InsertRoute(ctx, nil)
|
||||
if err != models.ErrDatastoreEmptyRoute {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertRoute(nil): expected error `%v`, but it was `%v`", models.ErrDatastoreEmptyRoute, err)
|
||||
}
|
||||
|
||||
_, err = ds.InsertRoute(ctx, &models.Route{AppName: "notreal", Path: "/test"})
|
||||
if err != models.ErrAppsNotFound {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertRoute: expected error `%v`, but it was `%v`", models.ErrAppsNotFound, err)
|
||||
}
|
||||
|
||||
_, err = ds.InsertRoute(ctx, testRoute)
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertRoute: error when storing new route: %s", err)
|
||||
}
|
||||
|
||||
_, err = ds.InsertRoute(ctx, testRoute)
|
||||
if err != models.ErrRoutesAlreadyExists {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertRoute duplicated: expected error to be `%v`, but it was `%v`", models.ErrRoutesAlreadyExists, err)
|
||||
}
|
||||
}
|
||||
@@ -421,24 +379,20 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
{
|
||||
_, err = ds.GetRoute(ctx, "a", "")
|
||||
if err != models.ErrDatastoreEmptyRoutePath {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoute(empty route path): expected error `%v`, but it was `%v`", models.ErrDatastoreEmptyRoutePath, err)
|
||||
}
|
||||
|
||||
_, err = ds.GetRoute(ctx, "", "a")
|
||||
if err != models.ErrDatastoreEmptyAppName {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoute(empty app name): expected error `%v`, but it was `%v`", models.ErrDatastoreEmptyAppName, err)
|
||||
}
|
||||
|
||||
route, err := ds.GetRoute(ctx, testApp.Name, testRoute.Path)
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoute: unexpected error %v", err)
|
||||
}
|
||||
var expected models.Route = *testRoute
|
||||
if !reflect.DeepEqual(*route, expected) {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test InsertApp: expected to insert:\n%v\nbut got:\n%v", expected, *route)
|
||||
}
|
||||
}
|
||||
@@ -462,7 +416,6 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateRoute: unexpected error: %v", err)
|
||||
}
|
||||
expected := &models.Route{
|
||||
@@ -488,7 +441,6 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
},
|
||||
}
|
||||
if !reflect.DeepEqual(*updated, *expected) {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateRoute: expected updated `%v` but got `%v`", expected, updated)
|
||||
}
|
||||
|
||||
@@ -507,7 +459,6 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateRoute: unexpected error: %v", err)
|
||||
}
|
||||
expected = &models.Route{
|
||||
@@ -531,7 +482,6 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
},
|
||||
}
|
||||
if !reflect.DeepEqual(*updated, *expected) {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateRoute: expected updated:\n`%v`\nbut got:\n`%v`", expected, updated)
|
||||
}
|
||||
}
|
||||
@@ -539,39 +489,32 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
// Testing list routes
|
||||
routes, err := ds.GetRoutesByApp(ctx, testApp.Name, &models.RouteFilter{PerPage: 1})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: unexpected error %v", err)
|
||||
}
|
||||
if len(routes) == 0 {
|
||||
t.Fatal("Test GetRoutesByApp: expected result count to be greater than 0")
|
||||
}
|
||||
if routes[0] == nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutes: expected non-nil route")
|
||||
} else if routes[0].Path != testRoute.Path {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutes: expected `app.Name` to be `%s` but it was `%s`", testRoute.Path, routes[0].Path)
|
||||
}
|
||||
|
||||
routes, err = ds.GetRoutesByApp(ctx, testApp.Name, &models.RouteFilter{Image: testRoute.Image, PerPage: 1})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: unexpected error %v", err)
|
||||
}
|
||||
if len(routes) == 0 {
|
||||
t.Fatal("Test GetRoutesByApp: expected result count to be greater than 0")
|
||||
}
|
||||
if routes[0] == nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: expected non-nil route")
|
||||
} else if routes[0].Path != testRoute.Path {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: expected `route.Path` to be `%s` but it was `%s`", testRoute.Path, routes[0].Path)
|
||||
}
|
||||
|
||||
routes, err = ds.GetRoutesByApp(ctx, "notreal", &models.RouteFilter{PerPage: 1})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: error: %s", err)
|
||||
}
|
||||
if len(routes) != 0 {
|
||||
@@ -593,28 +536,23 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
|
||||
routes, err = ds.GetRoutesByApp(ctx, testApp.Name, &models.RouteFilter{PerPage: 1})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: error: %s", err)
|
||||
}
|
||||
if len(routes) != 1 {
|
||||
t.Fatalf("Test GetRoutesByApp: expected result count to be 1 but got %d", len(routes))
|
||||
} else if routes[0].Path != testRoute.Path {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: expected `route.Path` to be `%s` but it was `%s`", testRoute.Path, routes[0].Path)
|
||||
}
|
||||
|
||||
routes, err = ds.GetRoutesByApp(ctx, testApp.Name, &models.RouteFilter{PerPage: 2, Cursor: routes[0].Path})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: error: %s", err)
|
||||
}
|
||||
if len(routes) != 2 {
|
||||
t.Fatalf("Test GetRoutesByApp: expected result count to be 2 but got %d", len(routes))
|
||||
} else if routes[0].Path != r2.Path {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: expected `route.Path` to be `%s` but it was `%s`", r2.Path, routes[0].Path)
|
||||
} else if routes[1].Path != r3.Path {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: expected `route.Path` to be `%s` but it was `%s`", r3.Path, routes[1].Path)
|
||||
}
|
||||
|
||||
@@ -627,13 +565,11 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
|
||||
routes, err = ds.GetRoutesByApp(ctx, testApp.Name, &models.RouteFilter{PerPage: 100})
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: error: %s", err)
|
||||
}
|
||||
if len(routes) != 4 {
|
||||
t.Fatalf("Test GetRoutesByApp: expected result count to be 4 but got %d", len(routes))
|
||||
} else if routes[0].Path != r4.Path {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoutesByApp: expected `route.Path` to be `%s` but it was `%s`", r4.Path, routes[0].Path)
|
||||
}
|
||||
|
||||
@@ -643,29 +579,24 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
// Testing route delete
|
||||
err = ds.RemoveRoute(ctx, "", "")
|
||||
if err != models.ErrDatastoreEmptyAppName {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test RemoveRoute(empty app name): expected error `%v`, but it was `%v`", models.ErrDatastoreEmptyAppName, err)
|
||||
}
|
||||
|
||||
err = ds.RemoveRoute(ctx, "a", "")
|
||||
if err != models.ErrDatastoreEmptyRoutePath {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test RemoveRoute(empty route path): expected error `%v`, but it was `%v`", models.ErrDatastoreEmptyRoutePath, err)
|
||||
}
|
||||
|
||||
err = ds.RemoveRoute(ctx, testRoute.AppName, testRoute.Path)
|
||||
if err != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test RemoveApp: unexpected error: %v", err)
|
||||
}
|
||||
|
||||
route, err := ds.GetRoute(ctx, testRoute.AppName, testRoute.Path)
|
||||
if err != nil && err != models.ErrRoutesNotFound {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test GetRoute: expected error `%v`, but it was `%v`", models.ErrRoutesNotFound, err)
|
||||
}
|
||||
if route != nil {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test RemoveApp: failed to remove the route: %v", route)
|
||||
}
|
||||
|
||||
@@ -675,7 +606,6 @@ func Test(t *testing.T, dsf func() models.Datastore) {
|
||||
Image: "test",
|
||||
})
|
||||
if err != models.ErrRoutesNotFound {
|
||||
t.Log(buf.String())
|
||||
t.Fatalf("Test UpdateRoute inexistent: expected error to be `%v`, but it was `%v`", models.ErrRoutesNotFound, err)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user