-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from aslakknutsen/protocol_update
Remap the ports returned by the Container in the ProtocolMetaData
- Loading branch information
Showing
6 changed files
with
259 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
docker/src/main/java/org/arquillian/cube/client/ProtocolMetadataUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.arquillian.cube.client; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jboss.arquillian.container.spi.Container; | ||
import org.jboss.arquillian.container.spi.client.protocol.metadata.HTTPContext; | ||
import org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData; | ||
import org.jboss.arquillian.container.spi.client.protocol.metadata.Servlet; | ||
import org.jboss.arquillian.container.spi.context.annotation.DeploymentScoped; | ||
import org.jboss.arquillian.core.api.Instance; | ||
import org.jboss.arquillian.core.api.InstanceProducer; | ||
import org.jboss.arquillian.core.api.annotation.Inject; | ||
import org.jboss.arquillian.core.api.annotation.Observes; | ||
import org.jboss.arquillian.core.spi.EventContext; | ||
|
||
public class ProtocolMetadataUpdater { | ||
|
||
private static final String EXPOSED_PORT = "exposedPort"; | ||
private static final String PORT = "port"; | ||
private static final String PORT_BINDINGS = "portBindings"; | ||
|
||
@Inject @DeploymentScoped | ||
private InstanceProducer<ProtocolMetaData> protocolMetaDataProducer; | ||
|
||
@Inject | ||
private Instance<CubeConfiguration> configInst; | ||
|
||
@Inject | ||
private Instance<Container> containerInst; | ||
|
||
public void update(@Observes EventContext<ProtocolMetaData> eventContext) { | ||
|
||
ProtocolMetaData originalMetaData = eventContext.getEvent(); | ||
ProtocolMetaData updatedMetaData = new ProtocolMetaData(); | ||
boolean updated = false; | ||
|
||
Map<Integer, Integer> portMapping = getPortMappings(); | ||
for(Object contextObj : originalMetaData.getContexts()) { | ||
if(contextObj instanceof HTTPContext) { | ||
HTTPContext context = (HTTPContext)contextObj; | ||
Integer mapped = portMapping.get(context.getPort()); | ||
if(mapped != null && mapped != context.getPort()) { | ||
updated = true; | ||
HTTPContext newContext = new HTTPContext(context.getHost(), mapped); | ||
for(Servlet servlet : context.getServlets()) { | ||
newContext.add(servlet); | ||
} | ||
updatedMetaData.addContext(newContext); | ||
} | ||
|
||
} else { | ||
updatedMetaData.addContext(contextObj); | ||
} | ||
} | ||
|
||
if(updated) { | ||
protocolMetaDataProducer.set(updatedMetaData); | ||
} else { | ||
eventContext.proceed(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Map<Integer, Integer> getPortMappings() { | ||
Map<Integer, Integer> result = new HashMap<Integer, Integer>(); | ||
|
||
CubeConfiguration config = configInst.get(); | ||
Container container = containerInst.get(); | ||
|
||
if(config == null || container == null) { | ||
return result; | ||
} | ||
|
||
Map<String, Object> containerConfig = (Map<String, Object>)config.getDockerContainersContent().get(container.getName()); | ||
List<Object> bindings = (List<Object>)containerConfig.get(PORT_BINDINGS); | ||
for(Object bindingObj: bindings) { | ||
Map<String, Object> binding = (Map<String, Object>)bindingObj; | ||
Integer port = (Integer)binding.get(PORT); | ||
String exposedPort = (String)binding.get(EXPOSED_PORT); | ||
if(exposedPort.indexOf("/") != -1) { | ||
exposedPort = exposedPort.substring(0, exposedPort.indexOf("/")); | ||
} | ||
result.put(Integer.parseInt(exposedPort), port); | ||
} | ||
|
||
return result; | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
docker/src/test/java/org/arquillian/cube/client/ProtocolMetaDataUpdaterTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package org.arquillian.cube.client; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jboss.arquillian.config.descriptor.impl.ContainerDefImpl; | ||
import org.jboss.arquillian.container.impl.ContainerImpl; | ||
import org.jboss.arquillian.container.spi.Container; | ||
import org.jboss.arquillian.container.spi.client.container.DeployableContainer; | ||
import org.jboss.arquillian.container.spi.client.deployment.Deployment; | ||
import org.jboss.arquillian.container.spi.client.protocol.metadata.HTTPContext; | ||
import org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData; | ||
import org.jboss.arquillian.container.spi.client.protocol.metadata.Servlet; | ||
import org.jboss.arquillian.container.spi.context.ContainerContext; | ||
import org.jboss.arquillian.container.spi.context.DeploymentContext; | ||
import org.jboss.arquillian.container.spi.context.annotation.ContainerScoped; | ||
import org.jboss.arquillian.container.spi.context.annotation.DeploymentScoped; | ||
import org.jboss.arquillian.container.test.AbstractContainerTestBase; | ||
import org.jboss.arquillian.core.api.annotation.ApplicationScoped; | ||
import org.jboss.arquillian.core.spi.Manager; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ProtocolMetaDataUpdaterTestCase extends AbstractContainerTestBase { | ||
|
||
@Mock | ||
private Deployment deployment; | ||
|
||
@Mock | ||
private DeployableContainer<?> deployableContainer; | ||
|
||
@Override | ||
protected void addExtensions(List<Class<?>> extensions) { | ||
super.addExtensions(extensions); | ||
extensions.add(ProtocolMetadataUpdater.class); | ||
} | ||
|
||
@Override | ||
protected void startContexts(Manager manager) { | ||
super.startContexts(manager); | ||
manager.getContext(ContainerContext.class).activate("test"); | ||
manager.getContext(DeploymentContext.class).activate(deployment); | ||
} | ||
|
||
@Test | ||
public void shouldNotUpdateWhenPortsAreEqual() throws Exception { | ||
Map<String, String> configuration = new HashMap<String, String>(); | ||
configuration.put("dockerContainers", "test:\n portBindings:\n - exposedPort: 80/tcp\n ports: 80\n"); | ||
|
||
bind(ContainerScoped.class, | ||
Container.class, | ||
new ContainerImpl("test", deployableContainer, new ContainerDefImpl("arquillian.xml"))); | ||
|
||
getManager().bind(ApplicationScoped.class, CubeConfiguration.class, CubeConfiguration.fromMap(configuration)); | ||
|
||
ProtocolMetaData metadata = new ProtocolMetaData(); | ||
metadata.addContext(new HTTPContext("localhost", 80).add(new Servlet("A", "B"))); | ||
|
||
bind(DeploymentScoped.class, ProtocolMetaData.class, metadata); | ||
fire(metadata); | ||
|
||
ProtocolMetaData updated = getManager().getContext(DeploymentContext.class) | ||
.getObjectStore().get(ProtocolMetaData.class); | ||
|
||
Assert.assertEquals(metadata.getContexts(HTTPContext.class).iterator().next(), updated.getContexts(HTTPContext.class).iterator().next()); | ||
assertEventFired(ProtocolMetaData.class, 1); | ||
} | ||
|
||
@Test | ||
public void shouldUpdateWhenPortsAreNotEqual() throws Exception { | ||
Map<String, String> configuration = new HashMap<String, String>(); | ||
configuration.put("dockerContainers", "test:\n portBindings:\n - exposedPort: 80/tcp\n port: 90\n"); | ||
|
||
bind(ContainerScoped.class, | ||
Container.class, | ||
new ContainerImpl("test", deployableContainer, new ContainerDefImpl("arquillian.xml"))); | ||
|
||
getManager().bind(ApplicationScoped.class, CubeConfiguration.class, CubeConfiguration.fromMap(configuration)); | ||
|
||
ProtocolMetaData metadata = new ProtocolMetaData(); | ||
metadata.addContext(new HTTPContext("localhost", 80).add(new Servlet("A", "B"))); | ||
|
||
bind(DeploymentScoped.class, ProtocolMetaData.class, metadata); | ||
fire(metadata); | ||
|
||
ProtocolMetaData updated = getManager().getContext(DeploymentContext.class) | ||
.getObjectStore().get(ProtocolMetaData.class); | ||
|
||
Assert.assertEquals(90, updated.getContexts(HTTPContext.class).iterator().next().getPort()); | ||
assertEventFired(ProtocolMetaData.class, 1); // twice, but original fire is intercepted and never hit the Counter | ||
} | ||
|
||
@Test | ||
public void shouldNotFailOnMissingContainer() throws Exception { | ||
Map<String, String> configuration = new HashMap<String, String>(); | ||
configuration.put("dockerContainers", "test:\n portBindings:\n - exposedPort: 80/tcp\n port: 90\n"); | ||
|
||
getManager().bind(ApplicationScoped.class, CubeConfiguration.class, CubeConfiguration.fromMap(configuration)); | ||
|
||
ProtocolMetaData metadata = new ProtocolMetaData(); | ||
metadata.addContext(new HTTPContext("localhost", 80).add(new Servlet("A", "B"))); | ||
|
||
bind(DeploymentScoped.class, ProtocolMetaData.class, metadata); | ||
fire(metadata); | ||
|
||
ProtocolMetaData updated = getManager().getContext(DeploymentContext.class) | ||
.getObjectStore().get(ProtocolMetaData.class); | ||
|
||
Assert.assertEquals(80, updated.getContexts(HTTPContext.class).iterator().next().getPort()); | ||
assertEventFired(ProtocolMetaData.class, 1); | ||
} | ||
|
||
@Test | ||
public void shouldNotFailOnMissingCubeConfiguration() throws Exception { | ||
bind(ContainerScoped.class, | ||
Container.class, | ||
new ContainerImpl("test", deployableContainer, new ContainerDefImpl("arquillian.xml"))); | ||
|
||
ProtocolMetaData metadata = new ProtocolMetaData(); | ||
metadata.addContext(new HTTPContext("localhost", 80).add(new Servlet("A", "B"))); | ||
|
||
bind(DeploymentScoped.class, ProtocolMetaData.class, metadata); | ||
fire(metadata); | ||
|
||
ProtocolMetaData updated = getManager().getContext(DeploymentContext.class) | ||
.getObjectStore().get(ProtocolMetaData.class); | ||
|
||
Assert.assertEquals(80, updated.getContexts(HTTPContext.class).iterator().next().getPort()); | ||
assertEventFired(ProtocolMetaData.class, 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters