@@ -234,6 +234,106 @@ func Test_generateRedisReplicationContainerParams(t *testing.T) {
234234 assert .EqualValues (t , expected , actual , "Expected %+v, got %+v" , expected , actual )
235235}
236236
237+ func Test_generateRedisReplicationContainerParams_hostnameEnvVars (t * testing.T ) {
238+ tests := []struct {
239+ name string
240+ resolveHostnames string
241+ announceHostnames string
242+ expectEnvVars bool
243+ }{
244+ {
245+ name : "both yes - env vars injected" ,
246+ resolveHostnames : "yes" ,
247+ announceHostnames : "yes" ,
248+ expectEnvVars : true ,
249+ },
250+ {
251+ name : "both no - no env vars injected" ,
252+ resolveHostnames : "no" ,
253+ announceHostnames : "no" ,
254+ expectEnvVars : false ,
255+ },
256+ {
257+ name : "both empty - no env vars injected" ,
258+ resolveHostnames : "" ,
259+ announceHostnames : "" ,
260+ expectEnvVars : false ,
261+ },
262+ {
263+ name : "only resolveHostnames yes - env vars injected" ,
264+ resolveHostnames : "yes" ,
265+ announceHostnames : "no" ,
266+ expectEnvVars : true ,
267+ },
268+ {
269+ name : "only announceHostnames yes - env vars injected" ,
270+ resolveHostnames : "no" ,
271+ announceHostnames : "yes" ,
272+ expectEnvVars : true ,
273+ },
274+ }
275+
276+ for _ , tt := range tests {
277+ t .Run (tt .name , func (t * testing.T ) {
278+ cr := & rrvb2.RedisReplication {
279+ Spec : rrvb2.RedisReplicationSpec {
280+ Size : ptr .To (int32 (3 )),
281+ KubernetesConfig : common.KubernetesConfig {
282+ Image : "quay.io/opstree/redis:v7.0.12" ,
283+ },
284+ ResolveHostnames : tt .resolveHostnames ,
285+ AnnounceHostnames : tt .announceHostnames ,
286+ },
287+ }
288+
289+ actual := generateRedisReplicationContainerParams (cr )
290+
291+ if tt .expectEnvVars {
292+ assert .NotNil (t , actual .EnvVars , "EnvVars should not be nil" )
293+ envVars := * actual .EnvVars
294+ found := map [string ]string {}
295+ for _ , e := range envVars {
296+ found [e .Name ] = e .Value
297+ }
298+ assert .Equal (t , tt .resolveHostnames , found ["RESOLVE_HOSTNAMES" ], "RESOLVE_HOSTNAMES env var mismatch" )
299+ assert .Equal (t , tt .announceHostnames , found ["ANNOUNCE_HOSTNAMES" ], "ANNOUNCE_HOSTNAMES env var mismatch" )
300+ } else {
301+ assert .Nil (t , actual .EnvVars , "EnvVars should be nil when hostname fields are not enabled" )
302+ }
303+ })
304+ }
305+ }
306+
307+ func Test_generateRedisReplicationContainerParams_hostnameEnvVarsAppendedToExisting (t * testing.T ) {
308+ existingEnvVars := []corev1.EnvVar {
309+ {Name : "EXISTING_VAR" , Value : "existing_value" },
310+ }
311+ cr := & rrvb2.RedisReplication {
312+ Spec : rrvb2.RedisReplicationSpec {
313+ Size : ptr .To (int32 (3 )),
314+ KubernetesConfig : common.KubernetesConfig {
315+ Image : "quay.io/opstree/redis:v7.0.12" ,
316+ },
317+ EnvVars : & existingEnvVars ,
318+ ResolveHostnames : "yes" ,
319+ AnnounceHostnames : "yes" ,
320+ },
321+ }
322+
323+ actual := generateRedisReplicationContainerParams (cr )
324+
325+ assert .NotNil (t , actual .EnvVars )
326+ envVars := * actual .EnvVars
327+ assert .Len (t , envVars , 3 , "Should have existing env var plus two hostname env vars" )
328+
329+ assert .Equal (t , "EXISTING_VAR" , envVars [0 ].Name )
330+ assert .Equal (t , "existing_value" , envVars [0 ].Value )
331+ assert .Equal (t , "RESOLVE_HOSTNAMES" , envVars [1 ].Name )
332+ assert .Equal (t , "yes" , envVars [1 ].Value )
333+ assert .Equal (t , "ANNOUNCE_HOSTNAMES" , envVars [2 ].Name )
334+ assert .Equal (t , "yes" , envVars [2 ].Value )
335+ }
336+
237337func Test_generateRedisReplicationInitContainerParams (t * testing.T ) {
238338 path := filepath .Join (".." , ".." , "tests" , "testdata" , "redis-replication.yaml" )
239339 expected := initContainerParameters {
0 commit comments