Skip to content

Commit

Permalink
Fill postgres database.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Aug 25, 2023
1 parent 536fd77 commit 1ded009
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ LINKMODE := -extldflags '-static -s -w' \
-X 'github.com/metal-stack/v.BuildDate=$(BUILDDATE)'

KUBECONFIG := $(shell pwd)/.kubeconfig
GO_RUN := $(or $(GO_RUN),)
ifneq ($(GO_RUN),)
GO_RUN_ARG := -run $(GO_RUN)
endif

.PHONY: all
all:
Expand All @@ -25,7 +29,7 @@ all:
.PHONY: test-integration
test-integration: dockerimage
kind --name backup-restore-sidecar load docker-image ghcr.io/metal-stack/backup-restore-sidecar:latest
KUBECONFIG=$(KUBECONFIG) go test -v -p 1 -timeout 10m ./integration/...
KUBECONFIG=$(KUBECONFIG) go test $(GO_RUN_ARG) -v -p 1 -timeout 10m ./integration/...

.PHONY: proto
proto:
Expand Down
2 changes: 1 addition & 1 deletion deploy/postgres-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ spec:
- 127.0.0.1
- -p
- "5432"
initialDelaySeconds: 10
initialDelaySeconds: 5
periodSeconds: 10
timeoutSeconds: 5
resources: {}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/avast/retry-go/v4 v4.5.0
github.com/aws/aws-sdk-go v1.44.324
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/lib/pq v1.10.9
github.com/metal-stack/metal-lib v0.13.2
github.com/metal-stack/v v1.0.3
github.com/mholt/archiver/v3 v3.5.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
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/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
Expand Down
64 changes: 59 additions & 5 deletions integration/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ package integration_test

import (
"context"
"database/sql"
"fmt"
"testing"

"github.com/avast/retry-go/v4"
"github.com/metal-stack/backup-restore-sidecar/pkg/constants"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"

_ "github.com/lib/pq"
)

const (
Expand All @@ -20,7 +27,7 @@ const (

func Test_Postgres(t *testing.T) {
const (
db = "postgres"
postgresDB = "postgres"
postgresPassword = "test123!"
postgresUser = "test"
table = "precioustestdata"
Expand Down Expand Up @@ -79,7 +86,7 @@ func Test_Postgres(t *testing.T) {
Command: []string{"/bin/sh", "-c", "exec", "pg_isready", "-U", postgresUser, "-h", "127.0.0.1", "-p", "5432"},
},
},
InitialDelaySeconds: 10,
InitialDelaySeconds: 5,
TimeoutSeconds: 5,
PeriodSeconds: 10,
},
Expand Down Expand Up @@ -330,7 +337,7 @@ post-exec-cmds:
Namespace: namespace,
},
StringData: map[string]string{
"POSTGRES_DB": db,
"POSTGRES_DB": postgresDB,
"POSTGRES_USER": postgresUser,
"POSTGRES_PASSWORD": postgresPassword,
"POSTGRES_DATA": "/data/postgres/",
Expand Down Expand Up @@ -369,12 +376,59 @@ post-exec-cmds:
}
}

newPostgresSession = func(t *testing.T, ctx context.Context) *sql.DB {
var db *sql.DB
err := retry.Do(func() error {
connString := fmt.Sprintf("host=127.0.0.1 port=5432 user=%s password=%s dbname=%s sslmode=disable", postgresUser, postgresPassword, postgresDB)

var err error
db, err = sql.Open("postgres", connString)
if err != nil {
return err
}

return nil
}, retry.Context(ctx))
require.NoError(t, err)

return db
}

addTestData = func(t *testing.T, ctx context.Context) {
// TODO: Implement
db := newPostgresSession(t, ctx)
defer db.Close()

var (
createStmt = `CREATE TABLE backuprestore (
data text NOT NULL
);`
insertStmt = `INSERT INTO backuprestore("data") VALUES ('I am precious');`
)

_, err := db.Exec(createStmt)
require.NoError(t, err)

_, err = db.Exec(insertStmt)
require.NoError(t, err)
}

verifyTestData = func(t *testing.T, ctx context.Context) {
// TODO: Implement
db := newPostgresSession(t, ctx)
defer db.Close()

rows, err := db.Query(`SELECT "data" FROM backuprestore`)
require.NoError(t, err)
require.NoError(t, rows.Err())
defer rows.Close()

require.True(t, rows.Next())
var data string

err = rows.Scan(&data)
require.NoError(t, err)

assert.Equal(t, "I am precious", data)
assert.False(t, rows.Next())
}
)

Expand Down
3 changes: 3 additions & 0 deletions kind.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ nodes:
- containerPort: 28015
hostPort: 28015
listenAddress: 0.0.0.0
- containerPort: 5432
hostPort: 5432
listenAddress: 0.0.0.0

0 comments on commit 1ded009

Please sign in to comment.