Search in sources :

Example 1 with ConfigurationService

use of io.javaoperatorsdk.operator.api.config.ConfigurationService in project java-operator-sdk by java-operator-sdk.

the class CustomResourceSelectorTest method resourceWatchedByLabel.

@Test
void resourceWatchedByLabel() {
    assertThat(server).isNotNull();
    assertThat(client).isNotNull();
    Operator o1 = new Operator(client, configurationService);
    Operator o2 = new Operator(client, configurationService);
    try {
        AtomicInteger c1 = new AtomicInteger();
        AtomicInteger c1err = new AtomicInteger();
        AtomicInteger c2 = new AtomicInteger();
        AtomicInteger c2err = new AtomicInteger();
        o1.register(new MyController(resource -> {
            if ("foo".equals(resource.getMetadata().getName())) {
                c1.incrementAndGet();
            }
            if ("bar".equals(resource.getMetadata().getName())) {
                c1err.incrementAndGet();
            }
        }), new MyConfiguration(configurationService, "app=foo"));
        o1.start();
        o2.register(new MyController(resource -> {
            if ("bar".equals(resource.getMetadata().getName())) {
                c2.incrementAndGet();
            }
            if ("foo".equals(resource.getMetadata().getName())) {
                c2err.incrementAndGet();
            }
        }), new MyConfiguration(configurationService, "app=bar"));
        o2.start();
        client.resources(TestCustomResource.class).inNamespace(NAMESPACE).create(newMyResource("foo", NAMESPACE));
        client.resources(TestCustomResource.class).inNamespace(NAMESPACE).create(newMyResource("bar", NAMESPACE));
        await().atMost(5, TimeUnit.SECONDS).pollInterval(100, TimeUnit.MILLISECONDS).until(() -> c1.get() == 1 && c1err.get() == 0);
        await().atMost(5, TimeUnit.SECONDS).pollInterval(100, TimeUnit.MILLISECONDS).until(() -> c2.get() == 1 && c2err.get() == 0);
        assertThrows(ConditionTimeoutException.class, () -> await().atMost(2, TimeUnit.SECONDS).untilAtomic(c1err, is(greaterThan(0))));
        assertThrows(ConditionTimeoutException.class, () -> await().atMost(2, TimeUnit.SECONDS).untilAtomic(c2err, is(greaterThan(0))));
    } finally {
        o1.stop();
        o2.stop();
    }
}
Also used : Operator(io.javaoperatorsdk.operator.Operator) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ControllerConfiguration(io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) UpdateControl(io.javaoperatorsdk.operator.api.reconciler.UpdateControl) Mockito.spy(org.mockito.Mockito.spy) TestCustomResource(io.javaoperatorsdk.operator.sample.simple.TestCustomResource) Reconciler(io.javaoperatorsdk.operator.api.reconciler.Reconciler) Sets(org.mockito.internal.util.collections.Sets) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EnableKubernetesMockClient(io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient) Operator(io.javaoperatorsdk.operator.Operator) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) ConfigurationService(io.javaoperatorsdk.operator.api.config.ConfigurationService) Awaitility.await(org.awaitility.Awaitility.await) Logger(org.slf4j.Logger) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ConditionTimeoutException(org.awaitility.core.ConditionTimeoutException) Test(org.junit.jupiter.api.Test) Context(io.javaoperatorsdk.operator.api.reconciler.Context) KubernetesMockServer(io.fabric8.kubernetes.client.server.mock.KubernetesMockServer) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Version(io.javaoperatorsdk.operator.api.config.Version) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigurationService

use of io.javaoperatorsdk.operator.api.config.ConfigurationService in project java-operator-sdk by java-operator-sdk.

the class CustomResourceSelectorTest method setUpResources.

@SuppressWarnings("unchecked")
@BeforeEach
void setUpResources() {
    configurationService = spy(ConfigurationService.class);
    when(configurationService.checkCRDAndValidateLocalModel()).thenReturn(false);
    when(configurationService.getVersion()).thenReturn(new Version("1", "1", new Date()));
    when(configurationService.getConfigurationFor(any(MyController.class))).thenReturn(new MyConfiguration(configurationService, null));
}
Also used : Version(io.javaoperatorsdk.operator.api.config.Version) ConfigurationService(io.javaoperatorsdk.operator.api.config.ConfigurationService) Date(java.util.Date) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ConfigurationService (io.javaoperatorsdk.operator.api.config.ConfigurationService)2 Version (io.javaoperatorsdk.operator.api.config.Version)2 Date (java.util.Date)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 EnableKubernetesMockClient (io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient)1 KubernetesMockServer (io.fabric8.kubernetes.client.server.mock.KubernetesMockServer)1 Operator (io.javaoperatorsdk.operator.Operator)1 Context (io.javaoperatorsdk.operator.api.reconciler.Context)1 ControllerConfiguration (io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration)1 Reconciler (io.javaoperatorsdk.operator.api.reconciler.Reconciler)1 UpdateControl (io.javaoperatorsdk.operator.api.reconciler.UpdateControl)1 TestCustomResource (io.javaoperatorsdk.operator.sample.simple.TestCustomResource)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Consumer (java.util.function.Consumer)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Awaitility.await (org.awaitility.Awaitility.await)1