Search in sources :

Example 1 with Collector

use of io.fabric8.support.api.Collector in project fabric8 by jboss-fuse.

the class SupportServiceImpl method collect.

@Override
public File collect() {
    String name = String.format("%s-%s", "SUPPORT", DATE_TIME_SUFFIX.format(new Date()));
    File result = null;
    ZipOutputStream file = null;
    try {
        result = File.createTempFile(name, ".zip");
        LOGGER.info("Collecting information for support in file {}", result.getAbsolutePath());
        file = new ZipOutputStream(new FileOutputStream(result));
        for (Collector collector : collectors) {
            for (Resource resource : collector.collect(resourceFactory)) {
                collectFromResource(file, resource);
            }
        }
    } catch (IOException e) {
        LOGGER.error("Exception occured while collecting support information - resulting support file may not be usable", e);
    } finally {
        IOHelpers.close(file);
    }
    return result;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) Collector(io.fabric8.support.api.Collector) Resource(io.fabric8.support.api.Resource) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date)

Example 2 with Collector

use of io.fabric8.support.api.Collector in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildServiceTest method testFailedBuild.

@Test(expected = Fabric8ServiceException.class)
public void testFailedBuild() throws Exception {
    BuildService.BuildServiceConfig config = defaultConfig.build();
    WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, false, 50, false, false);
    OpenShiftMockServer mockServer = collector.getMockServer();
    OpenShiftClient client = mockServer.createOpenShiftClient();
    OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config);
    service.build(image);
}
Also used : OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) BuildService(io.fabric8.maven.core.service.BuildService) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Test(org.junit.Test)

Example 3 with Collector

use of io.fabric8.support.api.Collector in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildServiceTest method testSuccessfulSecondBuild.

@Test
public void testSuccessfulSecondBuild() throws Exception {
    int nTries = 0;
    boolean bTestComplete = false;
    do {
        try {
            nTries++;
            BuildService.BuildServiceConfig config = defaultConfig.build();
            WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, true, true);
            OpenShiftMockServer mockServer = collector.getMockServer();
            OpenShiftClient client = mockServer.createOpenShiftClient();
            OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config);
            service.build(image);
            assertTrue(mockServer.getRequestCount() > 8);
            collector.assertEventsRecordedInOrder("build-config-check", "patch-build-config", "pushed");
            collector.assertEventsNotRecorded("new-build-config");
            bTestComplete = true;
        } catch (Fabric8ServiceException exception) {
            Throwable rootCause = getRootCause(exception);
            logger.warn("A problem encountered while running test {}, retrying..", exception.getMessage());
            // Let's wait for a while, and then retry again
            if (rootCause != null && rootCause instanceof IOException) {
                continue;
            }
        }
    } while (nTries < MAX_TIMEOUT_RETRIES && !bTestComplete);
}
Also used : OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) BuildService(io.fabric8.maven.core.service.BuildService) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with Collector

use of io.fabric8.support.api.Collector in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildServiceTest method createMockServer.

protected WebServerEventCollector<OpenShiftMockServer> createMockServer(BuildService.BuildServiceConfig config, boolean success, long buildDelay, boolean buildConfigExists, boolean imageStreamExists) {
    OpenShiftMockServer mockServer = new OpenShiftMockServer(false);
    WebServerEventCollector<OpenShiftMockServer> collector = new WebServerEventCollector<>(mockServer);
    BuildConfig bc = new BuildConfigBuilder().withNewMetadata().withName(projectName + config.getS2iBuildNameSuffix()).endMetadata().withNewSpec().endSpec().build();
    ImageStream imageStream = new ImageStreamBuilder().withNewMetadata().withName(projectName).endMetadata().withStatus(new ImageStreamStatusBuilder().addNewTagLike(new NamedTagEventListBuilder().addNewItem().withImage("abcdef0123456789").endItem().build()).endTag().build()).build();
    KubernetesList builds = new KubernetesListBuilder().withItems(new BuildBuilder().withNewMetadata().withName(projectName).endMetadata().build()).withNewMetadata().withResourceVersion("1").endMetadata().build();
    String buildStatus = success ? "Complete" : "Fail";
    Build build = new BuildBuilder().withNewMetadata().withResourceVersion("2").endMetadata().withNewStatus().withPhase(buildStatus).endStatus().build();
    if (!buildConfigExists) {
        mockServer.expect().get().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix()).andReply(collector.record("build-config-check").andReturn(404, "")).once();
        mockServer.expect().post().withPath("/oapi/v1/namespaces/test/buildconfigs").andReply(collector.record("new-build-config").andReturn(201, bc)).once();
    } else {
        mockServer.expect().patch().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix()).andReply(collector.record("patch-build-config").andReturn(200, bc)).once();
    }
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix()).andReply(collector.record("build-config-check").andReturn(200, bc)).always();
    if (!imageStreamExists) {
        mockServer.expect().get().withPath("/oapi/v1/namespaces/test/imagestreams/" + projectName).andReturn(404, "").once();
    }
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/imagestreams/" + projectName).andReturn(200, imageStream).always();
    mockServer.expect().post().withPath("/oapi/v1/namespaces/test/imagestreams").andReturn(201, imageStream).once();
    mockServer.expect().post().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix() + "/instantiatebinary?commit=").andReply(collector.record("pushed").andReturn(201, imageStream)).once();
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/builds").andReply(collector.record("check-build").andReturn(200, builds)).always();
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/builds?labelSelector=openshift.io/build-config.name%3D" + projectName + config.getS2iBuildNameSuffix()).andReturn(200, builds).always();
    mockServer.expect().withPath("/oapi/v1/namespaces/test/builds/" + projectName).andReturn(200, build).always();
    mockServer.expect().withPath("/oapi/v1/namespaces/test/builds?fieldSelector=metadata.name%3D" + projectName + "&watch=true").andUpgradeToWebSocket().open().waitFor(buildDelay).andEmit(new WatchEvent(build, "MODIFIED")).done().always();
    return collector;
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ImageStreamStatusBuilder(io.fabric8.openshift.api.model.ImageStreamStatusBuilder) ImageStream(io.fabric8.openshift.api.model.ImageStream) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) WebServerEventCollector(io.fabric8.maven.core.util.WebServerEventCollector) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) BuildBuilder(io.fabric8.openshift.api.model.BuildBuilder) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) Build(io.fabric8.openshift.api.model.Build) NamedTagEventListBuilder(io.fabric8.openshift.api.model.NamedTagEventListBuilder) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent)

