/
utils.go
49 lines (40 loc) · 1.06 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package testdb
import (
"context"
"math/rand"
"github.com/testcontainers/testcontainers-go"
)
func extractTestcontainerHostAndPort(container testcontainers.Container) (host string, port string, err error) {
host, err = container.Host(context.Background())
if err != nil {
return
}
natPort, err := container.MappedPort(context.Background(), "5432")
if err != nil {
return
}
port = natPort.Port()
// strip /tcp suffix from port
if port[len(port)-4:] == "/tcp" {
port = port[:len(port)-4]
}
return
}
func createNetwork(networkName string) (testcontainers.Network, error) {
network, err := testcontainers.GenericNetwork(context.Background(),
testcontainers.GenericNetworkRequest{
NetworkRequest: testcontainers.NetworkRequest{
Name: networkName,
CheckDuplicate: true,
},
})
return network, err
}
func randomLowercaseLetterString(length int) string {
const letters = "abcdefghijklmnopqrstuvwxyz"
result := make([]byte, length)
for i := 0; i < length; i++ {
result[i] = letters[rand.Intn(len(letters))]
}
return string(result)
}