Search in sources :

Example 1 with KubernetesHelper

use of org.eclipse.jkube.kit.common.util.KubernetesHelper in project jkube by eclipse.

the class OpenShiftResourceTaskTest method runTask_resolvesGroupInImageNameToNamespaceSetViaConfiguration_whenNoNamespaceDetected.

@Test
public void runTask_resolvesGroupInImageNameToNamespaceSetViaConfiguration_whenNoNamespaceDetected() {
    try (MockedStatic<KubernetesHelper> kubernetesHelper = Mockito.mockStatic(KubernetesHelper.class)) {
        // Given
        kubernetesHelper.when(KubernetesHelper::getDefaultNamespace).thenReturn("test-custom-namespace");
        kubernetesHelper.when(() -> KubernetesHelper.getKind(any())).thenReturn("DeploymentConfig");
        kubernetesHelper.when(() -> KubernetesHelper.getName((HasMetadata) any())).thenReturn("test-project");
        ImageConfiguration imageConfiguration = ImageConfiguration.builder().name("%g/%a").build(BuildConfiguration.builder().from("test-base-image:latest").build()).build();
        extension.images = Collections.singletonList(imageConfiguration);
        OpenShiftResourceTask resourceTask = new OpenShiftResourceTask(OpenShiftExtension.class);
        // When
        resourceTask.runTask();
        // Then
        assertThat(resourceTask.resolvedImages).hasSize(1).singleElement().hasFieldOrPropertyWithValue("name", "test-custom-namespace/test-project");
    }
}
Also used : ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) KubernetesHelper(org.eclipse.jkube.kit.common.util.KubernetesHelper) Test(org.junit.Test)

Example 2 with KubernetesHelper

use of org.eclipse.jkube.kit.common.util.KubernetesHelper in project jkube by eclipse.

the class OpenShiftWatchTaskTest method setUp.

@Before
public void setUp() throws IOException {
    clusterAccessMockedConstruction = mockConstruction(ClusterAccess.class, (mock, ctx) -> {
        final OpenShiftClient openShiftClient = mock(OpenShiftClient.class);
        when(openShiftClient.getMasterUrl()).thenReturn(new URL("http://openshiftapps.com:6443"));
        when(mock.createDefaultClient()).thenReturn(openShiftClient);
    });
    watcherManagerMockedStatic = mockStatic(WatcherManager.class);
    kubernetesHelperMockedStatic = mockStatic(KubernetesHelper.class);
    extension = new TestOpenShiftExtension();
    when(taskEnvironment.project.getExtensions().getByType(OpenShiftExtension.class)).thenReturn(extension);
    kubernetesHelperMockedStatic.when(KubernetesHelper::getDefaultNamespace).thenReturn(null);
    extension.isFailOnNoKubernetesJson = false;
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) ClusterAccess(org.eclipse.jkube.kit.config.access.ClusterAccess) URL(java.net.URL) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertThrows(org.junit.Assert.assertThrows) Mockito.mockStatic(org.mockito.Mockito.mockStatic) KubernetesHelper(org.eclipse.jkube.kit.common.util.KubernetesHelper) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) OpenShiftExtension(org.eclipse.jkube.gradle.plugin.OpenShiftExtension) Mockito.when(org.mockito.Mockito.when) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) MockedStatic(org.mockito.MockedStatic) TestOpenShiftExtension(org.eclipse.jkube.gradle.plugin.TestOpenShiftExtension) Rule(org.junit.Rule) After(org.junit.After) Mockito.mockConstruction(org.mockito.Mockito.mockConstruction) WatcherManager(org.eclipse.jkube.watcher.api.WatcherManager) Before(org.junit.Before) MockedConstruction(org.mockito.MockedConstruction) Mockito.mock(org.mockito.Mockito.mock) WatcherManager(org.eclipse.jkube.watcher.api.WatcherManager) ClusterAccess(org.eclipse.jkube.kit.config.access.ClusterAccess) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) TestOpenShiftExtension(org.eclipse.jkube.gradle.plugin.TestOpenShiftExtension) URL(java.net.URL) KubernetesHelper(org.eclipse.jkube.kit.common.util.KubernetesHelper) Before(org.junit.Before)

Example 3 with KubernetesHelper

use of org.eclipse.jkube.kit.common.util.KubernetesHelper in project jkube by eclipse.

the class KubernetesWatchTaskTest method setUp.

