Skip to content

Commit fc4a993

Browse files
Brett LoganBrett Logan
authored andcommitted
[FABC-816] Add connection string to db runners
Added a function to the MySQL and Postgres db runners to return the connection string specific to the implementation. Removed address parameter of the readycheck function as the readiness check now depends on a direct connection to the db, rather than dialing the endpoing Change-Id: Ia7835c1b43953068c1397e218abc61f515d7b3b3 Signed-off-by: Brett Logan <Brett.T.Logan@ibm.com>
1 parent a60d006 commit fc4a993

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

integration/runner/mysql.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ type MySQL struct {
4747
containerID string
4848
hostAddress string
4949
containerAddress string
50-
address string
5150

5251
mutex sync.Mutex
5352
stopped bool
@@ -158,10 +157,8 @@ func (c *MySQL) Run(sigCh <-chan os.Signal, ready chan<- struct{}) error {
158157
return errors.Wrapf(ctx.Err(), "database in container %s did not start", c.containerID)
159158
case <-containerExit:
160159
return errors.New("container exited before ready")
161-
case <-c.ready(ctx, c.hostAddress):
162-
c.address = c.hostAddress
163-
case <-c.ready(ctx, c.containerAddress):
164-
c.address = c.containerAddress
160+
case <-c.ready(ctx):
161+
break
165162
}
166163

167164
cancel()
@@ -191,11 +188,11 @@ func (c *MySQL) endpointReady(ctx context.Context, db *sqlx.DB) bool {
191188
return true
192189
}
193190

194-
func (c *MySQL) ready(ctx context.Context, addr string) <-chan struct{} {
191+
func (c *MySQL) ready(ctx context.Context) <-chan struct{} {
195192
readyCh := make(chan struct{})
196193

197-
str := fmt.Sprintf("root:@(%s:%d)/mysql", c.HostIP, c.HostPort)
198-
db, err := sqlx.Open("mysql", str)
194+
connStr, _ := c.GetConnectionString()
195+
db, err := sqlx.Open("mysql", connStr)
199196
db.SetConnMaxLifetime(time.Second * 5)
200197
if err != nil {
201198
ctx.Done()
@@ -258,11 +255,6 @@ func (c *MySQL) streamLogs(ctx context.Context) {
258255
stdcopy.StdCopy(c.OutputStream, c.ErrorStream, out)
259256
}
260257

261-
// Address returns the address successfully used by the readiness check.
262-
func (c *MySQL) Address() string {
263-
return c.address
264-
}
265-
266258
// HostAddress returns the host address where this MySQL instance is available.
267259
func (c *MySQL) HostAddress() string {
268260
return c.hostAddress
@@ -308,3 +300,11 @@ func (c *MySQL) Stop() error {
308300

309301
return nil
310302
}
303+
304+
// GetConnectionString returns the sql connection string for connecting to the DB
305+
func (c *MySQL) GetConnectionString() (string, error) {
306+
if c.HostIP != "" && c.HostPort != 0 {
307+
return fmt.Sprintf("root:@(%s:%d)/mysql", c.HostIP, c.HostPort), nil
308+
}
309+
return "", fmt.Errorf("mysql db not initialized")
310+
}

integration/runner/postgres.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ type PostgresDB struct {
4747
containerID string
4848
hostAddress string
4949
containerAddress string
50-
address string
5150

5251
mutex sync.Mutex
5352
stopped bool
@@ -155,10 +154,8 @@ func (c *PostgresDB) Run(sigCh <-chan os.Signal, ready chan<- struct{}) error {
155154
return errors.Wrapf(ctx.Err(), "database in container %s did not start", c.containerID)
156155
case <-containerExit:
157156
return errors.New("container exited before ready")
158-
case <-c.ready(ctx, c.hostAddress):
159-
c.address = c.hostAddress
160-
case <-c.ready(ctx, c.containerAddress):
161-
c.address = c.containerAddress
157+
case <-c.ready(ctx):
158+
break
162159
}
163160

164161
cancel()
@@ -178,14 +175,8 @@ func (c *PostgresDB) Run(sigCh <-chan os.Signal, ready chan<- struct{}) error {
178175
}
179176
}
180177

181-
func (c *PostgresDB) endpointReady(ctx context.Context, addr string) bool {
182-
dataSource := fmt.Sprintf("host=%s port=%d user=postgres dbname=postgres sslmode=disable", c.HostIP, c.HostPort)
183-
db, err := sqlx.Open("postgres", dataSource)
184-
if err != nil {
185-
return false
186-
}
187-
188-
_, err = db.Conn(ctx)
178+
func (c *PostgresDB) endpointReady(ctx context.Context, db *sqlx.DB) bool {
179+
_, err := db.Conn(ctx)
189180
if err != nil {
190181
return false
191182
}
@@ -194,13 +185,20 @@ func (c *PostgresDB) endpointReady(ctx context.Context, addr string) bool {
194185
return true
195186
}
196187

197-
func (c *PostgresDB) ready(ctx context.Context, addr string) <-chan struct{} {
188+
func (c *PostgresDB) ready(ctx context.Context) <-chan struct{} {
198189
readyCh := make(chan struct{})
190+
191+
connStr, _ := c.GetConnectionString()
192+
db, err := sqlx.Open("postgres", connStr)
193+
if err != nil {
194+
ctx.Done()
195+
}
196+
199197
go func() {
200198
ticker := time.NewTicker(100 * time.Millisecond)
201199
defer ticker.Stop()
202200
for {
203-
if c.endpointReady(ctx, addr) {
201+
if c.endpointReady(ctx, db) {
204202
close(readyCh)
205203
return
206204
}
@@ -253,11 +251,6 @@ func (c *PostgresDB) streamLogs(ctx context.Context) {
253251
stdcopy.StdCopy(c.OutputStream, c.ErrorStream, out)
254252
}
255253

256-
// Address returns the address successfully used by the readiness check.
257-
func (c *PostgresDB) Address() string {
258-
return c.address
259-
}
260-
261254
// HostAddress returns the host address where this PostgresDB instance is available.
262255
func (c *PostgresDB) HostAddress() string {
263256
return c.hostAddress
@@ -303,3 +296,12 @@ func (c *PostgresDB) Stop() error {
303296

304297
return nil
305298
}
299+
300+
// GetConnectionString returns the sql connection string for connecting to the DB
301+
func (c *PostgresDB) GetConnectionString() (string, error) {
302+
if c.HostIP != "" && c.HostPort != 0 {
303+
return fmt.Sprintf("host=%s port=%d user=postgres dbname=postgres sslmode=disable",
304+
c.HostIP, c.HostPort), nil
305+
}
306+
return "", fmt.Errorf("DB not initialized")
307+
}

0 commit comments

Comments
 (0)