...
1 package couchctl
2
3 import (
4 "strings"
5 "testing"
6
7 "edge-infra.dev/pkg/edge/constants/api/cluster"
8 "edge-infra.dev/pkg/edge/constants/api/fleet"
9 "edge-infra.dev/test/f2"
10 "edge-infra.dev/test/f2/x/ktest"
11
12 "github.com/go-kivik/kivik/v4"
13 "github.com/stretchr/testify/require"
14 "gotest.tools/v3/assert"
15
16 "sigs.k8s.io/controller-runtime/pkg/client"
17 )
18
19 func TestIndexReconciler(t *testing.T) {
20 fin := f2.NewFeature("CouchIndexReconciler").
21 WithLabel(_fleetType, fleet.Store, fleet.CouchDB).
22 WithLabel(_clusterType, cluster.Generic, cluster.DSDS).
23 Setup("CouchDBIndex Created", func(ctx f2.Context, t *testing.T) f2.Context {
24 k := ktest.FromContextT(ctx, t)
25 k.WaitOn(t, k.ObjExists(couchDBDatabase))
26 assert.NilError(t, client.IgnoreAlreadyExists(k.Client.Create(ctx, couchDBIndex)))
27 return ctx
28 }).
29 Test("CouchDBDatabase Ready", testReady(couchDBDatabase)).
30 Test("CouchDBIndex Ready", testReady(couchDBIndex)).
31 Test("CouchDBIndex Exist", func(ctx f2.Context, t *testing.T) f2.Context {
32 k := ktest.FromContextT(ctx, t)
33 cc, err := couchdbServerClient(ctx, k.Client, couchDBServer)
34 require.NoError(t, err)
35
36 indices, err := cc.Client.DB(couchDBDatabase.Name).GetIndexes(ctx)
37 require.NoError(t, err)
38 require.True(t, indexExists(indices, couchDBIndex.Spec.DDoc, couchDBIndex.IndexName()))
39 return ctx
40 }).
41 Feature()
42
43 f.Test(t, fin)
44 }
45
46 func indexExists(indices []kivik.Index, doc string, name string) bool {
47 if !strings.HasPrefix(doc, "_design/") {
48 doc = "_design/" + doc
49 }
50 for _, index := range indices {
51 if index.DesignDoc == doc && index.Name == name {
52 return true
53 }
54 }
55 return false
56 }
57
View as plain text