@Before
public void setUp() throws IOException {
    // Mock required for environments with no DOCKER available (don't remove)
    dockerAccessFactoryMockedConstruction = mockConstruction(DockerAccessFactory.class, (mock, ctx) -> when(mock.createDockerAccess(any())).thenReturn(mock(DockerAccess.class)));
    dockerBuildServiceMockedConstruction = mockConstruction(DockerBuildService.class, (mock, ctx) -> {
        when(mock.isApplicable()).thenReturn(true);
    });
    clusterAccessMockedConstruction = mockConstruction(ClusterAccess.class, (mock, ctx) -> {
        final KubernetesClient kubernetesClient = mock(KubernetesClient.class);
        when(kubernetesClient.getMasterUrl()).thenReturn(new URL("http://kubernetes-cluster"));
        when(mock.createDefaultClient()).thenReturn(kubernetesClient);
    });
    watcherManagerMockedStatic = mockStatic(WatcherManager.class);
    kubernetesHelperMockedStatic = mockStatic(KubernetesHelper.class);
    extension = new TestKubernetesExtension();
    when(taskEnvironment.project.getExtensions().getByType(KubernetesExtension.class)).thenReturn(extension);
    kubernetesHelperMockedStatic.when(KubernetesHelper::getDefaultNamespace).thenReturn(null);
    extension.isFailOnNoKubernetesJson = false;
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ClusterAccess(org.eclipse.jkube.kit.config.access.ClusterAccess) URL(java.net.URL) DockerBuildService(org.eclipse.jkube.kit.config.service.kubernetes.DockerBuildService) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertThrows(org.junit.Assert.assertThrows) Mockito.mockStatic(org.mockito.Mockito.mockStatic) AssertionsForClassTypes(org.assertj.core.api.AssertionsForClassTypes) After(org.junit.After) Mockito.mockConstruction(org.mockito.Mockito.mockConstruction) WatcherManager(org.eclipse.jkube.watcher.api.WatcherManager) TestKubernetesExtension(org.eclipse.jkube.gradle.plugin.TestKubernetesExtension) Before(org.junit.Before) MockedConstruction(org.mockito.MockedConstruction) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) DockerAccessFactory(org.eclipse.jkube.kit.build.service.docker.DockerAccessFactory) KubernetesHelper(org.eclipse.jkube.kit.common.util.KubernetesHelper) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) MockedStatic(org.mockito.MockedStatic) Rule(org.junit.Rule) KubernetesExtension(org.eclipse.jkube.gradle.plugin.KubernetesExtension) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DockerAccess(org.eclipse.jkube.kit.build.service.docker.access.DockerAccess) Mockito.mock(org.mockito.Mockito.mock) DockerAccess(org.eclipse.jkube.kit.build.service.docker.access.DockerAccess) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) WatcherManager(org.eclipse.jkube.watcher.api.WatcherManager) ClusterAccess(org.eclipse.jkube.kit.config.access.ClusterAccess) DockerBuildService(org.eclipse.jkube.kit.config.service.kubernetes.DockerBuildService) DockerAccessFactory(org.eclipse.jkube.kit.build.service.docker.DockerAccessFactory) URL(java.net.URL) KubernetesHelper(org.eclipse.jkube.kit.common.util.KubernetesHelper) TestKubernetesExtension(org.eclipse.jkube.gradle.plugin.TestKubernetesExtension) Before(org.junit.Before)

Example 4 with KubernetesHelper

use of org.eclipse.jkube.kit.common.util.KubernetesHelper in project jkube by eclipse.

the class KubernetesLogTaskTest method runTask_withIOException_shouldThrowException.

@Test
public void runTask_withIOException_shouldThrowException() {
    try (MockedStatic<KubernetesHelper> mockStatic = Mockito.mockStatic(KubernetesHelper.class)) {
        // Given
        mockStatic.when(() -> KubernetesHelper.loadResources(any())).thenThrow(new IOException("IO error with logs"));
        KubernetesLogTask kubernetesLogTask = new KubernetesLogTask(KubernetesExtension.class);
        // When
        final Exception result = assertThrows(GradleException.class, kubernetesLogTask::runTask);
        // Then
        assertThat(result).hasMessage("Failure in getting logs");
    }
}
Also used : IOException(java.io.IOException) KubernetesHelper(org.eclipse.jkube.kit.common.util.KubernetesHelper) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) Test(org.junit.Test)

Aggregations

KubernetesHelper (org.eclipse.jkube.kit.common.util.KubernetesHelper)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 URL (java.net.URL)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ClusterAccess (org.eclipse.jkube.kit.config.access.ClusterAccess)2 WatcherManager (org.eclipse.jkube.watcher.api.WatcherManager)2 After (org.junit.After)2 Assert.assertThrows (org.junit.Assert.assertThrows)2 Before (org.junit.Before)2 Rule (org.junit.Rule)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 ArgumentMatchers.isNull (org.mockito.ArgumentMatchers.isNull)2 MockedConstruction (org.mockito.MockedConstruction)2 MockedStatic (org.mockito.MockedStatic)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.mockConstruction (org.mockito.Mockito.mockConstruction)2 Mockito.mockStatic (org.mockito.Mockito.mockStatic)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.when (org.mockito.Mockito.when)2