Skip to content

Commit

Permalink
Add E2E tests
Browse files Browse the repository at this point in the history
  • Loading branch information
assafad1 committed Oct 10, 2024
1 parent cf81e28 commit 2f3ff14
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/coralogix/coralogix-operator
go 1.22.5

require (
github.com/coralogix/coralogix-management-sdk v0.2.2-0.20241010083215-2b986bff8a4e
github.com/coralogix/coralogix-management-sdk v0.2.2-0.20241010112755-3e0665911b8e
github.com/go-logr/logr v1.3.0
github.com/golang/protobuf v1.5.4
github.com/google/uuid v1.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/coralogix/coralogix-management-sdk v0.2.2-0.20241010082835-a716a0125e
github.com/coralogix/coralogix-management-sdk v0.2.2-0.20241010082835-a716a0125e89/go.mod h1:1aa/coMEMe5M1NvnRymOrBF2iCdefaWR0CMaMjPu0oI=
github.com/coralogix/coralogix-management-sdk v0.2.2-0.20241010083215-2b986bff8a4e h1:YZhAgtvQdlavo+RlLe1dF7ogmlPlltqX7eCM78GrGqI=
github.com/coralogix/coralogix-management-sdk v0.2.2-0.20241010083215-2b986bff8a4e/go.mod h1:1aa/coMEMe5M1NvnRymOrBF2iCdefaWR0CMaMjPu0oI=
github.com/coralogix/coralogix-management-sdk v0.2.2-0.20241010112755-3e0665911b8e h1:suehHoHaJt1A8DzNiuDjFK6eFvkGy5C1pmVlMBvZ3jc=
github.com/coralogix/coralogix-management-sdk v0.2.2-0.20241010112755-3e0665911b8e/go.mod h1:1aa/coMEMe5M1NvnRymOrBF2iCdefaWR0CMaMjPu0oI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
153 changes: 153 additions & 0 deletions tests/e2e/alert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/utils/ptr"

"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"google.golang.org/protobuf/types/known/wrapperspb"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

cxsdk "github.com/coralogix/coralogix-management-sdk/go"

utils "github.com/coralogix/coralogix-operator/apis"
coralogixv1alpha1 "github.com/coralogix/coralogix-operator/apis/coralogix/v1alpha1"
)

