|
| 1 | +// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package server_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "time" |
| 8 | + |
| 9 | + storagev1alpha1 "github.com/ironcore-dev/ironcore/api/storage/v1alpha1" |
| 10 | + irievent "github.com/ironcore-dev/ironcore/iri/apis/event/v1alpha1" |
| 11 | + iri "github.com/ironcore-dev/ironcore/iri/apis/volume/v1alpha1" |
| 12 | + |
| 13 | + irimeta "github.com/ironcore-dev/ironcore/iri/apis/meta/v1alpha1" |
| 14 | + volumepoolletv1alpha1 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/api/v1alpha1" |
| 15 | + . "github.com/onsi/ginkgo/v2" |
| 16 | + . "github.com/onsi/gomega" |
| 17 | + |
| 18 | + corev1 "k8s.io/api/core/v1" |
| 19 | + "k8s.io/client-go/kubernetes/scheme" |
| 20 | + ctrl "sigs.k8s.io/controller-runtime" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = Describe("ListEvents", func() { |
| 25 | + ns, srv := SetupTest() |
| 26 | + volumeClass := SetupVolumeClass() |
| 27 | + |
| 28 | + It("should correctly list events", func(ctx SpecContext) { |
| 29 | + By("creating volume") |
| 30 | + Expect(storagev1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) |
| 31 | + |
| 32 | + k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ |
| 33 | + Scheme: scheme.Scheme, |
| 34 | + }) |
| 35 | + Expect(err).ToNot(HaveOccurred()) |
| 36 | + |
| 37 | + res, err := srv.CreateVolume(ctx, &iri.CreateVolumeRequest{ |
| 38 | + Volume: &iri.Volume{ |
| 39 | + Metadata: &irimeta.ObjectMetadata{ |
| 40 | + Labels: map[string]string{ |
| 41 | + volumepoolletv1alpha1.VolumeUIDLabel: "foobar", |
| 42 | + }, |
| 43 | + }, |
| 44 | + Spec: &iri.VolumeSpec{ |
| 45 | + Class: volumeClass.Name, |
| 46 | + Resources: &iri.VolumeResources{ |
| 47 | + StorageBytes: 100, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }) |
| 52 | + |
| 53 | + Expect(err).NotTo(HaveOccurred()) |
| 54 | + Expect(res).NotTo(BeNil()) |
| 55 | + |
| 56 | + By("getting the ironcore volume") |
| 57 | + ironcoreVolume := &storagev1alpha1.Volume{} |
| 58 | + ironcoreVolumeKey := client.ObjectKey{Namespace: ns.Name, Name: res.Volume.Metadata.Id} |
| 59 | + Expect(k8sClient.Get(ctx, ironcoreVolumeKey, ironcoreVolume)).To(Succeed()) |
| 60 | + |
| 61 | + By("generating the volume events") |
| 62 | + eventGeneratedTime := time.Now() |
| 63 | + eventRecorder := k8sManager.GetEventRecorderFor("test-recorder") |
| 64 | + eventRecorder.Event(ironcoreVolume, corev1.EventTypeNormal, "testing", "this is test event") |
| 65 | + |
| 66 | + By("listing the volume events with no filters") |
| 67 | + Eventually(func(g Gomega) []*irievent.Event { |
| 68 | + resp, err := srv.ListEvents(ctx, &iri.ListEventsRequest{}) |
| 69 | + g.Expect(err).NotTo(HaveOccurred()) |
| 70 | + return resp.Events |
| 71 | + }).Should(ConsistOf( |
| 72 | + HaveField("Spec", SatisfyAll( |
| 73 | + HaveField("InvolvedObjectMeta.Id", Equal(ironcoreVolume.Name)), |
| 74 | + HaveField("Reason", Equal("testing")), |
| 75 | + HaveField("Message", Equal("this is test event")), |
| 76 | + HaveField("Type", Equal(corev1.EventTypeNormal)), |
| 77 | + )), |
| 78 | + ), |
| 79 | + ) |
| 80 | + |
| 81 | + By("listing the volume events with matching label and time filters") |
| 82 | + resp, err := srv.ListEvents(ctx, &iri.ListEventsRequest{Filter: &iri.EventFilter{ |
| 83 | + LabelSelector: map[string]string{volumepoolletv1alpha1.VolumeUIDLabel: "foobar"}, |
| 84 | + EventsFromTime: eventGeneratedTime.Unix(), |
| 85 | + EventsToTime: time.Now().Unix(), |
| 86 | + }}) |
| 87 | + |
| 88 | + Expect(err).NotTo(HaveOccurred()) |
| 89 | + |
| 90 | + Expect(resp.Events).To(ConsistOf( |
| 91 | + HaveField("Spec", SatisfyAll( |
| 92 | + HaveField("InvolvedObjectMeta.Id", Equal(ironcoreVolume.Name)), |
| 93 | + HaveField("Reason", Equal("testing")), |
| 94 | + HaveField("Message", Equal("this is test event")), |
| 95 | + HaveField("Type", Equal(corev1.EventTypeNormal)), |
| 96 | + )), |
| 97 | + ), |
| 98 | + ) |
| 99 | + |
| 100 | + By("listing the volume events with non matching label filter") |
| 101 | + resp, err = srv.ListEvents(ctx, &iri.ListEventsRequest{Filter: &iri.EventFilter{ |
| 102 | + LabelSelector: map[string]string{"foo": "bar"}, |
| 103 | + EventsFromTime: eventGeneratedTime.Unix(), |
| 104 | + EventsToTime: time.Now().Unix(), |
| 105 | + }}) |
| 106 | + Expect(err).NotTo(HaveOccurred()) |
| 107 | + |
| 108 | + Expect(resp.Events).To(BeEmpty()) |
| 109 | + |
| 110 | + By("listing the volume events with matching label filter and non matching time filter") |
| 111 | + resp, err = srv.ListEvents(ctx, &iri.ListEventsRequest{Filter: &iri.EventFilter{ |
| 112 | + LabelSelector: map[string]string{volumepoolletv1alpha1.VolumeUIDLabel: "foobar"}, |
| 113 | + EventsFromTime: eventGeneratedTime.Add(-10 * time.Minute).Unix(), |
| 114 | + EventsToTime: eventGeneratedTime.Add(-5 * time.Minute).Unix(), |
| 115 | + }}) |
| 116 | + Expect(err).NotTo(HaveOccurred()) |
| 117 | + |
| 118 | + Expect(resp.Events).To(BeEmpty()) |
| 119 | + }) |
| 120 | +}) |
0 commit comments