@@ -3,10 +3,10 @@ package cmd
33import (
44 "os"
55 "testing"
6- "time"
76
8- "github.com/uselagoon/build-deploy-tool/internal/dbaasclient"
97 "github.com/uselagoon/build-deploy-tool/internal/helpers"
8+ "github.com/uselagoon/build-deploy-tool/internal/lagoon"
9+ "github.com/uselagoon/build-deploy-tool/internal/testdata"
1010)
1111
1212func TestIdentifyFeatureFlag (t * testing.T ) {
@@ -35,67 +35,65 @@ func TestIdentifyFeatureFlag(t *testing.T) {
3535 templatePath string
3636 }
3737 tests := []struct {
38- name string
39- args args
40- vars []helpers.EnvironmentVariable
41- want string
42- wantErr bool
38+ name string
39+ args testdata.TestData
40+ templatePath string
41+ varName string
42+ vars []helpers.EnvironmentVariable
43+ want string
44+ wantErr bool
4345 }{
4446 {
45- name : "test1 check if flag is defined in lagoon project variables" ,
46- args : args {
47- name : "ROOTLESS_WORKLOAD" ,
48- alertContact : "alertcontact" ,
49- statusPageID : "statuspageid" ,
50- projectName : "example-project" ,
51- environmentName : "main" ,
52- environmentType : "production" ,
53- buildType : "branch" ,
54- lagoonVersion : "v2.7.x" ,
55- branch : "main" ,
56- projectVars : `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":"enabled","scope":"build"}]` ,
57- envVars : `[]` ,
58- lagoonYAML : "../test-resources/identify-feature/alltest/lagoon.yml" ,
59- templatePath : "../test-resources/output" ,
60- },
61- want : "enabled" ,
47+ name : "test1 check if flag is defined in lagoon project variables" ,
48+ varName : "ROOTLESS_WORKLOAD" ,
49+ args : testdata .GetSeedData (
50+ testdata.TestData {
51+ ProjectName : "example-project" ,
52+ EnvironmentName : "main" ,
53+ Branch : "main" ,
54+ LagoonYAML : "../internal/testdata/node/lagoon.yml" ,
55+ ProjectVariables : []lagoon.EnvironmentVariable {
56+ {
57+ Name : "LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD" ,
58+ Value : "enabled" ,
59+ Scope : "build" ,
60+ },
61+ },
62+ }, true ),
63+ templatePath : "testdata/output" ,
64+ want : "enabled" ,
6265 },
6366 {
64- name : "test2 check if flag is defined in lagoon environment variables" ,
65- args : args {
66- name : "ROOTLESS_WORKLOAD" ,
67- alertContact : "alertcontact" ,
68- statusPageID : "statuspageid" ,
69- projectName : "example-project" ,
70- environmentName : "main" ,
71- environmentType : "production" ,
72- buildType : "branch" ,
73- lagoonVersion : "v2.7.x" ,
74- branch : "main" ,
75- projectVars : `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]` ,
76- envVars : `[{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":"enabled","scope":"build"}]` ,
77- lagoonYAML : "../test-resources/identify-feature/alltest/lagoon.yml" ,
78- templatePath : "../test-resources/output" ,
79- },
80- want : "enabled" ,
67+ name : "test2 check if flag is defined in lagoon environment variables" ,
68+ varName : "ROOTLESS_WORKLOAD" ,
69+ args : testdata .GetSeedData (
70+ testdata.TestData {
71+ ProjectName : "example-project" ,
72+ EnvironmentName : "main" ,
73+ Branch : "main" ,
74+ LagoonYAML : "../internal/testdata/node/lagoon.yml" ,
75+ EnvVariables : []lagoon.EnvironmentVariable {
76+ {
77+ Name : "LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD" ,
78+ Value : "enabled" ,
79+ Scope : "build" ,
80+ },
81+ },
82+ }, true ),
83+ templatePath : "testdata/output" ,
84+ want : "enabled" ,
8185 },
8286 {
83- name : "test3 check if force flag is defined in build variables" ,
84- args : args {
85- name : "ROOTLESS_WORKLOAD" ,
86- alertContact : "alertcontact" ,
87- statusPageID : "statuspageid" ,
88- projectName : "example-project" ,
89- environmentName : "main" ,
90- environmentType : "production" ,
91- buildType : "branch" ,
92- lagoonVersion : "v2.7.x" ,
93- branch : "main" ,
94- projectVars : `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]` ,
95- envVars : `[]` ,
96- lagoonYAML : "../test-resources/identify-feature/alltest/lagoon.yml" ,
97- templatePath : "../test-resources/output" ,
98- },
87+ name : "test3 check if force flag is defined in build variables" ,
88+ varName : "ROOTLESS_WORKLOAD" ,
89+ args : testdata .GetSeedData (
90+ testdata.TestData {
91+ ProjectName : "example-project" ,
92+ EnvironmentName : "main" ,
93+ Branch : "main" ,
94+ LagoonYAML : "../internal/testdata/node/lagoon.yml" ,
95+ }, true ),
96+ templatePath : "testdata/output" ,
9997 vars : []helpers.EnvironmentVariable {
10098 {
10199 Name : "LAGOON_FEATURE_FLAG_FORCE_ROOTLESS_WORKLOAD" ,
@@ -105,22 +103,16 @@ func TestIdentifyFeatureFlag(t *testing.T) {
105103 want : "enabled" ,
106104 },
107105 {
108- name : "test4 check if force flag is defined in build variables and default flag is ignored" ,
109- args : args {
110- name : "ROOTLESS_WORKLOAD" ,
111- alertContact : "alertcontact" ,
112- statusPageID : "statuspageid" ,
113- projectName : "example-project" ,
114- environmentName : "main" ,
115- environmentType : "production" ,
116- buildType : "branch" ,
117- lagoonVersion : "v2.7.x" ,
118- branch : "main" ,
119- projectVars : `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]` ,
120- envVars : `[]` ,
121- lagoonYAML : "../test-resources/identify-feature/alltest/lagoon.yml" ,
122- templatePath : "../test-resources/output" ,
123- },
106+ name : "test4 check if force flag is defined in build variables and default flag is ignored" ,
107+ varName : "ROOTLESS_WORKLOAD" ,
108+ args : testdata .GetSeedData (
109+ testdata.TestData {
110+ ProjectName : "example-project" ,
111+ EnvironmentName : "main" ,
112+ Branch : "main" ,
113+ LagoonYAML : "../internal/testdata/node/lagoon.yml" ,
114+ }, true ),
115+ templatePath : "testdata/output" ,
124116 vars : []helpers.EnvironmentVariable {
125117 {
126118 Name : "LAGOON_FEATURE_FLAG_FORCE_ROOTLESS_WORKLOAD" ,
@@ -134,22 +126,22 @@ func TestIdentifyFeatureFlag(t *testing.T) {
134126 want : "enabled" ,
135127 },
136128 {
137- name : "test5 check if force flag is defined in build variables and one defined in lagoon project variables is ignored" ,
138- args : args {
139- name : "ROOTLESS_WORKLOAD" ,
140- alertContact : "alertcontact" ,
141- statusPageID : "statuspageid " ,
142- projectName : "example-project " ,
143- environmentName : "main" ,
144- environmentType : "production " ,
145- buildType : "branch" ,
146- lagoonVersion : "v2.7.x" ,
147- branch : "main " ,
148- projectVars : `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":" enabled","scope":"build"}]` ,
149- envVars : `[]` ,
150- lagoonYAML : "../test-resources/identify-feature/alltest/lagoon.yml" ,
151- templatePath : "../test-resources/output" ,
152- } ,
129+ name : "test5 check if force flag is defined in build variables and one defined in lagoon project variables is ignored" ,
130+ varName : "ROOTLESS_WORKLOAD" ,
131+ args : testdata . GetSeedData (
132+ testdata. TestData {
133+ ProjectName : "example-project " ,
134+ EnvironmentName : "main " ,
135+ Branch : "main" ,
136+ LagoonYAML : "../internal/testdata/node/lagoon.yml " ,
137+ ProjectVariables : []lagoon. EnvironmentVariable {
138+ {
139+ Name : "LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD " ,
140+ Value : " enabled" ,
141+ Scope : "build" ,
142+ } ,
143+ } ,
144+ }, true ) ,
153145 vars : []helpers.EnvironmentVariable {
154146 {
155147 Name : "LAGOON_FEATURE_FLAG_FORCE_ROOTLESS_WORKLOAD" ,
@@ -163,22 +155,22 @@ func TestIdentifyFeatureFlag(t *testing.T) {
163155 want : "disabled" ,
164156 },
165157 {
166- name : "test6 check if default flag is ignored and lagoon project variable is used" ,
167- args : args {
168- name : "ROOTLESS_WORKLOAD" ,
169- alertContact : "alertcontact" ,
170- statusPageID : "statuspageid " ,
171- projectName : "example-project " ,
172- environmentName : "main" ,
173- environmentType : "production " ,
174- buildType : "branch" ,
175- lagoonVersion : "v2.7.x" ,
176- branch : "main " ,
177- projectVars : `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":" enabled","scope":"build"}]` ,
178- envVars : `[]` ,
179- lagoonYAML : "../test-resources/identify-feature/alltest/lagoon.yml" ,
180- templatePath : "../test-resources/output" ,
181- } ,
158+ name : "test6 check if default flag is ignored and lagoon project variable is used" ,
159+ varName : "ROOTLESS_WORKLOAD" ,
160+ args : testdata . GetSeedData (
161+ testdata. TestData {
162+ ProjectName : "example-project " ,
163+ EnvironmentName : "main " ,
164+ Branch : "main" ,
165+ LagoonYAML : "../internal/testdata/node/lagoon.yml " ,
166+ ProjectVariables : []lagoon. EnvironmentVariable {
167+ {
168+ Name : "LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD " ,
169+ Value : " enabled" ,
170+ Scope : "build" ,
171+ } ,
172+ } ,
173+ }, true ) ,
182174 vars : []helpers.EnvironmentVariable {
183175 {
184176 Name : "LAGOON_FEATURE_FLAG_DEFAULT_ROOTLESS_WORKLOAD" ,
@@ -191,89 +183,18 @@ func TestIdentifyFeatureFlag(t *testing.T) {
191183 for _ , tt := range tests {
192184 t .Run (tt .name , func (t * testing.T ) {
193185 // set the environment variables from args
194- err := os .Setenv ("MONITORING_ALERTCONTACT" , tt .args .alertContact )
195- if err != nil {
196- t .Errorf ("%v" , err )
197- }
198- err = os .Setenv ("MONITORING_STATUSPAGEID" , tt .args .statusPageID )
199- if err != nil {
200- t .Errorf ("%v" , err )
201- }
202- err = os .Setenv ("PROJECT" , tt .args .projectName )
203- if err != nil {
204- t .Errorf ("%v" , err )
205- }
206- err = os .Setenv ("ENVIRONMENT" , tt .args .environmentName )
207- if err != nil {
208- t .Errorf ("%v" , err )
209- }
210- err = os .Setenv ("BRANCH" , tt .args .branch )
211- if err != nil {
212- t .Errorf ("%v" , err )
213- }
214- err = os .Setenv ("PR_NUMBER" , tt .args .prNumber )
215- if err != nil {
216- t .Errorf ("%v" , err )
217- }
218- err = os .Setenv ("PR_HEAD_BRANCH" , tt .args .prHeadBranch )
219- if err != nil {
220- t .Errorf ("%v" , err )
221- }
222- err = os .Setenv ("PR_BASE_BRANCH" , tt .args .prBaseBranch )
186+ savedTemplates := tt .templatePath
187+ generator , err := testdata .SetupEnvironment (* rootCmd , savedTemplates , tt .args )
223188 if err != nil {
224189 t .Errorf ("%v" , err )
225190 }
226- err = os .Setenv ("ENVIRONMENT_TYPE" , tt .args .environmentType )
227- if err != nil {
228- t .Errorf ("%v" , err )
229- }
230- err = os .Setenv ("BUILD_TYPE" , tt .args .buildType )
231- if err != nil {
232- t .Errorf ("%v" , err )
233- }
234- err = os .Setenv ("ACTIVE_ENVIRONMENT" , tt .args .activeEnvironment )
235- if err != nil {
236- t .Errorf ("%v" , err )
237- }
238- err = os .Setenv ("STANDBY_ENVIRONMENT" , tt .args .standbyEnvironment )
239- if err != nil {
240- t .Errorf ("%v" , err )
241- }
242- err = os .Setenv ("LAGOON_FASTLY_NOCACHE_SERVICE_ID" , tt .args .cacheNoCache )
243- if err != nil {
244- t .Errorf ("%v" , err )
245- }
246- err = os .Setenv ("LAGOON_PROJECT_VARIABLES" , tt .args .projectVars )
247- if err != nil {
248- t .Errorf ("%v" , err )
249- }
250- err = os .Setenv ("LAGOON_ENVIRONMENT_VARIABLES" , tt .args .envVars )
251- if err != nil {
252- t .Errorf ("%v" , err )
253- }
254- err = os .Setenv ("LAGOON_VERSION" , tt .args .lagoonVersion )
255- if err != nil {
256- t .Errorf ("%v" , err )
257- }
258- generator , err := generatorInput (false )
259- if err != nil {
260- t .Errorf ("%v" , err )
261- }
262- generator .LagoonYAML = tt .args .lagoonYAML
263- // add dbaasclient overrides for tests
264- generator .DBaaSClient = dbaasclient .NewClient (dbaasclient.Client {
265- RetryMax : 5 ,
266- RetryWaitMin : time .Duration (10 ) * time .Millisecond ,
267- RetryWaitMax : time .Duration (50 ) * time .Millisecond ,
268- })
269-
270191 for _ , envVar := range tt .vars {
271192 err = os .Setenv (envVar .Name , envVar .Value )
272193 if err != nil {
273194 t .Errorf ("%v" , err )
274195 }
275196 }
276- got , err := IdentifyFeatureFlag (generator , tt .args . name )
197+ got , err := IdentifyFeatureFlag (generator , tt .varName )
277198 if (err != nil ) != tt .wantErr {
278199 t .Errorf ("IdentifyFeatureFlag() error = %v, wantErr %v" , err , tt .wantErr )
279200 return
0 commit comments