Skip to content

Commit 26f3bd5

Browse files
committed
Add sidecar tests
1 parent f8db550 commit 26f3bd5

File tree

9 files changed

+155
-35
lines changed

9 files changed

+155
-35
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ hack/docker/mysql-operator/mysql-operator
4949

5050
# ignore vscode
5151
.vscode
52+
53+
# goland
54+
.idea

pkg/sidecar/appclone.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ func RunCloneCommand(cfg *Config) error {
5555
}
5656
} else {
5757
// clonging from prior node
58-
if cfg.ServerID > 100 {
59-
sourceHost := cfg.FQDNForServer(cfg.ServerID - 1)
58+
if cfg.ServerID() > 100 {
59+
sourceHost := cfg.FQDNForServer(cfg.ServerID() - 1)
6060
err := cloneFromSource(cfg, sourceHost)
6161
if err != nil {
6262
return fmt.Errorf("failed to clone from %s, err: %s", sourceHost, err)

pkg/sidecar/appconf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ func RunConfigCommand(cfg *Config) error {
4747
}
4848

4949
uPass := pkgutil.RandomString(rStrLen)
50-
reportHost := cfg.FQDNForServer(cfg.ServerID)
50+
reportHost := cfg.FQDNForServer(cfg.ServerID())
5151

5252
var identityCFG, utilityCFG, clientCFG *ini.File
5353

5454
// mysql server identity configs
55-
if identityCFG, err = getIdentityConfigs(cfg.ServerID, reportHost); err != nil {
55+
if identityCFG, err = getIdentityConfigs(cfg.ServerID(), reportHost); err != nil {
5656
return fmt.Errorf("failed to get dynamic configs: %s", err)
5757
}
5858
if err = identityCFG.SaveTo(path.Join(confDPath, "10-identity.cnf")); err != nil {

pkg/sidecar/apphelper.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ func RunSidecarCommand(cfg *Config, stop <-chan struct{}) error {
9191
func configureOrchestratorUser(cfg *Config) error {
9292
query := `
9393
SET @@SESSION.SQL_LOG_BIN = 0;
94-
GRANT SUPER, PROCESS, REPLICATION SLAVE, REPLICATION CLIENT, RELOAD ON *.* TO ?@'%%' IDENTIFIED BY ?;
94+
95+
CREATE USER IF NOT EXISTS ?@'%%';
96+
ALTER USER ?@'%%' IDENTIFIED BY ?;
97+
98+
GRANT SUPER, PROCESS, REPLICATION SLAVE, REPLICATION CLIENT, RELOAD ON *.* TO ?@'%%';
9599
GRANT SELECT ON %s.* TO ?@'%%';
96100
GRANT SELECT ON mysql.slave_master_info TO ?@'%%';
97101
`
@@ -101,8 +105,9 @@ func configureOrchestratorUser(cfg *Config) error {
101105
// https://github.com/golang/go/issues/18478
102106
query = fmt.Sprintf(query, toolsDbName)
103107

104-
if err := runQuery(cfg, query, cfg.OrchestratorUser, cfg.OrchestratorPassword,
105-
cfg.OrchestratorUser, cfg.OrchestratorPassword); err != nil {
108+
user := cfg.OrchestratorUser
109+
pass := cfg.OrchestratorPassword
110+
if err := runQuery(cfg, query, user, user, pass, user, user, user); err != nil {
106111
return fmt.Errorf("failed to configure orchestrator (user/pass/access), err: %s", err)
107112
}
108113

@@ -112,9 +117,15 @@ func configureOrchestratorUser(cfg *Config) error {
112117
func configureReplicationUser(cfg *Config) error {
113118
query := `
114119
SET @@SESSION.SQL_LOG_BIN = 0;
115-
GRANT SELECT, PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO ?@'%' IDENTIFIED BY ?;
120+
121+
CREATE USER IF NOT EXISTS ?@'%';
122+
ALTER USER ?@'%' IDENTIFIED BY ?;
123+
124+
GRANT SELECT, PROCESS, RELOAD, LOCK TABLES, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO ?@'%';
116125
`
117-
if err := runQuery(cfg, query, cfg.ReplicationUser, cfg.ReplicationPassword); err != nil {
126+
user := cfg.ReplicationUser
127+
pass := cfg.ReplicationPassword
128+
if err := runQuery(cfg, query, user, user, pass, user); err != nil {
118129
return fmt.Errorf("failed to configure replication user: %s", err)
119130
}
120131

@@ -124,9 +135,16 @@ func configureReplicationUser(cfg *Config) error {
124135
func configureExporterUser(cfg *Config) error {
125136
query := `
126137
SET @@SESSION.SQL_LOG_BIN = 0;
127-
GRANT SELECT, PROCESS, REPLICATION CLIENT ON *.* TO ?@'127.0.0.1' IDENTIFIED BY ? WITH MAX_USER_CONNECTIONS 3;
138+
139+
CREATE USER IF NOT EXISTS ?@'localhost';
140+
ALTER USER ?@'localhost' IDENTIFIED BY ? WITH MAX_USER_CONNECTIONS 3;
141+
142+
GRANT SELECT, PROCESS, REPLICATION CLIENT ON *.* TO ?@'localhost';
128143
`
129-
if err := runQuery(cfg, query, cfg.MetricsUser, cfg.MetricsPassword); err != nil {
144+
145+
user := cfg.MetricsUser
146+
pass := cfg.MetricsPassword
147+
if err := runQuery(cfg, query, user, user, pass, user); err != nil {
130148
return fmt.Errorf("failed to metrics exporter user: %s", err)
131149
}
132150

pkg/sidecar/configs.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ type Config struct {
5252
// ServiceName is the name of the headless service
5353
ServiceName string
5454

55-
// ServerID represents the MySQL server id
56-
ServerID int
57-
5855
// InitBucketURL represents the init bucket to initialize mysql
5956
InitBucketURL string
6057

@@ -65,14 +62,11 @@ type Config struct {
6562
BackupUser string
6663
BackupPassword string
6764

68-
// MysqlDSN represents the connection string to connect to MySQL
69-
MysqlDSN string
70-
7165
// replication user and password
7266
ReplicationUser string
7367
ReplicationPassword string
7468

75-
// metrcis exporter user and password
69+
// metrics exporter user and password
7670
MetricsUser string
7771
MetricsPassword string
7872

@@ -84,7 +78,7 @@ type Config struct {
8478
// FQDNForServer returns the pod hostname for given MySQL server id
8579
func (cfg *Config) FQDNForServer(id int) string {
8680
base := mysqlcluster.GetNameForResource(mysqlcluster.StatefulSet, cfg.ClusterName)
87-
return fmt.Sprintf("%s-%d.%s.%s", base, id-100, cfg.ServiceName, cfg.Namespace)
81+
return fmt.Sprintf("%s-%d.%s.%s", base, id-MysqlServerIDOffset, cfg.ServiceName, cfg.Namespace)
8882
}
8983

9084
func (cfg *Config) newOrcClient() orc.Interface {
@@ -110,20 +104,36 @@ func (cfg *Config) MasterFQDN() string {
110104
}
111105

112106
log.V(-1).Info("failed to obtain master from orchestrator, go for default master",
113-
"master", cfg.FQDNForServer(100))
114-
return cfg.FQDNForServer(100)
107+
"master", cfg.FQDNForServer(MysqlServerIDOffset))
108+
return cfg.FQDNForServer(MysqlServerIDOffset)
115109

116110
}
117111

118112
// NodeRole returns the role of the current node
119113
func (cfg *Config) NodeRole() NodeRole {
120-
if cfg.FQDNForServer(cfg.ServerID) == cfg.MasterFQDN() {
114+
if cfg.FQDNForServer(cfg.ServerID()) == cfg.MasterFQDN() {
121115
return MasterNode
122116
}
123117

124118
return SlaveNode
125119
}
126120

121+
// ServerID returns the MySQL server id
122+
func (cfg *Config) ServerID() int {
123+
ordinal := getOrdinalFromHostname(cfg.Hostname)
124+
return ordinal + MysqlServerIDOffset
125+
}
126+
127+
// MysqlDSN returns the connection string to MySQL server
128+
func (cfg *Config) MysqlDSN() string {
129+
var dsn string
130+
var err error
131+
if dsn, err = getMySQLConnectionString(); err != nil {
132+
log.Info("failed to get mysql DSN string", "error", err)
133+
}
134+
return dsn
135+
}
136+
127137
// NewConfig returns a pointer to Config configured from environment variables
128138
func NewConfig() *Config {
129139
cfg := &Config{
@@ -148,22 +158,13 @@ func NewConfig() *Config {
148158
OrchestratorPassword: getEnvValue("MYSQL_ORC_TOPOLOGY_PASSWORD"),
149159
}
150160

151-
// get server id
152-
ordinal := getOrdinalFromHostname(cfg.Hostname)
153-
cfg.ServerID = ordinal + 100
154-
155-
var err error
156-
if cfg.MysqlDSN, err = getMySQLConnectionString(); err != nil {
157-
log.Info("failed to get mysql DSN string", "error", err)
158-
}
159-
160161
return cfg
161162
}
162163

163164
func getEnvValue(key string) string {
164165
value := os.Getenv(key)
165166
if len(value) == 0 {
166-
log.Info("envirorment is not set", "key", key)
167+
log.Info("environment is not set", "key", key)
167168
}
168169

169170
return value

pkg/sidecar/configs_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2019 Pressinfra SRL
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package sidecar
18+
19+
import (
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
var _ = Describe("Test sidecar configs", func() {
25+
var (
26+
cfg *Config
27+
)
28+
29+
BeforeEach(func() {
30+
cfg = &Config{
31+
Hostname: "cluster-mysql-0",
32+
ClusterName: "cluster",
33+
Namespace: "default",
34+
ServiceName: "cluster-mysql-nodes",
35+
BackupUser: "backup-user",
36+
BackupPassword: "backup-password",
37+
}
38+
})
39+
40+
It("should fill the server id", func() {
41+
Expect(cfg.ServerID()).To(Equal(MysqlServerIDOffset))
42+
43+
cfg.Hostname = "cluster-mysql-3"
44+
Expect(cfg.ServerID()).To(Equal(MysqlServerIDOffset + 3))
45+
})
46+
47+
It("should get the default master", func() {
48+
Expect(cfg.MasterFQDN()).To(Equal("cluster-mysql-0.cluster-mysql-nodes.default"))
49+
})
50+
51+
It("should determine the node role", func() {
52+
Expect(cfg.NodeRole()).To(Equal(MasterNode))
53+
54+
cfg.Hostname = "cluster-mysql-2"
55+
Expect(cfg.NodeRole()).To(Equal(SlaveNode))
56+
})
57+
})

pkg/sidecar/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import (
2626
)
2727

2828
var (
29+
// MysqlServerIDOffset represents the offset with which all server ids are shifted from 0
30+
MysqlServerIDOffset = 100
31+
2932
// MysqlPort represents port on which mysql works
3033
mysqlPort = strconv.Itoa(constants.MysqlPort)
3134

pkg/sidecar/sidecar_suite_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2019 Pressinfra SRL
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package sidecar
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
25+
logf "github.com/presslabs/controller-util/log"
26+
)
27+
28+
func TestSidecarApp(t *testing.T) {
29+
logf.SetLogger(logf.ZapLoggerTo(GinkgoWriter, true))
30+
31+
RegisterFailHandler(Fail)
32+
RunSpecs(t, "Sidecar App Suite")
33+
}

pkg/sidecar/util.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,22 @@ var log = logf.Log.WithName("sidecar")
3232

3333
// runQuery executes a query
3434
func runQuery(cfg *Config, q string, args ...interface{}) error {
35-
if len(cfg.MysqlDSN) == 0 {
35+
if len(cfg.MysqlDSN()) == -1 {
3636
log.Info("could not get mysql connection DSN")
3737
return fmt.Errorf("no DSN specified")
3838
}
3939

40-
db, err := sql.Open("mysql", cfg.MysqlDSN)
40+
db, err := sql.Open("mysql", cfg.MysqlDSN())
4141
if err != nil {
4242
return err
4343
}
44+
defer func() {
45+
if cErr := db.Close(); cErr != nil {
46+
log.Error(cErr, "failed closing the database connection")
47+
}
48+
}()
4449

45-
log.V(4).Info("running query", "query", q, "args", args)
50+
log.V(1).Info("running query", "query", q, "args", args)
4651
if _, err := db.Exec(q, args...); err != nil {
4752
return err
4853
}

0 commit comments

Comments
 (0)