Example 5 with Collector

use of io.fabric8.support.api.Collector in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildServiceTest method checkTarPackage.

@Test
public void checkTarPackage() throws Exception {
    int nTries = 0;
    boolean bTestComplete = false;
    do {
        try {
            nTries++;
            BuildService.BuildServiceConfig config = defaultConfig.build();
            WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, true, true);
            OpenShiftMockServer mockServer = collector.getMockServer();
            OpenShiftClient client = mockServer.createOpenShiftClient();
            final OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config);
            ImageConfiguration imageWithEnv = new ImageConfiguration.Builder(image).buildConfig(new BuildImageConfiguration.Builder(image.getBuildConfiguration()).env(Collections.singletonMap("FOO", "BAR")).build()).build();
            service.createBuildArchive(imageWithEnv);
            final List<ArchiverCustomizer> customizer = new LinkedList<>();
            new Verifications() {

                {
                    archiveService.createDockerBuildArchive(withInstanceOf(ImageConfiguration.class), withInstanceOf(MojoParameters.class), withCapture(customizer));
                    assertTrue(customizer.size() == 1);
                }
            };
            customizer.get(0).customize(tarArchiver);
            final List<File> file = new LinkedList<>();
            new Verifications() {

                {
                    String path;
                    tarArchiver.addFile(withCapture(file), path = withCapture());
                    assertEquals(".s2i/environment", path);
                }
            };
            assertEquals(1, file.size());
            List<String> lines;
            try (FileReader reader = new FileReader(file.get(0))) {
                lines = IOUtils.readLines(reader);
            }
            assertTrue(lines.contains("FOO=BAR"));
            bTestComplete = true;
        } catch (Fabric8ServiceException exception) {
            Throwable rootCause = getRootCause(exception);
            logger.warn("A problem encountered while running test {}, retrying..", exception.getMessage());
            // Let's wait for a while, and then retry again
            if (rootCause != null && rootCause instanceof IOException) {
                continue;
            }
        }
    } while (nTries < MAX_TIMEOUT_RETRIES && !bTestComplete);
}
Also used : BuildService(io.fabric8.maven.core.service.BuildService) ArchiverCustomizer(io.fabric8.maven.docker.assembly.ArchiverCustomizer) ImageStreamStatusBuilder(io.fabric8.openshift.api.model.ImageStreamStatusBuilder) BuildBuilder(io.fabric8.openshift.api.model.BuildBuilder) NamedTagEventListBuilder(io.fabric8.openshift.api.model.NamedTagEventListBuilder) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) IOException(java.io.IOException) Verifications(mockit.Verifications) LinkedList(java.util.LinkedList) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) MojoParameters(io.fabric8.maven.docker.util.MojoParameters) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

Aggregations

OpenShiftMockServer (io.fabric8.openshift.client.server.mock.OpenShiftMockServer)5 BuildService (io.fabric8.maven.core.service.BuildService)4 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)3 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)3 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)2 BuildBuilder (io.fabric8.openshift.api.model.BuildBuilder)2 BuildConfigBuilder (io.fabric8.openshift.api.model.BuildConfigBuilder)2 ImageStreamBuilder (io.fabric8.openshift.api.model.ImageStreamBuilder)2 ImageStreamStatusBuilder (io.fabric8.openshift.api.model.ImageStreamStatusBuilder)2 NamedTagEventListBuilder (io.fabric8.openshift.api.model.NamedTagEventListBuilder)2 File (java.io.File)2 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)1 WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)1 WebServerEventCollector (io.fabric8.maven.core.util.WebServerEventCollector)1 ArchiverCustomizer (io.fabric8.maven.docker.assembly.ArchiverCustomizer)1 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)1 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)1