From 6d9c7586b59812f1d674d34d30fbc151b8940455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20J=C3=BAno=C5=A1?= Date: Mon, 22 Feb 2021 23:12:39 +0100 Subject: [PATCH] Make test pass - cache connections Reorder grant-revoke, escape db name, reduce conns. Without connection caching, program goes crazy and utilizes everything it can - that results in failed test. --- GNUmakefile | 2 +- mysql/provider.go | 21 +++++++++++++++++++++ mysql/resource_grant.go | 22 +++++++++++----------- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index 0bda4f52..0f4d1c7f 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -14,7 +14,7 @@ test: fmtcheck xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4 testacc: fmtcheck - TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m + TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout=30s vet: @echo "go vet ." diff --git a/mysql/provider.go b/mysql/provider.go index 42556d85..d99d7f7c 100644 --- a/mysql/provider.go +++ b/mysql/provider.go @@ -7,6 +7,7 @@ import ( "net/url" "regexp" "strings" + "sync" "time" "github.com/go-sql-driver/mysql" @@ -33,6 +34,18 @@ type MySQLConfiguration struct { ConnectRetryTimeoutSec time.Duration } +var ( + connectionCacheMtx sync.Mutex + connectionCache map[string]*sql.DB +) + +func init() { + connectionCacheMtx.Lock() + defer connectionCacheMtx.Unlock() + + connectionCache = map[string]*sql.DB{} +} + func Provider() terraform.ResourceProvider { return &schema.Provider{ Schema: map[string]*schema.Schema{ @@ -234,8 +247,15 @@ func serverVersionString(db *sql.DB) (string, error) { } func connectToMySQL(conf *MySQLConfiguration) (*sql.DB, error) { + // This is fine - we'll connect serially, but we don't expect more than + // 1 or 2 connections at once. + connectionCacheMtx.Lock() + defer connectionCacheMtx.Unlock() dsn := conf.Config.FormatDSN() + if connectionCache[dsn] != nil { + return connectionCache[dsn], nil + } var db *sql.DB var err error @@ -260,6 +280,7 @@ func connectToMySQL(conf *MySQLConfiguration) (*sql.DB, error) { if retryError != nil { return nil, fmt.Errorf("Could not connect to server: %s", retryError) } + connectionCache[dsn] = db db.SetConnMaxLifetime(conf.MaxConnLifetime) db.SetMaxOpenConns(conf.MaxOpenConns) return db, nil diff --git a/mysql/resource_grant.go b/mysql/resource_grant.go index 83374f27..959ceee6 100644 --- a/mysql/resource_grant.go +++ b/mysql/resource_grant.go @@ -314,14 +314,14 @@ func updatePrivileges(d *schema.ResourceData, db *sql.DB, user string, database grantIfs := newPrivs.Difference(oldPrivs).List() revokeIfs := oldPrivs.Difference(newPrivs).List() - if len(grantIfs) > 0 { - grants := make([]string, len(grantIfs)) + if len(revokeIfs) > 0 { + revokes := make([]string, len(revokeIfs)) - for i, v := range grantIfs { - grants[i] = v.(string) + for i, v := range revokeIfs { + revokes[i] = v.(string) } - sql := fmt.Sprintf("GRANT %s ON %s.%s TO %s", strings.Join(grants, ","), database, table, user) + sql := fmt.Sprintf("REVOKE %s ON %s.%s FROM %s", strings.Join(revokes, ","), formatDatabaseName(database), formatTableName(table), user) log.Printf("[DEBUG] SQL: %s", sql) @@ -330,14 +330,14 @@ func updatePrivileges(d *schema.ResourceData, db *sql.DB, user string, database } } - if len(revokeIfs) > 0 { - revokes := make([]string, len(revokeIfs)) + if len(grantIfs) > 0 { + grants := make([]string, len(grantIfs)) - for i, v := range revokeIfs { - revokes[i] = v.(string) + for i, v := range grantIfs { + grants[i] = v.(string) } - sql := fmt.Sprintf("REVOKE %s ON %s.%s FROM %s", strings.Join(revokes, ","), database, table, user) + sql := fmt.Sprintf("GRANT %s ON %s.%s TO %s", strings.Join(grants, ","), formatDatabaseName(database), formatTableName(table), user) log.Printf("[DEBUG] SQL: %s", sql) @@ -493,7 +493,7 @@ func showGrants(db *sql.DB, user string) ([]*MySQLGrant, error) { } privsStr := m[1] - priv_list := strings.Split(privsStr, ",") + priv_list := extractPermTypes(privsStr) privileges := make([]string, len(priv_list)) for i, priv := range priv_list {