diff --git a/src/main/helm/values.yaml b/src/main/helm/values.yaml index 564fb6e..6c25f03 100644 --- a/src/main/helm/values.yaml +++ b/src/main/helm/values.yaml @@ -4,7 +4,7 @@ app: repository: "onecx/onecx-permission-operator" env: # See watchNamespaces - "QUARKUS_OPERATOR_SDK_CONTROLLERS_PRODUCT_NAMESPACES": "JOSDK_WATCH_CURRENT" + "QUARKUS_OPERATOR_SDK_CONTROLLERS_PERMISSION_NAMESPACES": "JOSDK_WATCH_CURRENT" envCustom: - name: KUBERNETES_NAMESPACE valueFrom: diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index dfaee94..7c359a6 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,6 +5,7 @@ quarkus.kubernetes-client.devservices.override-kubeconfig=true %prod.quarkus.rest-client.permission_client.url=http://onecx-permission-svc:8080 # BUILD +quarkus.operator-sdk.helm.enabled=true quarkus.openapi-generator.codegen.input-base-dir=target/tmp/openapi quarkus.openapi-generator.codegen.spec.onecx_permission_operator_v1_yaml.config-key=permission_client quarkus.openapi-generator.codegen.spec.onecx_permission_operator_v1_yaml.base-package=gen.io.github.onecx.permission.operator.v1 @@ -17,5 +18,5 @@ quarkus.openapi-generator.codegen.spec.onecx_permission_operator_v1_yaml.return- %test.quarkus.mockserver.devservices.config-dir=/mockserver %test.quarkus.mockserver.devservices.log=false %test.quarkus.mockserver.devservices.reuse=true -%test.quarkus.rest-client.product_store_client.url=${quarkus.mockserver.endpoint} +%test.quarkus.rest-client.permission_client.url=${quarkus.mockserver.endpoint} diff --git a/src/test/java/io/github/onecx/permission/operator/PermissionControllerResponseTest.java b/src/test/java/io/github/onecx/permission/operator/PermissionControllerResponseTest.java new file mode 100644 index 0000000..6b697fa --- /dev/null +++ b/src/test/java/io/github/onecx/permission/operator/PermissionControllerResponseTest.java @@ -0,0 +1,57 @@ +package io.github.onecx.permission.operator; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; + +import java.util.List; + +import jakarta.inject.Inject; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import io.github.onecx.permission.operator.client.PermissionService; +import io.github.onecx.permission.test.AbstractTest; +import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; +import io.quarkus.test.InjectMock; +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +class PermissionControllerResponseTest extends AbstractTest { + + @InjectMock + PermissionService productStoreService; + + @Inject + PermissionController reconciler; + + @BeforeEach + void beforeAll() { + Mockito.when(productStoreService.updatePermission(any())).thenReturn(404); + } + + @Test + void testWrongResponse() throws Exception { + + var p1 = new PermissionSpec.PermissionItemSpec(); + p1.setName("n2"); + p1.setAction("a2"); + p1.setDescription("d1"); + p1.setResource("r1"); + + var s = new PermissionSpec(); + s.setAppId("test-3"); + s.setPermissions(List.of(p1)); + + Permission m = new Permission(); + m.setSpec(s); + + UpdateControl result = reconciler.reconcile(m, null); + assertThat(result).isNotNull(); + assertThat(result.getResource()).isNotNull(); + assertThat(result.getResource().getStatus()).isNotNull(); + assertThat(result.getResource().getStatus().getStatus()).isNotNull().isEqualTo(PermissionStatus.Status.UNDEFINED); + + } +} diff --git a/src/test/java/io/github/onecx/permission/operator/PermissionControllerTest.java b/src/test/java/io/github/onecx/permission/operator/PermissionControllerTest.java new file mode 100644 index 0000000..85ed03f --- /dev/null +++ b/src/test/java/io/github/onecx/permission/operator/PermissionControllerTest.java @@ -0,0 +1,159 @@ +package io.github.onecx.permission.operator; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; + +import java.util.List; + +import jakarta.inject.Inject; + +import org.awaitility.Awaitility; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.github.onecx.permission.test.AbstractTest; +import io.javaoperatorsdk.operator.Operator; +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +class PermissionControllerTest extends AbstractTest { + + final static Logger log = LoggerFactory.getLogger(PermissionControllerTest.class); + + @Inject + Operator operator; + + @Inject + KubernetesClient client; + + @BeforeAll + public static void init() { + Awaitility.setDefaultPollDelay(2, SECONDS); + Awaitility.setDefaultPollInterval(2, SECONDS); + Awaitility.setDefaultTimeout(10, SECONDS); + } + + @Test + void productEmptySpecTest() { + + operator.start(); + + Permission data = new Permission(); + data.setMetadata(new ObjectMetaBuilder().withName("empty-spec").withNamespace(client.getNamespace()).build()); + data.setSpec(new PermissionSpec()); + + log.info("Creating test permission object: {}", data); + client.resource(data).serverSideApply(); + + log.info("Waiting 4 seconds and status muss be still null"); + + await().pollDelay(2, SECONDS).untilAsserted(() -> { + PermissionStatus mfeStatus = client.resource(data).get().getStatus(); + assertThat(mfeStatus).isNotNull(); + assertThat(mfeStatus.getStatus()).isNotNull().isEqualTo(PermissionStatus.Status.ERROR); + }); + } + + @Test + void productNullSpecTest() { + + operator.start(); + + Permission data = new Permission(); + data.setMetadata(new ObjectMetaBuilder().withName("null-spec").withNamespace(client.getNamespace()).build()); + data.setSpec(null); + + log.info("Creating test permission object: {}", data); + client.resource(data).serverSideApply(); + + log.info("Waiting 4 seconds and status muss be still null"); + + await().pollDelay(4, SECONDS).untilAsserted(() -> { + PermissionStatus mfeStatus = client.resource(data).get().getStatus(); + assertThat(mfeStatus).isNull(); + }); + + } + + @Test + void productUpdateEmptySpecTest() { + + operator.start(); + + var p1 = new PermissionSpec.PermissionItemSpec(); + p1.setName("n2"); + p1.setAction("a2"); + p1.setDescription("d1"); + p1.setResource("r1"); + + var m = new PermissionSpec(); + m.setAppId("test-3"); + m.setPermissions(List.of(p1)); + + var data = new Permission(); + data + .setMetadata(new ObjectMetaBuilder().withName("to-update-spec").withNamespace(client.getNamespace()).build()); + data.setSpec(m); + + log.info("Creating test permission object: {}", data); + client.resource(data).serverSideApply(); + + log.info("Waiting 4 seconds and status muss be still null"); + + await().pollDelay(2, SECONDS).untilAsserted(() -> { + PermissionStatus mfeStatus = client.resource(data).get().getStatus(); + assertThat(mfeStatus).isNotNull(); + assertThat(mfeStatus.getStatus()).isNotNull().isEqualTo(PermissionStatus.Status.UPDATED); + }); + + client.resource(data).inNamespace(client.getNamespace()) + .edit(s -> { + s.setSpec(null); + return s; + }); + + await().pollDelay(4, SECONDS).untilAsserted(() -> { + PermissionStatus mfeStatus = client.resource(data).get().getStatus(); + assertThat(mfeStatus).isNotNull(); + assertThat(mfeStatus.getStatus()).isNotNull().isEqualTo(PermissionStatus.Status.UPDATED); + }); + } + + @Test + void productRestClientExceptionTest() { + + operator.start(); + + var p1 = new PermissionSpec.PermissionItemSpec(); + p1.setName("n2"); + p1.setAction("a2"); + p1.setDescription("d1"); + p1.setResource("r1"); + + var m = new PermissionSpec(); + m.setAppId("test-error-1"); + m.setPermissions(List.of(p1)); + + var data = new Permission(); + data + .setMetadata(new ObjectMetaBuilder().withName("client-error").withNamespace(client.getNamespace()).build()); + data.setSpec(m); + + log.info("Creating test permission object: {}", data); + client.resource(data).serverSideApply(); + + log.info("Waiting 4 seconds and status muss be still null"); + + await().pollDelay(2, SECONDS).untilAsserted(() -> { + PermissionStatus mfeStatus = client.resource(data).get().getStatus(); + assertThat(mfeStatus).isNotNull(); + assertThat(mfeStatus.getStatus()).isNotNull().isEqualTo(PermissionStatus.Status.ERROR); + }); + + } +} diff --git a/src/test/java/io/github/onecx/permission/test/AbstractTest.java b/src/test/java/io/github/onecx/permission/test/AbstractTest.java new file mode 100644 index 0000000..8026a95 --- /dev/null +++ b/src/test/java/io/github/onecx/permission/test/AbstractTest.java @@ -0,0 +1,4 @@ +package io.github.onecx.permission.test; + +public abstract class AbstractTest { +} diff --git a/src/test/resources/mockserver/permission-mock.json b/src/test/resources/mockserver/permission-mock.json index 746bb6e..5514f5c 100644 --- a/src/test/resources/mockserver/permission-mock.json +++ b/src/test/resources/mockserver/permission-mock.json @@ -18,15 +18,5 @@ "httpResponse": { "statusCode": 500 } - }, - { - "id": "5", - "httpRequest": { - "method" : "PUT", - "path": "/operator/v1/update/test-error-2" - }, - "httpResponse": { - "statusCode": 400 - } } ] \ No newline at end of file