Skip to content

Commit 780d438

Browse files
authored
PBM-1455: oplog restore tests with tc (#1075)
Use testcontainers for unit tests with PSMDB: * Add testcontainers dependency * Add tests for oplog db logic using testcontainers * Use PSMDB as tc image
1 parent 407221c commit 780d438

File tree

698 files changed

+95157
-1366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

698 files changed

+95157
-1366
lines changed

go.mod

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,72 @@ require (
1818
github.com/pkg/errors v0.9.1
1919
github.com/spf13/cobra v1.8.1
2020
github.com/spf13/viper v1.19.0
21+
github.com/testcontainers/testcontainers-go v0.34.0
22+
github.com/testcontainers/testcontainers-go/modules/mongodb v0.34.0
2123
go.mongodb.org/mongo-driver v1.17.1
2224
golang.org/x/mod v0.19.0
2325
golang.org/x/sync v0.10.0
2426
gopkg.in/yaml.v2 v2.4.0
2527
)
2628

2729
require (
30+
dario.cat/mergo v1.0.0 // indirect
2831
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 // indirect
29-
github.com/Microsoft/go-winio v0.4.14 // indirect
32+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
33+
github.com/Microsoft/go-winio v0.6.2 // indirect
3034
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
3135
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
36+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
37+
github.com/containerd/containerd v1.7.18 // indirect
3238
github.com/containerd/log v0.1.0 // indirect
39+
github.com/containerd/platforms v0.2.1 // indirect
40+
github.com/cpuguy83/dockercfg v0.3.2 // indirect
3341
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
34-
github.com/distribution/reference v0.5.0 // indirect
42+
github.com/distribution/reference v0.6.0 // indirect
3543
github.com/docker/go-connections v0.5.0 // indirect
3644
github.com/docker/go-units v0.5.0 // indirect
3745
github.com/felixge/httpsnoop v1.0.4 // indirect
3846
github.com/go-logr/logr v1.4.1 // indirect
3947
github.com/go-logr/stdr v1.2.2 // indirect
48+
github.com/go-ole/go-ole v1.2.6 // indirect
4049
github.com/gogo/protobuf v1.3.2 // indirect
4150
github.com/hashicorp/hcl v1.0.0 // indirect
4251
github.com/inconshreveable/mousetrap v1.1.0 // indirect
4352
github.com/jessevdk/go-flags v1.5.0 // indirect
4453
github.com/jmespath/go-jmespath v0.4.0 // indirect
54+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
4555
github.com/magiconair/properties v1.8.7 // indirect
4656
github.com/mitchellh/mapstructure v1.5.0 // indirect
4757
github.com/moby/docker-image-spec v1.3.1 // indirect
58+
github.com/moby/patternmatcher v0.6.0 // indirect
59+
github.com/moby/sys/sequential v0.5.0 // indirect
60+
github.com/moby/sys/user v0.1.0 // indirect
4861
github.com/moby/term v0.5.0 // indirect
4962
github.com/montanaflynn/stats v0.7.1 // indirect
5063
github.com/morikuni/aec v1.0.0 // indirect
5164
github.com/opencontainers/go-digest v1.0.0 // indirect
5265
github.com/opencontainers/image-spec v1.1.0 // indirect
5366
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
67+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
68+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
5469
github.com/sagikazarmark/locafero v0.4.0 // indirect
5570
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
71+
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
72+
github.com/shoenig/go-m1cpu v0.1.6 // indirect
73+
github.com/sirupsen/logrus v1.9.3 // indirect
5674
github.com/sourcegraph/conc v0.3.0 // indirect
5775
github.com/spf13/afero v1.11.0 // indirect
5876
github.com/spf13/cast v1.6.0 // indirect
5977
github.com/spf13/pflag v1.0.5 // indirect
78+
github.com/stretchr/testify v1.9.0 // indirect
6079
github.com/subosito/gotenv v1.6.0 // indirect
80+
github.com/tklauser/go-sysconf v0.3.12 // indirect
81+
github.com/tklauser/numcpus v0.6.1 // indirect
6182
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
6283
github.com/xdg-go/scram v1.1.2 // indirect
6384
github.com/xdg-go/stringprep v1.0.4 // indirect
6485
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
86+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
6587
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
6688
go.opentelemetry.io/otel v1.24.0 // indirect
6789
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 // indirect
@@ -72,11 +94,10 @@ require (
7294
go.uber.org/multierr v1.9.0 // indirect
7395
golang.org/x/crypto v0.31.0 // indirect
7496
golang.org/x/exp v0.0.0-20240529005216-23cca8864a10 // indirect
75-
golang.org/x/net v0.25.0 // indirect
97+
golang.org/x/net v0.26.0 // indirect
7698
golang.org/x/sys v0.28.0 // indirect
7799
golang.org/x/term v0.27.0 // indirect
78100
golang.org/x/text v0.21.0 // indirect
79101
gopkg.in/ini.v1 v1.67.0 // indirect
80102
gopkg.in/yaml.v3 v3.0.1 // indirect
81-
gotest.tools/v3 v3.5.1 // indirect
82103
)

go.sum

Lines changed: 65 additions & 23 deletions
Large diffs are not rendered by default.

pbm/oplog/db_tc_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package oplog
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"testing"
8+
9+
"github.com/testcontainers/testcontainers-go"
10+
"github.com/testcontainers/testcontainers-go/modules/mongodb"
11+
"go.mongodb.org/mongo-driver/bson"
12+
"go.mongodb.org/mongo-driver/mongo"
13+
"go.mongodb.org/mongo-driver/mongo/options"
14+
)
15+
16+
var mClient *mongo.Client
17+
18+
func TestMain(m *testing.M) {
19+
ctx := context.Background()
20+
mongodbContainer, err := mongodb.Run(ctx, "perconalab/percona-server-mongodb:7")
21+
if err != nil {
22+
fmt.Fprintf(os.Stderr, "error while creating mongo test container: %v", err)
23+
return
24+
}
25+
defer func() {
26+
if err := testcontainers.TerminateContainer(mongodbContainer); err != nil {
27+
fmt.Fprintf(os.Stderr, "failed to terminate container: %s", err)
28+
}
29+
}()
30+
31+
connStr, err := mongodbContainer.ConnectionString(ctx)
32+
if err != nil {
33+
fmt.Fprintf(os.Stderr, "conn string error: %v", err)
34+
return
35+
}
36+
mClient, err = mongo.Connect(ctx, options.Client().ApplyURI(connStr))
37+
if err != nil {
38+
fmt.Fprintf(os.Stderr, "mongo client connect error: %v", err)
39+
return
40+
}
41+
42+
m.Run()
43+
}
44+
45+
func TestGetUUIDForNSv2(t *testing.T) {
46+
t.Run("uuid for existing collection", func(t *testing.T) {
47+
db := newMDB(mClient)
48+
49+
tDB, tColl := "my_test_db", "my_test_coll"
50+
err := mClient.Database(tDB).CreateCollection(context.Background(), tColl)
51+
if err != nil {
52+
t.Errorf("create collection err: %v", err)
53+
}
54+
55+
uuid, err := db.getUUIDForNS(context.Background(), fmt.Sprintf("%s.%s", tDB, tColl))
56+
if err != nil {
57+
t.Errorf("got err=%v", err)
58+
}
59+
if uuid.IsZero() {
60+
t.Error("expected to get uuid for collection")
61+
}
62+
})
63+
64+
t.Run("uuid for not existing collection", func(t *testing.T) {
65+
db := newMDB(mClient)
66+
67+
tDB, tColl := "xDB", "yColl"
68+
uuid, err := db.getUUIDForNS(context.Background(), fmt.Sprintf("%s.%s", tDB, tColl))
69+
if err != nil {
70+
t.Errorf("got err=%v", err)
71+
}
72+
if !uuid.IsZero() {
73+
t.Errorf("expected to get zero value for uuid for not existing collection, got=%v", uuid)
74+
}
75+
})
76+
}
77+
78+
func TestApplyOps(t *testing.T) {
79+
db := newMDB(mClient)
80+
81+
tDB, tColl := "tAODB", "dAOColl"
82+
if _, err := mClient.Database(tDB).Collection(tColl).InsertOne(context.Background(), bson.D{}); err != nil {
83+
t.Errorf("insert doc err: %v", err)
84+
}
85+
iOps := createInsertSimpleOp(t, fmt.Sprintf("%s.%s", tDB, tColl))
86+
87+
err := db.applyOps([]any{iOps})
88+
if err != nil {
89+
t.Fatalf("error when using applyOps, err=%v", err)
90+
}
91+
cnt, err := mClient.Database(tDB).Collection(tColl).CountDocuments(context.Background(), bson.D{})
92+
if err != nil {
93+
t.Fatalf("error when counting docs within new collection, err=%v", err)
94+
}
95+
if cnt != 2 {
96+
t.Fatalf("wrong number of docs in new collection, got=%d, want=1", cnt)
97+
}
98+
}

pbm/oplog/restore_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,62 @@ func createInsertOp(t *testing.T, ns string) *db.Oplog {
427427
return replaceNsWithinOpEntry(t, iOpJSON, ns)
428428
}
429429

430+
func createInsertSimpleOp(t *testing.T, ns string) *db.Oplog {
431+
t.Helper()
432+
iOpJSON := `
433+
{
434+
"op": "i",
435+
"ns": "db.coll",
436+
"o": {
437+
"_id": {
438+
"$oid": "6747008178d82a2b1134a2b8"
439+
},
440+
"d": {
441+
"$numberInt": "6"
442+
},
443+
"desc": "doc-6"
444+
},
445+
"o2": {
446+
"_id": {
447+
"$oid": "6747008178d82a2b1134a2b8"
448+
}
449+
},
450+
"stmtId": {
451+
"$numberInt": "0"
452+
},
453+
"ts": {
454+
"$timestamp": {
455+
"t": 1732706433,
456+
"i": 1
457+
}
458+
},
459+
"t": {
460+
"$numberLong": "2"
461+
},
462+
"v": {
463+
"$numberLong": "2"
464+
},
465+
"wall": {
466+
"$date": {
467+
"$numberLong": "1732706433987"
468+
}
469+
},
470+
"prevOpTime": {
471+
"ts": {
472+
"$timestamp": {
473+
"t": 0,
474+
"i": 0
475+
}
476+
},
477+
"t": {
478+
"$numberLong": "-1"
479+
}
480+
}
481+
}`
482+
483+
return replaceNsWithinOpEntry(t, iOpJSON, ns)
484+
}
485+
430486
func createUpdateOp(t *testing.T, ns string) *db.Oplog {
431487
t.Helper()
432488

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version = 1
2+
3+
test_patterns = [
4+
"*_test.go"
5+
]
6+
7+
[[analyzers]]
8+
name = "go"
9+
enabled = true
10+
11+
[analyzers.meta]
12+
import_path = "dario.cat/mergo"

vendor/dario.cat/mergo/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#### joe made this: http://goel.io/joe
2+
3+
#### go ####
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary, build with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
17+
.glide/
18+
19+
#### vim ####
20+
# Swap
21+
[._]*.s[a-v][a-z]
22+
[._]*.sw[a-p]
23+
[._]s[a-v][a-z]
24+
[._]sw[a-p]
25+
26+
# Session
27+
Session.vim
28+
29+
# Temporary
30+
.netrwhist
31+
*~
32+
# Auto-generated tag files
33+
tags

vendor/dario.cat/mergo/.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: go
2+
arch:
3+
- amd64
4+
- ppc64le
5+
install:
6+
- go get -t
7+
- go get golang.org/x/tools/cmd/cover
8+
- go get github.com/mattn/goveralls
9+
script:
10+
- go test -race -v ./...
11+
after_script:
12+
- $HOME/gopath/bin/goveralls -service=travis-ci -repotoken $COVERALLS_TOKEN
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [email protected]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

0 commit comments

Comments
 (0)