Skip to content

Commit ea9ae11

Browse files
committed
Test against Elastic 7 and 8
1 parent 334038a commit ea9ae11

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

.github/workflows/ci.yml

+19-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ jobs:
66
test:
77
name: Test
88
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
elastic-port: [9207, 9208]
912

1013
services:
1114
postgres:
@@ -17,13 +20,18 @@ jobs:
1720
ports:
1821
- 5432:5432
1922
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
20-
elastic:
21-
image: elasticsearch:7.17.9
22-
ports:
23-
- 9200:9200
24-
- 9300:9300
25-
env:
26-
discovery.type: single-node
23+
elastic7:
24+
image: elasticsearch:7.17.9
25+
ports:
26+
- 9207:9200
27+
env:
28+
discovery.type: single-node
29+
elastic8:
30+
image: elasticsearch:8.13.4
31+
ports:
32+
- 9208:9200
33+
env:
34+
discovery.type: single-node
2735

2836
steps:
2937
- name: Checkout code
@@ -34,6 +42,10 @@ jobs:
3442
with:
3543
go-version: ${{ env.go-version }}
3644

45+
- name: Setup environment
46+
run: |
47+
echo "INDEXER_ELASTIC_URL=http://localhost:${{ matrix.elastic-port }}" >> "$GITHUB_ENV"
48+
3749
- name: Run tests
3850
run: go test -p=1 -coverprofile=coverage.text -covermode=atomic ./...
3951

indexers/base_test.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,47 @@ import (
1515

1616
"github.com/nyaruka/gocommon/httpx"
1717
"github.com/nyaruka/gocommon/jsonx"
18+
indexer "github.com/nyaruka/rp-indexer/v9"
1819
"github.com/nyaruka/rp-indexer/v9/indexers"
1920
"github.com/stretchr/testify/assert"
2021
"github.com/stretchr/testify/require"
2122
)
2223

23-
const elasticURL = "http://localhost:9200"
2424
const aliasName = "indexer_test"
2525

