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

Improve unit tests #3

Merged
merged 1 commit into from
Apr 30, 2014
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
16 changes: 16 additions & 0 deletions build-client/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{42} - %msg%n</pattern>
</encoder>
</appender>

<logger name="nl.tudelft.ewi.build" level="INFO"></logger>

<root level="WARN">
<appender-ref ref="STDOUT" />
</root>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@

@NoArgsConstructor(access = AccessLevel.NONE)
class CommandParser {

public static List<String> parse(String command) {
List<String> commands = Lists.newArrayList();

int characterIndex = 0;
boolean inDoubleQuotes = false;

while (characterIndex < command.length()) {
char character = command.charAt(characterIndex);
if (character == '\"') {
int lastCommand = commands.size() - 1;
String last = lastCommand >= 0 ? commands.remove(lastCommand) : "";
commands.add(last + character);
inDoubleQuotes = !inDoubleQuotes;
}
else if (character == ' ') {
Expand All @@ -38,7 +41,7 @@ else if (character == ' ') {
}
characterIndex++;
}

return commands;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package nl.tudelft.ewi.build.builds;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
import lombok.SneakyThrows;
import nl.tudelft.ewi.build.Config;
import nl.tudelft.ewi.build.docker.DockerJob;
import nl.tudelft.ewi.build.docker.DockerManager;
import nl.tudelft.ewi.build.docker.DockerManager.Logger;
import nl.tudelft.ewi.build.jaxrs.models.BuildRequest;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

@RunWith(MockitoJUnitRunner.class)
public class BuildManagerTest {

public static final int CONCURRENT_JOBS = 3;
public static final int JOB_DURATION = 500;

@Mock
private Config config;

@Mock
private DockerManager dockerManager;

private BuildManager manager;

@Before
public void setUp() {
when(config.getMaximumConcurrentJobs()).thenReturn(CONCURRENT_JOBS);

doAnswer(new Answer<Void>() {
@Override
@SneakyThrows
public Void answer(InvocationOnMock invocation) throws Throwable {
Logger logger = (Logger) invocation.getArguments()[0];
Thread.sleep(JOB_DURATION);
logger.onClose(0);
return null;
}
}).when(dockerManager)
.run(any(Logger.class), any(DockerJob.class));

manager = new BuildManager(dockerManager, config);
}

@Test
public void testThatWeCanScheduleSingleJob() {
assertNotNull(manager.schedule(new BuildRequest()));
}

@Test
public void testThatWeCanScheduleMultipleJobs() {
for (int i = 0; i < CONCURRENT_JOBS - 1; i++) {
manager.schedule(new BuildRequest());
}
assertNotNull(manager.schedule(new BuildRequest()));
}

@Test
public void testThatJobIsRejectedWhenAtCapacity() {
for (int i = 0; i < CONCURRENT_JOBS; i++) {
manager.schedule(new BuildRequest());
}
assertNull(manager.schedule(new BuildRequest()));
}

@Test
public void testThatJobCanBeScheduledWhenCapacityIsRestored() throws InterruptedException {
for (int i = 0; i < CONCURRENT_JOBS; i++) {
manager.schedule(new BuildRequest());
}

Thread.sleep(JOB_DURATION + 10);
assertNotNull(manager.schedule(new BuildRequest()));
}

@Test
public void testThatJobCanBeScheduledWhenCapacityIsRestoredThroughTermination() throws InterruptedException {
for (int i = 0; i < CONCURRENT_JOBS - 1; i++) {
manager.schedule(new BuildRequest());
}

assertTrue(manager.killBuild(manager.schedule(new BuildRequest())));

Thread.sleep(100);
assertNotNull(manager.schedule(new BuildRequest()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package nl.tudelft.ewi.build.docker;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.google.common.collect.Lists;

@RunWith(Parameterized.class)
public class CommandParserTest {

@Parameters
public static Collection<Object[]> getParameters() {
return Arrays.asList(new Object[][] {
{ "", Lists.newArrayList() },
{ "mvn", Lists.newArrayList("mvn") },
{ "mvn package", Lists.newArrayList("mvn", "package") },
{ "mvn package -DskipTests=true", Lists.newArrayList("mvn", "package", "-DskipTests=true") },
{ "echo \"hello\"", Lists.newArrayList("echo", "\"hello\"") },
{ "echo \"hello world\"", Lists.newArrayList("echo", "\"hello world\"") },
});
}

private final String command;
private final List<String> parts;

public CommandParserTest(String command, List<String> parts) {
this.command = command;
this.parts = parts;
}

@Test
public void verifyParseCommand() {
List<String> parsedCommand = CommandParser.parse(command);
Assert.assertEquals(parts, parsedCommand);
}

}