@@ -18,7 +18,9 @@ package consumergroup
1818
1919import (
2020 "context"
21+ "strconv"
2122 "testing"
23+ "time"
2224
2325 "github.com/stretchr/testify/require"
2426 corev1 "k8s.io/api/core/v1"
@@ -61,15 +63,20 @@ const (
6163 ConfigKafkaAutoscalerName = "config-kafka-autoscaler"
6264)
6365
64- func TestNewController (t * testing.T ) {
66+ func fakeCtxWithDispatcher (t * testing.T ) context. Context {
6567 ctx , _ := reconcilertesting .SetupFakeContext (t , func (ctx context.Context ) context.Context {
6668 return filteredFactory .WithSelectors (ctx ,
6769 internalsapi .DispatcherLabelSelectorStr ,
6870 )
6971 })
70- ctx , _ = kedaclient .With (ctx )
7172
72- t .Setenv ("SYSTEM_NAMESPACE" , systemNamespace )
73+ return ctx
74+ }
75+
76+ func TestNewController (t * testing.T ) {
77+ ctx := fakeCtxWithDispatcher (t )
78+
79+ ctx , _ = kedaclient .With (ctx )
7380
7481 ctx , _ = kubeclient .With (ctx ,
7582 & corev1.ConfigMap {
@@ -79,16 +86,16 @@ func TestNewController(t *testing.T) {
7986 },
8087 Data : map [string ]string {
8188 "predicates" : `
82- [
83- {"Name": "PodFitsResources"},
84- {"Name": "NoMaxResourceCount", "Args": "{\"NumPartitions\": 100}"},
85- {"Name": "EvenPodSpread", "Args": "{\"MaxSkew\": 2}"}
86- ]` ,
89+ [
90+ {"Name": "PodFitsResources"},
91+ {"Name": "NoMaxResourceCount", "Args": "{\"NumPartitions\": 100}"},
92+ {"Name": "EvenPodSpread", "Args": "{\"MaxSkew\": 2}"}
93+ ]` ,
8794 "priorities" : `
88- [
89- {"Name": "AvailabilityZonePriority", "Weight": 10, "Args": "{\"MaxSkew\": 2}"},
90- {"Name": "LowestOrdinalPriority", "Weight": 2}
91- ]` ,
95+ [
96+ {"Name": "AvailabilityZonePriority", "Weight": 10, "Args": "{\"MaxSkew\": 2}"},
97+ {"Name": "LowestOrdinalPriority", "Weight": 2}
98+ ]` ,
9299 },
93100 },
94101 & corev1.ConfigMap {
@@ -99,23 +106,25 @@ func TestNewController(t *testing.T) {
99106 Data : map [string ]string {
100107 "predicates" : `[]` ,
101108 "priorities" : `
102- [
103- {"Name": "RemoveWithEvenPodSpreadPriority", "Weight": 10, "Args": "{\"MaxSkew\": 2}"},
104- {"Name": "RemoveWithAvailabilityZonePriority", "Weight": 10, "Args": "{\"MaxSkew\": 2}"},
105- {"Name": "RemoveWithHighestOrdinalPriority", "Weight": 2}
106- ]` ,
109+ [
110+ {"Name": "RemoveWithEvenPodSpreadPriority", "Weight": 10, "Args": "{\"MaxSkew\": 2}"},
111+ {"Name": "RemoveWithAvailabilityZonePriority", "Weight": 10, "Args": "{\"MaxSkew\": 2}"},
112+ {"Name": "RemoveWithHighestOrdinalPriority", "Weight": 2}
113+ ]` ,
107114 },
108115 },
109116 )
110117
111118 ctx = clientpool .WithKafkaClientPool (ctx )
112119
120+ t .Setenv ("SYSTEM_NAMESPACE" , systemNamespace )
113121 t .Setenv ("AUTOSCALER_REFRESH_PERIOD" , RefreshPeriod )
114122 t .Setenv ("POD_CAPACITY" , PodCapacity )
115123 t .Setenv ("DISPATCHERS_MIN_REPLICAS" , MinReplicas )
116124 t .Setenv ("SCHEDULER_CONFIG" , ConfigKafkaSchedulerName )
117125 t .Setenv ("DESCHEDULER_CONFIG" , ConfigKafkaDeSchedulerName )
118126 t .Setenv ("AUTOSCALER_CONFIG" , ConfigKafkaAutoscalerName )
127+
119128 controller := NewController (ctx , configmap .NewStaticWatcher (& corev1.ConfigMap {
120129 ObjectMeta : metav1.ObjectMeta {
121130 Name : "config-kafka-features" ,
@@ -127,7 +136,6 @@ func TestNewController(t *testing.T) {
127136}
128137
129138func TestEnqueueConsumerFromConsumerGroup (t * testing.T ) {
130-
131139 capture := types.NamespacedName {}
132140
133141 f := enqueueConsumerGroupFromConsumer (func (name types.NamespacedName ) {
@@ -155,3 +163,36 @@ func TestEnqueueConsumerFromConsumerGroup(t *testing.T) {
155163 require .Equal (t , or .Name , capture .Name )
156164 require .Equal (t , ns , capture .Namespace )
157165}
166+
167+ // Test covering the bug:
168+ // - https://github.com/knative-extensions/eventing-kafka-broker/issues/4542
169+ //
170+ // The config value "MinReplicas" was ignored in createKafkaScheduler when the config was copied.
171+ // To make sure this does not happen for the other values too, they were added to the test.
172+ func TestCreateKafkaSchedulerSetsCorrectConfig (t * testing.T ) {
173+ ctx := fakeCtxWithDispatcher (t )
174+ ctx , cancel := context .WithCancel (ctx )
175+ t .Cleanup (cancel )
176+
177+ rp , _ := time .ParseDuration (RefreshPeriod )
178+ c , _ := strconv .ParseInt (PodCapacity , 10 , 32 )
179+ mr , _ := strconv .ParseInt (MinReplicas , 10 , 32 )
180+ expectedConfig := SchedulerConfig {
181+ RefreshPeriod : rp ,
182+ Capacity : int32 (c ),
183+ MinReplicas : int32 (mr ),
184+ }
185+ informer := filteredFactory .Get (ctx , internalsapi .DispatcherLabelSelectorStr ).Core ().V1 ().Pods ()
186+
187+ scheduler := createKafkaScheduler (
188+ ctx ,
189+ expectedConfig ,
190+ kafkainternals .SourceStatefulSetName ,
191+ informer ,
192+ )
193+
194+ require .Equal (t , kafkainternals .SourceStatefulSetName , scheduler .StatefulSetName )
195+ require .Equal (t , expectedConfig .RefreshPeriod , scheduler .RefreshPeriod )
196+ require .Equal (t , expectedConfig .Capacity , scheduler .Capacity )
197+ require .Equal (t , expectedConfig .MinReplicas , scheduler .MinReplicas )
198+ }
0 commit comments