From d455691edce941aa1172f253c3af2439ccf716b6 Mon Sep 17 00:00:00 2001 From: amecea Date: Wed, 1 May 2019 15:31:58 +0300 Subject: [PATCH] Fix linting typos and some tests to sidecar --- pkg/controller/node/node_controller.go | 11 ++++------- pkg/controller/node/node_ctrl_test.go | 18 ++---------------- pkg/controller/node/sql.go | 3 +++ pkg/controller/node/sql_fake_test.go | 5 +---- pkg/sidecar/appconf.go | 2 ++ pkg/sidecar/appconf_test.go | 24 ++++++++++++++++++++++++ pkg/sidecar/util.go | 26 -------------------------- pkg/util/constants/constants.go | 5 +---- 8 files changed, 37 insertions(+), 57 deletions(-) diff --git a/pkg/controller/node/node_controller.go b/pkg/controller/node/node_controller.go index 312de70c1..41ce5327d 100644 --- a/pkg/controller/node/node_controller.go +++ b/pkg/controller/node/node_controller.go @@ -179,11 +179,8 @@ func (r *ReconcileMysqlNode) Reconcile(request reconcile.Request) (reconcile.Res return reconcile.Result{}, err } - var sql SQLInterface - sql, err = r.getMySQLConnection(cluster, pod, creds) - if err != nil { - return reconcile.Result{}, err - } + // initialize SQL interface + sql := r.getMySQLConnection(cluster, pod, creds) err = r.initializeMySQL(sql, cluster, creds) if err != nil { @@ -252,7 +249,7 @@ func (r *ReconcileMysqlNode) getNodeCluster(pod *corev1.Pod) (*mysqlcluster.Mysq } // getMySQLConnectionString returns the DSN that contains credentials to connect to given pod from a MySQL cluster -func (r *ReconcileMysqlNode) getMySQLConnection(cluster *mysqlcluster.MysqlCluster, pod *corev1.Pod, c *credentials) (SQLInterface, error) { +func (r *ReconcileMysqlNode) getMySQLConnection(cluster *mysqlcluster.MysqlCluster, pod *corev1.Pod, c *credentials) SQLInterface { host := fmt.Sprintf("%s.%s.%s", pod.Spec.Hostname, cluster.GetNameForResource(mysqlcluster.HeadlessSVC), pod.Namespace) @@ -260,7 +257,7 @@ func (r *ReconcileMysqlNode) getMySQLConnection(cluster *mysqlcluster.MysqlClust c.User, c.Password, host, constants.MysqlPort, ) - return r.newSQLInterface(dsn, host), nil + return r.newSQLInterface(dsn, host) } type credentials struct { diff --git a/pkg/controller/node/node_ctrl_test.go b/pkg/controller/node/node_ctrl_test.go index 806675f12..2f79b36f4 100644 --- a/pkg/controller/node/node_ctrl_test.go +++ b/pkg/controller/node/node_ctrl_test.go @@ -19,13 +19,9 @@ package node import ( "fmt" - "math/rand" - "time" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - . "github.com/onsi/gomega/gstruct" - gomegatypes "github.com/onsi/gomega/types" + "math/rand" "golang.org/x/net/context" corev1 "k8s.io/api/core/v1" @@ -41,11 +37,8 @@ import ( "github.com/presslabs/mysql-operator/pkg/internal/mysqlcluster" ) -const timeout = time.Second * 2 - var ( one = int32(1) - two = int32(2) ) var _ = Describe("MysqlNode controller", func() { @@ -182,6 +175,7 @@ func podKey(cluster *mysqlcluster.MysqlCluster, index int) types.NamespacedName } } +// nolint: unparam func getOrCreatePod(c client.Client, cluster *mysqlcluster.MysqlCluster, index int) *corev1.Pod { pod := &corev1.Pod{} err := c.Get(context.TODO(), podKey(cluster, index), pod) @@ -210,11 +204,3 @@ func getOrCreatePod(c client.Client, cluster *mysqlcluster.MysqlCluster, index i Expect(err).To(BeNil()) return pod } - -func haveLabelWithValue(label, value string) gomegatypes.GomegaMatcher { - return PointTo(MatchFields(IgnoreExtras, Fields{ - "ObjectMeta": MatchFields(IgnoreExtras, Fields{ - "Labels": HaveKeyWithValue(label, value), - }), - })) -} diff --git a/pkg/controller/node/sql.go b/pkg/controller/node/sql.go index d55d891d9..9945523e5 100644 --- a/pkg/controller/node/sql.go +++ b/pkg/controller/node/sql.go @@ -35,6 +35,7 @@ const ( connRetry = 10 ) +// SQLInterface expose abstract operations that can be applied on a MySQL node type SQLInterface interface { Wait() error DisableSuperReadOnly() (func(), error) @@ -207,6 +208,7 @@ func (r *nodeSQLRunner) dbConn() (*sql.DB, func(), error) { func (r *nodeSQLRunner) SetPurgedGTID() error { // first check if the GTID should be set, if the table exists or if the GTID was set before (used) + // nolint: gosec qq := fmt.Sprintf("SELECT used FROM %[1]s.%[2]s WHERE id=1", constants.OperatorDbName, constants.OperatorGtidsTableName) @@ -226,6 +228,7 @@ func (r *nodeSQLRunner) SetPurgedGTID() error { } // GTID exists and should be set in a transaction + // nolint: gosec query := fmt.Sprintf(` SET @@SESSION.SQL_LOG_BIN = 0; START TRANSACTION; diff --git a/pkg/controller/node/sql_fake_test.go b/pkg/controller/node/sql_fake_test.go index d4aa854ad..acf2b8fa3 100644 --- a/pkg/controller/node/sql_fake_test.go +++ b/pkg/controller/node/sql_fake_test.go @@ -22,10 +22,7 @@ import ( . "github.com/onsi/gomega" ) -type fakeSQLRunner struct { - dsn string - host string -} +type fakeSQLRunner struct{} // test if fakeer implements interface var _ SQLInterface = &fakeSQLRunner{} diff --git a/pkg/sidecar/appconf.go b/pkg/sidecar/appconf.go index ccfbaeb16..51c1b9311 100644 --- a/pkg/sidecar/appconf.go +++ b/pkg/sidecar/appconf.go @@ -180,6 +180,8 @@ func initFileQuery(cfg *Config, gtidPurged string) []byte { []string{"REPLICATION CLIENT"}, "*.*")...) if len(gtidPurged) != 0 { + // If the xtrabackup information has GTID_PURGED then insert it into a table + // nolint: gosec queries = append(queries, fmt.Sprintf(` CREATE TABLE IF NOT EXISTS %[1]s.%[2]s ( id int PRIMARY KEY, diff --git a/pkg/sidecar/appconf_test.go b/pkg/sidecar/appconf_test.go index b1dac8c47..9d84af2cd 100644 --- a/pkg/sidecar/appconf_test.go +++ b/pkg/sidecar/appconf_test.go @@ -19,6 +19,7 @@ package sidecar import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "strings" ) var _ = Describe("Test sidecar appconf", func() { @@ -55,3 +56,26 @@ var _ = Describe("Test sidecar appconf", func() { Expect(func() { createUserQuery("a", "b", "c", []string{"d"}, 1) }).To(Panic()) }) }) + +var _ = Describe("Test sidecar GTID extraction", func() { + It("should find the single gtid set in backup", func() { + var ( + source string = "mysql-bin.000002 6552870 684ca0cf-495e-11e9-9fe8-0a580af407e9:1-176661\n" + result string = "684ca0cf-495e-11e9-9fe8-0a580af407e9:1-176661" + ) + Expect(getGTIDFrom(strings.NewReader(source))).To(Equal(result)) + }) + It("should find all gtid sets in backup", func() { + var ( + source string = `mysql-bin.006394 154349713 00003306-1111-0000-0000-000000000001:1-48861335, +00003306-1111-1111-1111-111111111111:1-11000155952, +00003306-2222-2222-2222-222222222222:1-8706021957 +` + result string = "00003306-1111-0000-0000-000000000001:1-48861335," + + "00003306-1111-1111-1111-111111111111:1-11000155952," + + "00003306-2222-2222-2222-222222222222:1-8706021957" + ) + Expect(getGTIDFrom(strings.NewReader(source))).To(Equal(result)) + }) + +}) diff --git a/pkg/sidecar/util.go b/pkg/sidecar/util.go index 3811c248b..0d8ad17c0 100644 --- a/pkg/sidecar/util.go +++ b/pkg/sidecar/util.go @@ -18,7 +18,6 @@ package sidecar import ( "bufio" - "database/sql" "fmt" "io" "os" @@ -30,31 +29,6 @@ import ( var log = logf.Log.WithName("sidecar") -// runQuery executes a query -func runQuery(cfg *Config, q string, args ...interface{}) error { - if len(cfg.MysqlDSN()) == -1 { - log.Info("could not get mysql connection DSN") - return fmt.Errorf("no DSN specified") - } - - db, err := sql.Open("mysql", cfg.MysqlDSN()) - if err != nil { - return err - } - defer func() { - if cErr := db.Close(); cErr != nil { - log.Error(cErr, "failed closing the database connection") - } - }() - - log.V(1).Info("running query", "query", q) - if _, err := db.Exec(q, args...); err != nil { - return err - } - - return nil -} - // copyFile the src file to dst. Any existing file will be overwritten and will not // copy file attributes. // nolint: gosec diff --git a/pkg/util/constants/constants.go b/pkg/util/constants/constants.go index a04e9213d..988d2477b 100644 --- a/pkg/util/constants/constants.go +++ b/pkg/util/constants/constants.go @@ -41,15 +41,12 @@ const ( // SlaveLagQuery in hack/charts/mysql-operator/values.yaml. OperatorDbName = "sys_operator" - // OperatorInitTableName represents the name of the table that is used to mark configuration complete + // OperatorGtidsTableName represents the name of the table that is used to store the GTID OperatorGtidsTableName = "gtids" // ConfVolumeMountPath is the path where mysql configs will be mounted ConfVolumeMountPath = "/etc/mysql" - // InitDBVolumeMountPath the path where to put *.sql init files. Docker entrypoint runs the scripts from this path. - InitDBVolumeMountPath = "/docker-entrypoint-initdb.d/" - // DataVolumeMountPath is the path to mysql data DataVolumeMountPath = "/var/lib/mysql"