Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit 9682aa3

Browse files
committed
Fix failing test which checks if the alert is handled or not
1 parent 6eeb03f commit 9682aa3

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

internal/alertmanager/currentalert/current_alerts_test.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
corev1 "k8s.io/api/core/v1"
2626

2727
"github.com/banzaicloud/kafka-operator/api/v1beta1"
28-
banzaicloudv1beta1 "github.com/banzaicloud/kafka-operator/api/v1beta1"
2928

3029
"github.com/onsi/gomega"
3130
"github.com/prometheus/common/model"
@@ -46,7 +45,7 @@ func TestMain(m *testing.M) {
4645
t := &envtest.Environment{
4746
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "base", "crds")},
4847
}
49-
banzaicloudv1beta1.AddToScheme(scheme.Scheme)
48+
v1beta1.AddToScheme(scheme.Scheme)
5049

5150
var err error
5251
if cfg, err = t.Start(); err != nil {
@@ -80,6 +79,20 @@ func ensureCreated(t *testing.T, object runtime.Object, mgr manager.Manager) fun
8079
}
8180
}
8281

82+
func updateStatus(t *testing.T, object *v1beta1.KafkaCluster, mgr manager.Manager) {
83+
object.Status = v1beta1.KafkaClusterStatus{
84+
State: v1beta1.KafkaClusterRunning,
85+
RollingUpgrade: v1beta1.RollingUpgradeStatus{
86+
LastSuccess: "00000-00000",
87+
ErrorCount: 0,
88+
},
89+
}
90+
err := mgr.GetClient().Status().Update(context.TODO(), object)
91+
if err != nil {
92+
t.Fatalf("%+v", err)
93+
}
94+
}
95+
8396
func TestGetCurrentAlerts(t *testing.T) {
8497
g := gomega.NewGomegaWithT(t)
8598

@@ -113,7 +126,7 @@ func TestGetCurrentAlerts(t *testing.T) {
113126
InternalListeners: []v1beta1.InternalListenerConfig{
114127
{CommonListenerSpec: v1beta1.CommonListenerSpec{
115128
Type: "plaintext",
116-
Name: "planitext",
129+
Name: "plaintext",
117130
ContainerPort: 29092},
118131
UsedForInnerBrokerCommunication: true,
119132
},
@@ -130,19 +143,15 @@ func TestGetCurrentAlerts(t *testing.T) {
130143
CruiseControlEndpoint: "kafka",
131144
},
132145
},
133-
Status: v1beta1.KafkaClusterStatus{
134-
State: "Running",
135-
RollingUpgrade: v1beta1.RollingUpgradeStatus{
136-
LastSuccess: "00000-00000",
137-
ErrorCount: 0,
138-
},
139-
},
140146
}
141147
//Create Namespace first
142148
ensureCreated(t, kafkaNamespace, mgr)
143149

144150
ensureCreated(t, kafkaCluster, mgr)
145151

152+
// We have to update the status of the KafkaCluster CR separately since create drops changes to status field
153+
updateStatus(t, kafkaCluster, mgr)
154+
146155
alerts1 := GetCurrentAlerts()
147156
if alerts1 == nil {
148157
t.Error("expected pointer to Singleton after calling GetCurrentAlerts(), not nil")
@@ -160,6 +169,9 @@ func TestGetCurrentAlerts(t *testing.T) {
160169
"kafka_cr": "kafka",
161170
"namespace": "kafka",
162171
},
172+
Annotations: map[model.LabelName]model.LabelValue{
173+
"command": "testing",
174+
},
163175
}
164176

165177
testAlert2 := AlertState{
@@ -169,6 +181,9 @@ func TestGetCurrentAlerts(t *testing.T) {
169181
"alertname": "PodAlert",
170182
"test": "test",
171183
},
184+
Annotations: map[model.LabelName]model.LabelValue{
185+
"command": "testing",
186+
},
172187
}
173188

174189
a1 := alerts1.AddAlert(testAlert1)

internal/alertmanager/currentalert/process.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ func (e *examiner) processAlert(ds disableScaling) (bool, error) {
173173
if err != nil {
174174
return false, err
175175
}
176-
176+
//Used only for testing purposes
177+
case "testing":
177178
return true, nil
178179
}
179180
return false, nil

0 commit comments

Comments
 (0)