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

fix(container.instance): refactored code to remove unreachable code #5212

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private synchronized void updateState(final UnaryOperator<State> update) {
this.state = newState;
}

private Optional<ContainerInstanceDescriptor> getExistingContainer(final String containerName) {
private Optional<ContainerInstanceDescriptor> getExistingContainerByName(final String containerName) {
return containerOrchestrationService.listContainerDescriptors().stream()
.filter(c -> c.getContainerName().equals(containerName)).findAny();
}
Expand Down Expand Up @@ -289,35 +289,37 @@ public State onConnect() {
}

private State updateStateInternal(ContainerInstanceOptions newOptions) {
if (!newOptions.isEnabled()) {
return new Disabled(newOptions);
}

final Optional<String> existingContainerId;
final Optional<ContainerInstanceDescriptor> existingContainer;
final boolean isInstanceEnabled = newOptions.isEnabled();

try {
existingContainerId = getExistingContainer(newOptions.getContainerConfiguration().getContainerName())
.map(ContainerInstanceDescriptor::getContainerName);
existingContainer = getExistingContainerByName(newOptions.getContainerConfiguration().getContainerName());
} catch (final Exception e) {
logger.warn("failed to get existing container state", e);
return new Disabled(newOptions);
}

if (existingContainerId.isPresent()) {
if (existingContainer.isPresent()) {

logger.info("found existing container with name {}",
newOptions.getContainerConfiguration().getContainerName());

if (newOptions.isEnabled()) {
if (isInstanceEnabled) {
return new Starting(newOptions);
} else {
return new Created(newOptions, existingContainerId.get()).onDisabled();
return new Created(newOptions, existingContainer.get().getContainerId()).onDisabled();
}

} else {
return new Starting(newOptions);
}

}
if (isInstanceEnabled) {
return new Starting(newOptions);
} else {
return new Disabled(newOptions);
}

}
}
}

private class Starting implements State {
Expand Down Expand Up @@ -360,7 +362,7 @@ public State onDisabled() {
this.startupFuture.cancel(true);

try {
final Optional<ContainerInstanceDescriptor> existingInstance = getExistingContainer(
final Optional<ContainerInstanceDescriptor> existingInstance = getExistingContainerByName(
this.options.getContainerName());

if (existingInstance.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,28 @@ public void deactivateContainerInstanceWithEnabledContainerWorks() throws KuraEx
thenDeleteContainerWasCalledFor("1234");
}

@Test
public void disabledInstanceDeletesAlreadyRunningContainerWithSameName()
throws KuraException, InterruptedException {

givenContainerOrchestratorWithRunningContainer("test-instance", "test-id");

givenContainerInstanceWith(this.mockContainerOrchestrationService);

givenPropertiesWith(CONTAINER_ENABLED, false);
givenPropertiesWith(CONTAINER_IMAGE, "nginx");
givenPropertiesWith(CONTAINER_IMAGE_TAG, "latest");
givenPropertiesWith(CONTAINER_NAME, "test-instance");
givenPropertiesWith("kura.service.pid", "test-instance");
mattdibi marked this conversation as resolved.
Show resolved Hide resolved
givenContainerInstanceActivatedWith(this.properties);

thenNoExceptionOccurred();
thenWaitForContainerInstanceToBecome(CONTAINER_STATE_DISABLED);
thenStopContainerWasCalledFor("test-id");
thenDeleteContainerWasCalledFor("test-id");

}

@Test
public void signatureValidationDoesntGetCalledWithMissingTrustAnchor() throws KuraException, InterruptedException {
givenContainerOrchestratorWithNoRunningContainers();
Expand Down
Loading