-
Notifications
You must be signed in to change notification settings - Fork 1
/
meilisearch_test.go
139 lines (122 loc) · 3.99 KB
/
meilisearch_test.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//go:build integration
package integration_test
import (
"context"
"fmt"
"strconv"
"testing"
"github.com/avast/retry-go/v4"
"github.com/meilisearch/meilisearch-go"
"github.com/metal-stack/backup-restore-sidecar/pkg/generate/examples/examples"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
meilisearchIndex = "backup-restore-sidecar"
)
func Test_Meilisearch_Restore(t *testing.T) {
restoreFlow(t, &flowSpec{
databaseType: examples.Meilisearch,
sts: examples.MeilisearchSts,
backingResources: examples.MeilisearchBackingResources,
addTestData: addMeilisearchTestData,
verifyTestData: verifyMeilisearchTestData,
})
}
func Test_Meilisearch_RestoreLatestFromMultipleBackups(t *testing.T) {
restoreLatestFromMultipleBackupsFlow(t, &flowSpec{
databaseType: examples.Meilisearch,
sts: examples.MeilisearchSts,
backingResources: examples.MeilisearchBackingResources,
addTestDataWithIndex: addMeilisearchTestDataWithIndex,
verifyTestDataWithIndex: verifyMeilisearchTestDataWithIndex,
})
}
func Test_Meilisearch_Upgrade(t *testing.T) {
upgradeFlow(t, &upgradeFlowSpec{
flowSpec: flowSpec{
databaseType: examples.Meilisearch,
sts: examples.MeilisearchSts,
backingResources: examples.MeilisearchBackingResources,
addTestData: addMeilisearchTestData,
verifyTestData: verifyMeilisearchTestData,
},
databaseImages: []string{
"getmeili/meilisearch:v1.8.0",
"getmeili/meilisearch:v1.8.1",
"getmeili/meilisearch:v1.9.0",
},
})
}
func newMeilisearchSession(t *testing.T, ctx context.Context) *meilisearch.Client {
var client *meilisearch.Client
err := retry.Do(func() error {
client = meilisearch.NewClient(meilisearch.ClientConfig{
Host: "http://localhost:7700",
APIKey: examples.MeilisearchPassword,
})
ok := client.IsHealthy()
if !ok {
return fmt.Errorf("meilisearch is not yet healthy")
}
return nil
}, retry.Context(ctx))
require.NoError(t, err)
return client
}
func addMeilisearchTestData(t *testing.T, ctx context.Context) {
client := newMeilisearchSession(t, ctx)
creationTask, err := client.CreateIndex(&meilisearch.IndexConfig{
Uid: meilisearchIndex,
PrimaryKey: "id",
})
require.NoError(t, err)
_, err = client.WaitForTask(creationTask.TaskUID)
require.NoError(t, err)
index := client.Index(meilisearchIndex)
testdata := map[string]any{
"id": "1",
"key": "I am precious",
}
indexTask, err := index.AddDocuments(testdata, "id")
require.NoError(t, err)
_, err = client.WaitForTask(indexTask.TaskUID)
require.NoError(t, err)
}
func verifyMeilisearchTestData(t *testing.T, ctx context.Context) {
client := newMeilisearchSession(t, ctx)
index, err := client.GetIndex(meilisearchIndex)
require.NoError(t, err)
testdata := make(map[string]any)
err = index.GetDocument("1", &meilisearch.DocumentQuery{}, &testdata)
require.NoError(t, err)
assert.Equal(t, "I am precious", testdata["key"])
}
func addMeilisearchTestDataWithIndex(t *testing.T, ctx context.Context, i int) {
client := newMeilisearchSession(t, ctx)
creationTask, err := client.CreateIndex(&meilisearch.IndexConfig{
Uid: meilisearchIndex,
PrimaryKey: "id",
})
require.NoError(t, err)
_, err = client.WaitForTask(creationTask.TaskUID)
require.NoError(t, err)
index := client.Index(meilisearchIndex)
testdata := map[string]any{
"id": strconv.Itoa(i),
"key": fmt.Sprintf("idx-%d", i),
}
indexTask, err := index.AddDocuments(testdata, "id")
require.NoError(t, err)
_, err = client.WaitForTask(indexTask.TaskUID)
require.NoError(t, err)
}
func verifyMeilisearchTestDataWithIndex(t *testing.T, ctx context.Context, i int) {
client := newMeilisearchSession(t, ctx)
index, err := client.GetIndex(meilisearchIndex)
require.NoError(t, err)
testdata := make(map[string]any)
err = index.GetDocument(strconv.Itoa(i), &meilisearch.DocumentQuery{}, &testdata)
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("idx-%d", i), testdata["key"])
}