@@ -6,6 +6,7 @@ import com.cloudogu.gitops.scmm.api.Permission
66import com.cloudogu.gitops.scmm.api.Repository
77import com.cloudogu.gitops.scmm.api.ScmmApiClient
88import com.cloudogu.gitops.utils.*
9+ import groovy.util.logging.Slf4j
910import groovy.yaml.YamlSlurper
1011import org.apache.commons.io.FileUtils
1112import org.eclipse.jgit.api.CloneCommand
@@ -16,10 +17,12 @@ import org.junit.jupiter.api.AfterAll
1617import org.junit.jupiter.api.Test
1718import org.mockito.ArgumentCaptor
1819
20+ import static groovy.test.GroovyAssert.shouldFail
1921import static org.assertj.core.api.Assertions.assertThat
2022import static org.mockito.ArgumentMatchers.*
2123import static org.mockito.Mockito.*
2224
25+ @Slf4j
2326class ContentTest {
2427 // bareRepo
2528 static List<File > foldersToDelete = new ArrayList<File > ()
@@ -191,16 +194,36 @@ class ContentTest {
191194 }
192195
193196 @Test
194- void ' Checks out commit refs and tags for content repos' () {
197+ void ' Checks out commit refs, tags and non-default branches for content repos' () {
195198 config. content. repos = [
196- new Config.ContentSchema.ContentRepositorySchema (url : createContentRepo(' nonFolderBasedRepo1' ), ref : ' someTag' , folderBased : false , target : ' common/tag' ),
197- new Config.ContentSchema.ContentRepositorySchema (url : createContentRepo(' nonFolderBasedRepo2' ), ref : ' 56d2e3f4b7c95d5645c823f7be8ea6f8a853ac40' , folderBased : false , target : ' common/ref' )
199+ new Config.ContentSchema.ContentRepositorySchema (url : createContentRepo(), ref : ' someTag' , folderBased : false , target : ' common/tag' ),
200+ new Config.ContentSchema.ContentRepositorySchema (url : createContentRepo(), ref : ' 8bc1d1165468359b16d9771d4a9a3df26afc03e8' , folderBased : false , target : ' common/ref' ),
201+ new Config.ContentSchema.ContentRepositorySchema (url : createContentRepo(), ref : ' someBranch' , folderBased : false , target : ' common/branch' )
198202 ]
199203
200204 def repos = createContent(). cloneContentRepos()
201205
202206 assertThat (new File (findRoot(repos), " common/tag/README.md" )). exists(). isFile()
207+ assertThat (new File (findRoot(repos), " common/tag/README.md" ). text). contains(" someTag" )
208+
203209 assertThat (new File (findRoot(repos), " common/ref/README.md" )). exists(). isFile()
210+ assertThat (new File (findRoot(repos), " common/ref/README.md" ). text). contains(" main" )
211+
212+ assertThat (new File (findRoot(repos), " common/branch/README.md" )). exists(). isFile()
213+ assertThat (new File (findRoot(repos), " common/branch/README.md" ). text). contains(" someBranch" )
214+ }
215+
216+ @Test
217+ void ' Fails if commit refs does not exit' () {
218+ config. content. repos = [
219+ new Config.ContentSchema.ContentRepositorySchema (url : createContentRepo(), ref : ' someTag' , folderBased : false , target : ' common/tag' ),
220+ new Config.ContentSchema.ContentRepositorySchema (url : createContentRepo(), ref : ' does/not/exist' , folderBased : true , target : ' does not matter' ),
221+ ]
222+
223+ def exception = shouldFail (RuntimeException ) {
224+ createContent(). cloneContentRepos()
225+ }
226+ assertThat (exception. message). startsWith(" Reference 'does/not/exist' not found in repository" )
204227 }
205228
206229 @Test
@@ -466,36 +489,38 @@ class ContentTest {
466489 }
467490
468491
469- static String createContentRepo (String srcPath ) {
492+ static String createContentRepo (String srcPath = ' ' ) {
470493 // The bare repo works as the "remote"
471494 def bareRepoDir = File . createTempDir(' gitops-playground-test-content-repo' )
472495 bareRepoDir. deleteOnExit()
473496 foldersToDelete << bareRepoDir
474497 // init with bare repo
475498 FileUtils . copyDirectory(new File (System . getProperty(" user.dir" ) + " /src/test/groovy/com/cloudogu/gitops/utils/data/git-repository/" ), bareRepoDir)
476499 def bareRepoUri = ' file://' + bareRepoDir. absolutePath
477- println (" Repo $srcPath : bare repo $bareRepoUri " )
478-
479- // Add srcPath to bare repo
480- def tempRepo = File . createTempDir(' gitops-playground-temp-repo' )
481- tempRepo. deleteOnExit()
482- foldersToDelete << tempRepo
483- println (" Repo $srcPath : cloned bare repo to $tempRepo " )
484- def git = Git . cloneRepository()
485- .setURI(bareRepoUri)
486- .setBranch(' main' )
487- .setDirectory(tempRepo)
488- .call()
489-
490- FileUtils . copyDirectory(new File (System . getProperty(" user.dir" ) + ' /src/test/groovy/com/cloudogu/gitops/utils/data/contentRepos/' + srcPath), tempRepo)
491-
492- git. add(). addFilepattern(" ." ). call()
493-
494- // Avoid complications with local developer's git config, e.g. when git config --global commit.gpgSign true
495- SystemReader . getInstance(). userConfig. clear()
496- git. commit(). setMessage(" Initialize with $srcPath " ). call()
497- git. push(). call()
498- tempRepo. delete()
500+ log. debug(" Repo $srcPath : bare repo $bareRepoUri " )
501+
502+ if (srcPath) {
503+ // Add srcPath to bare repo
504+ def tempRepo = File . createTempDir(' gitops-playground-temp-repo' )
505+ tempRepo. deleteOnExit()
506+ foldersToDelete << tempRepo
507+ log. debug(" Repo $srcPath : cloned bare repo to $tempRepo " )
508+ def git = Git . cloneRepository()
509+ .setURI(bareRepoUri)
510+ .setBranch(' main' )
511+ .setDirectory(tempRepo)
512+ .call()
513+
514+ FileUtils . copyDirectory(new File (System . getProperty(" user.dir" ) + ' /src/test/groovy/com/cloudogu/gitops/utils/data/contentRepos/' + srcPath), tempRepo)
515+
516+ git. add(). addFilepattern(" ." ). call()
517+
518+ // Avoid complications with local developer's git config, e.g. when git config --global commit.gpgSign true
519+ SystemReader . getInstance(). userConfig. clear()
520+ git. commit(). setMessage(" Initialize with $srcPath " ). call()
521+ git. push(). call()
522+ tempRepo. delete()
523+ }
499524
500525 return bareRepoUri
501526 }
0 commit comments