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 14, 2024
1 parent ef690a5 commit cfa98f1
Show file tree
Hide file tree
Showing 10 changed files with 729 additions and 17 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
151 changes: 151 additions & 0 deletions tests/e2e/alert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
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"
"k8s.io/utils/ptr"
"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("Creating Alert")
alertName := "promql-alert"
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),
},
},
},
},
},
}
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")
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() bool {
getAlertRes, err := alertClient.Get(ctx, &cxsdk.GetAlertDefRequest{Id: wrapperspb.String(alertID)})
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")
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))
})
})
150 changes: 150 additions & 0 deletions tests/e2e/alertmanager_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
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"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
prometheus "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

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

var _ = Describe("AlertmanagerConfig", Ordered, func() {
var (
crClient client.Client
config *prometheus.AlertmanagerConfig
)

BeforeAll(func() {
crClient = ClientsInstance.GetControllerRuntimeClient()
})

It("Should be created successfully", func(ctx context.Context) {
By("Creating secret")
secret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: "slack-webhook-secret",
Namespace: testNamespace,
},
Type: corev1.SecretTypeOpaque,
Data: map[string][]byte{
"webhook-url": []byte("aHR0cHM6Ly9zbGFjay5jb20vYXBpL2NoYXQucG9zdE1lc3NhZ2U="),
},
}
Expect(crClient.Create(ctx, secret)).To(Succeed())

By("Creating AlertmanagerConfig")
config = &prometheus.AlertmanagerConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: "monitoring.coreos.com/v1alpha1",
Kind: "AlertmanagerConfig",
},
ObjectMeta: metav1.ObjectMeta{
Name: "slack-config",
Namespace: testNamespace,
Labels: map[string]string{
"app.coralogix.com/track-alertmanger-config": "true",
},
},
Spec: prometheus.AlertmanagerConfigSpec{
Route: &prometheus.Route{
GroupBy: []string{"alertname"},
Receiver: "slack-default",
RepeatInterval: "3h",
Routes: []apiextensionsv1.JSON{
{
Raw: []byte(`{
"receiver": "slack-general",
"matchers": [
{
"matchType": "=~",
"name": "slack_channel",
"value": ".+"
}
],
"continue": true
}`),
},
},
},
Receivers: []prometheus.Receiver{
{
Name: "slack-general",
SlackConfigs: []prometheus.SlackConfig{
{
APIURL: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "slack-webhook-secret",
},
Key: "webhook-url",
},
},
},
},
},
},
}
Expect(crClient.Create(ctx, config)).To(Succeed())

By("Verifying underlying OutboundWebhook was created")
fetchedOutboundWebhook := &coralogixv1alpha1.OutboundWebhook{}
Eventually(func() error {
return crClient.Get(ctx, types.NamespacedName{Name: "slack-general.slack.0", Namespace: testNamespace}, fetchedOutboundWebhook)
}, time.Minute, time.Second).Should(Succeed())
})

It("Should be updated successfully", func(ctx context.Context) {
By("Patching the AlertmanagerConfig")
modifiedConfig := config.DeepCopy()
modifiedConfig.Spec.Receivers[0].Name = "slack-general-updated"
err := crClient.Patch(ctx, modifiedConfig, client.MergeFrom(config))
Expect(err).NotTo(HaveOccurred())

By("Verifying underlying outboundWebhook was updated")
Eventually(func() error {
fetchedOutboundWebhook := &coralogixv1alpha1.OutboundWebhook{}
return crClient.Get(ctx, types.NamespacedName{Name: "slack-general-updated.slack.0", Namespace: testNamespace}, fetchedOutboundWebhook)
}, time.Minute, time.Second).Should(Succeed())
})

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

By("Verifying underlying outboundWebhook was deleted")
fetchedOutboundWebhook := &coralogixv1alpha1.OutboundWebhook{}
Eventually(func() bool {
err := crClient.Get(ctx, types.NamespacedName{Name: "slack-general-updated.slack.0", Namespace: testNamespace}, fetchedOutboundWebhook)
return errors.IsNotFound(err)
}, time.Minute, time.Second).Should(BeTrue())
})
})
8 changes: 6 additions & 2 deletions tests/e2e/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
package e2e

import (
prometheus "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
prometheusv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
prometheusv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand Down Expand Up @@ -47,7 +48,10 @@ func (c *Clients) InitControllerRuntimeClient() error {
if err != nil {
return err
}
if err = prometheus.AddToScheme(crClient.Scheme()); err != nil {
if err = prometheusv1.AddToScheme(crClient.Scheme()); err != nil {
return err
}
if err = prometheusv1alpha1.AddToScheme(crClient.Scheme()); err != nil {
return err
}
if err = coralogixv1alpha1.AddToScheme(crClient.Scheme()); err != nil {
Expand Down
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())
})
Loading

0 comments on commit cfa98f1

Please sign in to comment.