var _ = Describe("Alert", Ordered, func() {
var (
crClient client.Client
alertClient *cxsdk.AlertsClient
alertID string
alert *coralogixv1alpha1.Alert
)

BeforeAll(func() {
crClient = ClientsInstance.GetControllerRuntimeClient()
alertClient = ClientsInstance.GetCoralogixClientSet().Alerts()
})

It("Should be created successfully", func(ctx context.Context) {
By("Defining an Alert resource")
const alertName = "promql-alert-zz"
alert = &coralogixv1alpha1.Alert{
ObjectMeta: metav1.ObjectMeta{
Name: alertName,
Namespace: testNamespace,
},
Spec: coralogixv1alpha1.AlertSpec{
Name: alertName,
Description: "alert from k8s operator",
Severity: "Critical",
NotificationGroups: []coralogixv1alpha1.NotificationGroup{
{
GroupByFields: []string{"coralogix.metadata.sdkId"},
Notifications: []coralogixv1alpha1.Notification{
{
NotifyOn: "TriggeredOnly",
IntegrationName: ptr.To("Email"),
RetriggeringPeriodMinutes: 1,
},
},
},
},
Scheduling: &coralogixv1alpha1.Scheduling{
DaysEnabled: []coralogixv1alpha1.Day{"Wednesday", "Thursday"},
TimeZone: "UTC+02",
StartTime: ptr.To(coralogixv1alpha1.Time("08:30")),
EndTime: ptr.To(coralogixv1alpha1.Time("20:30")),
},
AlertType: coralogixv1alpha1.AlertType{
Metric: &coralogixv1alpha1.Metric{
Promql: &coralogixv1alpha1.Promql{
SearchQuery: "http_requests_total{status!~\"4..\"}",
Conditions: coralogixv1alpha1.PromqlConditions{
AlertWhen: "More",
Threshold: utils.FloatToQuantity(3),
SampleThresholdPercentage: 50,
TimeWindow: "TwelveHours",
MinNonNullValuesPercentage: ptr.To(10),
},
},
},
},
},
}

By("Creating the Alert resource in the cluster")
Expect(crClient.Create(ctx, alert)).To(Succeed())

By("Fetching the Alert ID")
fetchedAlert := &coralogixv1alpha1.Alert{}
Eventually(func(g Gomega) error {
err := crClient.Get(ctx, types.NamespacedName{Name: alertName, Namespace: testNamespace}, fetchedAlert)
g.Expect(err).NotTo(HaveOccurred())

if fetchedAlert.Status.ID != nil {
alertID = *fetchedAlert.Status.ID
return nil
}

return fmt.Errorf("Alert ID is not set")
}, time.Minute, time.Second).Should(Succeed())

By("Verifying Alert exists in Coralogix backend")
Eventually(func() error {
_, err := alertClient.Get(ctx, &cxsdk.GetAlertDefRequest{Id: wrapperspb.String(alertID)})
return err
}, time.Minute, time.Second).Should(Succeed())
})

It("Should be updated successfully", func(ctx context.Context) {
By("Patching the Alert resource")
const newAlertName = "promql-alert-updated"
modifiedAlert := alert.DeepCopy()
modifiedAlert.Spec.Name = newAlertName
err := crClient.Patch(ctx, modifiedAlert, client.MergeFrom(alert))
Expect(err).NotTo(HaveOccurred())

By("Verifying Alert is updated in Coralogix backend")
Eventually(func(g Gomega) bool {
getAlertRes, err := alertClient.Get(ctx, &cxsdk.GetAlertDefRequest{Id: wrapperspb.String(alertID)})
g.Expect(err).NotTo(HaveOccurred())
return getAlertRes.GetAlertDef().GetUpdatedTime().AsTime().
After(getAlertRes.GetAlertDef().GetCreatedTime().AsTime())
}, time.Minute, time.Second).Should(BeTrue())
})

It("Should be deleted successfully", func(ctx context.Context) {
By("Deleting the Alert resource")
err := crClient.Delete(ctx, alert)
Expect(err).NotTo(HaveOccurred())

By("Verifying Alert is deleted from Coralogix backend")
Eventually(func() codes.Code {
_, err := alertClient.Get(ctx, &cxsdk.GetAlertDefRequest{Id: wrapperspb.String(alertID)})
return status.Code(err)
}, time.Minute, time.Second).Should(Equal(codes.NotFound))
})
})
19 changes: 18 additions & 1 deletion tests/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
cxsdk "github.com/coralogix/coralogix-management-sdk/go"
)

const testNamespace = "coralogix-e2e-test"

func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Coralogix operator E2E test suite")
Expand All @@ -45,13 +47,28 @@ var _ = BeforeSuite(func(ctx context.Context) {
Expect(ClientsInstance.InitControllerRuntimeClient()).To(Succeed())
Expect(ClientsInstance.InitK8sClient()).To(Succeed())

k8sClient := ClientsInstance.GetK8sClient()

By("Creating test namespace")
_, err := k8sClient.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: testNamespace,
},
}, metav1.CreateOptions{})
Expect(err).NotTo(HaveOccurred())

By("Validating that the controller-manager pod is running")
Eventually(func() corev1.PodPhase {
k8sClient := ClientsInstance.GetK8sClient()
podList, err := k8sClient.CoreV1().
Pods("coralogix-operator-system").
List(ctx, metav1.ListOptions{LabelSelector: "control-plane=controller-manager"})
Expect(err).NotTo(HaveOccurred())
return podList.Items[0].Status.Phase
}, time.Minute, time.Second).Should(Equal(corev1.PodRunning))
})

var _ = AfterSuite(func(ctx context.Context) {
By("Deleting test namespace")
k8sClient := ClientsInstance.GetK8sClient()
Expect(k8sClient.CoreV1().Namespaces().Delete(ctx, testNamespace, metav1.DeleteOptions{})).To(Succeed())
})
133 changes: 133 additions & 0 deletions tests/e2e/outbound_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"context"
"fmt"

"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

cxsdk "github.com/coralogix/coralogix-management-sdk/go"

coralogixv1alpha1 "github.com/coralogix/coralogix-operator/apis/coralogix/v1alpha1"
)

