Skip to content

Commit f45558d

Browse files
refactor: Remove consul sdk dep
Some postgres test uses a helper function from the consul sdk to get a free network port to start a postgres container instance; to reduce code dependencies, we let the container runtime to select a free port. Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
1 parent 892f844 commit f45558d

File tree

8 files changed

+199
-186
lines changed

8 files changed

+199
-186
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ require (
1919
github.com/gorilla/mux v1.8.1
2020
github.com/gorilla/websocket v1.5.3
2121
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
22-
github.com/hashicorp/consul/sdk v0.16.1
2322
github.com/hyperledger/fabric v1.4.0-rc1.0.20250510200036-435a7f1a780a
2423
github.com/hyperledger/fabric-chaincode-go/v2 v2.3.0
2524
github.com/hyperledger/fabric-contract-api-go/v2 v2.2.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,8 +1054,6 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4Zs
10541054
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w=
10551055
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
10561056
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I=
1057-
github.com/hashicorp/consul/sdk v0.16.1 h1:V8TxTnImoPD5cj0U9Spl0TUxcytjcbbJeADFF07KdHg=
1058-
github.com/hashicorp/consul/sdk v0.16.1/go.mod h1:fSXvwxB2hmh1FMZCNl6PwX0Q/1wdWtHJcZ7Ea5tns0s=
10591057
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
10601058
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
10611059
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=

integration/nwo/fsc/fsc.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package fsc
99
import (
1010
"bytes"
1111
"cmp"
12+
"context"
1213
"fmt"
1314
"io"
1415
"net"
@@ -213,19 +214,20 @@ func (p *Platform) Members() []grouper.Member {
213214

214215
func (p *Platform) PreRun() {
215216
// Start DBs
216-
configs := map[string]*postgres2.ContainerConfig{}
217+
configs := make(map[string]*postgres2.ContainerConfig)
218+
// update data sources
217219
for _, node := range p.Peers {
218220
for _, sqlOpts := range node.Options.GetPostgresPersistences() {
219221
if _, ok := configs[sqlOpts.DataSource]; !ok {
220-
c, err := postgres2.ReadDataSource(sqlOpts.DataSource)
222+
c, err := postgres2.ConfigFromDataSource(sqlOpts.DataSource)
221223
gomega.Expect(err).ToNot(gomega.HaveOccurred())
222224
configs[sqlOpts.DataSource] = c
223225
}
224226

225227
}
226228
}
227229
logger.Infof("Starting DBs for following data sources: [%s]...", logging.Keys(configs))
228-
closeF, err := postgres2.StartPostgresWithFmt(collections.Values(configs))
230+
closeF, err := postgres2.StartMultiplePostgres(context.TODO(), collections.Values(configs))
229231
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "failed to start dbs")
230232
p.cleanDB = closeF
231233
}

platform/fabric/services/storage/vault/helpers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77
package vault
88

99
import (
10+
"context"
1011
"fmt"
1112
"path"
1213

@@ -33,14 +34,14 @@ func OpenSqliteVault(key, tempDir string) (driver.VaultStore, error) {
3334
}
3435

3536
func OpenPostgresVault(name string) (driver.VaultStore, func(), error) {
36-
postgresConfig := postgres3.DefaultConfig(fmt.Sprintf("%s-db", name))
37-
terminate, err := postgres3.StartPostgresWithFmt([]*postgres3.ContainerConfig{postgresConfig})
37+
cfg := postgres3.DefaultConfig(fmt.Sprintf("%s-db", name))
38+
terminate, pgConnStr, err := postgres3.StartPostgres(context.TODO(), cfg, nil)
3839
if err != nil {
3940
return nil, nil, err
4041
}
4142

4243
cp := postgres3.NewConfigProvider(testing.MockConfig(postgres3.Config{
43-
DataSource: postgresConfig.DataSource(),
44+
DataSource: pgConnStr,
4445
MaxOpenConns: 50,
4546
}))
4647
persistence, err := postgres2.NewPersistenceWithOpts(cp, postgres3.NewDbProvider(), "", postgres2.NewVaultStore)

platform/view/services/storage/driver/sql/postgres/bench_test.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ import (
1616
)
1717

1818
func BenchmarkReadExistingPostgres(b *testing.B) {
19-
terminate, pgConnStr, err := StartPostgres(b, false)
20-
if err != nil {
21-
b.Fatal(err)
22-
}
23-
defer terminate()
19+
pgConnStr := setupDB(b)
2420
cp := NewConfigProvider(testing2.MockConfig(Config{
2521
DataSource: pgConnStr,
2622
MaxOpenConns: 50,
@@ -35,12 +31,7 @@ func BenchmarkReadExistingPostgres(b *testing.B) {
3531
}
3632

3733
func BenchmarkReadNonExistingPostgres(b *testing.B) {
38-
terminate, pgConnStr, err := StartPostgres(b, false)
39-
if err != nil {
40-
b.Fatal(err)
41-
}
42-
defer terminate()
43-
34+
pgConnStr := setupDB(b)
4435
cp := NewConfigProvider(testing2.MockConfig(Config{
4536
DataSource: pgConnStr,
4637
MaxOpenConns: 50,
@@ -55,12 +46,7 @@ func BenchmarkReadNonExistingPostgres(b *testing.B) {
5546
}
5647

5748
func BenchmarkWriteOnePostgres(b *testing.B) {
58-
terminate, pgConnStr, err := StartPostgres(b, false)
59-
if err != nil {
60-
b.Fatal(err)
61-
}
62-
defer terminate()
63-
49+
pgConnStr := setupDB(b)
6450
cp := NewConfigProvider(testing2.MockConfig(Config{
6551
DataSource: pgConnStr,
6652
MaxOpenConns: 50,
@@ -75,11 +61,7 @@ func BenchmarkWriteOnePostgres(b *testing.B) {
7561
}
7662

7763
func BenchmarkWriteManyPostgres(b *testing.B) {
78-
terminate, pgConnStr, err := StartPostgres(b, false)
79-
if err != nil {
80-
b.Fatal(err)
81-
}
82-
defer terminate()
64+
pgConnStr := setupDB(b)
8365
cp := NewConfigProvider(testing2.MockConfig(Config{
8466
DataSource: pgConnStr,
8567
MaxOpenConns: 50,
@@ -94,11 +76,7 @@ func BenchmarkWriteManyPostgres(b *testing.B) {
9476
}
9577

9678
func BenchmarkWriteManyPostgresWithIdle(b *testing.B) {
97-
terminate, pgConnStr, err := StartPostgres(b, false)
98-
if err != nil {
99-
b.Fatal(err)
100-
}
101-
defer terminate()
79+
pgConnStr := setupDB(b)
10280
cp := NewConfigProvider(testing2.MockConfig(Config{
10381
DataSource: pgConnStr,
10482
MaxOpenConns: 50,

platform/view/services/storage/driver/sql/postgres/sql_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ import (
1616
_ "modernc.org/sqlite"
1717
)
1818

19-
func TestPostgres(t *testing.T) {
20-
t.Log("starting postgres")
21-
terminate, pgConnStr, err := StartPostgres(t, false)
19+
func setupDB(tb testing.TB) string {
20+
tb.Helper()
21+
22+
terminate, pgConnStr, err := StartPostgres(tb.Context(), ConfigFromEnv(), nil)
2223
if err != nil {
23-
t.Fatal(err)
24+
tb.Fatal(err)
2425
}
25-
defer terminate()
26+
tb.Cleanup(terminate)
27+
28+
return pgConnStr
29+
}
30+
31+
func TestPostgres(t *testing.T) {
32+
pgConnStr := setupDB(t)
2633
t.Log("postgres ready")
2734

2835
cp := NewConfigProvider(testing2.MockConfig(Config{

0 commit comments

Comments
 (0)