Skip to content

Commit bc23cba

Browse files
create tests for create projects
1 parent 8fe3285 commit bc23cba

2 files changed

Lines changed: 126 additions & 4 deletions

File tree

‎src/main/groovy/com/cloudogu/gitops/cli/GitopsPlaygroundCli.groovy‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class GitopsPlaygroundCli {
9696
app = context.getBean(Application)
9797
app.start()
9898

99-
printWelcomeScreen()
99+
printWelcomeScreen(config)
100100
}
101101

102102
return ReturnCode.SUCCESS
@@ -225,8 +225,8 @@ class GitopsPlaygroundCli {
225225
return map as Map
226226
}
227227

228-
void printWelcomeScreen() {
229-
def password = Config.application.password
228+
void printWelcomeScreen(Config newConfig) {
229+
def password = newConfig.application.password
230230
log.info '''\n
231231
|----------------------------------------------------------------------------------------------|
232232
| Welcome to the GitOps playground by Cloudogu!

‎src/test/groovy/com/cloudogu/gitops/infrastructure/kubernetes/api/K8sClientTest.groovy‎

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import static groovy.test.GroovyAssert.shouldFail
44
import static org.assertj.core.api.Assertions.assertThat
55

66
import com.cloudogu.gitops.config.Credentials
7+
import com.cloudogu.gitops.config.Config
78

89
import java.nio.file.Files
910
import java.nio.file.Path
@@ -13,6 +14,7 @@ import io.fabric8.kubernetes.api.model.*
1314
import io.fabric8.kubernetes.client.KubernetesClient
1415
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient
1516
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer
17+
import io.fabric8.openshift.api.model.ProjectBuilder
1618
import org.junit.jupiter.api.BeforeEach
1719
import org.junit.jupiter.api.Test
1820
import 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

Comments
 (0)