Skip to content

Commit

Permalink
Import pq package to enable Postgres support
Browse files Browse the repository at this point in the history
Migate tests to fit new Postgres system
  • Loading branch information
Tetrino committed Sep 6, 2023
1 parent 2523dec commit 266ed28
Show file tree
Hide file tree
Showing 96 changed files with 7,509 additions and 19,478 deletions.
30 changes: 25 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,34 @@ lint:
unit_tests:
go test -race $$(go list ./... | grep -v integration_tests)

integration_tests: build start_mongo
integration_tests: build start_postgres init_test_db
go test -race -v ./integration_tests

start_mongo:
./mongo.sh start
start_postgres:
@if [ -z $$(docker ps -aqf name=router-postgres-test-db) ]; then \
docker run \
--name router-postgres-test-db \
-e POSTGRES_HOST_AUTH_METHOD=trust \
-d \
-p 5432:5432 \
--user 'postgres' \
--health-cmd 'pg_isready' \
--health-start-period 5s \
postgres:14; \
echo Waiting for postgres to be up; \
until [ "`docker inspect -f '{{.State.Health.Status}}' router-postgres-test-db`" = "healthy" ]; do \
echo '.\c'; \
sleep 1; \
done; \
else \
echo "PostgreSQL container 'router-postgres-test-db' already exists. Skipping creation."; \
fi

stop_mongo:
./mongo.sh stop
init_test_db:
docker exec -i router-postgres-test-db psql < localdb_init.sql

cleanup_postgres:
@docker rm -f router-postgres-test-db || true

update_deps:
go get -t -u ./... && go mod tidy && go mod vendor
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ go 1.20
require (
github.com/getsentry/sentry-go v0.23.0
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8
github.com/onsi/ginkgo/v2 v2.12.0
github.com/lib/pq v1.10.9
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/client_model v0.4.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFF
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU=
github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM=
github.com/onsi/ginkgo/v2 v2.12.0 h1:UIVDowFPwpg6yMUpPjGkYvf06K3RAiJXUhCxEwQVHRI=
github.com/onsi/ginkgo/v2 v2.12.0/go.mod h1:ZNEzXISYlqpb8S36iN71ifqLi3vVD1rVJGvWRCJOUpQ=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
Expand Down
73 changes: 49 additions & 24 deletions integration_tests/route_helpers.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package integration

import (
"database/sql"
"fmt"
"os"
"time"

"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
_ "github.com/lib/pq" // Without which we can't use PSQL calls

// revive:disable:dot-imports
. "github.com/onsi/ginkgo/v2"
Expand All @@ -19,18 +19,18 @@ var _ = AfterEach(func() {
})

var (
routerDB *mgo.Database
routerDB *sql.DB
)

type Route struct {
IncomingPath string `bson:"incoming_path"`
RouteType string `bson:"route_type"`
Handler string `bson:"handler"`
BackendID string `bson:"backend_id"`
RedirectTo string `bson:"redirect_to"`
RedirectType string `bson:"redirect_type"`
SegmentsMode string `bson:"segments_mode"`
Disabled bool `bson:"disabled"`
IncomingPath string
RouteType string
Handler string
BackendID string
RedirectTo string
RedirectType string
SegmentsMode string
Disabled bool
}

func NewBackendRoute(backendID string, extraParams ...string) Route {
Expand Down Expand Up @@ -80,36 +80,61 @@ func NewGoneRoute(extraParams ...string) Route {
}

func initRouteHelper() error {
databaseURL := os.Getenv("ROUTER_MONGO_URL")
databaseURL := os.Getenv("DATABASE_URL")

if databaseURL == "" {
databaseURL = "127.0.0.1"
databaseURL = "postgresql://postgres@127.0.0.1:5432/router_test?sslmode=disable"
}

sess, err := mgo.Dial(databaseURL)
db, err := sql.Open("postgres", databaseURL)
if err != nil {
return fmt.Errorf("failed to connect to mongo: %w", err)
return fmt.Errorf("Failed to connect to Postgres: " + err.Error())
}
sess.SetSyncTimeout(10 * time.Minute)
sess.SetSocketTimeout(10 * time.Minute)

routerDB = sess.DB("router_test")
db.SetConnMaxLifetime(10 * time.Minute)
db.SetMaxIdleConns(0)
db.SetMaxOpenConns(10)

routerDB = db
return nil
}

func addBackend(id, url string) {
err := routerDB.C("backends").Insert(bson.M{"backend_id": id, "backend_url": url})
Expect(err).NotTo(HaveOccurred())
query := `
INSERT INTO backends (backend_id, backend_url, created_at, updated_at)
VALUES ($1, $2, $3, $4)
`

_, err := routerDB.Exec(query, id, url, time.Now(), time.Now())
Expect(err).ToNot(HaveOccurred())
}

func addRoute(path string, route Route) {
route.IncomingPath = path

err := routerDB.C("routes").Insert(route)
Expect(err).NotTo(HaveOccurred())
query := `
INSERT INTO routes (incoming_path, route_type, handler, backend_id, redirect_to, redirect_type, segments_mode, disabled, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
`

_, err := routerDB.Exec(
query,
route.IncomingPath,
route.RouteType,
route.Handler,
route.BackendID,
route.RedirectTo,
route.RedirectType,
route.SegmentsMode,
route.Disabled,
time.Now(),
time.Now(),
)

Expect(err).ToNot(HaveOccurred())
}

func clearRoutes() {
_ = routerDB.C("routes").DropCollection()
_ = routerDB.C("backends").DropCollection()
_, err := routerDB.Exec("DELETE FROM routes; DELETE FROM backends")
Expect(err).ToNot(HaveOccurred())
}
6 changes: 3 additions & 3 deletions integration_tests/router_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
)

const (
routerPort = 3169
apiPort = 3168
routerPort = 5434
apiPort = 5433
)

func routerURL(port int, path string) string {
Expand Down Expand Up @@ -56,7 +56,7 @@ func startRouter(port, apiPort int, extraEnv []string) error {
}
cmd := exec.Command(bin)

cmd.Env = append(cmd.Environ(), "ROUTER_MONGO_DB=router_test")
cmd.Env = append(cmd.Environ(), "DATABASE_NAME=router_test")
cmd.Env = append(cmd.Env, fmt.Sprintf("ROUTER_PUBADDR=%s", pubAddr))
cmd.Env = append(cmd.Env, fmt.Sprintf("ROUTER_APIADDR=%s", apiAddr))
cmd.Env = append(cmd.Env, fmt.Sprintf("ROUTER_ERROR_LOG=%s", tempLogfile.Name()))
Expand Down
Loading

0 comments on commit 266ed28

Please sign in to comment.