-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add VulnerabilityReport Controller tests
- Loading branch information
Showing
3 changed files
with
3,883 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
internal/controller/vulnerabilityreports_controller_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
|
||
}) |
Oops, something went wrong.