Skip to content

Commit

Permalink
Merge pull request #1 from borgeorgiev/master
Browse files Browse the repository at this point in the history
Fix for JENKINS-16279
  • Loading branch information
kiy0taka committed Jan 10, 2013
2 parents c0db6d8 + 7d8bb4b commit 71c881f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 83 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/target
/work
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -26,6 +26,12 @@
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
73 changes: 38 additions & 35 deletions src/main/java/org/jenkinsci/plugins/mongodb/MongoBuildWrapper.java
Expand Up @@ -8,13 +8,16 @@
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.FilePath.FileCallable;
import hudson.Launcher;
import hudson.Launcher.ProcStarter;
import hudson.Proc;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Computer;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.util.ArgumentListBuilder;
Expand Down Expand Up @@ -91,23 +94,24 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, final Bui
MongoDBInstallation mongo = getMongoDB()
.forNode(Computer.currentComputer().getNode(), listener)
.forEnvironment(env);

ArgumentListBuilder args = new ArgumentListBuilder().add(mongo.getExecutable(launcher));
final File dbpathFile = setupCmd(args, new File(env.get("WORKSPACE")), false);
final FilePath dbpathFile = setupCmd(launcher,args, build.getWorkspace(), false);

try {
launcher.getChannel().call(new DbpathCleanCommand(dbpathFile));
} catch (Exception e) {
e.printStackTrace(listener.getLogger());
}
return launch(dbpathFile, launcher, args, listener);
dbpathFile.deleteRecursive();
dbpathFile.mkdirs();
return launch(launcher, args, listener);
}

