Skip to content

Commit 37dd96d

Browse files
committed
test: add integration test cases for PV and PVC
1 parent 5eb2097 commit 37dd96d

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

tests/itest/helper/helper.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,26 @@ func (h *Helper) HasConfigAuditReportOwnedBy(ctx context.Context, obj client.Obj
379379
}
380380
}
381381

382+
func (h *Helper) HasClusterConfigAuditReportOwnedBy(ctx context.Context, obj client.Object) func() (bool, error) {
383+
return func() (bool, error) {
384+
gvk, err := apiutil.GVKForObject(obj, h.scheme)
385+
if err != nil {
386+
return false, err
387+
}
388+
var reportsList v1alpha1.ClusterConfigAuditReportList
389+
err = h.kubeClient.List(ctx, &reportsList, client.MatchingLabels{
390+
trivyoperator.LabelResourceKind: gvk.Kind,
391+
trivyoperator.LabelResourceName: obj.GetName(),
392+
trivyoperator.LabelResourceNamespace: obj.GetNamespace(),
393+
})
394+
if err != nil {
395+
return false, err
396+
}
397+
398+
return len(reportsList.Items) == 1 && reportsList.Items[0].DeletionTimestamp == nil, nil
399+
}
400+
}
401+
382402
func (h *Helper) HasScanJobPodOwnedBy(ctx context.Context, obj client.Object) func() (bool, error) {
383403
return func() (bool, error) {
384404
gvk, err := apiutil.GVKForObject(obj, h.scheme)

tests/itest/trivy-operator/behavior/behavior.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
appsv1 "k8s.io/api/apps/v1"
99
batchv1 "k8s.io/api/batch/v1"
1010
corev1 "k8s.io/api/core/v1"
11+
"k8s.io/apimachinery/pkg/api/resource"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
"k8s.io/apimachinery/pkg/util/intstr"
1314
"k8s.io/apimachinery/pkg/util/rand"
@@ -456,6 +457,79 @@ func ConfigurationCheckerBehavior(inputs *Inputs) func() {
456457
})
457458

458459
})
460+
461+
Context("When PersistentVolume is created", func() {
462+
463+
var ctx context.Context
464+
var pv *corev1.PersistentVolume
465+
466+
BeforeEach(func() {
467+
ctx = context.Background()
468+
pv = &corev1.PersistentVolume{
469+
ObjectMeta: metav1.ObjectMeta{
470+
Name: "pv-" + rand.String(5),
471+
},
472+
Spec: corev1.PersistentVolumeSpec{
473+
Capacity: corev1.ResourceList{
474+
corev1.ResourceStorage: resource.MustParse("1Gi"),
475+
},
476+
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
477+
PersistentVolumeReclaimPolicy: corev1.PersistentVolumeReclaimDelete,
478+
PersistentVolumeSource: corev1.PersistentVolumeSource{
479+
HostPath: &corev1.HostPathVolumeSource{Path: "/tmp"},
480+
},
481+
},
482+
}
483+
484+
err := inputs.Create(ctx, pv)
485+
Expect(err).ToNot(HaveOccurred())
486+
})
487+
488+
It("Should create ClusterConfigAuditReport", func() {
489+
Eventually(inputs.HasClusterConfigAuditReportOwnedBy(ctx, pv), inputs.AssertTimeout).Should(BeTrue())
490+
})
491+
492+
AfterEach(func() {
493+
err := inputs.Delete(ctx, pv)
494+
Expect(err).ToNot(HaveOccurred())
495+
})
496+
})
497+
498+
Context("When PersistentVolumeClaim is created", func() {
499+
500+
var ctx context.Context
501+
var pvc *corev1.PersistentVolumeClaim
502+
503+
BeforeEach(func() {
504+
ctx = context.Background()
505+
qty := resource.MustParse("1Gi")
506+
pvc = &corev1.PersistentVolumeClaim{
507+
ObjectMeta: metav1.ObjectMeta{
508+
Namespace: inputs.PrimaryNamespace,
509+
Name: "pvc-" + rand.String(5),
510+
},
511+
Spec: corev1.PersistentVolumeClaimSpec{
512+
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
513+
Resources: corev1.VolumeResourceRequirements{
514+
Requests: corev1.ResourceList{
515+
corev1.ResourceStorage: qty,
516+
},
517+
},
518+
},
519+
}
520+
err := inputs.Create(ctx, pvc)
521+
Expect(err).ToNot(HaveOccurred())
522+
})
523+
524+
It("Should create ConfigAuditReport", func() {
525+
Eventually(inputs.HasConfigAuditReportOwnedBy(ctx, pvc), inputs.AssertTimeout).Should(BeTrue())
526+
})
527+
528+
AfterEach(func() {
529+
err := inputs.Delete(ctx, pvc)
530+
Expect(err).ToNot(HaveOccurred())
531+
})
532+
})
459533
}
460534
}
461535

tests/itest/trivy-operator/suite_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package trivy_operator
22

33
import (
44
"context"
5+
"strings"
56
"testing"
67
"time"
78

89
corev1 "k8s.io/api/core/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
911
"k8s.io/apimachinery/pkg/runtime"
1012
ctrl "sigs.k8s.io/controller-runtime"
1113
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -66,6 +68,26 @@ var _ = BeforeSuite(func() {
6668
})
6769
Expect(err).ToNot(HaveOccurred())
6870

71+
installMode, operatorNamespace, _, err := operatorConfig.ResolveInstallMode()
72+
Expect(err).ToNot(HaveOccurred(), "install mode: %s", installMode)
73+
74+
pluginCM := &corev1.ConfigMap{}
75+
pluginCM.Namespace = operatorNamespace
76+
pluginCM.Name = trivyoperator.GetPluginConfigMapName("Trivy")
77+
_ = kubeClient.Delete(context.Background(), pluginCM)
78+
pluginCM = &corev1.ConfigMap{
79+
ObjectMeta: metav1.ObjectMeta{
80+
Namespace: operatorNamespace,
81+
Name: trivyoperator.GetPluginConfigMapName("Trivy"),
82+
},
83+
Data: map[string]string{
84+
"trivy.useBuiltinRegoPolicies": "true",
85+
"trivy.supportedConfigAuditKinds": "Workload,Service,Role,ClusterRole,NetworkPolicy,Ingress,LimitRange,ResourceQuota,PersistentVolume,PersistentVolumeClaim",
86+
},
87+
}
88+
_ = kubeClient.Create(context.Background(), pluginCM)
89+
_ = kubeClient.Update(context.Background(), pluginCM)
90+
6991
inputs = behavior.Inputs{
7092
AssertTimeout: 5 * time.Minute,
7193
PollingInterval: 5 * time.Second,
@@ -90,6 +112,12 @@ func ApplyTestConfiguration(operatorConfig *etc.Config) {
90112
// Default is 0. Set to 30 seconds for testing scan job TTL behavior.
91113
scanJobTTL := 30 * time.Second
92114
operatorConfig.ScanJobTTL = &scanJobTTL
115+
116+
if operatorConfig.TargetWorkloads == "" {
117+
operatorConfig.TargetWorkloads = "Pod,ReplicaSet,ReplicationController,StatefulSet,DaemonSet,CronJob,Job,PersistentVolume,PersistentVolumeClaim"
118+
} else if !strings.Contains(operatorConfig.TargetWorkloads, "PersistentVolumeClaim") {
119+
operatorConfig.TargetWorkloads += ",PersistentVolumeClaim"
120+
}
93121
}
94122

95123
var _ = AfterSuite(func() {

0 commit comments

Comments
 (0)