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

[OSJC-245] Allow retrieval of container on rc #126

Merged
merged 1 commit into from Mar 16, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,6 +25,7 @@

import com.openshift.internal.restclient.model.volume.VolumeMount;
import com.openshift.internal.restclient.model.volume.VolumeSource;
import com.openshift.internal.util.JBossDmrExtentions;
import com.openshift.restclient.IClient;
import com.openshift.restclient.images.DockerImageURI;
import com.openshift.restclient.model.IContainer;
Expand Down Expand Up @@ -60,8 +61,6 @@ public void setEnvironmentVariable(String name, String value) {
setEnvironmentVariable(null, name, value);
}



@Override
public void setEnvironmentVariable(String containerName, String name, String value) {
String defaultedContainerName = StringUtils.defaultIfBlank(containerName, "");
Expand Down Expand Up @@ -176,6 +175,30 @@ public Collection<String> getImages() {
return list;
}

@Override
public IContainer getContainer(String name) {
if(StringUtils.isBlank(name)) {
return null;
}
ModelNode containers = get(SPEC_TEMPLATE_CONTAINERS);
if(containers.isDefined() && containers.getType() == ModelType.LIST ) {
Optional<ModelNode> first = containers.asList().stream().filter(n->name.equals(JBossDmrExtentions.asString(n, this.propertyKeys, NAME))).findFirst();
if(first.isPresent()) {
return new Container(first.get(), this.propertyKeys);
}
}
return null;
}

@Override
public Collection<IContainer> getContainers() {
ModelNode containers = get(SPEC_TEMPLATE_CONTAINERS);
if(containers.isDefined() && containers.getType() == ModelType.LIST ) {
return containers.asList().stream().map(n->new Container(n, this.propertyKeys)).collect(Collectors.toList());
}
return Collections.emptyList();
}

@Override
public void addTemplateLabel(String key, String value) {
ModelNode labels = get(SPEC_TEMPLATE_LABELS);
Expand Down Expand Up @@ -239,6 +262,15 @@ public IContainer addContainer(String name) {
return container;
}

@Override
public void setContainers(Collection<IContainer> containers) {
ModelNode nodeContainers = get(SPEC_TEMPLATE_CONTAINERS);
nodeContainers.clear();
if (containers != null) {
containers.forEach(c -> nodeContainers.add(ModelNode.fromJSONString(c.toJSONString())));
}
}

@Override
public Set<IVolumeSource> getVolumes() {
ModelNode vol = get(VOLUMES);
Expand Down
Expand Up @@ -118,8 +118,27 @@ public interface IReplicationController extends IResource{
*/
IContainer addContainer(DockerImageURI tag, Set<IPort> containerPorts, Map<String, String> envVars);

/**
* Add a container with the given name
* @param name
* @return
*/
IContainer addContainer(String name);

/**
* Retrieve a container by name or null if it is not found
* @param name
* @return the container or null if not found
*/
IContainer getContainer(String name);

/**
* Retrieve the containers defined in spec
* @param name
* @return collection of containers or empty collection
*/
Collection<IContainer> getContainers();

/**
* Add or update a label to the template spec;
* @param key
Expand All @@ -132,4 +151,6 @@ public interface IReplicationController extends IResource{
* @return
*/
Set<IVolumeSource> getVolumes();

void setContainers(Collection<IContainer> containers);
}
Expand Up @@ -12,6 +12,8 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -45,6 +47,7 @@
import com.openshift.restclient.model.IReplicationController;
import com.openshift.restclient.model.ISecretKeySelector;
import com.openshift.restclient.model.volume.IVolume;
import com.openshift.restclient.model.volume.IVolumeMount;
import com.openshift.restclient.model.volume.IVolumeSource;
import com.openshift.restclient.utils.Samples;

Expand Down Expand Up @@ -178,6 +181,22 @@ public void testGetImages(){
assertArrayEquals(exp , rc.getImages().toArray());
}

@Test
public void testGetContainer() {
assertNull(rc.getContainer(" "));
assertNotNull(rc.getContainer("ruby-helloworld-database"));
}
@Test
public void testGetContainers() {
Collection<IContainer> containers = rc.getContainers();
assertNotNull(containers);
assertEquals(1, containers.size());

String[] path = getPath(ReplicationController.SPEC_TEMPLATE_CONTAINERS);
node.get(path).clear();
assertNotNull(rc.getContainers());
}

@Test
public void testAddContainer() throws JSONException {
//remove containers hack
Expand Down Expand Up @@ -218,5 +237,49 @@ public void testAddContainer() throws JSONException {
Object [] contVolNames = container.getVolumes().stream().map(IVolume::getName).toArray();
assertArrayEquals(sourceNames,contVolNames);
}

@Test
public void testAddContainerAllowsContainerToBeFurtherManipulated() throws JSONException{
//remove containers hack
String[] path = getPath(ReplicationController.SPEC_TEMPLATE_CONTAINERS);
node.get(path).clear();

//setup
DockerImageURI uri = new DockerImageURI("aproject/an_image_name");
IPort port = mock(IPort.class);
when(port.getProtocol()).thenReturn("TCP");
when(port.getContainerPort()).thenReturn(8080);
Set<IPort> ports = new HashSet<>();
ports.add(port);

IVolumeMount mount = mock(IVolumeMount.class);
when(mount.getName()).thenReturn(uri.getName() +"-"+1);
when(mount.getMountPath()).thenReturn("/tmp");
when(mount.isReadOnly()).thenReturn(Boolean.FALSE);
Set<IVolumeMount> mounts = new HashSet<>();
mounts.add(mount);

IContainer container = rc.addContainer(uri.getName());
container.setImage(uri);
container.setPorts(ports);
container.setVolumeMounts(mounts);

ModelNode exp = new ModelNodeBuilder()
.set("name", uri.getName())
.set("image",uri.toString())
.add("ports", new ModelNodeBuilder()
.set("containerPort", port.getContainerPort())
.set("protocol", port.getProtocol())
)
.add("volumeMounts", new ModelNodeBuilder()
.set("name", uri.getName() +"-"+1)
.set("mountPath", "/tmp")
.set("readOnly", false)
)
.build();

JSONAssert.assertEquals(exp.toJSONString(false), container.toJSONString(), true);
}


}