-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
438 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) |
Oops, something went wrong.