@@ -4,6 +4,7 @@ import static groovy.test.GroovyAssert.shouldFail
44import static org.assertj.core.api.Assertions.assertThat
55
66import com.cloudogu.gitops.config.Credentials
7+ import com.cloudogu.gitops.config.Config
78
89import java.nio.file.Files
910import java.nio.file.Path
@@ -13,6 +14,7 @@ import io.fabric8.kubernetes.api.model.*
1314import io.fabric8.kubernetes.client.KubernetesClient
1415import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient
1516import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer
17+ import io.fabric8.openshift.api.model.ProjectBuilder
1618import org.junit.jupiter.api.BeforeEach
1719import org.junit.jupiter.api.Test
1820import org.junit.jupiter.api.io.TempDir
@@ -362,6 +364,68 @@ class K8sClientTest {
362364 // Then - Verify namespace was created
363365 }
364366
367+ @Test
368+ void ' createNamespace creates OpenShift project when openshift config is enabled' () {
369+ // Given
370+ Config config = Config . fromMap([application : [openshift : true ]])
371+ k8sApiClient. gopConfig = config
372+
373+ server. expect()
374+ .get()
375+ .withPath(" /api/v1/namespaces/test-project" )
376+ .andReturn(404 , " " )
377+ .once()
378+
379+ server. expect()
380+ .post()
381+ .withPath(" /apis/project.openshift.io/v1/projects" )
382+ .andReturn(201 , new ProjectBuilder ()
383+ .withNewMetadata()
384+ .withName(" test-project" )
385+ .endMetadata()
386+ .build())
387+ .once()
388+
389+ // When
390+ k8sApiClient. createNamespace(" test-project" )
391+
392+ // Then
393+ def requestBody = new JsonSlurper (). parseText(server. getLastRequest(). getUtf8Body()) as Map
394+ assertThat (requestBody[" kind" ]). isEqualTo(" Project" )
395+ assertThat (requestBody[" metadata" ][" name" ]). isEqualTo(" test-project" )
396+ }
397+
398+ @Test
399+ void ' createNamespace creates Kubernetes namespace when openshift config is disabled' () {
400+ // Given
401+ Config config = Config . fromMap([application : [openshift : false ]])
402+ k8sApiClient. gopConfig = config
403+
404+ server. expect()
405+ .get()
406+ .withPath(" /api/v1/namespaces/test-ns" )
407+ .andReturn(404 , " " )
408+ .once()
409+
410+ server. expect()
411+ .post()
412+ .withPath(" /api/v1/namespaces" )
413+ .andReturn(201 , new NamespaceBuilder ()
414+ .withNewMetadata()
415+ .withName(" test-ns" )
416+ .endMetadata()
417+ .build())
418+ .once()
419+
420+ // When
421+ k8sApiClient. createNamespace(" test-ns" )
422+
423+ // Then
424+ def requestBody = new JsonSlurper (). parseText(server. getLastRequest(). getUtf8Body()) as Map
425+ assertThat (requestBody[" kind" ]). isEqualTo(" Namespace" )
426+ assertThat (requestBody[" metadata" ][" name" ]). isEqualTo(" test-ns" )
427+ }
428+
365429 @Test
366430 void ' createNamespace does not create existing namespace' () {
367431 // Given
@@ -380,7 +444,65 @@ class K8sClientTest {
380444 // When
381445 k8sApiClient. createNamespace(" test-ns" )
382446
383- // Then - No POST request should be made
447+ // Then
448+ assertThat (server. getLastRequest(). method). isEqualTo(" GET" )
449+ assertThat (server. getLastRequest(). path). isEqualTo(" /api/v1/namespaces/test-ns" )
450+ }
451+
452+ @Test
453+ void ' createNamespace creates Kubernetes namespace when config is null' () {
454+ // Given
455+ k8sApiClient. gopConfig = null
456+
457+ server. expect()
458+ .get()
459+ .withPath(" /api/v1/namespaces/test-ns" )
460+ .andReturn(404 , " " )
461+ .once()
462+
463+ server. expect()
464+ .post()
465+ .withPath(" /api/v1/namespaces" )
466+ .andReturn(201 , new NamespaceBuilder ()
467+ .withNewMetadata()
468+ .withName(" test-ns" )
469+ .endMetadata()
470+ .build())
471+ .once()
472+
473+ // When
474+ k8sApiClient. createNamespace(" test-ns" )
475+
476+ // Then
477+ def requestBody = new JsonSlurper (). parseText(server. getLastRequest(). getUtf8Body()) as Map
478+ assertThat (requestBody[" kind" ]). isEqualTo(" Namespace" )
479+ assertThat (requestBody[" metadata" ][" name" ]). isEqualTo(" test-ns" )
480+ }
481+
482+ @Test
483+ void ' createNamespace does not create OpenShift project when namespace already exists' () {
484+ // Given
485+ Config config = Config . fromMap([application : [openshift : true ]])
486+ k8sApiClient. gopConfig = config
487+
488+ def namespace = new NamespaceBuilder ()
489+ .withNewMetadata()
490+ .withName(" existing-project" )
491+ .endMetadata()
492+ .build()
493+
494+ server. expect()
495+ .get()
496+ .withPath(" /api/v1/namespaces/existing-project" )
497+ .andReturn(200 , namespace)
498+ .once()
499+
500+ // When
501+ k8sApiClient. createNamespace(" existing-project" )
502+
503+ // Then
504+ assertThat (server. getLastRequest(). method). isEqualTo(" GET" )
505+ assertThat (server. getLastRequest(). path). isEqualTo(" /api/v1/namespaces/existing-project" )
384506 }
385507
386508 @Test
0 commit comments