Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always copy logs to circle in finally #384

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-384.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: |-
copy logs to circle even if there's an error initializing in `before`
links:
- https://github.com/palantir/docker-compose-rule/pull/384
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.docker.compose;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.palantir.docker.compose.configuration.DockerComposeFiles;
import com.palantir.docker.compose.connection.waiting.ClusterWait;
import com.palantir.docker.compose.events.EventConsumer;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.mockito.Mockito;

public class DockerComposeExtensionTest {

@Test
public void calls_after_only_once() {
AtomicInteger count = new AtomicInteger();
DockerComposeExtension dockerComposeExtension = new DockerComposeExtension() {
@Override
public void before() {
throw new IllegalStateException("some error");
}

@Override
public void after() {
count.incrementAndGet();
}

@Override
public DockerComposeFiles files() {
return null;
}

@Override
protected List<ClusterWait> clusterWaits() {
return null;
}

@Override
protected List<EventConsumer> eventConsumers() {
return null;
}
};

ExtensionContext extensionContext = Mockito.mock(ExtensionContext.class);
assertThatThrownBy(() -> dockerComposeExtension.beforeAll(extensionContext))
.isInstanceOf(IllegalStateException.class);
dockerComposeExtension.afterAll(extensionContext);
assertThat(count.get()).isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public abstract class DockerComposeManager {
public static final int DEFAULT_RETRY_ATTEMPTS = 2;

private final RunRecorder runRecorder = RunRecorder.defaults();
private boolean hasCalledAfterMethod;

public DockerPort hostNetworkedPort(int port) {
return new DockerPort(machine().getIp(), port, port);
Expand Down Expand Up @@ -177,13 +178,19 @@ protected void setDescription(TestDescription testDescription) {
}

public void before() throws IOException, InterruptedException {
log.debug("Starting docker-compose cluster");
try {
log.debug("Starting docker-compose cluster");

runRecorder.before(() -> dockerCompose().config());
runRecorder.before(() -> dockerCompose().config());

pullBuildAndUp();
pullBuildAndUp();

emitEventsFor().waitingForServices(this::waitForServices);
emitEventsFor().waitingForServices(this::waitForServices);
} catch (RuntimeException e) {
after();
hasCalledAfterMethod = true;
throw e;
}
}

private void pullBuildAndUp() throws IOException, InterruptedException {
Expand Down Expand Up @@ -254,6 +261,10 @@ private void waitForAllClusterWaits(List<InterruptableClusterWait> allClusterWai
}

public void after() {
if (hasCalledAfterMethod) {
return;
}

try {
emitEventsFor().shutdownStop(() ->
shutdownStrategy().stop(this.dockerCompose()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public DockerPort port(int internalPort) {
.stream()
.filter(port -> port.getInternalPort() == internalPort)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No internal port '" + internalPort + "' for container '" + containerName + "': " + portMappings));
.orElseThrow(() -> new IllegalArgumentException("No internal port '" + internalPort + "' "
+ "for container '" + containerName + "': " + portMappings.get()));
}

public void start() throws IOException, InterruptedException {
Expand Down