Skip to content

Commit

Permalink
add VulnerabilityReport Controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Nov 18, 2024
1 parent 06f048c commit 1b228bb
Show file tree
Hide file tree
Showing 3 changed files with 3,883 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"runtime"
"testing"

trivy "github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
templatesv1 "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates/v1"
Expand Down Expand Up @@ -80,6 +81,9 @@ var _ = BeforeSuite(func() {
err = templatesv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

err = trivy.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

kClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(kClient).NotTo(BeNil())
Expand Down
118 changes: 118 additions & 0 deletions internal/controller/vulnerabilityreports_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package controller

import (
"context"

trivy "github.com/aquasecurity/trivy-operator/pkg/apis/aquasecurity/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
cmap "github.com/orcaman/concurrent-map/v2"
console "github.com/pluralsh/console/go/client"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ = Describe("VulnerabilityReport Controller", Ordered, func() {
Context("When reconciling a resource", func() {
const (
resourceName = "default"
namespace = "default"
)

ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: namespace,
}

vulnerabilityReport := &trivy.VulnerabilityReport{}
pod := &corev1.Pod{}
BeforeAll(func() {
By("creating the custom resource for the Kind Pod")
err := kClient.Get(ctx, typeNamespacedName, pod)
if err != nil && errors.IsNotFound(err) {
resource := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: namespace,
Annotations: map[string]string{
inventory.OwningInventoryKey: "abc",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "nginx-container",
Image: "nginx:1.21",
Ports: []corev1.ContainerPort{
{
ContainerPort: 80,
},
},
},
},
},
}
Expect(kClient.Create(ctx, resource)).To(Succeed())
}
By("creating the custom resource for the Kind VulnerabilityReport")
err = kClient.Get(ctx, typeNamespacedName, vulnerabilityReport)
if err != nil && errors.IsNotFound(err) {
err = kClient.Get(ctx, typeNamespacedName, pod)
Expect(err).NotTo(HaveOccurred())

resource := &trivy.VulnerabilityReport{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: namespace,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(pod, schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Pod",
}),
},
},
Report: trivy.VulnerabilityReportData{
UpdateTimestamp: metav1.Now(),
Vulnerabilities: []trivy.Vulnerability{},
},
}
Expect(kClient.Create(ctx, resource)).To(Succeed())
}
})

AfterAll(func() {
resource := &trivy.VulnerabilityReport{}
err := kClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance VulnerabilityReport")
Expect(kClient.Delete(ctx, resource)).To(Succeed())
})

It("should successfully reconcile resource", func() {
reconciler := &VulnerabilityReportReconciler{
Client: kClient,
Scheme: kClient.Scheme(),
Ctx: ctx,
reports: cmap.New[console.VulnerabilityReportAttributes](),
}
_, err := reconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())
items := reconciler.reports.Items()
Expect(items).To(HaveLen(1))
Expect(items["default/default"].Services).To(HaveLen(1))
Expect(items["default/default"].Namespaces).To(HaveLen(1))
})
})

})
Loading

0 comments on commit 1b228bb

Please sign in to comment.