storj/storagenode/preflight/db_test.go
Moby von Briesen 178dbb4683 storagenode/storagenodedb: allow storagenodes to start test_table exists
In many cases when a storagenode fails the preflight check, it is due to
test_table existing, which is used to determine read/write capabilities
after the initial schema verification. If preflight ends early due to a
failure or stopped storagenode, it may not get the chance to drop this
table.

This change excludes test_table from the schema comparison to ensure
that it never prevents a storagenode from starting up.

It also adds Preflight DB test for storagenode.

Change-Id: Ib8e71df2e42fda3b2a364fbf7a801891c5831d39
2020-03-09 14:29:46 -04:00

74 lines
2.4 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package preflight_test
import (
"testing"
"github.com/stretchr/testify/require"
"storj.io/common/testcontext"
"storj.io/storj/storagenode"
"storj.io/storj/storagenode/storagenodedb"
"storj.io/storj/storagenode/storagenodedb/storagenodedbtest"
)
func TestPreflightSchema(t *testing.T) {
// no change should not cause a preflight error
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
err := db.Preflight(ctx)
require.NoError(t, err)
})
// adding something to the schema should cause a preflight error
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
err := db.Preflight(ctx)
require.NoError(t, err)
// add index to used serials db
rawDBs := db.(*storagenodedb.DB).RawDatabases()
usedSerialsDB := rawDBs[storagenodedb.UsedSerialsDBName]
_, err = usedSerialsDB.GetDB().Exec(ctx, "CREATE INDEX a_new_index ON used_serial_(serial_number)")
require.NoError(t, err)
// expect error from preflight check for addition
err = db.Preflight(ctx)
require.Error(t, err)
require.True(t, storagenodedb.ErrPreflight.Has(err))
})
// removing something from the schema should cause a preflight error
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
err := db.Preflight(ctx)
require.NoError(t, err)
// remove index from used serials db
rawDBs := db.(*storagenodedb.DB).RawDatabases()
usedSerialsDB := rawDBs[storagenodedb.UsedSerialsDBName]
_, err = usedSerialsDB.GetDB().Exec(ctx, "DROP INDEX idx_used_serial_;")
require.NoError(t, err)
// expect error from preflight check for removal
err = db.Preflight(ctx)
require.Error(t, err)
require.True(t, storagenodedb.ErrPreflight.Has(err))
})
// having a test table should not cause the preflight check to fail
storagenodedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db storagenode.DB) {
err := db.Preflight(ctx)
require.NoError(t, err)
// add test_table to used serials db
rawDBs := db.(*storagenodedb.DB).RawDatabases()
usedSerialsDB := rawDBs[storagenodedb.UsedSerialsDBName]
_, err = usedSerialsDB.GetDB().Exec(ctx, "CREATE TABLE test_table(id int NOT NULL, name varchar(30), PRIMARY KEY (id));")
require.NoError(t, err)
// expect no error from preflight check with added test_table
err = db.Preflight(ctx)
require.NoError(t, err)
})
}