Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
tom1299 committed Dec 5, 2024
1 parent 2a5e315 commit 4866101
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion pkg/vulnerabilityreport/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,95 @@ func TestScanJobBuilder(t *testing.T) {
},
}))
})

t.Run("Should get scan job with custom Volume and VolumeMounts", func(t *testing.T) {
g := gomega.NewGomegaWithT(t)
job, _, err := vulnerabilityreport.NewScanJobBuilder().
WithPlugin(&testPlugin{
container: &corev1.Container{
Name: "nginx",
Image: "nginx:1.16",
},
initContainer: &corev1.Container{
Name: "init-container",
Image: "init-container:1.0",
},
}).
WithPluginContext(trivyoperator.NewPluginContext().
WithName("test-plugin").
WithNamespace("trivy-operator-ns").
WithServiceAccountName("trivy-operator-sa").
Get()).
WithCustomVolumes([]corev1.Volume{
{
Name: "trivy-cache",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "trivy-cache",
},
},
},
}).
WithCustomVolumesMount([]corev1.VolumeMount{
{
Name: "trivy-cache",
MountPath: "/var/tmp/trivy-cache",
},
}).
WithObject(&appsv1.ReplicaSet{}).
Get()
g.Expect(err).ToNot(gomega.HaveOccurred())
g.Expect(job).ToNot(gomega.BeNil())
g.Expect(job.Spec.Template.Spec.Volumes).To(gomega.Equal([]corev1.Volume{
{
Name: "trivy-cache",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "trivy-cache",
},
},
},
}))
g.Expect(job.Spec.Template.Spec.Containers[0].VolumeMounts).To(gomega.Equal([]corev1.VolumeMount{
{
Name: "trivy-cache",
MountPath: "/var/tmp/trivy-cache",
},
}))
g.Expect(job.Spec.Template.Spec.InitContainers[0].VolumeMounts).To(gomega.Equal([]corev1.VolumeMount{
{
Name: "trivy-cache",
MountPath: "/var/tmp/trivy-cache",
},
}))
})
}

type testPlugin struct {
initContainer *corev1.Container
container *corev1.Container
}

func (p *testPlugin) Init(_ trivyoperator.PluginContext) error {
return nil
}

func (p *testPlugin) GetScanJobSpec(_ trivyoperator.PluginContext, _ client.Object, _ map[string]docker.Auth, _ *corev1.SecurityContext, _ map[string]v1alpha1.SbomReportData) (corev1.PodSpec, []*corev1.Secret, error) {
return corev1.PodSpec{}, nil, nil
podSpec := corev1.PodSpec{}

if p.initContainer != nil {
podSpec.InitContainers = []corev1.Container{
*p.initContainer,
}
}

if p.container != nil {
podSpec.Containers = []corev1.Container{
*p.container,
}
}

return podSpec, nil, nil
}

func (p *testPlugin) ParseReportData(_ trivyoperator.PluginContext, _ string, _ io.ReadCloser) (v1alpha1.VulnerabilityReportData, v1alpha1.ExposedSecretReportData, *v1alpha1.SbomReportData, error) {
Expand Down

0 comments on commit 4866101

Please sign in to comment.