Skip to content

Commit

Permalink
add a test for the case of blank inventory
Browse files Browse the repository at this point in the history
Signed-off-by: Chanwit Kaewkasi <chanwit@weave.works>
  • Loading branch information
Chanwit Kaewkasi committed Aug 1, 2023
1 parent d851ca2 commit 94ffd18
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions core/server/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 94ffd18

Please sign in to comment.