-
Notifications
You must be signed in to change notification settings - Fork 342
/
cosmosdb.go
86 lines (75 loc) · 2.49 KB
/
cosmosdb.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package testutil
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
)
const (
CosmosDBLocalPort = "8081"
)
var cosmosdbLocalURI string
func GetCosmosDBInstance() (string, func(), error) {
dockerPool, err := dockertest.NewPool("")
if err != nil {
return "", nil, fmt.Errorf("could not connect to Docker: %w", err)
}
cosmosdbDockerRunOptions := &dockertest.RunOptions{
Repository: "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator",
Tag: "latest",
Env: []string{"AZURE_COSMOS_EMULATOR_PARTITION_COUNT=5",
"AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true",
"AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=127.0.0.1"},
PortBindings: map[docker.Port][]docker.PortBinding{
"8081/tcp": {{HostPort: CosmosDBLocalPort}},
"10251/tcp": {{HostPort: "10251"}},
"10252/tcp": {{HostPort: "10252"}},
"10253/tcp": {{HostPort: "10253"}},
"10254/tcp": {{HostPort: "10254"}},
"10255/tcp": {{HostPort: "10255"}},
},
ExposedPorts: []string{CosmosDBLocalPort, "10251", "10252", "10253", "10254", "10255"},
}
resource, err := dockerPool.RunWithOptions(cosmosdbDockerRunOptions)
if err != nil {
return "", nil, fmt.Errorf("could not start cosmosdb emulator: %w", err)
}
cosmosdbLocalURI = "https://localhost:" + resource.GetPort("8081/tcp")
// set cleanup
closer := func() {
err = dockerPool.Purge(resource)
if err != nil {
panic("could not kill cosmosdb local container")
}
}
// expire, just to make sure
err = resource.Expire(dbContainerTimeoutSeconds)
if err != nil {
return "", nil, fmt.Errorf("could not expire cosmosdb local emulator: %w", err)
}
p, err := url.JoinPath(cosmosdbLocalURI, "/_explorer/emulator.pem")
if err != nil {
return "", nil, fmt.Errorf("joining urls: %w", err)
}
err = dockerPool.Retry(func() error {
// waiting for cosmosdb container to be ready by issuing an HTTP get request with
// exponential backoff retry. The response is not really meaningful for that case
// and so is ignored
client := http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec // ignore self-signed cert for local testing using the emulator
}}
resp, err := client.Get(p)
if err != nil {
return err
}
_ = resp.Body.Close()
return nil
})
if err != nil {
return "", nil, fmt.Errorf("could not connect to cosmosdb emulator at %s: %w", cosmosdbLocalURI, err)
}
// return DB URI
return cosmosdbLocalURI, closer, nil
}