@@ -20,9 +20,17 @@ import (
2020 policyv1beta1 "k8s.io/api/policy/v1beta1"
2121 "k8s.io/apimachinery/pkg/api/resource"
2222 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+ "k8s.io/apimachinery/pkg/types"
2324 "k8s.io/apimachinery/pkg/util/intstr"
2425)
2526
27+ // For testing purposes
28+ type ExpectedValue struct {
29+ envIndex int
30+ envVarConstant string
31+ envVarValue string
32+ }
33+
2634func toIntStr (val int ) * intstr.IntOrString {
2735 b := intstr .FromInt (val )
2836 return & b
@@ -93,6 +101,119 @@ func TestGenerateSpiloJSONConfiguration(t *testing.T) {
93101 }
94102}
95103
104+ func TestGenerateSpiloPodEnvVars (t * testing.T ) {
105+ var cluster = New (
106+ Config {
107+ OpConfig : config.Config {
108+ WALGSBucket : "wale-gs-bucket" ,
109+ ProtectedRoles : []string {"admin" },
110+ Auth : config.Auth {
111+ SuperUsername : superUserName ,
112+ ReplicationUsername : replicationUserName ,
113+ },
114+ },
115+ }, k8sutil.KubernetesClient {}, acidv1.Postgresql {}, logger , eventRecorder )
116+
117+ expectedValuesGSBucket := []ExpectedValue {
118+ ExpectedValue {
119+ envIndex : 14 ,
120+ envVarConstant : "WAL_GS_BUCKET" ,
121+ envVarValue : "wale-gs-bucket" ,
122+ },
123+ ExpectedValue {
124+ envIndex : 15 ,
125+ envVarConstant : "WAL_BUCKET_SCOPE_SUFFIX" ,
126+ envVarValue : "/SomeUUID" ,
127+ },
128+ ExpectedValue {
129+ envIndex : 16 ,
130+ envVarConstant : "WAL_BUCKET_SCOPE_PREFIX" ,
131+ envVarValue : "" ,
132+ },
133+ }
134+
135+ expectedValuesGCPCreds := []ExpectedValue {
136+ ExpectedValue {
137+ envIndex : 14 ,
138+ envVarConstant : "WAL_GS_BUCKET" ,
139+ envVarValue : "wale-gs-bucket" ,
140+ },
141+ ExpectedValue {
142+ envIndex : 15 ,
143+ envVarConstant : "WAL_BUCKET_SCOPE_SUFFIX" ,
144+ envVarValue : "/SomeUUID" ,
145+ },
146+ ExpectedValue {
147+ envIndex : 16 ,
148+ envVarConstant : "WAL_BUCKET_SCOPE_PREFIX" ,
149+ envVarValue : "" ,
150+ },
151+ ExpectedValue {
152+ envIndex : 17 ,
153+ envVarConstant : "GOOGLE_APPLICATION_CREDENTIALS" ,
154+ envVarValue : "some_path_to_credentials" ,
155+ },
156+ }
157+
158+ testName := "TestGenerateSpiloPodEnvVars"
159+ tests := []struct {
160+ subTest string
161+ opConfig config.Config
162+ uid types.UID
163+ spiloConfig string
164+ cloneDescription * acidv1.CloneDescription
165+ standbyDescription * acidv1.StandbyDescription
166+ customEnvList []v1.EnvVar
167+ expectedValues []ExpectedValue
168+ }{
169+ {
170+ subTest : "Will set WAL_GS_BUCKET env" ,
171+ opConfig : config.Config {
172+ WALGSBucket : "wale-gs-bucket" ,
173+ },
174+ uid : "SomeUUID" ,
175+ spiloConfig : "someConfig" ,
176+ cloneDescription : & acidv1.CloneDescription {},
177+ standbyDescription : & acidv1.StandbyDescription {},
178+ customEnvList : []v1.EnvVar {},
179+ expectedValues : expectedValuesGSBucket ,
180+ },
181+ {
182+ subTest : "Will set GOOGLE_APPLICATION_CREDENTIALS env" ,
183+ opConfig : config.Config {
184+ WALGSBucket : "wale-gs-bucket" ,
185+ GCPCredentials : "some_path_to_credentials" ,
186+ },
187+ uid : "SomeUUID" ,
188+ spiloConfig : "someConfig" ,
189+ cloneDescription : & acidv1.CloneDescription {},
190+ standbyDescription : & acidv1.StandbyDescription {},
191+ customEnvList : []v1.EnvVar {},
192+ expectedValues : expectedValuesGCPCreds ,
193+ },
194+ }
195+
196+ for _ , tt := range tests {
197+ cluster .OpConfig = tt .opConfig
198+
199+ actualEnvs := cluster .generateSpiloPodEnvVars (tt .uid , tt .spiloConfig , tt .cloneDescription , tt .standbyDescription , tt .customEnvList )
200+
201+ for _ , ev := range tt .expectedValues {
202+ env := actualEnvs [ev .envIndex ]
203+
204+ if env .Name != ev .envVarConstant {
205+ t .Errorf ("%s %s: Expected env name %s, have %s instead" ,
206+ testName , tt .subTest , ev .envVarConstant , env .Name )
207+ }
208+
209+ if env .Value != ev .envVarValue {
210+ t .Errorf ("%s %s: Expected env value %s, have %s instead" ,
211+ testName , tt .subTest , ev .envVarValue , env .Value )
212+ }
213+ }
214+ }
215+ }
216+
96217func TestCreateLoadBalancerLogic (t * testing.T ) {
97218 var cluster = New (
98219 Config {
0 commit comments