Skip to content

Commit

Permalink
Add read path test for Tenants collector
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Pellizzari committed Dec 15, 2023
1 parent 07ae486 commit d0863ae
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions pkg/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,77 @@ func TestQueryOrdering_Realistic(t *testing.T) {

}

func TestQuery_Tenants(t *testing.T) {
g := NewGomegaWithT(t)

dir, err := os.MkdirTemp("", "test")
g.Expect(err).NotTo(HaveOccurred())

db, err := store.CreateSQLiteDB(dir)
g.Expect(err).NotTo(HaveOccurred())

s, err := store.NewSQLiteStore(db, logr.Discard())
g.Expect(err).NotTo(HaveOccurred())

idx, err := store.NewIndexer(s, dir, logr.Discard())
g.Expect(err).NotTo(HaveOccurred())

ctx := auth.WithPrincipal(context.Background(), &auth.UserPrincipal{
ID: "test",
})

objects := []models.Object{
{
Cluster: "test-cluster-1",
Name: "obj-1",
Namespace: "namespace-a",
Kind: "Deployment",
APIGroup: "apps",
APIVersion: "v1",
},
{
Cluster: "test-cluster-1",
Name: "obj-2",
Namespace: "namespace-b",
Kind: "Deployment",
APIGroup: "apps",
APIVersion: "v1",
},
}

tenants := []models.Tenant{
{
ID: "tenant-a",
Name: "Tenant A",
Namespace: objects[0].Namespace,
ClusterName: objects[0].Cluster,
},
}

g.Expect(idx.Add(context.Background(), objects)).To(Succeed())
g.Expect(store.SeedObjects(db, objects)).To(Succeed())

result := db.Create(&tenants)
g.Expect(result.Error).NotTo(HaveOccurred())

q := &qs{
log: logr.Discard(),
debug: logr.Discard(),
r: s,
index: idx,
authorizer: allowAll,
}

res, err := q.RunQuery(ctx, &query{terms: "", descending: true}, nil)
g.Expect(err).NotTo(HaveOccurred())

obj1 := res[1]
g.Expect(obj1.Tenant).To(Equal(tenants[0].Name))

obj2 := res[0]
g.Expect(obj2.Tenant).To(Equal(""))
}

type query struct {
terms string
filters []string
Expand Down

0 comments on commit d0863ae

Please sign in to comment.