Search in sources :

Example 21 with Build

use of io.fabric8.openshift.api.model.Build in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testCreateIngressResourceForInvalidResource.

@Test
public void testCreateIngressResourceForInvalidResource() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    ScalableResource scalableResource = Mockito.mock(ScalableResource.class);
    Mockito.when(scalableResource.get()).thenReturn(null);
    HasMetadata invalidMetadata = Mockito.mock(Deployment.class);
    List<HasMetadata> ingressResources = new ArrayList<>();
    ingressResources.add(invalidMetadata);
    Mockito.when(openShiftClient.load(Mockito.any()).get()).thenReturn(getServiceResources(), getDeploymentResources(), ingressResources);
    Service service = createService(openShiftClient, nonNamespaceOperation);
    Deployment deployment = createDeployment(openShiftClient, nonNamespaceOperation, scalableResource);
    Mockito.when(nonNamespaceOperation.create(Mockito.any())).thenReturn(service, deployment);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    try {
        kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    } catch (ContainerBasedGatewayException e) {
        Assert.assertEquals(e.getErrorHandler(), ExceptionCodes.LOADED_RESOURCE_DEFINITION_IS_NOT_VALID);
    }
}
Also used : ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 22 with Build

use of io.fabric8.openshift.api.model.Build in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testCreateIngressResourceForKubernetesException.

@Test
public void testCreateIngressResourceForKubernetesException() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    Mockito.when(openShiftClient.load(Mockito.any()).get()).thenReturn(getServiceResources(), getDeploymentResources(), getIngressResources());
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    ScalableResource scalableResource = Mockito.mock(ScalableResource.class);
    Mockito.when(scalableResource.get()).thenReturn(null);
    Service service = createService(openShiftClient, nonNamespaceOperation);
    Deployment deployment = createDeployment(openShiftClient, nonNamespaceOperation, scalableResource);
    Mockito.when(nonNamespaceOperation.create(Mockito.any())).thenReturn(service, deployment);
    ScalableResource scalableResourceIngress = Mockito.mock(ScalableResource.class);
    String ingressName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX + ContainerBasedGatewayConstants.CMS_INGRESS_SUFFIX;
    Mockito.when(openShiftClient.extensions().ingresses().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withName(ingressName)).thenReturn(scalableResourceIngress);
    Mockito.when(scalableResourceIngress.get()).thenThrow(KubernetesClientException.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    try {
        kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    } catch (ContainerBasedGatewayException e) {
        Assert.assertEquals(e.getErrorHandler(), ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
    }
}
Also used : ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 23 with Build

use of io.fabric8.openshift.api.model.Build in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testGetResourcesFromTemplateWhenResourceIsEmpty.

@Test
public void testGetResourcesFromTemplateWhenResourceIsEmpty() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    List<HasMetadata> serviceResources = new ArrayList<>();
    Mockito.when(openShiftClient.load(Mockito.any()).get()).thenReturn(serviceResources);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    try {
        kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    } catch (ContainerBasedGatewayException e) {
        Assert.assertEquals(e.getErrorHandler(), ExceptionCodes.NO_RESOURCE_LOADED_FROM_DEFINITION);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ArrayList(java.util.ArrayList) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) Test(org.junit.Test)

Example 24 with Build

use of io.fabric8.openshift.api.model.Build in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testCreateServiceResourceForKubernetesClientException.

@Test
public void testCreateServiceResourceForKubernetesClientException() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    Mockito.when(openShiftClient.load(Mockito.any()).get()).thenReturn(getServiceResources());
    NonNamespaceOperation nonNamespaceOperationService = Mockito.mock(NonNamespaceOperation.class);
    BaseOperation baseOperationService = Mockito.mock(BaseOperation.class);
    String serviceName = ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX + ContainerBasedGatewayConstants.CMS_SERVICE_SUFFIX;
    Mockito.when(openShiftClient.services().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperationService);
    Mockito.when(nonNamespaceOperationService.withName(serviceName)).thenReturn(baseOperationService);
    Mockito.when(baseOperationService.get()).thenThrow(KubernetesClientException.class);
    API api = SampleTestObjectCreator.createDefaultAPI().context("").build();
    try {
        kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    } catch (ContainerBasedGatewayException e) {
        Assert.assertEquals(e.getErrorHandler(), ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) BaseOperation(io.fabric8.kubernetes.client.dsl.base.BaseOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 25 with Build

use of io.fabric8.openshift.api.model.Build in project docker-maven-plugin by fabric8io.

the class BuildService method buildImage.

/**
 * Build an image
 *
 * @param imageConfig the image configuration
 * @param params mojo params for the project
 * @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
 * @param buildArgs
 * @throws DockerAccessException
 * @throws MojoExecutionException
 */
protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean noCache, Map<String, String> buildArgs) throws DockerAccessException, MojoExecutionException {
    String imageName = imageConfig.getName();
    ImageName.validate(imageName);
    BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
    String oldImageId = null;
    CleanupMode cleanupMode = buildConfig.cleanupMode();
    if (cleanupMode.isRemove()) {
        oldImageId = queryService.getImageId(imageName);
    }
    long time = System.currentTimeMillis();
    if (buildConfig.getDockerArchive() != null) {
        docker.loadImage(imageName, buildConfig.getAbsoluteDockerTarPath(params));
        log.info("%s: Loaded tarball in %s", buildConfig.getDockerArchive(), EnvUtil.formatDurationTill(time));
        return;
    }
    File dockerArchive = archiveService.createArchive(imageName, buildConfig, params, log);
    log.info("%s: Created %s in %s", imageConfig.getDescription(), dockerArchive.getName(), EnvUtil.formatDurationTill(time));
    Map<String, String> mergedBuildMap = prepareBuildArgs(buildArgs, buildConfig);
    // auto is now supported by docker, consider switching?
    BuildOptions opts = new BuildOptions(buildConfig.getBuildOptions()).dockerfile(getDockerfileName(buildConfig)).forceRemove(cleanupMode.isRemove()).noCache(noCache).buildArgs(mergedBuildMap);
    String newImageId = doBuildImage(imageName, dockerArchive, opts);
    log.info("%s: Built image %s", imageConfig.getDescription(), newImageId);
    if (oldImageId != null && !oldImageId.equals(newImageId)) {
        try {
            docker.removeImage(oldImageId, true);
            log.info("%s: Removed old image %s", imageConfig.getDescription(), oldImageId);
        } catch (DockerAccessException exp) {
            if (cleanupMode == CleanupMode.TRY_TO_REMOVE) {
                log.warn("%s: %s (old image)%s", imageConfig.getDescription(), exp.getMessage(), (exp.getCause() != null ? " [" + exp.getCause().getMessage() + "]" : ""));
            } else {
                throw exp;
            }
        }
    }
}
Also used : BuildOptions(io.fabric8.maven.docker.access.BuildOptions) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) CleanupMode(io.fabric8.maven.docker.config.CleanupMode) File(java.io.File) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Aggregations

Test (org.junit.Test)255 ArrayList (java.util.ArrayList)74 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)69 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)68 HashMap (java.util.HashMap)67 File (java.io.File)53 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)51 IOException (java.io.IOException)50 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)45 Pod (io.fabric8.kubernetes.api.model.Pod)38 Map (java.util.Map)35 Service (io.fabric8.kubernetes.api.model.Service)34 FabricService (io.fabric8.api.FabricService)33 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)30 Container (io.fabric8.api.Container)29 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)28 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)27 List (java.util.List)26 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)25 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)25