Skip to content

Commit

Permalink
Merge 8c35d1d into 4c187f9
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Nov 16, 2017
2 parents 4c187f9 + 8c35d1d commit 9657db3
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 5 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Expand Up @@ -225,11 +225,6 @@
<artifactId>jcabi-email</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.github.detro</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.amihaiemil.web</groupId>
<artifactId>camel</artifactId>
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/co/comdor/Container.java
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of comdor nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package co.comdor;

import org.slf4j.Logger;

/**
* A container where the scripts are run.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.3
*/
public interface Container extends AutoCloseable {

/**
* Start this container.
* @return Started Container.
*/
Container start();

/**
* Execute some scripts.
* @param scripts To execute.
* @param logger Logger to write the output into.
*/
void execute(final String scripts, final Logger logger);

@Override
void close();

/**
* Is this container started or not?
* @return boolean.
*/
boolean isStarted();
}
91 changes: 91 additions & 0 deletions src/main/java/co/comdor/Docker.java
@@ -0,0 +1,91 @@
/**
* Copyright (c) 2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of comdor nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package co.comdor;

import org.slf4j.Logger;

/**
* A Docker container where the scripts are run.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.3
* @todo #49:30min Continue implementing this class. It should use a Docker
* client which communicates with the Docker Host and starts a container,
* executes something and can close (terminate) the container.
*/
public final class Docker implements Container {

/**
* Is this container started or not?
*/
private final boolean started;

/**
* Image of this container.
*/
private final String image;

/**
* Ctor.
* @param image Image for this container.
*/
public Docker(final String image) {
this(false, image);
}

/**
* Private ctor (for immutability).
* @param started Is it started, or not?
* @param image Image for this container.
*/
private Docker(final boolean started, final String image) {
this.started = started;
this.image = image;
}

@Override
public Container start() {
return new Docker(true, this.image);
}

@Override
public void execute(final String scripts, final Logger logger) {
if(!this.started) {
throw new IllegalStateException("Docker container not started!");
}
}

@Override
public void close() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean isStarted() {
return this.started;
}

}
73 changes: 73 additions & 0 deletions src/test/java/co/comdor/DockerTestCase.java
@@ -0,0 +1,73 @@
/**
* Copyright (c) 2017, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of comdor nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package co.comdor;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;

/**
* Unit tests for {@link Docker}.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.3
*/
public final class DockerTestCase {

/**
* Docker can start.
*/
@Test
public void startsContainer() {
Container container = new Docker("amihaiemil/comdor");
MatcherAssert.assertThat(container.isStarted(), Matchers.is(false));
MatcherAssert.assertThat(
container.start().isStarted(), Matchers.is(true)
);
}

/**
* Docker throws ISE if we try to execute something when it's not started.
*/
@Test(expected = IllegalStateException.class)
public void executionFailsIfContainerIsStopped() {
Container container = new Docker("amihaiemil/comdor");
container.execute("cloc .", Mockito.mock(Logger.class));
}

/**
* Docker can be closed.
* TODO: edit this test when the method will be implemented.
*/
@Test(expected = UnsupportedOperationException.class)
public void containerCloses() {
Container container = new Docker("amihaiemil/comdor");
container.close();
}

}

0 comments on commit 9657db3

Please sign in to comment.