26-
func setup(t *testing.T) *sql.DB {
26+
func setup(t *testing.T) (*indexer.Config, *sql.DB) {
27+
cfg := indexer.NewDefaultConfig()
28+
cfg.DB = "postgres://indexer_test:temba@localhost:5432/indexer_test?sslmode=disable"
29+
cfg.ElasticURL = os.Getenv("INDEXER_ELASTIC_URL")
30+
if cfg.ElasticURL == "" {
31+
cfg.ElasticURL = "http://localhost:9200"
32+
}
33+
2734
testDB, err := os.ReadFile("../testdb.sql")
2835
require.NoError(t, err)
2936

30-
db, err := sql.Open("postgres", "postgres://indexer_test:temba@localhost:5432/indexer_test?sslmode=disable")
37+
db, err := sql.Open("postgres", cfg.DB)
3138
require.NoError(t, err)
3239

3340
_, err = db.Exec(string(testDB))
3441
require.NoError(t, err)
3542

3643
// delete all indexes with our alias prefix
37-
existing := elasticRequest(t, http.MethodGet, "/_aliases", nil)
44+
existing := elasticRequest(t, cfg, http.MethodGet, "/_aliases", nil)
3845

3946
for name := range existing {
4047
if strings.HasPrefix(name, aliasName) {
41-
elasticRequest(t, http.MethodDelete, "/"+name, nil)
48+
elasticRequest(t, cfg, http.MethodDelete, "/"+name, nil)
4249
}
4350
}
4451

4552
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
4653

47-
return db
54+
return cfg, db
4855
}
4956

50-
func assertQuery(t *testing.T, query []byte, expected []int64, msgAndArgs ...interface{}) {
51-
results := elasticRequest(t, http.MethodPost, "/"+aliasName+"/_search",
57+
func assertQuery(t *testing.T, cfg *indexer.Config, query []byte, expected []int64, msgAndArgs ...interface{}) {
58+
results := elasticRequest(t, cfg, http.MethodPost, "/"+aliasName+"/_search",
5259
map[string]any{"query": json.RawMessage(query), "sort": []map[string]any{{"id": "asc"}}},
5360
)
5461
hits := results["hits"].(map[string]any)["hits"].([]any)
@@ -63,8 +70,8 @@ func assertQuery(t *testing.T, query []byte, expected []int64, msgAndArgs ...int
6370
assert.Equal(t, expected, actual, msgAndArgs...)
6471
}
6572

66-
func assertIndexesWithPrefix(t *testing.T, prefix string, expected []string) {
67-
all := elasticRequest(t, http.MethodGet, "/_aliases", nil)
73+
func assertIndexesWithPrefix(t *testing.T, cfg *indexer.Config, prefix string, expected []string) {
74+
all := elasticRequest(t, cfg, http.MethodGet, "/_aliases", nil)
6875

6976
actual := []string{}
7077
for name := range all {
@@ -82,12 +89,12 @@ func assertIndexerStats(t *testing.T, ix indexers.Indexer, expectedIndexed, expe
8289
assert.Equal(t, expectedDeleted, actual.Deleted, "deleted mismatch")
8390
}
8491

85-
func elasticRequest(t *testing.T, method, path string, data map[string]any) map[string]any {
92+
func elasticRequest(t *testing.T, cfg *indexer.Config, method, path string, data map[string]any) map[string]any {
8693
var body io.Reader
8794
if data != nil {
8895
body = bytes.NewReader(jsonx.MustMarshal(data))
8996
}
90-
req, err := httpx.NewRequest(method, elasticURL+path, body, map[string]string{"Content-Type": "application/json"})
97+
req, err := httpx.NewRequest(method, cfg.ElasticURL+path, body, map[string]string{"Content-Type": "application/json"})
9198
require.NoError(t, err)
9299

93100
trace, err := httpx.DoTrace(http.DefaultClient, req, nil, nil, -1)

indexers/contacts_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ var contactQueryTests = []struct {
6161
}
6262

6363
func TestContacts(t *testing.T) {
64-
db := setup(t)
64+
cfg, db := setup(t)
6565

66-
ix1 := indexers.NewContactIndexer(elasticURL, aliasName, 2, 1, 4)
66+
ix1 := indexers.NewContactIndexer(cfg.ElasticURL, aliasName, 2, 1, 4)
6767
assert.Equal(t, "indexer_test", ix1.Name())
6868

6969
dbModified, err := ix1.GetDBLastModified(context.Background(), db)
@@ -87,10 +87,10 @@ func TestContacts(t *testing.T) {
8787
assert.WithinDuration(t, time.Date(2017, 11, 10, 21, 11, 59, 890662000, time.UTC), esModified, 0)
8888

8989
assertIndexerStats(t, ix1, 9, 0)
90-
assertIndexesWithPrefix(t, aliasName, []string{expectedIndexName})
90+
assertIndexesWithPrefix(t, cfg, aliasName, []string{expectedIndexName})
9191

9292
for _, tc := range contactQueryTests {
93-
assertQuery(t, []byte(tc.query), tc.expected, "query mismatch for %s", tc.query)
93+
assertQuery(t, cfg, []byte(tc.query), tc.expected, "query mismatch for %s", tc.query)
9494
}
9595

9696
lastModified, err := ix1.GetESLastModified(indexName)
@@ -112,21 +112,21 @@ func TestContacts(t *testing.T) {
112112

113113
time.Sleep(1 * time.Second)
114114

115-
assertIndexesWithPrefix(t, aliasName, []string{expectedIndexName})
115+
assertIndexesWithPrefix(t, cfg, aliasName, []string{expectedIndexName})
116116

117117
// should only match new john, old john is gone
118-
assertQuery(t, []byte(`{"match": {"name": {"query": "john"}}}`), []int64{2})
118+
assertQuery(t, cfg, []byte(`{"match": {"name": {"query": "john"}}}`), []int64{2})
119119

120120
// 3 is no longer in our group
121-
assertQuery(t, []byte(`{"match": {"group_ids": {"query": 4}}}`), []int64{1})
121+
assertQuery(t, cfg, []byte(`{"match": {"group_ids": {"query": 4}}}`), []int64{1})
122122

123123
// change John's name to Eric..
124124
_, err = db.Exec(`
125125
UPDATE contacts_contact SET name = 'Eric', modified_on = '2020-08-20 14:00:00+00' where id = 2;`)
126126
require.NoError(t, err)
127127

128128
// and simulate another indexer doing a parallel rebuild
129-
ix2 := indexers.NewContactIndexer(elasticURL, aliasName, 2, 1, 4)
129+
ix2 := indexers.NewContactIndexer(cfg.ElasticURL, aliasName, 2, 1, 4)
130130

131131
indexName2, err := ix2.Index(db, true, false)
132132
assert.NoError(t, err)
@@ -136,20 +136,20 @@ func TestContacts(t *testing.T) {
136136
time.Sleep(1 * time.Second)
137137

138138
// check we have a new index but the old index is still around
139-
assertIndexesWithPrefix(t, aliasName, []string{expectedIndexName, expectedIndexName + "_1"})
139+
assertIndexesWithPrefix(t, cfg, aliasName, []string{expectedIndexName, expectedIndexName + "_1"})
140140

141141
// and the alias points to the new index
142-
assertQuery(t, []byte(`{"match": {"name": {"query": "eric"}}}`), []int64{2})
142+
assertQuery(t, cfg, []byte(`{"match": {"name": {"query": "eric"}}}`), []int64{2})
143143

144144
// simulate another indexer doing a parallel rebuild with cleanup
145-
ix3 := indexers.NewContactIndexer(elasticURL, aliasName, 2, 1, 4)
145+
ix3 := indexers.NewContactIndexer(cfg.ElasticURL, aliasName, 2, 1, 4)
146146
indexName3, err := ix3.Index(db, true, true)
147147
assert.NoError(t, err)
148148
assert.Equal(t, expectedIndexName+"_2", indexName3) // new index used
149149
assertIndexerStats(t, ix3, 8, 0)
150150

151151
// check we cleaned up indexes besides the new one
152-
assertIndexesWithPrefix(t, aliasName, []string{expectedIndexName + "_2"})
152+
assertIndexesWithPrefix(t, cfg, aliasName, []string{expectedIndexName + "_2"})
153153

154154
// check that the original indexer now indexes against the new index
155155
indexName, err = ix1.Index(db, false, false)

0 commit comments

Comments
 (0)