Skip to content

Commit

Permalink
JENKINS-28441
Browse files Browse the repository at this point in the history
Added Workspace zip tests and ensured that only the contents are zipped instead of the enclosing folder
  • Loading branch information
cast committed May 25, 2015
1 parent 1f80544 commit 804cea1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
@@ -1,6 +1,7 @@
package com.xti.jenkins.plugin.awslambda.service;

import hudson.FilePath;
import hudson.util.DirScanner;
import org.apache.commons.lang.StringUtils;

import java.io.File;
Expand Down Expand Up @@ -38,7 +39,7 @@ private File getArtifactZip(FilePath artifactLocation) throws IOException, Inter
artifactLocation.copyTo(new FileOutputStream(resultFile));
} else {
logger.log("Zipping folder ..., copying zip file");
artifactLocation.zip(new FileOutputStream(resultFile));
artifactLocation.zip(new FileOutputStream(resultFile), new DirScanner.Glob("**", null, false));
}

logger.log("File Name: %s%nAbsolute Path: %s%nFile Size: %d", resultFile.getName(), resultFile.getAbsolutePath(), resultFile.length());
Expand Down
@@ -0,0 +1,95 @@
package com.xti.jenkins.plugin.awslambda.service;

import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.FreeStyleProject;
import hudson.util.OneShotEvent;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.zip.ZipFile;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class WorkSpaceZipperTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void testGetZipWithZip() throws Exception {
final OneShotEvent buildEnded = new OneShotEvent();

FreeStyleProject p = j.createFreeStyleProject();
p.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("echo.zip").copyFrom(new FileInputStream(getResource("echo.zip")));
buildEnded.signal();
return true;
}
});

p.scheduleBuild2(0);
buildEnded.block();

JenkinsLogger logger = new JenkinsLogger(System.out);
WorkSpaceZipper workSpaceZipper = new WorkSpaceZipper(p.getSomeWorkspace(), logger);
File zip = workSpaceZipper.getZip("echo.zip");

assertTrue(zip.exists());
assertFalse(zip.getAbsolutePath().contains("aws-lambda"));

ZipFile zipFile = new ZipFile(zip);
assertNotNull(zipFile);
assertNotNull(zipFile.getEntry("index.js"));
}

@Test
public void testGetZipWithFolder() throws Exception {
final OneShotEvent buildEnded = new OneShotEvent();

FreeStyleProject p = j.createFreeStyleProject();
p.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("echo").child("index.js").copyFrom(new FileInputStream(getResource("echo/index.js")));
buildEnded.signal();
return true;
}
});

p.scheduleBuild2(0);
buildEnded.block();

JenkinsLogger logger = new JenkinsLogger(System.out);
WorkSpaceZipper workSpaceZipper = new WorkSpaceZipper(p.getSomeWorkspace(), logger);
File zip = workSpaceZipper.getZip("echo");

assertTrue(zip.exists());
assertFalse(zip.getAbsolutePath().contains("aws-lambda"));

ZipFile zipFile = new ZipFile(zip);
assertNotNull(zipFile);
assertNotNull(zipFile.getEntry("index.js"));
}

private File getResource(String resourcePath){
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource(resourcePath);
if(resource != null){
return new File(resource.getFile());
} else {
throw new IllegalStateException("Could not load " + resourcePath);
}
}
}

0 comments on commit 804cea1

Please sign in to comment.