Skip to content

Commit af3de46

Browse files
committed
feat: add dataStateAt tests (#249)
1 parent 9d10246 commit af3de46

File tree

13 files changed

+1023
-70
lines changed

13 files changed

+1023
-70
lines changed

.gitlab-ci.yml

+16-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ build-image-swagger-latest:
222222
- export LATEST_TAG=$(echo ${CI_COMMIT_TAG%.*}-latest)
223223
- export TAGS="${DOCKER_NAME}:${LATEST_TAG}"
224224

225-
integration-test:
225+
bash-test:
226226
<<: *only_feature
227227
stage: integration-test
228228
variables:
@@ -234,3 +234,18 @@ integration-test:
234234
- bash test/_cleanup.sh
235235
tags:
236236
- dle-test
237+
238+
integration-test:
239+
services:
240+
- docker:dind
241+
<<: *only_feature
242+
stage: integration-test
243+
variables:
244+
# Instruct Testcontainers to use the daemon of DinD.
245+
DOCKER_HOST: "tcp://docker:2375"
246+
# Instruct Docker not to start over TLS.
247+
DOCKER_TLS_CERTDIR: ""
248+
# Improve performance with overlayfs.
249+
DOCKER_DRIVER: overlay2
250+
script:
251+
- make test-ci-integration

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,4 @@ issues:
9999
max-issues-per-linter: 0
100100
max-same-issues: 0
101101

102-
new-from-rev: 67c2feac
102+
new-from-rev: 66fcab89

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ build-client:
5151
test:
5252
${GOTEST} ./...
5353

54+
test-ci-integration:
55+
GO111MODULE=on go test -race -tags=integration ./...
56+
57+
test-local-integration:
58+
sudo GO111MODULE=on /usr/local/go/bin/go test -race -tags=integration ./...
59+
5460
fmt:
5561
go fmt $$(go list ./... | grep -v /vendor/)
5662

go.mod

+5-14
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,32 @@ go 1.15
44

55
require (
66
github.com/AlekSi/pointer v1.1.0
7-
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
8-
github.com/Microsoft/go-winio v0.4.16 // indirect
97
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
108
github.com/araddon/dateparse v0.0.0-20210207001429-0eec95c9db7e
119
github.com/aws/aws-sdk-go v1.33.8
12-
github.com/containerd/containerd v1.4.0 // indirect
1310
github.com/docker/cli v0.0.0-20200721130541-80fd48bcb7e7
14-
github.com/docker/distribution v2.7.1+incompatible // indirect
15-
github.com/docker/docker v1.13.1
16-
github.com/docker/go-connections v0.4.0 // indirect
17-
github.com/docker/go-units v0.4.0 // indirect
11+
github.com/docker/docker v20.10.5+incompatible
12+
github.com/docker/go-connections v0.4.0
1813
github.com/dustin/go-humanize v1.0.0
1914
github.com/go-ole/go-ole v1.2.4 // indirect
20-
github.com/gogo/protobuf v1.3.1 // indirect
2115
github.com/gorilla/mux v1.8.0
2216
github.com/gorilla/websocket v1.4.2
2317
github.com/jackc/pgtype v1.5.0
2418
github.com/jackc/pgx/v4 v4.9.0
2519
github.com/lib/pq v1.8.0
2620
github.com/morikuni/aec v1.0.0 // indirect
27-
github.com/opencontainers/go-digest v1.0.0 // indirect
28-
github.com/opencontainers/image-spec v1.0.1 // indirect
21+
github.com/opencontainers/image-spec v1.0.1
2922
github.com/pkg/errors v0.9.1
3023
github.com/robfig/cron/v3 v3.0.1
3124
github.com/rs/xid v1.2.1
3225
github.com/sergi/go-diff v1.1.0
3326
github.com/sethvargo/go-password v0.2.0
3427
github.com/shirou/gopsutil v2.20.9+incompatible
3528
github.com/stretchr/testify v1.7.0
29+
github.com/testcontainers/testcontainers-go v0.10.0
3630
github.com/urfave/cli/v2 v2.1.1
3731
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392
38-
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
39-
google.golang.org/grpc v1.31.0 // indirect
40-
gopkg.in/yaml.v2 v2.2.8
41-
gotest.tools v2.2.0+incompatible // indirect
32+
gopkg.in/yaml.v2 v2.4.0
4233
)
4334

4435
replace github.com/docker/docker v1.13.1 => github.com/docker/engine v0.0.0-20200618181300-9dc6525e6118

go.sum

+681-48
Large diffs are not rendered by default.

pkg/retrieval/engine/postgres/logical/dump.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/docker/docker/api/types/container"
1717
"github.com/docker/docker/api/types/network"
1818
"github.com/docker/docker/client"
19+
specs "github.com/opencontainers/image-spec/specs-go/v1"
1920
"github.com/pkg/errors"
2021

2122
dblabCfg "gitlab.com/postgres-ai/database-lab/v2/pkg/config"
@@ -241,7 +242,7 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
241242
}
242243

