Skip to content

Commit

Permalink
feat: create permission controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Jan 6, 2024
1 parent 7d662a7 commit 9c546c0
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}

Original file line number Diff line number Diff line change
@@ -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<Permission> 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);

}
}
Original file line number Diff line number Diff line change
@@ -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);
});

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.onecx.permission.test;

public abstract class AbstractTest {
}
10 changes: 0 additions & 10 deletions src/test/resources/mockserver/permission-mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,5 @@
"httpResponse": {
"statusCode": 500
}
},
{
"id": "5",
"httpRequest": {
"method" : "PUT",
"path": "/operator/v1/update/test-error-2"
},
"httpResponse": {
"statusCode": 400
}
}
]

0 comments on commit 9c546c0

Please sign in to comment.