var _ = Describe("Outbound Webhook", Ordered, func() {
var (
crClient client.Client
OutboundWebhookClient *cxsdk.WebhooksClient
outboundWebhookID string
outBoundWebhhok *coralogixv1alpha1.OutboundWebhook
)

BeforeAll(func() {
crClient = ClientsInstance.GetControllerRuntimeClient()
OutboundWebhookClient = ClientsInstance.GetCoralogixClientSet().Webhooks()
})

It("Should be created successfully", func(ctx context.Context) {
By("Defining an Outbound webhook resource")
const outboundWebhookName = "slack-outbound-webhook"
outBoundWebhhok = &coralogixv1alpha1.OutboundWebhook{
ObjectMeta: metav1.ObjectMeta{
Name: outboundWebhookName,
Namespace: testNamespace,
},
Spec: coralogixv1alpha1.OutboundWebhookSpec{
Name: outboundWebhookName,
OutboundWebhookType: coralogixv1alpha1.OutboundWebhookType{
Slack: &coralogixv1alpha1.Slack{
Url: "https://hooks.slack.com/services",
Attachments: []coralogixv1alpha1.SlackConfigAttachment{
{
Type: "MetricSnapshot",
IsActive: true,
},
},
Digests: []coralogixv1alpha1.SlackConfigDigest{
{
Type: "FlowAnomalies",
IsActive: true,
},
},
},
},
},
}

By("Creating the OutboundWebhook resource in the cluster")
Expect(crClient.Create(ctx, outBoundWebhhok)).To(Succeed())

By("Fetching the OutboundWebhook ID")
fetchedOutboundWebhook := &coralogixv1alpha1.OutboundWebhook{}
Eventually(func(g Gomega) error {
err := crClient.Get(ctx, types.NamespacedName{Name: outboundWebhookName, Namespace: testNamespace}, fetchedOutboundWebhook)
g.Expect(err).NotTo(HaveOccurred())

if fetchedOutboundWebhook.Status.ID != nil {
outboundWebhookID = *fetchedOutboundWebhook.Status.ID
return nil
}

return fmt.Errorf("OutboundWebhook ID is not set")
}, time.Minute, time.Second).Should(Succeed())

By("Verifying OutboundWebhook exists in Coralogix backend")
Eventually(func() error {
_, err := OutboundWebhookClient.Get(ctx, &cxsdk.GetOutgoingWebhookRequest{Id: wrapperspb.String(outboundWebhookID)})
return err
}, time.Minute, time.Second).Should(Succeed())
})

It("Should be updated successfully", func(ctx context.Context) {
By("Patching the OutboundWebhook resource")
const newOutboundWebhookName = "slack-outbound-webhook-updated"
modifiedOutboundWebhook := outBoundWebhhok.DeepCopy()
modifiedOutboundWebhook.Spec.Name = newOutboundWebhookName
err := crClient.Patch(ctx, modifiedOutboundWebhook, client.MergeFrom(outBoundWebhhok))
Expect(err).NotTo(HaveOccurred())

By("Verifying OutboundWebhook is updated in Coralogix backend")
Eventually(func(g Gomega) string {
getOutboundWebhookRes, err := OutboundWebhookClient.Get(ctx, &cxsdk.GetOutgoingWebhookRequest{Id: wrapperspb.String(outboundWebhookID)})
g.Expect(err).NotTo(HaveOccurred())
return getOutboundWebhookRes.GetWebhook().GetName().GetValue()
}, time.Minute, time.Second).Should(Equal(newOutboundWebhookName))
})

It("Should be deleted successfully", func(ctx context.Context) {
By("Deleting the OutboundWebhook resource")
err := crClient.Delete(ctx, outBoundWebhhok)
Expect(err).NotTo(HaveOccurred())

By("Verifying OutboundWebhook is deleted from Coralogix backend")
Eventually(func() codes.Code {
_, err := OutboundWebhookClient.Get(ctx, &cxsdk.GetOutgoingWebhookRequest{Id: wrapperspb.String(outboundWebhookID)})
return status.Code(err)
}, time.Minute, time.Second).Should(Equal(codes.NotFound))
})
})
Loading

0 comments on commit 2f3ff14

Please sign in to comment.