243244
dumpCont, err := d.dockerClient.ContainerCreate(ctx, d.buildContainerConfig(pwd), hostConfig, &network.NetworkingConfig{},
244-
d.dumpContainerName(),
245+
&specs.Platform{}, d.dumpContainerName(),
245246
)
246247
if err != nil {
247248
log.Err(err)

pkg/retrieval/engine/postgres/logical/restore.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/docker/docker/api/types/container"
1818
"github.com/docker/docker/api/types/network"
1919
"github.com/docker/docker/client"
20+
specs "github.com/opencontainers/image-spec/specs-go/v1"
2021
"github.com/pkg/errors"
2122

2223
dblabCfg "gitlab.com/postgres-ai/database-lab/v2/pkg/config"
@@ -152,6 +153,7 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
152153
r.buildContainerConfig(pwd),
153154
hostConfig,
154155
&network.NetworkingConfig{},
156+
&specs.Platform{},
155157
r.restoreContainerName(),
156158
)
157159
if err != nil {

pkg/retrieval/engine/postgres/physical/physical.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/docker/docker/api/types/container"
1919
"github.com/docker/docker/api/types/network"
2020
"github.com/docker/docker/client"
21+
specs "github.com/opencontainers/image-spec/specs-go/v1"
2122
"github.com/pkg/errors"
2223

2324
dblabCfg "gitlab.com/postgres-ai/database-lab/v2/pkg/config"
@@ -266,7 +267,8 @@ func (r *RestoreJob) startContainer(ctx context.Context, containerName string, c
266267
return "", err
267268
}
268269

269-
newContainer, err := r.dockerClient.ContainerCreate(ctx, containerConfig, hostConfig, &network.NetworkingConfig{}, containerName)
270+
newContainer, err := r.dockerClient.ContainerCreate(ctx, containerConfig, hostConfig, &network.NetworkingConfig{},
271+
&specs.Platform{}, containerName)
270272
if err != nil {
271273
return "", errors.Wrapf(err, "failed to create container %s", containerName)
272274
}

pkg/retrieval/engine/postgres/snapshot/logical.go

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/docker/docker/api/types/container"
1515
"github.com/docker/docker/api/types/network"
1616
"github.com/docker/docker/client"
17+
specs "github.com/opencontainers/image-spec/specs-go/v1"
1718
"github.com/pkg/errors"
1819

1920
dblabCfg "gitlab.com/postgres-ai/database-lab/v2/pkg/config"
@@ -181,6 +182,7 @@ func (s *LogicalInitial) runPreprocessingQueries(ctx context.Context, dataDir st
181182
s.buildContainerConfig(dataDir, patchImage, pwd),
182183
hostConfig,
183184
&network.NetworkingConfig{},
185+
&specs.Platform{},
184186
s.patchContainerName(),
185187
)
186188
if err != nil {

pkg/retrieval/engine/postgres/snapshot/physical.go

+29-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/docker/docker/api/types/container"
2323
"github.com/docker/docker/api/types/network"
2424
"github.com/docker/docker/client"
25+
specs "github.com/opencontainers/image-spec/specs-go/v1"
2526
"github.com/pkg/errors"
2627
"github.com/robfig/cron/v3"
2728

@@ -57,7 +58,8 @@ const (
5758
promoteTargetAction = "promote"
5859

5960
// WAL parsing constants.
60-
walNameLen = 24
61+
walNameLen = 24
62+
pgVersion10 = 10
6163
)
6264

6365
var defaultRecoveryCfg = map[string]string{
@@ -533,6 +535,7 @@ func (p *PhysicalInitial) promoteInstance(ctx context.Context, clonePath string,
533535
p.buildContainerConfig(clonePath, promoteImage, pwd, recoveryConfig[targetActionOption]),
534536
hostConfig,
535537
&network.NetworkingConfig{},
538+
&specs.Platform{},
536539
p.promoteContainerName(),
537540
)
538541

@@ -643,15 +646,17 @@ func (p *PhysicalInitial) promoteInstance(ctx context.Context, clonePath string,
643646
func (p *PhysicalInitial) getDSAFromWAL(ctx context.Context, pgVersion float64, containerID, cloneDir string) (string, error) {
644647
log.Dbg(cloneDir)
645648

646-
infos, err := ioutil.ReadDir(path.Join(cloneDir, "pg_wal"))
649+
walDirectory := walDir(cloneDir, pgVersion)
650+
651+
infos, err := ioutil.ReadDir(walDirectory)
647652
if err != nil {
648653
return "", errors.Wrap(err, "failed to read the pg_wal dir")
649654
}
650655

651656
// Walk in the reverse order.
652657
for i := len(infos) - 1; i >= 0; i-- {
653658
fileName := infos[i].Name()
654-
walFilePath := path.Join(cloneDir, "pg_wal", fileName)
659+
walFilePath := path.Join(walDirectory, fileName)
655660

656661
log.Dbg("Look up into file: ", walFilePath)
657662

@@ -670,8 +675,18 @@ func (p *PhysicalInitial) getDSAFromWAL(ctx context.Context, pgVersion float64,
670675
return "", nil
671676
}
672677

678+
func walDir(cloneDir string, pgVersion float64) string {
679+
dir := "pg_wal"
680+
681+
if pgVersion < pgVersion10 {
682+
dir = "pg_xlog"
683+
}
684+
685+
return path.Join(cloneDir, dir)
686+
}
687+
673688
func (p *PhysicalInitial) parseWAL(ctx context.Context, containerID string, pgVersion float64, walFilePath string) string {
674-
cmd := fmt.Sprintf("/usr/lib/postgresql/%g/bin/pg_waldump %s -r Transaction | tail -1", pgVersion, walFilePath)
689+
cmd := walCommand(pgVersion, walFilePath)
675690

676691
output, err := tools.ExecCommandWithOutput(ctx, p.dockerClient, containerID, types.ExecConfig{
677692
Cmd: []string{"sh", "-c", cmd},
@@ -691,6 +706,16 @@ func (p *PhysicalInitial) parseWAL(ctx context.Context, containerID string, pgVe
691706
return parseWALLine(output)
692707
}
693708

709+
func walCommand(pgVersion float64, walFilePath string) string {
710+
walDumpUtil := "pg_waldump"
711+
712+
if pgVersion < pgVersion10 {
713+
walDumpUtil = "pg_xlogdump"
714+
}
715+
716+
return fmt.Sprintf("/usr/lib/postgresql/%g/bin/%s %s -r Transaction | tail -1", pgVersion, walDumpUtil, walFilePath)
717+
}
718+
694719
func parseWALLine(line string) string {
695720
const (
696721
commitToken = "COMMIT"

0 commit comments

Comments
 (0)