protected Environment launch(final File dbpathFile, final Launcher launcher, ArgumentListBuilder args, final BuildListener listener) throws IOException, InterruptedException {
final Proc proc = launcher.launch().cmds(args).start();
protected Environment launch(final Launcher launcher, ArgumentListBuilder args, final BuildListener listener) throws IOException, InterruptedException {
ProcStarter procStarter = launcher.launch().cmds(args);
listener.getLogger().println("Executing mongodb start command: "+procStarter.cmds());
final Proc proc = procStarter.start();

try {
launcher.getChannel().call(new WaitForStartCommand(listener, port));
Boolean startResult = launcher.getChannel().call(new WaitForStartCommand(listener, port));
if(!startResult) {
listener.getLogger().println("ERROR: Filed to start mongodb");
}
} catch (Exception e) {
e.printStackTrace(listener.getLogger());
return null;
Expand All @@ -118,28 +122,41 @@ protected Environment launch(final File dbpathFile, final Launcher launcher, Arg
public boolean tearDown(AbstractBuild build, BuildListener listener)
throws IOException, InterruptedException {
if (proc.isAlive()) {
listener.getLogger().println("Killing mongodb process...");
proc.kill();
} else {
listener.getLogger().println("Will not kill mongodb process as it is already dead.");
}
return super.tearDown(build, listener);
}
};
}

protected File setupCmd(ArgumentListBuilder args, File workspace, boolean fork) {
protected FilePath setupCmd(Launcher launcher, ArgumentListBuilder args, FilePath workspace, boolean fork) throws IOException, InterruptedException {

if (fork) args.add("--fork");
args.add("--logpath").add(new File(workspace, "mongodb.log").getPath());
if (fork) {
args.add("--fork");
}
args.add("--logpath").add(workspace.child("mongodb.log").getRemote());

File dbpathFile;
FilePath dbpathFile;
if (isEmpty(dbpath)) {
dbpathFile = new File(workspace, "/data/db");
dbpathFile = workspace.child("data").child("db");
} else {
dbpathFile = new File(dbpath);
if (!dbpathFile.isAbsolute()) {
dbpathFile = new File(workspace, dbpath);
dbpathFile = new FilePath(launcher.getChannel(),dbpath);
boolean isAbsolute = dbpathFile.act(new FileCallable<Boolean>() {

public Boolean invoke(File f, VirtualChannel channel){
return f.isAbsolute();
}
});

if (!isAbsolute) {
dbpathFile = workspace.child(dbpath);
}
}
args.add("--dbpath").add(dbpathFile.getPath());

args.add("--dbpath").add(dbpathFile.getRemote());

if (StringUtils.isNotEmpty(port)) {
args.add("--port", port);
Expand Down Expand Up @@ -173,6 +190,7 @@ protected boolean waitForStart() throws Exception {
try {
conn = (HttpURLConnection) new URL("http://localhost:" + port).openConnection();
if (conn.getResponseCode() == 200) {
listener.getLogger().println("MongoDB running at:"+new URL("http://localhost:" + port).toString());
return true;
} else {
return false;
Expand All @@ -199,21 +217,6 @@ protected boolean waitForStart() throws Exception {
}
}

private static class DbpathCleanCommand implements Callable<Void, Exception> {

private File file;

public DbpathCleanCommand(File file) {
this.file = file;
}

public Void call() throws Exception {
new FilePath(file).deleteRecursive();
file.mkdirs();
return null;
}
}

@Extension
public static final class DescriptorImpl extends BuildWrapperDescriptor {

Expand Down
120 changes: 72 additions & 48 deletions src/test/java/org/jenkinsci/plugins/mongodb/MongoBuildWrapperTest.java
Expand Up @@ -2,100 +2,124 @@

import static java.lang.String.format;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import hudson.FilePath;
import hudson.Launcher;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
import hudson.util.ArgumentListBuilder;

import java.io.File;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.For;
import org.mockito.Mockito;

@SuppressWarnings({ "unchecked", "rawtypes" })
@For(MongoBuildWrapper.class)
public class MongoBuildWrapperTest {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

private File workspace;
private FilePath workspace;

private Launcher mockLauncher;

private VirtualChannel mockChannel;

@Before
public void init() {
workspace = tempFolder.newFolder("workspace");
workspace = new FilePath(tempFolder.newFolder("workspace"));
mockLauncher = mock(Launcher.class);
mockChannel = mock(VirtualChannel.class);
when(mockLauncher.getChannel()).thenReturn(mockChannel);
}

@Test
public void setupCmd_with_defaults() {
public void setupCmd_with_defaults() throws Exception {

ArgumentListBuilder args = new ArgumentListBuilder();
File actualDbpath = new MongoBuildWrapper("mongo", null, null)
.setupCmd(args, workspace, true);

File expectedDbpath = new File(workspace, "data/db");
FilePath actualDbpath = new MongoBuildWrapper("mongo", null, null)
.setupCmd(mockLauncher,args, workspace, true);

FilePath expectedDbpath = workspace.child("data").child("db");

assertEquals(expectedDbpath, actualDbpath);
assertEquals(format("--fork --logpath %s/mongodb.log --dbpath %s",
workspace.getAbsolutePath(),
expectedDbpath.getAbsolutePath()),
assertEquals(format("--fork --logpath %s --dbpath %s",
workspace.child("mongodb.log").getRemote(),
expectedDbpath.getRemote()),
args.toStringWithQuote());
}

@Test
public void setupCmd_with_dbpath() {

@Test
public void setupCmd_with_dbpath() throws Throwable {

ArgumentListBuilder args = new ArgumentListBuilder();
File actualDbpath = new MongoBuildWrapper("mongo", "data_dir", null)
.setupCmd(args, workspace, true);
when(mockChannel.call((Callable) Mockito.any())).thenReturn(Boolean.FALSE);

FilePath actualDbpath = new MongoBuildWrapper("mongo", "data_dir", null)
.setupCmd(mockLauncher,args, workspace, true);

File expectedDbpath = new File(workspace, "data_dir");
FilePath expectedDbpath = workspace.child("data_dir");
FilePath expectedLogpath = workspace.child("mongodb.log");
assertEquals(expectedDbpath, actualDbpath);
assertEquals(format("--fork --logpath %s/mongodb.log --dbpath %s",
workspace.getAbsolutePath(),
expectedDbpath.getAbsolutePath()),
assertEquals(format("--fork --logpath %s --dbpath %s",
expectedLogpath.getRemote(),
expectedDbpath.getRemote()),
args.toStringWithQuote());
}

@Test
public void setupCmd_with_abusolute_dbpath() {
public void setupCmd_with_abusolute_dbpath() throws Throwable {

ArgumentListBuilder args = new ArgumentListBuilder();

String absolutePath = new File(tempFolder.getRoot(), "foo/bar/data_dir").getAbsolutePath();
File actualDbpath = new MongoBuildWrapper("mongo", absolutePath, null)
.setupCmd(args, workspace, true);

assertEquals(new File(absolutePath), actualDbpath);
assertEquals(format("--fork --logpath %s/mongodb.log --dbpath %s",
workspace.getAbsolutePath(),
absolutePath),
when(mockChannel.call((Callable) Mockito.any())).thenReturn(Boolean.TRUE);

FilePath absolutePath = new FilePath(tempFolder.getRoot()).child("foo").child("bar").child("data_dir");
FilePath actualDbpath = new MongoBuildWrapper("mongo", absolutePath.getRemote(), null)
.setupCmd(mockLauncher,args, workspace, true);

assertEquals(absolutePath.getRemote(), actualDbpath.getRemote());

assertEquals(format("--fork --logpath %s --dbpath %s",
workspace.child("mongodb.log").getRemote(),
absolutePath.getRemote()),
args.toStringWithQuote());
}

@Test
public void setupCmd_with_port() {
public void setupCmd_with_port() throws Exception {

ArgumentListBuilder args = new ArgumentListBuilder();
File actualDbpath = new MongoBuildWrapper("mongo", null, "1234")
.setupCmd(args, workspace, true);

File expectedDbpath = new File(workspace, "data/db");
assertEquals(expectedDbpath, actualDbpath);
assertEquals(format("--fork --logpath %s/mongodb.log --dbpath %s --port 1234",
workspace.getAbsolutePath(),
expectedDbpath.getAbsolutePath()),
FilePath actualDbpath = new MongoBuildWrapper("mongo", null, "1234").setupCmd(mockLauncher,args, workspace, true);

FilePath expectedDbpath = workspace.child("data").child("db");
assertEquals(expectedDbpath.getRemote(), actualDbpath.getRemote());

assertEquals(format("--fork --logpath %s --dbpath %s --port 1234",
workspace.child("mongodb.log").getRemote(),
expectedDbpath.getRemote()),
args.toStringWithQuote());
}

@Test
public void setupCmd_without_fork() {
public void setupCmd_without_fork() throws Exception{

ArgumentListBuilder args = new ArgumentListBuilder();
File actualDbpath = new MongoBuildWrapper("mongo", null, null)
.setupCmd(args, workspace, false);
ArgumentListBuilder args = new ArgumentListBuilder();
FilePath actualDbpath = new MongoBuildWrapper("mongo", null, "1234").setupCmd(mockLauncher,args, workspace, false);

File expectedDbpath = new File(workspace, "data/db");
assertEquals(expectedDbpath, actualDbpath);
assertEquals(format("--logpath %s/mongodb.log --dbpath %s",
workspace.getAbsolutePath(),
expectedDbpath.getAbsolutePath()),
FilePath expectedDbpath = workspace.child("data").child("db");
assertEquals(expectedDbpath.getRemote(), actualDbpath.getRemote());

assertEquals(format("--logpath %s --dbpath %s --port 1234",
workspace.child("mongodb.log").getRemote(),
expectedDbpath.getRemote()),
args.toStringWithQuote());
}
}

}

0 comments on commit 71c881f

Please sign in to comment.