Skip to content

Commit

Permalink
[JENKINS-41876] Add test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
nfalco79 committed Feb 12, 2017
1 parent e092acd commit 652e9f4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 13 deletions.
24 changes: 21 additions & 3 deletions pom.xml
Expand Up @@ -33,13 +33,18 @@
<timezone>+1</timezone>
</developer>
</developers>

<licenses>
<license>
<name>MIT License</name>
<url>LICENSE.txt</url>
</license>
</licenses>

<properties>
<powermock.version>1.7.0RC2</powermock.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand All @@ -57,6 +62,18 @@
<version>2.6.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down Expand Up @@ -129,18 +146,19 @@
</plugin>
</plugins>
</reporting>

<scm>
<connection>scm:git:git://github.com/jenkinsci/nodejs-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/nodejs-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/nodejs-plugin</url>
<tag>HEAD</tag>
</scm>
<tag>HEAD</tag>
</scm>

<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</project>

</project>
43 changes: 33 additions & 10 deletions src/main/java/jenkins/plugins/nodejs/tools/NodeJSInstaller.java
Expand Up @@ -71,6 +71,13 @@
public class NodeJSInstaller extends DownloadFromUrlInstaller {

public static final String NPM_PACKAGES_RECORD_FILENAME = ".npmPackages";

/**
* Define the elapse time before perform a new npm install for defined
* global packages.
*/
public static final int NPM_PACKAGES_REFRESH_HOURS = 72;

private final String npmPackages;
private final Long npmPackagesRefreshHours;
private Platform platform;
Expand Down Expand Up @@ -105,8 +112,8 @@ public Installable getInstallable() throws IOException {
// implementation
@Override
public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
this.platform = Platform.of(node);
this.cpu = CPU.of(node);
this.platform = getPlatform(node);
this.cpu = getCPU(node);

FilePath expected;
Installable installable = getInstallable();
Expand All @@ -126,9 +133,27 @@ public FilePath performInstallation(ToolInstallation tool, Node node, TaskListen
}
}

// Installing npm packages if needed
if (StringUtils.isNotBlank(this.npmPackages)) { // JENKINS-41876
boolean skipNpmPackageInstallation = areNpmPackagesUpToDate(expected, this.npmPackages, this.npmPackagesRefreshHours);
refreshGlobalPackages(node, log, expected);

return expected;
}

private CPU getCPU(Node node) throws IOException, InterruptedException {
return CPU.of(node);
}

private Platform getPlatform(Node node) throws DetectionFailedException {
return Platform.of(node);
}

/*
* Installing npm packages if needed
*/
protected void refreshGlobalPackages(Node node, TaskListener log, FilePath expected) throws IOException, InterruptedException {
String globalPackages = getNpmPackages();

if (StringUtils.isNotBlank(globalPackages)) { // JENKINS-41876
boolean skipNpmPackageInstallation = areNpmPackagesUpToDate(expected, globalPackages, getNpmPackagesRefreshHours());
if (!skipNpmPackageInstallation) {
expected.child(NPM_PACKAGES_RECORD_FILENAME).delete();

Expand All @@ -144,7 +169,7 @@ public FilePath performInstallation(ToolInstallation tool, Node node, TaskListen

npmScriptArgs.add("install");
npmScriptArgs.add("-g");
for (String packageName : this.npmPackages.split("\\s")) {
for (String packageName : globalPackages.split("\\s")) {
npmScriptArgs.add(packageName);
}

Expand All @@ -153,16 +178,14 @@ public FilePath performInstallation(ToolInstallation tool, Node node, TaskListen

if (returnCode == 0) {
// leave a record for the next up-to-date check
expected.child(NPM_PACKAGES_RECORD_FILENAME).write(this.npmPackages, "UTF-8");
expected.child(NPM_PACKAGES_RECORD_FILENAME).write(globalPackages, "UTF-8");
expected.child(NPM_PACKAGES_RECORD_FILENAME).act(new ChmodRecAPlusX());
}
}
}

return expected;
}

private static boolean areNpmPackagesUpToDate(FilePath expected, String npmPackages, long npmPackagesRefreshHours) throws IOException, InterruptedException {
public static boolean areNpmPackagesUpToDate(FilePath expected, String npmPackages, long npmPackagesRefreshHours) throws IOException, InterruptedException {
FilePath marker = expected.child(NPM_PACKAGES_RECORD_FILENAME);
return marker.exists() && marker.readToString().equals(npmPackages) && System.currentTimeMillis() < marker.lastModified()+ TimeUnit.HOURS.toMillis(npmPackagesRefreshHours);
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/jenkins/plugins/nodejs/NodeJSInstallerTest.java
@@ -0,0 +1,41 @@
package jenkins.plugins.nodejs;

import java.io.File;
import java.io.IOException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import hudson.FilePath;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.tools.ToolInstallation;
import jenkins.plugins.nodejs.tools.NodeJSInstaller;

@RunWith(PowerMockRunner.class)
@PrepareForTest(NodeJSInstaller.class)
public class NodeJSInstallerTest {

@Test
public void test_skip_install_global_packages_when_empty() throws Exception {
MockNodeJSInstaller mock = new MockNodeJSInstaller("test-id", " ", NodeJSInstaller.NPM_PACKAGES_REFRESH_HOURS);
mock.performInstallation(null, null, null);
}

private class MockNodeJSInstaller extends NodeJSInstaller {

public MockNodeJSInstaller(String id, String npmPackages, long npmPackagesRefreshHours) {
super(id, npmPackages, npmPackagesRefreshHours);
}

@Override
public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
FilePath expected = new FilePath(new File("/home/tools"));
refreshGlobalPackages(node, log, expected);
return expected;
}
}

}

0 comments on commit 652e9f4

Please sign in to comment.