|
| 1 | +// SPDX-FileCopyrightText: 2021 iteratec GmbH |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package controllers |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "time" |
| 10 | + |
| 11 | + . "github.com/onsi/ginkgo" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + executionv1 "github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1" |
| 14 | + "k8s.io/apimachinery/pkg/api/errors" |
| 15 | + |
| 16 | + "k8s.io/apimachinery/pkg/types" |
| 17 | + //+kubebuilder:scaffold:imports |
| 18 | +) |
| 19 | + |
| 20 | +// These tests use Ginkgo (BDD-style Go testing framework). Refer to |
| 21 | +// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. |
| 22 | +// Define utility constants for object names and testing timeouts and intervals. |
| 23 | +const ( |
| 24 | + timeout = time.Second * 10 |
| 25 | + interval = time.Millisecond * 250 |
| 26 | +) |
| 27 | + |
| 28 | +var _ = Describe("ScanType controller", func() { |
| 29 | + Context("Restarting ScheduledScans on ScanType Config Changes", func() { |
| 30 | + It("Should restart a scheduledScan when the scantype was update", func() { |
| 31 | + ctx := context.Background() |
| 32 | + namespace := "scantype-autorestart-config-change-test" |
| 33 | + |
| 34 | + createNamespace(ctx, namespace) |
| 35 | + createScanType(ctx, namespace) |
| 36 | + scheduledScan := createScheduledScan(ctx, namespace) |
| 37 | + |
| 38 | + // ensure that the ScheduledScan has been triggered |
| 39 | + waitForScheduledScanToBeTriggered(ctx, namespace) |
| 40 | + k8sClient.Get(ctx, types.NamespacedName{Name: "test-scan", Namespace: namespace}, &scheduledScan) |
| 41 | + initialExecutionTime := *scheduledScan.Status.LastScheduleTime |
| 42 | + |
| 43 | + // wait at least one second to ensure that the unix timestamps are at least one second apart. |
| 44 | + time.Sleep(1 * time.Second) |
| 45 | + |
| 46 | + By("Update ScanType to trigger rescan") |
| 47 | + var scanType executionv1.ScanType |
| 48 | + k8sClient.Get(ctx, types.NamespacedName{Name: "nmap", Namespace: namespace}, &scanType) |
| 49 | + if scanType.ObjectMeta.Annotations == nil { |
| 50 | + scanType.ObjectMeta.Annotations = map[string]string{} |
| 51 | + } |
| 52 | + scanType.ObjectMeta.Annotations["foobar.securecodebox.io/example"] = "barfoo" |
| 53 | + err := k8sClient.Update(ctx, &scanType) |
| 54 | + if err != nil { |
| 55 | + panic(err) |
| 56 | + } |
| 57 | + |
| 58 | + By("Controller should set the lastScheduled Timestamp to the past to force a re-scan") |
| 59 | + Eventually(func() bool { |
| 60 | + err := k8sClient.Get(ctx, types.NamespacedName{Name: "test-scan", Namespace: namespace}, &scheduledScan) |
| 61 | + if errors.IsNotFound(err) { |
| 62 | + panic("ScheduledScan should be present for this check!") |
| 63 | + } |
| 64 | + |
| 65 | + return scheduledScan.Status.LastScheduleTime.Unix() != initialExecutionTime.Unix() |
| 66 | + }, timeout, interval).Should(BeTrue()) |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + Context("Should not trigger rescan when ScanType stays the same", func() { |
| 71 | + It("Should restart a scheduledScan when the scantype was update", func() { |
| 72 | + ctx := context.Background() |
| 73 | + namespace := "scantype-no-autorestart-config-change-test" |
| 74 | + |
| 75 | + createNamespace(ctx, namespace) |
| 76 | + createScanType(ctx, namespace) |
| 77 | + scheduledScan := createScheduledScan(ctx, namespace) |
| 78 | + |
| 79 | + // ensure that the ScheduledScan has been triggered |
| 80 | + waitForScheduledScanToBeTriggered(ctx, namespace) |
| 81 | + k8sClient.Get(ctx, types.NamespacedName{Name: "test-scan", Namespace: namespace}, &scheduledScan) |
| 82 | + initialExecutionTime := *scheduledScan.Status.LastScheduleTime |
| 83 | + |
| 84 | + // wait at least one second to ensure that the unix timestamps would be at least one second apart. |
| 85 | + time.Sleep(1 * time.Second) |
| 86 | + |
| 87 | + By("Controller should not restart scheduledscan") |
| 88 | + Consistently(func() bool { |
| 89 | + var scheduledScan executionv1.ScheduledScan |
| 90 | + err := k8sClient.Get(ctx, types.NamespacedName{Name: "test-scan", Namespace: namespace}, &scheduledScan) |
| 91 | + if errors.IsNotFound(err) { |
| 92 | + panic("ScheduledScan should be present for this check!") |
| 93 | + } |
| 94 | + |
| 95 | + return scheduledScan.Status.LastScheduleTime.Unix() == initialExecutionTime.Unix() |
| 96 | + }, timeout, interval).Should(BeTrue(), "Scan was restarted without need") |
| 97 | + }) |
| 98 | + }) |
| 99 | +}) |
| 100 | + |
| 101 | +func waitForScheduledScanToBeTriggered(ctx context.Context, namespace string) { |
| 102 | + var scheduledScan executionv1.ScheduledScan |
| 103 | + By("Wait for ScheduledScan to trigger the initial Scan") |
| 104 | + Eventually(func() bool { |
| 105 | + err := k8sClient.Get(ctx, types.NamespacedName{Name: "test-scan", Namespace: namespace}, &scheduledScan) |
| 106 | + if errors.IsNotFound(err) { |
| 107 | + panic("ScheduledScan should be present for this check!") |
| 108 | + } |
| 109 | + |
| 110 | + return scheduledScan.Status.LastScheduleTime != nil |
| 111 | + }, timeout, interval).Should(BeTrue()) |
| 112 | +} |
0 commit comments