mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* MM-31094: Adds tooling to develop and test using a MySQL instance with replication lag. Adds some lazy lookups to fallback to master if results are not found.
* MM-31094: Removes mysql-read-replica from default docker services.
* MM-31094: Switches (store..SessionStore).Get and (store.TeamStore).GetMember to using context.Context.
* MM-31094: Updates (store.UsersStore).Get to use context.
* MM-31094: Updates (store.PostStore).Get to use context.
* MM-31094: Removes feature flag and config setting.
* MM-31094: Rolls back some master reads.
* MM-31094: Rolls a non-cache read.
* MM-31094: Removes feature flag from the store.
* MM-31094: Removes unused constant and struct field.
* MM-31094: Removes some old feature flag references.
* MM-31094: Fixes some tests.
* MM-31094: App layers fix.
* MM-31094: Fixes mocks.
* MM-31094: Don't reparse flag.
* MM-31094: No reparse.
* MM-31094: Removed unused FeatureFlags field.
* MM-31094: Removes unnecessary feature flags variable declarations.
* MM-31094: Fixes copy-paste error.
* MM-31094: Fixes logical error.
* MM-30194: Removes test method from store.
* Revert "MM-30194: Removes test method from store."
This reverts commit d5a6e8529b.
* MM-31094: Conforming to make's strange syntax.
* MM-31094: Configures helper for read replica with option.
* MM-31094: Adds some missing ctx's.
* MM-31094: WIP
* MM-31094: Updates test names.
* MM-31094: WIP
* MM-31094: Removes unnecessary master reads.
* MM-31094: ID case changes out of scope.
* MM-31094: Removes unused context.
* MM-31094: Switches to a helper. Removes some var naming changes. Fixes a merge error.
* MM-31094: Removes SQLITE db driver ref.
* MM-31094: Layer generate fix.
* MM-31094: Removes unnecessary changes.
* MM-31094: Moves test method.
* MM-31094: Re-add previous fix.
* MM-31094: Removes make command for dev.
* MM-31094: Fix for login.
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
255 lines
7.2 KiB
Go
255 lines
7.2 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package storetest
|
|
|
|
import (
|
|
"database/sql"
|
|
"flag"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/go-sql-driver/mysql"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
_ "github.com/lib/pq"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
|
)
|
|
|
|
const (
|
|
defaultMysqlDSN = "mmuser:mostest@tcp(localhost:3306)/mattermost_test?charset=utf8mb4,utf8&readTimeout=30s&writeTimeout=30s&multiStatements=true"
|
|
defaultPostgresqlDSN = "postgres://mmuser:mostest@localhost:5432/mattermost_test?sslmode=disable&connect_timeout=10"
|
|
defaultMysqlRootPWD = "mostest"
|
|
defaultMysqlReplicaDSN = "root:mostest@tcp(localhost:3307)/mattermost_test?charset=utf8mb4,utf8\u0026readTimeout=30s"
|
|
)
|
|
|
|
func getEnv(name, defaultValue string) string {
|
|
if value := os.Getenv(name); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func log(message string) {
|
|
verbose := false
|
|
if verboseFlag := flag.Lookup("test.v"); verboseFlag != nil {
|
|
verbose = verboseFlag.Value.String() != ""
|
|
}
|
|
if verboseFlag := flag.Lookup("v"); verboseFlag != nil {
|
|
verbose = verboseFlag.Value.String() != ""
|
|
}
|
|
|
|
if verbose {
|
|
fmt.Println(message)
|
|
}
|
|
}
|
|
|
|
// MySQLSettings returns the database settings to connect to the MySQL unittesting database.
|
|
// The database name is generated randomly and must be created before use.
|
|
func MySQLSettings(withReplica bool) *model.SqlSettings {
|
|
dsn := getEnv("TEST_DATABASE_MYSQL_DSN", defaultMysqlDSN)
|
|
cfg, err := mysql.ParseDSN(dsn)
|
|
if err != nil {
|
|
panic("failed to parse dsn " + dsn + ": " + err.Error())
|
|
}
|
|
|
|
cfg.DBName = "db" + model.NewId()
|
|
|
|
mySQLSettings := databaseSettings("mysql", cfg.FormatDSN())
|
|
|
|
if withReplica {
|
|
mySQLSettings.DataSourceReplicas = []string{getEnv("TEST_DATABASE_MYSQL_REPLICA_DSN", defaultMysqlReplicaDSN)}
|
|
}
|
|
|
|
return mySQLSettings
|
|
}
|
|
|
|
// PostgresSQLSettings returns the database settings to connect to the PostgreSQL unittesting database.
|
|
// The database name is generated randomly and must be created before use.
|
|
func PostgreSQLSettings() *model.SqlSettings {
|
|
dsn := getEnv("TEST_DATABASE_POSTGRESQL_DSN", defaultPostgresqlDSN)
|
|
dsnUrl, err := url.Parse(dsn)
|
|
if err != nil {
|
|
panic("failed to parse dsn " + dsn + ": " + err.Error())
|
|
}
|
|
|
|
// Generate a random database name
|
|
dsnUrl.Path = "db" + model.NewId()
|
|
|
|
return databaseSettings("postgres", dsnUrl.String())
|
|
}
|
|
|
|
func mySQLRootDSN(dsn string) string {
|
|
rootPwd := getEnv("TEST_DATABASE_MYSQL_ROOT_PASSWD", defaultMysqlRootPWD)
|
|
cfg, err := mysql.ParseDSN(dsn)
|
|
if err != nil {
|
|
panic("failed to parse dsn " + dsn + ": " + err.Error())
|
|
}
|
|
|
|
cfg.User = "root"
|
|
cfg.Passwd = rootPwd
|
|
cfg.DBName = "mysql"
|
|
|
|
return cfg.FormatDSN()
|
|
}
|
|
|
|
func postgreSQLRootDSN(dsn string) string {
|
|
dsnUrl, err := url.Parse(dsn)
|
|
if err != nil {
|
|
panic("failed to parse dsn " + dsn + ": " + err.Error())
|
|
}
|
|
|
|
// // Assume the unittesting database has the same password.
|
|
// password := ""
|
|
// if dsnUrl.User != nil {
|
|
// password, _ = dsnUrl.User.Password()
|
|
// }
|
|
|
|
// dsnUrl.User = url.UserPassword("", password)
|
|
dsnUrl.Path = "postgres"
|
|
|
|
return dsnUrl.String()
|
|
}
|
|
|
|
func mySQLDSNDatabase(dsn string) string {
|
|
cfg, err := mysql.ParseDSN(dsn)
|
|
if err != nil {
|
|
panic("failed to parse dsn " + dsn + ": " + err.Error())
|
|
}
|
|
|
|
return cfg.DBName
|
|
}
|
|
|
|
func postgreSQLDSNDatabase(dsn string) string {
|
|
dsnUrl, err := url.Parse(dsn)
|
|
if err != nil {
|
|
panic("failed to parse dsn " + dsn + ": " + err.Error())
|
|
}
|
|
|
|
return path.Base(dsnUrl.Path)
|
|
}
|
|
|
|
func databaseSettings(driver, dataSource string) *model.SqlSettings {
|
|
settings := &model.SqlSettings{
|
|
DriverName: &driver,
|
|
DataSource: &dataSource,
|
|
DataSourceReplicas: []string{},
|
|
DataSourceSearchReplicas: []string{},
|
|
MaxIdleConns: new(int),
|
|
ConnMaxLifetimeMilliseconds: new(int),
|
|
ConnMaxIdleTimeMilliseconds: new(int),
|
|
MaxOpenConns: new(int),
|
|
Trace: model.NewBool(false),
|
|
AtRestEncryptKey: model.NewString(model.NewRandomString(32)),
|
|
QueryTimeout: new(int),
|
|
}
|
|
*settings.MaxIdleConns = 10
|
|
*settings.ConnMaxLifetimeMilliseconds = 3600000
|
|
*settings.ConnMaxIdleTimeMilliseconds = 300000
|
|
*settings.MaxOpenConns = 100
|
|
*settings.QueryTimeout = 60
|
|
|
|
return settings
|
|
}
|
|
|
|
// execAsRoot executes the given sql as root against the testing database
|
|
func execAsRoot(settings *model.SqlSettings, sqlCommand string) error {
|
|
var dsn string
|
|
var driver = *settings.DriverName
|
|
|
|
switch driver {
|
|
case model.DATABASE_DRIVER_MYSQL:
|
|
dsn = mySQLRootDSN(*settings.DataSource)
|
|
case model.DATABASE_DRIVER_POSTGRES:
|
|
dsn = postgreSQLRootDSN(*settings.DataSource)
|
|
default:
|
|
return fmt.Errorf("unsupported driver %s", driver)
|
|
}
|
|
|
|
db, err := sql.Open(driver, dsn)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "failed to connect to %s database as root", driver)
|
|
}
|
|
defer db.Close()
|
|
if _, err = db.Exec(sqlCommand); err != nil {
|
|
return errors.Wrapf(err, "failed to execute `%s` against %s database as root", sqlCommand, driver)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func replaceMySQLDatabaseName(dsn, newDBName string) string {
|
|
cfg, err := mysql.ParseDSN(dsn)
|
|
if err != nil {
|
|
panic("failed to parse dsn " + dsn + ": " + err.Error())
|
|
}
|
|
cfg.DBName = newDBName
|
|
return cfg.FormatDSN()
|
|
}
|
|
|
|
// MakeSqlSettings creates a randomly named database and returns the corresponding sql settings
|
|
func MakeSqlSettings(driver string, withReplica bool) *model.SqlSettings {
|
|
var settings *model.SqlSettings
|
|
var dbName string
|
|
|
|
switch driver {
|
|
case model.DATABASE_DRIVER_MYSQL:
|
|
settings = MySQLSettings(withReplica)
|
|
dbName = mySQLDSNDatabase(*settings.DataSource)
|
|
newDSRs := []string{}
|
|
for _, dataSource := range settings.DataSourceReplicas {
|
|
newDSRs = append(newDSRs, replaceMySQLDatabaseName(dataSource, dbName))
|
|
}
|
|
settings.DataSourceReplicas = newDSRs
|
|
case model.DATABASE_DRIVER_POSTGRES:
|
|
settings = PostgreSQLSettings()
|
|
dbName = postgreSQLDSNDatabase(*settings.DataSource)
|
|
default:
|
|
panic("unsupported driver " + driver)
|
|
}
|
|
|
|
if err := execAsRoot(settings, "CREATE DATABASE "+dbName); err != nil {
|
|
panic("failed to create temporary database " + dbName + ": " + err.Error())
|
|
}
|
|
|
|
switch driver {
|
|
case model.DATABASE_DRIVER_MYSQL:
|
|
if err := execAsRoot(settings, "GRANT ALL PRIVILEGES ON "+dbName+".* TO 'mmuser'"); err != nil {
|
|
panic("failed to grant mmuser permission to " + dbName + ":" + err.Error())
|
|
}
|
|
case model.DATABASE_DRIVER_POSTGRES:
|
|
if err := execAsRoot(settings, "GRANT ALL PRIVILEGES ON DATABASE \""+dbName+"\" TO mmuser"); err != nil {
|
|
panic("failed to grant mmuser permission to " + dbName + ":" + err.Error())
|
|
}
|
|
default:
|
|
panic("unsupported driver " + driver)
|
|
}
|
|
|
|
log("Created temporary " + driver + " database " + dbName)
|
|
|
|
return settings
|
|
}
|
|
|
|
func CleanupSqlSettings(settings *model.SqlSettings) {
|
|
var driver = *settings.DriverName
|
|
var dbName string
|
|
|
|
switch driver {
|
|
case model.DATABASE_DRIVER_MYSQL:
|
|
dbName = mySQLDSNDatabase(*settings.DataSource)
|
|
case model.DATABASE_DRIVER_POSTGRES:
|
|
dbName = postgreSQLDSNDatabase(*settings.DataSource)
|
|
default:
|
|
panic("unsupported driver " + driver)
|
|
}
|
|
|
|
if err := execAsRoot(settings, "DROP DATABASE "+dbName); err != nil {
|
|
panic("failed to drop temporary database " + dbName + ": " + err.Error())
|
|
}
|
|
|
|
log("Dropped temporary database " + dbName)
|
|
}
|