OLD | NEW |
1 package testutil | 1 package testutil |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "os" | 5 "os" |
6 "strings" | 6 "strings" |
7 "testing" | 7 "testing" |
8 ) | 8 ) |
9 | 9 |
10 import ( | 10 import ( |
11 // Using 'require' which is like using 'assert' but causes tests to fail
. | 11 // Using 'require' which is like using 'assert' but causes tests to fail
. |
12 assert "github.com/stretchr/testify/require" | 12 assert "github.com/stretchr/testify/require" |
13 | 13 |
14 "skia.googlesource.com/buildbot.git/go/database" | 14 "skia.googlesource.com/buildbot.git/go/database" |
15 ) | 15 ) |
16 | 16 |
17 // Connection string to the local MySQL database for testing. | 17 // Connection string to the local MySQL database for testing. |
18 const ( | 18 const ( |
19 // String to open a local database for testing. The string formatting | 19 // String to open a local database for testing. The string formatting |
20 // parameters are: username, password, database. | 20 // parameters are: username, password, database. |
21 MYSQL_DB_OPEN = "%s:%s@tcp(localhost:3306)/%s?parseTime=true" | 21 MYSQL_DB_OPEN = "%s:%s@tcp(localhost:3306)/%s?parseTime=true" |
22 | 22 |
23 // File path to the local SQLite testing databse. | |
24 SQLITE_DB_PATH = "./testing.db" | |
25 | |
26 // Name of the MySQL lock | 23 // Name of the MySQL lock |
27 SQL_LOCK = "mysql_testlock" | 24 SQL_LOCK = "mysql_testlock" |
28 ) | 25 ) |
29 | 26 |
30 // Creates an SQLite test database and runs migration tests against it using the | |
31 // given migration steps. | |
32 func SQLiteVersioningTests(t *testing.T, migrationSteps []database.MigrationStep
) { | |
33 // Initialize without argument to test against SQLite3 | |
34 conf := &database.DatabaseConfig{ | |
35 SQLiteFilePath: SQLITE_DB_PATH, | |
36 MigrationSteps: migrationSteps, | |
37 } | |
38 | |
39 vdb := database.NewVersionedDB(conf) | |
40 assert.False(t, vdb.IsMySQL) | |
41 testDBVersioning(t, vdb) | |
42 } | |
43 | |
44 // Creates an MySQL test database and runs migration tests against it using the | 27 // Creates an MySQL test database and runs migration tests against it using the |
45 // given migration steps. See Get for required credentials. | 28 // given migration steps. See Get for required credentials. |
46 // The test assumes that the database is empty and that the readwrite user is | 29 // The test assumes that the database is empty and that the readwrite user is |
47 // not allowed to create/drop/alter tables. | 30 // not allowed to create/drop/alter tables. |
48 func MySQLVersioningTests(t *testing.T, dbName string, migrationSteps []database
.MigrationStep) { | 31 func MySQLVersioningTests(t *testing.T, dbName string, migrationSteps []database
.MigrationStep) { |
49 // OpenDB as root user and remove all tables. | 32 // OpenDB as root user and remove all tables. |
50 rootConf := &database.DatabaseConfig{ | 33 rootConf := &database.DatabaseConfig{ |
51 MySQLString: GetTestMySQLConnStr(t, "root", dbName), | 34 MySQLString: GetTestMySQLConnStr(t, "root", dbName), |
52 MigrationSteps: migrationSteps, | 35 MigrationSteps: migrationSteps, |
53 } | 36 } |
54 lockVdb := GetMySQlLock(t, rootConf) | 37 lockVdb := GetMySQlLock(t, rootConf) |
55 defer func() { | 38 defer func() { |
56 ReleaseMySQLLock(t, lockVdb) | 39 ReleaseMySQLLock(t, lockVdb) |
57 lockVdb.Close() | 40 lockVdb.Close() |
58 }() | 41 }() |
59 | 42 |
60 rootVdb := database.NewVersionedDB(rootConf) | 43 rootVdb := database.NewVersionedDB(rootConf) |
61 assert.True(t, rootVdb.IsMySQL) | |
62 ClearMySQLTables(t, rootVdb) | 44 ClearMySQLTables(t, rootVdb) |
63 assert.Nil(t, rootVdb.Close()) | 45 assert.Nil(t, rootVdb.Close()) |
64 | 46 |
65 // Configuration for the readwrite user without DDL privileges. | 47 // Configuration for the readwrite user without DDL privileges. |
66 readWriteConf := &database.DatabaseConfig{ | 48 readWriteConf := &database.DatabaseConfig{ |
67 MySQLString: GetTestMySQLConnStr(t, "readwrite", dbName), | 49 MySQLString: GetTestMySQLConnStr(t, "readwrite", dbName), |
68 MigrationSteps: migrationSteps, | 50 MigrationSteps: migrationSteps, |
69 } | 51 } |
70 | 52 |
71 // Open DB as readwrite user and make sure it fails because of a missing | 53 // Open DB as readwrite user and make sure it fails because of a missing |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 assert.Nil(t, err) | 137 assert.Nil(t, err) |
156 assert.Equal(t, 0, dbVersion) | 138 assert.Equal(t, 0, dbVersion) |
157 | 139 |
158 // upgrade the the latest version | 140 // upgrade the the latest version |
159 err = vdb.Migrate(maxVersion) | 141 err = vdb.Migrate(maxVersion) |
160 assert.Nil(t, err) | 142 assert.Nil(t, err) |
161 dbVersion, err = vdb.DBVersion() | 143 dbVersion, err = vdb.DBVersion() |
162 assert.Nil(t, err) | 144 assert.Nil(t, err) |
163 assert.Equal(t, maxVersion, dbVersion) | 145 assert.Equal(t, maxVersion, dbVersion) |
164 } | 146 } |
OLD | NEW |