Skip to content

Commit 922f1a9

Browse files
committed
add unittests
1 parent 11a8c16 commit 922f1a9

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/main/groovy/com/cloudogu/gitops/config/CommonFeatureConfig.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import groovy.util.logging.Slf4j
77
class CommonFeatureConfig extends Feature {
88
@Override
99
void preConfigInit(Config configToSet) {
10+
validateConfig(configToSet)
11+
}
12+
13+
/**
14+
* Make sure that config does not contain contradictory values.
15+
* Throws RuntimeException which meaningful message, if invalid.
16+
*/
17+
void validateConfig(Config configToSet) {
1018
validateScmmAndJenkinsAreBothSet(configToSet)
1119
validateMirrorReposHelmChartFolderSet(configToSet)
1220
}

src/test/groovy/com/cloudogu/gitops/ApplicationConfiguratorTest.groovy

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package com.cloudogu.gitops
22

33
import com.cloudogu.gitops.config.ApplicationConfigurator
4+
import com.cloudogu.gitops.config.CommonFeatureConfig
45
import com.cloudogu.gitops.config.Config
6+
import com.cloudogu.gitops.features.Content
7+
import com.cloudogu.gitops.features.Jenkins
8+
import com.cloudogu.gitops.features.argocd.ArgoCD
9+
import com.cloudogu.gitops.scmm.ScmmRepoProvider
10+
import com.cloudogu.gitops.scmm.api.ScmmApiClient
511
import com.cloudogu.gitops.utils.FileSystemUtils
12+
import com.cloudogu.gitops.utils.HelmClient
13+
import com.cloudogu.gitops.utils.K8sClient
614
import com.cloudogu.gitops.utils.TestLogger
715
import org.junit.jupiter.api.BeforeEach
816
import org.junit.jupiter.api.Test
17+
import org.mockito.Mockito
918

1019
import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable
1120
import static groovy.test.GroovyAssert.shouldFail
@@ -22,6 +31,10 @@ class ApplicationConfiguratorTest {
2231
private ApplicationConfigurator applicationConfigurator
2332
private FileSystemUtils fileSystemUtils
2433
private TestLogger testLogger
34+
private CommonFeatureConfig commonFeatureConfig
35+
private Content featureContent
36+
private ArgoCD featureArgoCd
37+
2538
Config testConfig = Config.fromMap([
2639
application: [
2740
localHelmChartFolder : 'someValue',
@@ -61,6 +74,14 @@ class ApplicationConfiguratorTest {
6174
fileSystemUtils = new FileSystemUtils()
6275
applicationConfigurator = new ApplicationConfigurator(fileSystemUtils)
6376
testLogger = new TestLogger(applicationConfigurator.getClass())
77+
commonFeatureConfig = new CommonFeatureConfig()
78+
79+
K8sClient k8sClient = Mockito.mock(K8sClient)
80+
HelmClient helmClient = Mockito.mock(HelmClient)
81+
ScmmRepoProvider scmmRepoProvider = Mockito.mock(ScmmRepoProvider)
82+
83+
featureContent = Mockito.spy(new Content(testConfig, k8sClient, scmmRepoProvider, Mockito.mock(ScmmApiClient), Mockito.mock(Jenkins)))
84+
featureArgoCd = Mockito.spy(new ArgoCD(testConfig, k8sClient, helmClient, fileSystemUtils, scmmRepoProvider))
6485
}
6586

6687
@Test
@@ -107,21 +128,21 @@ class ApplicationConfiguratorTest {
107128
testConfig.scmm.url = ''
108129

109130
def exception = shouldFail(RuntimeException) {
110-
applicationConfigurator.validateConfig(testConfig)
131+
commonFeatureConfig.validateConfig(testConfig)
111132
}
112133
assertThat(exception.message).isEqualTo('When setting jenkins URL, scmm URL must also be set and the other way round')
113134

114135
testConfig.jenkins.url = ''
115136
testConfig.scmm.url = 'external'
116137

117138
exception = shouldFail(RuntimeException) {
118-
applicationConfigurator.validateConfig(testConfig)
139+
commonFeatureConfig.validateConfig(testConfig)
119140
}
120141
assertThat(exception.message).isEqualTo('When setting jenkins URL, scmm URL must also be set and the other way round')
121142

122143

123144
testConfig.jenkins.active = false
124-
applicationConfigurator.validateConfig(testConfig)
145+
commonFeatureConfig.validateConfig(testConfig)
125146
// no exception when jenkins is not active
126147
}
127148

@@ -131,7 +152,7 @@ class ApplicationConfiguratorTest {
131152
testConfig.application.localHelmChartFolder = ''
132153

133154
def exception = shouldFail(RuntimeException) {
134-
applicationConfigurator.validateConfig(testConfig)
155+
commonFeatureConfig.validateConfig(testConfig)
135156
}
136157
assertThat(exception.message).isEqualTo('Missing config for localHelmChartFolder.\n' +
137158
'Either run inside the official container image or setting env var LOCAL_HELM_CHART_FOLDER=\'charts\' ' +
@@ -150,11 +171,12 @@ class ApplicationConfiguratorTest {
150171

151172
@Test
152173
void 'Fails if content repo is set without mandatory params'() {
174+
153175
testConfig.content.repos = [
154176
new Config.ContentSchema.ContentRepositorySchema(url: ''),
155177
]
156178
def exception = shouldFail(RuntimeException) {
157-
applicationConfigurator.validateConfig(testConfig)
179+
featureContent.preConfigInit(testConfig)
158180
}
159181
assertThat(exception.message).isEqualTo('content.repos requires a url parameter.')
160182

@@ -163,7 +185,7 @@ class ApplicationConfiguratorTest {
163185
new Config.ContentSchema.ContentRepositorySchema(url: 'abc', type: Config.ContentRepoType.COPY, target: "missing_slash"),
164186
]
165187
exception = shouldFail(RuntimeException) {
166-
applicationConfigurator.validateConfig(testConfig)
188+
featureContent.preConfigInit(testConfig)
167189
}
168190
assertThat(exception.message).isEqualTo('content.target needs / to separate namespace/group from repo name. Repo: abc')
169191
}
@@ -174,7 +196,7 @@ class ApplicationConfiguratorTest {
174196
new Config.ContentSchema.ContentRepositorySchema(url: 'abc', type: Config.ContentRepoType.COPY),
175197
]
176198
def exception = shouldFail(RuntimeException) {
177-
applicationConfigurator.validateConfig(testConfig)
199+
featureContent.preConfigInit(testConfig)
178200
}
179201
assertThat(exception.message).isEqualTo('content.repos.type COPY requires content.repos.target to be set. Repo: abc')
180202
}
@@ -185,7 +207,7 @@ class ApplicationConfiguratorTest {
185207
new Config.ContentSchema.ContentRepositorySchema(url: 'abc', type: Config.ContentRepoType.FOLDER_BASED, target: 'namespace/repo'),
186208
]
187209
def exception = shouldFail(RuntimeException) {
188-
applicationConfigurator.validateConfig(testConfig)
210+
featureContent.preConfigInit(testConfig)
189211
}
190212
assertThat(exception.message).isEqualTo('content.repos.type FOLDER_BASED does not support target parameter. Repo: abc')
191213

@@ -194,7 +216,7 @@ class ApplicationConfiguratorTest {
194216
new Config.ContentSchema.ContentRepositorySchema(url: 'abc', type: Config.ContentRepoType.FOLDER_BASED, targetRef: 'someRef'),
195217
]
196218
exception = shouldFail(RuntimeException) {
197-
applicationConfigurator.validateConfig(testConfig)
219+
featureContent.preConfigInit(testConfig)
198220
}
199221
assertThat(exception.message).isEqualTo('content.repos.type FOLDER_BASED does not support targetRef parameter. Repo: abc')
200222
}
@@ -206,7 +228,7 @@ class ApplicationConfiguratorTest {
206228
new Config.ContentSchema.ContentRepositorySchema(url: 'abc', type: Config.ContentRepoType.MIRROR),
207229
]
208230
def exception = shouldFail(RuntimeException) {
209-
applicationConfigurator.validateConfig(testConfig)
231+
featureContent.preConfigInit(testConfig)
210232
}
211233
assertThat(exception.message).isEqualTo('content.repos.type MIRROR requires content.repos.target to be set. Repo: abc')
212234

@@ -216,7 +238,7 @@ class ApplicationConfiguratorTest {
216238
target: 'namespace/repo', path: 'non-default-path'),
217239
]
218240
exception = shouldFail(RuntimeException) {
219-
applicationConfigurator.validateConfig(testConfig)
241+
featureContent.preConfigInit(testConfig)
220242
}
221243
assertThat(exception.message).isEqualTo("content.repos.type MIRROR does not support path. Current path: non-default-path. Repo: abc")
222244

@@ -226,7 +248,7 @@ class ApplicationConfiguratorTest {
226248
target: 'namespace/repo', templating: true),
227249
]
228250
exception = shouldFail(RuntimeException) {
229-
applicationConfigurator.validateConfig(testConfig)
251+
featureContent.preConfigInit(testConfig)
230252
}
231253
assertThat(exception.message).isEqualTo('content.repos.type MIRROR does not support templating. Repo: abc')
232254
}
@@ -466,6 +488,7 @@ class ApplicationConfiguratorTest {
466488

467489
def exception = shouldFail(IllegalArgumentException) {
468490
applicationConfigurator.initConfig(testConfig)
491+
featureArgoCd.postConfigInit(testConfig)
469492
}
470493

471494
assertThat(exception.message).contains("Each env variable in features.argocd.env must be a map with 'name' and 'value'. Invalid entry found: [value:value2]")
@@ -482,6 +505,7 @@ class ApplicationConfiguratorTest {
482505

483506
def exception = shouldFail(IllegalArgumentException) {
484507
applicationConfigurator.initConfig(testConfig)
508+
featureArgoCd.postConfigInit(testConfig)
485509
}
486510

487511
assertThat(exception.message).contains("Each env variable in features.argocd.env must be a map with 'name' and 'value'. Invalid entry found: [name:ENV_VAR_2]")
@@ -498,6 +522,7 @@ class ApplicationConfiguratorTest {
498522

499523
def exception = shouldFail(IllegalArgumentException) {
500524
applicationConfigurator.initConfig(testConfig)
525+
featureArgoCd.postConfigInit(testConfig)
501526
}
502527

503528
assertThat(exception.message).contains("Each env variable in features.argocd.env must be a map with 'name' and 'value'. Invalid entry found: invalid_entry")

0 commit comments

Comments
 (0)