Skip to content

Commit

Permalink
unify database version handling (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Apr 13, 2021
1 parent 366e585 commit 7ae0c89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
CGO_ENABLED := $(or ${CGO_ENABLED},0)
GO := go
GO111MODULE := on
PG_VERSION := $(or ${PG_VERSION},"13-alpine")
COCKROACH_VERSION := $(or ${COCKROACH_VERSION},"v20.2.7")

all: test bench

Expand Down Expand Up @@ -31,7 +33,7 @@ lint: golangcicheck

.PHONY: postgres-up
postgres-up: postgres-rm
docker run -d --name ipamdb -p 5433:5432 -e POSTGRES_PASSWORD="password" postgres:12-alpine postgres -c 'max_connections=200'
docker run -d --name ipamdb -p 5433:5432 -e POSTGRES_PASSWORD="password" postgres:$(PG_VERSION) -c 'max_connections=200'

.PHONY: postgres-rm
postgres-rm:
Expand All @@ -41,15 +43,15 @@ postgres-rm:
cockroach-up: cockroach-rm postgres-rm
# https://www.cockroachlabs.com/docs/v19.2/start-a-local-cluster-in-docker-linux.html#main-content
docker network create -d bridge roachnet
docker run -d --name=roach1 --hostname=roach1 --net=roachnet -p 5433:26257 -p 8080:8080 cockroachdb/cockroach:v20.1.8 start-single-node --insecure --listen-addr=0.0.0.0
docker run -d --name=roach1 --hostname=roach1 --net=roachnet -p 5433:26257 -p 8080:8080 cockroachdb/cockroach:$(COCKROACH_VERSION) start-single-node --insecure --listen-addr=0.0.0.0

.PHONY: cockroach-up-cluster
cockroach-up-cluster: cockroach-rm
# https://www.cockroachlabs.com/docs/v19.2/start-a-local-cluster-in-docker-linux.html#main-content
docker network create -d bridge roachnet
docker run -d --name=roach1 --hostname=roach1 --net=roachnet -p 5433:26257 -p 8080:8080 cockroachdb/cockroach:v20.1.8 start --insecure --join=roach1,roach2,roach3
docker run -d --name=roach2 --hostname=roach2 --net=roachnet cockroachdb/cockroach:v20.1.8 start --insecure --join=roach1,roach2,roach3
docker run -d --name=roach3 --hostname=roach3 --net=roachnet cockroachdb/cockroach:v20.1.8 start --insecure --join=roach1,roach2,roach3
docker run -d --name=roach1 --hostname=roach1 --net=roachnet -p 5433:26257 -p 8080:8080 cockroachdb/cockroach:$(COCKROACH_VERSION) start --insecure --join=roach1,roach2,roach3
docker run -d --name=roach2 --hostname=roach2 --net=roachnet cockroachdb/cockroach:$(COCKROACH_VERSION) start --insecure --join=roach1,roach2,roach3
docker run -d --name=roach3 --hostname=roach3 --net=roachnet cockroachdb/cockroach:$(COCKROACH_VERSION) start --insecure --join=roach1,roach2,roach3
docker exec -it roach1 ./cockroach init --insecure

.PHONY: cockroach-rm
Expand Down
25 changes: 19 additions & 6 deletions testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package ipam

import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"sync"
"testing"

Expand All @@ -12,13 +14,24 @@ import (
)

var (
pgOnce sync.Once
crOnce sync.Once
pgContainer testcontainers.Container
crContainer testcontainers.Container
pgOnce sync.Once
crOnce sync.Once
pgContainer testcontainers.Container
crContainer testcontainers.Container
pgVersion string
cockroachVersion string
)

func init() {
pgVersion = os.Getenv("PG_VERSION")
if pgVersion == "" {
pgVersion = "13"
}
cockroachVersion = os.Getenv("COCKROACH_VERSION")
if cockroachVersion == "" {
cockroachVersion = "v20.2.7"
}
fmt.Printf("Using postgres:%s cockroach:%s\n", pgVersion, cockroachVersion)
// prevent testcontainer logging mangle test and benchmark output
log.SetOutput(ioutil.Discard)
}
Expand All @@ -28,7 +41,7 @@ func startPostgres() (container testcontainers.Container, dn *sql, err error) {
pgOnce.Do(func() {
var err error
req := testcontainers.ContainerRequest{
Image: "postgres:13-alpine",
Image: "postgres:" + pgVersion,
ExposedPorts: []string{"5432/tcp"},
Env: map[string]string{"POSTGRES_PASSWORD": "password"},
WaitingFor: wait.ForAll(
Expand Down Expand Up @@ -64,7 +77,7 @@ func startCockroach() (container testcontainers.Container, dn *sql, err error) {
crOnce.Do(func() {
var err error
req := testcontainers.ContainerRequest{
Image: "cockroachdb/cockroach:v20.2.4",
Image: "cockroachdb/cockroach:" + cockroachVersion,
ExposedPorts: []string{"26257/tcp", "8080/tcp"},
Env: map[string]string{"POSTGRES_PASSWORD": "password"},
WaitingFor: wait.ForAll(
Expand Down

0 comments on commit 7ae0c89

Please sign in to comment.