From 94ffd18ff4a695aea1f2ecf2819d845a204bcf49 Mon Sep 17 00:00:00 2001 From: Chanwit Kaewkasi Date: Tue, 1 Aug 2023 19:50:57 +0700 Subject: [PATCH] add a test for the case of blank inventory Signed-off-by: Chanwit Kaewkasi --- core/server/inventory_test.go | 99 +++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/core/server/inventory_test.go b/core/server/inventory_test.go index 839b69af309..2df52f29bbe 100644 --- a/core/server/inventory_test.go +++ b/core/server/inventory_test.go @@ -128,6 +128,105 @@ func TestGetInventoryKustomization(t *testing.T) { g.Expect(res.Entries[0].Tenant).To(Equal("tenant")) } +func TestGetBlankInventoryKustomization(t *testing.T) { + g := NewGomegaWithT(t) + + ctx := context.Background() + + automationName := "my-automation" + + ns := corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-namespace", + Labels: map[string]string{ + "toolkit.fluxcd.io/tenant": "tenant", + }, + }, + } + + deployment := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-deployment", + Namespace: ns.Name, + UID: "this-is-not-an-uid", + }, + Spec: appsv1.DeploymentSpec{ + Selector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + types.AppLabel: automationName, + }, + }, + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{types.AppLabel: automationName}, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{{ + Name: "nginx", + Image: "nginx", + }}, + }, + }, + }, + } + + rs := &appsv1.ReplicaSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: fmt.Sprintf("%s-123abcd", automationName), + Namespace: ns.Name, + }, + Spec: appsv1.ReplicaSetSpec{ + Template: deployment.Spec.Template, + Selector: deployment.Spec.Selector, + }, + Status: appsv1.ReplicaSetStatus{ + Replicas: 1, + }, + } + + rs.SetOwnerReferences([]metav1.OwnerReference{{ + UID: deployment.UID, + APIVersion: appsv1.SchemeGroupVersion.String(), + Kind: "Deployment", + Name: deployment.Name, + }}) + + kust := &kustomizev1.Kustomization{ + ObjectMeta: metav1.ObjectMeta{ + Name: automationName, + Namespace: ns.Name, + }, + Spec: kustomizev1.KustomizationSpec{ + SourceRef: kustomizev1.CrossNamespaceSourceReference{ + Kind: sourcev1.GitRepositoryKind, + }, + }, + Status: kustomizev1.KustomizationStatus{ + Inventory: nil, // blank inventory + }, + } + + scheme, err := kube.CreateScheme() + g.Expect(err).To(BeNil()) + + client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(&ns, kust, deployment, rs).Build() + cfg := makeServerConfig(client, t, "") + c := makeServer(cfg, t) + + res, err := c.GetInventory(ctx, &pb.GetInventoryRequest{ + Namespace: ns.Name, + ClusterName: cluster.DefaultCluster, + Kind: "Kustomization", + Name: kust.Name, + WithChildren: true, + }) + + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(res.Entries).To(HaveLen(0)) + + g.Expect(res.Entries[0].Children).To(HaveLen(0)) +} + func TestGetInventoryHelmRelease(t *testing.T) { g := NewGomegaWithT(t)