diff --git a/pkg/query/query_test.go b/pkg/query/query_test.go index cea47a7b4d..97c584cc83 100644 --- a/pkg/query/query_test.go +++ b/pkg/query/query_test.go @@ -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