Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Unified Diff: go/database/testutil/testutil.go

Issue 813443002: Overhaul database package (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Fix newline in password Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: go/database/testutil/testutil.go
diff --git a/go/database/testutil/testutil.go b/go/database/testutil/testutil.go
index cebea84683c5363dc4297580d7141aad933f23af..6cf8d036fddb7eef8ba3ea9ac1f0368d464076ab 100644
--- a/go/database/testutil/testutil.go
+++ b/go/database/testutil/testutil.go
@@ -1,8 +1,6 @@
package testutil
import (
- "fmt"
- "os"
"strings"
"testing"
)
@@ -20,25 +18,33 @@ const (
// parameters are: username, password, database.
MYSQL_DB_OPEN = "%s:%s@tcp(localhost:3306)/%s?parseTime=true"
- // File path to the local SQLite testing databse.
- SQLITE_DB_PATH = "./testing.db"
-
// Name of the MySQL lock
SQL_LOCK = "mysql_testlock"
+
+ // Name of the shared test database.
+ TEST_DB_HOST = "localhost"
+ TEST_DB_PORT = 3306
+ TEST_DB_NAME = "sk_testing"
+
+ // Names of test users. These users should have no password and be
+ // limited to accessing the sk_testing database.
+ USER_ROOT = "test_root"
+ USER_RW = "test_rw"
+
+ // Empty password for testing.
+ TEST_PASSWORD = ""
)
-// Creates an SQLite test database and runs migration tests against it using the
-// given migration steps.
-func SQLiteVersioningTests(t *testing.T, migrationSteps []database.MigrationStep) {
- // Initialize without argument to test against SQLite3
- conf := &database.DatabaseConfig{
- SQLiteFilePath: SQLITE_DB_PATH,
- MigrationSteps: migrationSteps,
- }
+// LocalTestDatabaseConfig returns a DatabaseConfig appropriate for local
+// testing.
+func LocalTestDatabaseConfig(m []database.MigrationStep) *database.DatabaseConfig {
+ return database.NewDatabaseConfig(USER_RW, "", TEST_DB_HOST, TEST_DB_PORT, TEST_DB_NAME, m)
+}
- vdb := database.NewVersionedDB(conf)
- assert.False(t, vdb.IsMySQL)
- testDBVersioning(t, vdb)
+// LocalTestDatabaseConfig returns a DatabaseConfig appropriate for local
jcgregorio 2014/12/19 20:27:06 LocalTestRootDatabaseConfig
borenet 2014/12/19 20:39:15 Done.
+// testing, with root access.
+func LocalTestRootDatabaseConfig(m []database.MigrationStep) *database.DatabaseConfig {
+ return database.NewDatabaseConfig(USER_ROOT, "", TEST_DB_HOST, TEST_DB_PORT, TEST_DB_NAME, m)
}
// Creates an MySQL test database and runs migration tests against it using the
@@ -47,10 +53,7 @@ func SQLiteVersioningTests(t *testing.T, migrationSteps []database.MigrationStep
// not allowed to create/drop/alter tables.
func MySQLVersioningTests(t *testing.T, dbName string, migrationSteps []database.MigrationStep) {
// OpenDB as root user and remove all tables.
- rootConf := &database.DatabaseConfig{
- MySQLString: GetTestMySQLConnStr(t, "root", dbName),
- MigrationSteps: migrationSteps,
- }
+ rootConf := LocalTestRootDatabaseConfig(migrationSteps)
lockVdb := GetMySQlLock(t, rootConf)
defer func() {
ReleaseMySQLLock(t, lockVdb)
@@ -58,15 +61,11 @@ func MySQLVersioningTests(t *testing.T, dbName string, migrationSteps []database
}()
rootVdb := database.NewVersionedDB(rootConf)
- assert.True(t, rootVdb.IsMySQL)
ClearMySQLTables(t, rootVdb)
assert.Nil(t, rootVdb.Close())
// Configuration for the readwrite user without DDL privileges.
- readWriteConf := &database.DatabaseConfig{
- MySQLString: GetTestMySQLConnStr(t, "readwrite", dbName),
- MigrationSteps: migrationSteps,
- }
+ readWriteConf := LocalTestDatabaseConfig(migrationSteps)
// Open DB as readwrite user and make sure it fails because of a missing
// version table.
@@ -84,28 +83,6 @@ func MySQLVersioningTests(t *testing.T, dbName string, migrationSteps []database
})
}
-// Returns a connection string to the local MySQL server and the given database.
-// The test will be skipped if these environement variables are not set:
-// MYSQL_TESTING_RWPW (password of readwrite user)
-// MYSQL_TESTING_ROOTPW (password of the db root user)
-func GetTestMySQLConnStr(t *testing.T, user string, dbName string) string {
- rwUserPw, rootPw := os.Getenv("MYSQL_TESTING_RWPW"), os.Getenv("MYSQL_TESTING_ROOTPW")
- if testing.Short() {
- t.Skip("Skipping test against MySQL in short mode.")
- }
-
- // Skip this test unless there are environment variables with the rwuser and
- // root password for the local MySQL instance.
- if (rwUserPw == "") || (rootPw == "") {
- t.Skip("Skipping test against MySQL. Set 'MYSQL_TESTING_ROOTPW' and 'MYSQL_TESTING_RWPW' to enable tests.")
- }
- pw := rwUserPw
- if user == "root" {
- pw = rootPw
- }
- return fmt.Sprintf(MYSQL_DB_OPEN, user, pw, dbName)
-}
-
// Get a lock from MySQL to serialize DB tests.
func GetMySQlLock(t *testing.T, conf *database.DatabaseConfig) *database.VersionedDB {
vdb := database.NewVersionedDB(conf)
@@ -141,6 +118,47 @@ func ClearMySQLTables(t *testing.T, vdb *database.VersionedDB) {
}
}
+// MySQLTestDatabase is a convenience struct for using a test database which
+// starts in a clean state.
+type MySQLTestDatabase struct {
+ rootVdb *database.VersionedDB
+ t *testing.T
+}
+
+// SetupMySQLTestDatabase returns a MySQLTestDatabase in a clean state. It must
+// be closed after use.
+//
+// Example usage:
+//
+// db := SetupMySQLTestDatabase(t, migrationSteps)
+// defer db.Close()
+// ... Tests here ...
+func SetupMySQLTestDatabase(t *testing.T, migrationSteps []database.MigrationStep) *MySQLTestDatabase {
+ conf := LocalTestRootDatabaseConfig(migrationSteps)
+ lockVdb := GetMySQlLock(t, conf)
+ rootVdb := database.NewVersionedDB(conf)
+ ClearMySQLTables(t, rootVdb)
+ if err := rootVdb.Close(); err != nil {
+ t.Fatal(err)
+ }
+ rootVdb = database.NewVersionedDB(conf)
+ if err := rootVdb.Migrate(rootVdb.MaxDBVersion()); err != nil {
+ t.Fatal(err)
+ }
+ if err := rootVdb.Close(); err != nil {
+ t.Fatal(err)
+ }
+ return &MySQLTestDatabase{lockVdb, t}
+}
+
+func (d *MySQLTestDatabase) Close() {
+ if err := d.rootVdb.Migrate(0); err != nil {
+ d.t.Fatal(err)
+ }
+ ReleaseMySQLLock(d.t, d.rootVdb)
+ d.rootVdb.Close()
+}
+
// Test wether the migration steps execute correctly.
func testDBVersioning(t *testing.T, vdb *database.VersionedDB) {
// get the DB version

Powered by Google App Engine
This is Rietveld 408576698