Skip to content

Commit fa5b104

Browse files
authored
Add config file support to integration tests (#2167)
* Added query-frontend support to local tsdb-blocks-storage-s3 dev env Signed-off-by: Marco Pracucci <[email protected]> * Introduced CortexService to have cleaner integration tests Signed-off-by: Marco Pracucci <[email protected]> * Added to writeFileToSharedDir() the ability to create the path of directories Signed-off-by: Marco Pracucci <[email protected]> * Enhanced query-frontend tests to run with config file too Signed-off-by: Marco Pracucci <[email protected]>
1 parent 82eae45 commit fa5b104

15 files changed

+294
-79
lines changed

development/tsdb-blocks-storage-s3/config/cortex.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,6 @@ compactor:
6666
store: consul
6767
consul:
6868
host: consul:8500
69+
70+
frontend_worker:
71+
address: "query-frontend:9007"

development/tsdb-blocks-storage-s3/docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,17 @@ services:
108108
- 8006:8006
109109
volumes:
110110
- ./config:/cortex/config
111+
112+
query-frontend:
113+
build:
114+
context: .
115+
dockerfile: dev.dockerfile
116+
image: cortex
117+
command: ["sh", "-c", "sleep 3 && exec ./cortex -config.file=./config/cortex.yaml -target=query-frontend -server.http-listen-port=8007 -server.grpc-listen-port=9007"]
118+
depends_on:
119+
- consul
120+
- minio
121+
ports:
122+
- 8007:8007
123+
volumes:
124+
- ./config:/cortex/config

integration/alertmanager_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package main
22

33
import (
44
"context"
5-
"io/ioutil"
6-
"os"
7-
"path/filepath"
85
"testing"
96

107
"github.com/stretchr/testify/require"
@@ -18,16 +15,9 @@ func TestAlertmanager(t *testing.T) {
1815
require.NoError(t, err)
1916
defer s.Close()
2017

21-
alertmanagerDir := filepath.Join(s.SharedDir(), "alertmanager_configs")
22-
require.NoError(t, os.Mkdir(alertmanagerDir, os.ModePerm))
18+
require.NoError(t, writeFileToSharedDir(s, "alertmanager_configs/user-1.yaml", []byte(cortexAlertmanagerUserConfigYaml)))
2319

24-
require.NoError(t, ioutil.WriteFile(
25-
filepath.Join(alertmanagerDir, "user-1.yaml"),
26-
[]byte(cortexAlertmanagerUserConfigYaml),
27-
os.ModePerm),
28-
)
29-
30-
alertmanager := e2ecortex.NewAlertmanager("alertmanager", AlertmanagerConfigs, "")
20+
alertmanager := e2ecortex.NewAlertmanager("alertmanager", AlertmanagerFlags, "")
3121
require.NoError(t, s.StartAndWaitReady(alertmanager))
3222
require.NoError(t, alertmanager.WaitSumMetrics(e2e.Equals(1), "cortex_alertmanager_configs"))
3323

integration/api_config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestConfigAPIEndpoint(t *testing.T) {
2525
"-config.file": filepath.Join(e2e.ContainerSharedDir, cortexConfigFile),
2626
}
2727

28-
cortex1 := e2ecortex.NewSingleBinary("cortex-1", flags, "", 9009)
28+
cortex1 := e2ecortex.NewSingleBinary("cortex-1", flags, "", 9009, 9095)
2929
require.NoError(t, s.StartAndWaitReady(cortex1))
3030

3131
// Get config from /config API endpoint.
@@ -40,6 +40,6 @@ func TestConfigAPIEndpoint(t *testing.T) {
4040
// Start again Cortex in single binary with the exported config
4141
// and ensure it starts (pass the readiness probe).
4242
require.NoError(t, writeFileToSharedDir(s, cortexConfigFile, body))
43-
cortex2 := e2ecortex.NewSingleBinary("cortex-2", flags, "", 9009)
43+
cortex2 := e2ecortex.NewSingleBinary("cortex-2", flags, "", 9009, 9095)
4444
require.NoError(t, s.StartAndWaitReady(cortex2))
4545
}

integration/backward_compatibility_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ func TestBackwardCompatibilityWithChunksStorage(t *testing.T) {
3131

3232
// Start Cortex components (ingester running on previous version).
3333
require.NoError(t, writeFileToSharedDir(s, cortexSchemaConfigFile, []byte(cortexSchemaConfigYaml)))
34-
tableManager := e2ecortex.NewTableManager("table-manager", ChunksStorage, previousVersionImage)
35-
ingester1 := e2ecortex.NewIngester("ingester-1", consul.NetworkHTTPEndpoint(), ChunksStorage, "")
36-
distributor := e2ecortex.NewDistributor("distributor", consul.NetworkHTTPEndpoint(), ChunksStorage, "")
34+
tableManager := e2ecortex.NewTableManager("table-manager", ChunksStorageFlags, previousVersionImage)
35+
ingester1 := e2ecortex.NewIngester("ingester-1", consul.NetworkHTTPEndpoint(), ChunksStorageFlags, "")
36+
distributor := e2ecortex.NewDistributor("distributor", consul.NetworkHTTPEndpoint(), ChunksStorageFlags, "")
3737
require.NoError(t, s.StartAndWaitReady(distributor, ingester1, tableManager))
3838

3939
// Wait until the first table-manager sync has completed, so that we're
@@ -54,7 +54,7 @@ func TestBackwardCompatibilityWithChunksStorage(t *testing.T) {
5454
require.NoError(t, err)
5555
require.Equal(t, 200, res.StatusCode)
5656

57-
ingester2 := e2ecortex.NewIngester("ingester-2", consul.NetworkHTTPEndpoint(), mergeFlags(ChunksStorage, map[string]string{
57+
ingester2 := e2ecortex.NewIngester("ingester-2", consul.NetworkHTTPEndpoint(), mergeFlags(ChunksStorageFlags, map[string]string{
5858
"-ingester.join-after": "10s",
5959
}), "")
6060
// Start ingester-2 on new version, to ensure the transfer is backward compatible.
@@ -66,7 +66,7 @@ func TestBackwardCompatibilityWithChunksStorage(t *testing.T) {
6666

6767
// Query the new ingester both with the old and the new querier.
6868
for _, image := range []string{previousVersionImage, ""} {
69-
querier := e2ecortex.NewQuerier("querier", consul.NetworkHTTPEndpoint(), ChunksStorage, image)
69+
querier := e2ecortex.NewQuerier("querier", consul.NetworkHTTPEndpoint(), ChunksStorageFlags, image)
7070
require.NoError(t, s.StartAndWaitReady(querier))
7171

7272
// Wait until the querier has updated the ring.

integration/configs.go

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"fmt"
55
"path/filepath"
6+
"strings"
7+
"text/template"
68

79
"github.com/cortexproject/cortex/integration/e2e"
810
e2edb "github.com/cortexproject/cortex/integration/e2e/db"
@@ -33,13 +35,13 @@ receivers:
3335
)
3436

3537
var (
36-
AlertmanagerConfigs = map[string]string{
38+
AlertmanagerFlags = map[string]string{
3739
"-alertmanager.storage.local.path": filepath.Join(e2e.ContainerSharedDir, "alertmanager_configs"),
3840
"-alertmanager.storage.type": "local",
3941
"-alertmanager.web.external-url": "http://localhost/api/prom",
4042
}
4143

42-
BlocksStorage = map[string]string{
44+
BlocksStorageFlags = map[string]string{
4345
"-store.engine": "tsdb",
4446
"-experimental.tsdb.backend": "s3",
4547
"-experimental.tsdb.block-ranges-period": "1m",
@@ -53,10 +55,90 @@ var (
5355
"-experimental.tsdb.s3.insecure": "true",
5456
}
5557

56-
ChunksStorage = map[string]string{
58+
BlocksStorageConfig = buildConfigFromTemplate(`
59+
storage:
60+
engine: tsdb
61+
62+
tsdb:
63+
backend: s3
64+
block_ranges_period: ["1m"]
65+
retention_period: 5m
66+
ship_interval: 1m
67+
68+
bucket_store:
69+
sync_interval: 5s
70+
71+
s3:
72+
bucket_name: cortex
73+
access_key_id: {{.MinioAccessKey}}
74+
secret_access_key: {{.MinioSecretKey}}
75+
endpoint: {{.MinioEndpoint}}
76+
insecure: true
77+
`, struct {
78+
MinioAccessKey string
79+
MinioSecretKey string
80+
MinioEndpoint string
81+
}{
82+
MinioAccessKey: e2edb.MinioAccessKey,
83+
MinioSecretKey: e2edb.MinioSecretKey,
84+
MinioEndpoint: fmt.Sprintf("%s-minio-9000:9000", networkName),
85+
})
86+
87+
ChunksStorageFlags = map[string]string{
5788
"-dynamodb.url": fmt.Sprintf("dynamodb://u:p@%s-dynamodb.:8000", networkName),
5889
"-dynamodb.poll-interval": "1m",
5990
"-config-yaml": filepath.Join(e2e.ContainerSharedDir, cortexSchemaConfigFile),
6091
"-table-manager.retention-period": "168h",
6192
}
93+
94+
ChunksStorageConfig = buildConfigFromTemplate(`
95+
storage:
96+
aws:
97+
dynamodbconfig:
98+
dynamodb: {{.DynamoDBURL}}
99+
100+
table_manager:
101+
dynamodb_poll_interval: 1m
102+
retention_period: 168h
103+
104+
schema:
105+
{{.SchemaConfig}}
106+
`, struct {
107+
DynamoDBURL string
108+
SchemaConfig string
109+
}{
110+
DynamoDBURL: fmt.Sprintf("dynamodb://u:p@%s-dynamodb.:8000", networkName),
111+
SchemaConfig: indentConfig(cortexSchemaConfigYaml, 2),
112+
})
62113
)
114+
115+
func buildConfigFromTemplate(tmpl string, data interface{}) string {
116+
t, err := template.New("config").Parse(tmpl)
117+
if err != nil {
118+
panic(err)
119+
}
120+
121+
w := &strings.Builder{}
122+
if err = t.Execute(w, data); err != nil {
123+
panic(err)
124+
}
125+
126+
return w.String()
127+
}
128+
129+
func indentConfig(config string, indentation int) string {
130+
output := strings.Builder{}
131+
132+
for _, line := range strings.Split(config, "\n") {
133+
if line == "" {
134+
output.WriteString("\n")
135+
continue
136+
}
137+
138+
output.WriteString(strings.Repeat(" ", indentation))
139+
output.WriteString(line)
140+
output.WriteString("\n")
141+
}
142+
143+
return output.String()
144+
}

integration/e2e/util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ func RunCommandAndGetOutput(name string, args ...string) ([]byte, error) {
1717
return cmd.CombinedOutput()
1818
}
1919

20+
func EmptyFlags() map[string]string {
21+
return map[string]string{}
22+
}
23+
2024
func MergeFlags(inputs ...map[string]string) map[string]string {
2125
output := map[string]string{}
2226

integration/e2ecortex/service.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package e2ecortex
2+
3+
import "github.com/cortexproject/cortex/integration/e2e"
4+
5+
// CortexService represents a Cortex service with at least an HTTP and GRPC port exposed.
6+
type CortexService struct {
7+
*e2e.HTTPService
8+
9+
grpcPort int
10+
}
11+
12+
func NewCortexService(
13+
name string,
14+
image string,
15+
command *e2e.Command,
16+
readiness *e2e.ReadinessProbe,
17+
httpPort int,
18+
grpcPort int,
19+
otherPorts ...int,
20+
) *CortexService {
21+
return &CortexService{
22+
HTTPService: e2e.NewHTTPService(name, image, command, readiness, httpPort, otherPorts...),
23+
grpcPort: grpcPort,
24+
}
25+
}
26+
27+
func (s *CortexService) GRPCEndpoint() string {
28+
return s.Endpoint(s.grpcPort)
29+
}
30+
31+
func (s *CortexService) NetworkGRPCEndpoint() string {
32+
return s.NetworkEndpoint(s.grpcPort)
33+
}

0 commit comments

Comments
 (0)