Skip to content

Commit

Permalink
CQI Speedup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nfalco79 committed Aug 16, 2023
1 parent a8070c3 commit 4c8a6b7
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 195 deletions.
33 changes: 19 additions & 14 deletions src/test/java/jenkins/plugins/nodejs/CredentialMaskingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,27 @@
package jenkins.plugins.nodejs;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jenkinsci.plugins.configfiles.GlobalConfigFiles;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;

import hudson.tools.InstallSourceProperty;
import hudson.util.Secret;
import jenkins.model.Jenkins;
Expand All @@ -59,8 +62,8 @@ public class CredentialMaskingTest {
@Rule
public BuildWatcher buildWatcher = new BuildWatcher();

@Rule
public JenkinsRule r = new JenkinsRule();
@ClassRule
public static JenkinsRule r = new JenkinsRule();

@Before
public void setupConfigWithCredentials() {
Expand All @@ -74,18 +77,19 @@ public void setupConfigWithCredentials() {

SystemCredentialsProvider.getInstance().getCredentials().add(new StringCredentialsImpl(CredentialsScope.GLOBAL, "stringcreds", "", Secret.fromString("sensitive")));

GlobalConfigFiles.get().save(new NPMConfig("npm", "npm config", "", ";empty config to populate with repos",
GlobalConfigFiles.get().save(new NPMConfig("npm", "npm config", "", ";empty config to populate with repos",
// Refusing to marshal java.util.ImmutableCollections$ListN
new ArrayList(List.of(new NPMRegistry("https://npmjs.example.com", "usercreds", "scope1"),
List.of(new NPMRegistry("https://npmjs.example.com", "usercreds", "scope1"),
new NPMRegistry("https://npmjs2.example.com", "usercreds2", null),
new NPMRegistry("https://npmjs3.example.com", "stringcreds", "scope2")))));
new NPMRegistry("https://npmjs3.example.com", "stringcreds", "scope2"))));
}

@Test @Issue("SECURITY-3196")
@Test
@Issue("SECURITY-3196")
public void testNPMConfigFileWithConfigFileProviderBlock() throws Exception {
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
WorkflowJob p = r.createProject(WorkflowJob.class, "p1");
p.setDefinition(new CpsFlowDefinition(
String.join("\n",
String.join("\n",
"node {",
" configFileProvider([configFile(fileId: 'npm', ",
" variable: 'NPM_RC_LOCATION')]) {",
Expand Down Expand Up @@ -119,7 +123,8 @@ public void testNPMConfigFileWithConfigFileProviderBlock() throws Exception {
r.assertLogNotContains("sensitive", b1);
}

@Test @Issue("SECURITY-3196")
@Test
@Issue("SECURITY-3196")
public void testNPMConfigFileWithNodejsBlock() throws Exception {
// fake enough of a nodejs install.
Platform platform = Platform.current();
Expand All @@ -128,14 +133,14 @@ public void testNPMConfigFileWithNodejsBlock() throws Exception {
bin.mkdir();
new File(bin, platform.nodeFileName).createNewFile();
new File(bin, platform.npmFileName).createNewFile();
NodeJSInstallation installation = new NodeJSInstallation("bogus-nodejs", dir.toString(),

NodeJSInstallation installation = new NodeJSInstallation("bogus-nodejs", dir.toString(),
Collections.singletonList(new InstallSourceProperty(Collections.singletonList(new NodeJSInstaller("anything", null, Long.MAX_VALUE)))));
Jenkins.get().getDescriptorByType(NodeJSInstallation.DescriptorImpl.class).setInstallations(installation);

WorkflowJob p = r.createProject(WorkflowJob.class, "p");
WorkflowJob p = r.createProject(WorkflowJob.class, "p2");
p.setDefinition(new CpsFlowDefinition(
String.join("\n",
String.join("\n",
"node {",
" nodejs(nodeJSInstallationName:'bogus-nodejs', configId:'npm') {",
" String content = readFile(env.npm_config_userconfig)",
Expand Down
46 changes: 6 additions & 40 deletions src/test/java/jenkins/plugins/nodejs/NodeJSBuildWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
import org.assertj.core.api.Assertions;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

import hudson.EnvVars;
import hudson.ExtensionList;
Expand All @@ -54,17 +54,15 @@
import hudson.model.TaskListener;
import jenkins.plugins.nodejs.VerifyEnvVariableBuilder.EnvVarVerifier;
import jenkins.plugins.nodejs.VerifyEnvVariableBuilder.FileVerifier;
import jenkins.plugins.nodejs.cache.DefaultCacheLocationLocator;
import jenkins.plugins.nodejs.cache.PerJobCacheLocationLocator;
import jenkins.plugins.nodejs.configfiles.NPMConfig;
import jenkins.plugins.nodejs.configfiles.NPMRegistry;
import jenkins.plugins.nodejs.tools.NodeJSInstallation;
import jenkins.plugins.nodejs.tools.Platform;

public class NodeJSBuildWrapperTest {

@Rule
public JenkinsRule j = new JenkinsRule();
@ClassRule
public static JenkinsRule j = new JenkinsRule();
@Rule
public TemporaryFolder fileRule = new TemporaryFolder();

Expand All @@ -86,7 +84,7 @@ public void test_calls_sequence_of_installer() throws Exception {

@Test
public void test_creation_of_config() throws Exception {
FreeStyleProject job = j.createFreeStyleProject("free");
FreeStyleProject job = j.createFreeStyleProject("free2");

final Config config = createSetting("my-config-id", "[email protected]", null);

Expand All @@ -102,7 +100,7 @@ public void test_creation_of_config() throws Exception {

@Test
public void test_inject_path_variable() throws Exception {
FreeStyleProject job = j.createFreeStyleProject("free");
FreeStyleProject job = j.createFreeStyleProject("free3");

final Config config = createSetting("my-config-id", "", null);

Expand All @@ -123,7 +121,7 @@ public void test_inject_path_variable() throws Exception {
@Issue("JENKINS-45840")
@Test
public void test_check_no_executable_in_installation_folder() throws Exception {
FreeStyleProject job = j.createFreeStyleProject("free");
FreeStyleProject job = j.createFreeStyleProject("free4");

NodeJSInstallation installation = mockInstaller();
when(installation.getExecutable(any(Launcher.class))).thenReturn(null);
Expand All @@ -134,38 +132,6 @@ public void test_check_no_executable_in_installation_folder() throws Exception {
j.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0));
}

/**
* Verify that the serialisation is backward compatible.
*/
@LocalData
@Test
@Issue("JENKINS-57844")
public void test_serialisation_is_compatible_with_version_1_2_x() throws Exception {
FreeStyleProject prj = j.jenkins.getAllItems(hudson.model.FreeStyleProject.class) //
.stream() //
.filter(p -> "test".equals(p.getName())) //
.findFirst().get();

NodeJSBuildWrapper step = prj.getBuildWrappersList().get(NodeJSBuildWrapper.class);
Assertions.assertThat(step.getCacheLocationStrategy()).isInstanceOf(DefaultCacheLocationLocator.class);
}

/**
* Verify reloading jenkins job configuration use the saved cache strategy instead reset to default.
*/
@LocalData
@Test
@Issue("JENKINS-58029")
public void test_reloading_job_configuration_contains_saved_cache_strategy() throws Exception {
FreeStyleProject prj = j.jenkins.getAllItems(hudson.model.FreeStyleProject.class) //
.stream() //
.filter(p -> "test".equals(p.getName())) //
.findFirst().get();

NodeJSBuildWrapper step = prj.getBuildWrappersList().get(NodeJSBuildWrapper.class);
Assertions.assertThat(step.getCacheLocationStrategy()).isInstanceOf(PerJobCacheLocationLocator.class);
}

@Test
public void test_set_of_cache_location() throws Exception {
FreeStyleProject job = j.createFreeStyleProject("cache");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
import org.assertj.core.api.Assertions;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

import hudson.EnvVars;
import hudson.FilePath;
Expand All @@ -58,8 +58,6 @@
import hudson.model.TaskListener;
import jenkins.plugins.nodejs.CIBuilderHelper.Verifier;
import jenkins.plugins.nodejs.VerifyEnvVariableBuilder.EnvVarVerifier;
import jenkins.plugins.nodejs.cache.DefaultCacheLocationLocator;
import jenkins.plugins.nodejs.cache.PerJobCacheLocationLocator;
import jenkins.plugins.nodejs.configfiles.NPMConfig;
import jenkins.plugins.nodejs.configfiles.NPMRegistry;
import jenkins.plugins.nodejs.tools.DetectionFailedException;
Expand All @@ -68,8 +66,8 @@

public class NodeJSCommandInterpreterTest {

@Rule
public JenkinsRule j = new JenkinsRule();
@ClassRule
public static JenkinsRule j = new JenkinsRule();
@Rule
public TemporaryFolder folder = new TemporaryFolder();

Expand Down Expand Up @@ -154,43 +152,11 @@ public void test_check_no_executable_in_installation_folder() throws Exception {

NodeJSCommandInterpreter builder = CIBuilderHelper.createMock("test_creation_of_config", installation, null);

FreeStyleProject job = j.createFreeStyleProject("free");
FreeStyleProject job = j.createFreeStyleProject("JENKINS-45840");
job.getBuildersList().add(builder);
j.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0));
}

/**
* Verify that the serialisation is backward compatible.
*/
@LocalData
@Test
@Issue("JENKINS-57844")
public void test_serialisation_is_compatible_with_version_1_2_x() throws Exception {
FreeStyleProject prj = j.jenkins.getAllItems(hudson.model.FreeStyleProject.class) //
.stream() //
.filter(p -> "test".equals(p.getName())) //
.findFirst().get();

NodeJSCommandInterpreter step = prj.getBuildersList().get(NodeJSCommandInterpreter.class);
Assertions.assertThat(step.getCacheLocationStrategy()).isInstanceOf(DefaultCacheLocationLocator.class);
}

/**
* Verify reloading jenkins job configuration use the saved cache strategy instead reset to default.
*/
@LocalData
@Test
@Issue("JENKINS-58029")
public void test_reloading_job_configuration_contains_saved_cache_strategy() throws Exception {
FreeStyleProject prj = j.jenkins.getAllItems(hudson.model.FreeStyleProject.class) //
.stream() //
.filter(p -> "test".equals(p.getName())) //
.findFirst().get();

NodeJSCommandInterpreter step = prj.getBuildersList().get(NodeJSCommandInterpreter.class);
Assertions.assertThat(step.getCacheLocationStrategy()).isInstanceOf(PerJobCacheLocationLocator.class);
}

@Test
public void test_set_of_cache_location() throws Exception {
final File cacheFolder = folder.newFolder();
Expand Down
109 changes: 109 additions & 0 deletions src/test/java/jenkins/plugins/nodejs/NodeJSSerialisationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* The MIT License
*
* Copyright (c) 2019, Nikolas Falco
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.plugins.nodejs;

import org.assertj.core.api.Assertions;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

import hudson.model.FreeStyleProject;
import jenkins.plugins.nodejs.cache.DefaultCacheLocationLocator;
import jenkins.plugins.nodejs.cache.PerJobCacheLocationLocator;

public class NodeJSSerialisationTest {

@Rule
public JenkinsRule j = new JenkinsRule();
@Rule
public TemporaryFolder fileRule = new TemporaryFolder();

/**
* Verify that the serialisation is backward compatible.
*/
@LocalData
@Test
@Issue("JENKINS-57844")
public void test_serialisation_is_compatible_with_version_1_2_x_interpreter() throws Exception {
FreeStyleProject prj = j.jenkins.getAllItems(hudson.model.FreeStyleProject.class) //
.stream() //
.filter(p -> "test".equals(p.getName())) //
.findFirst().get();

NodeJSCommandInterpreter step = prj.getBuildersList().get(NodeJSCommandInterpreter.class);
Assertions.assertThat(step.getCacheLocationStrategy()).isInstanceOf(DefaultCacheLocationLocator.class);
}

/**
* Verify reloading jenkins job configuration use the saved cache strategy instead reset to default.
*/
@LocalData
@Test
@Issue("JENKINS-58029")
public void test_reloading_job_configuration_contains_saved_cache_strategy_interpreter() throws Exception {
FreeStyleProject prj = j.jenkins.getAllItems(hudson.model.FreeStyleProject.class) //
.stream() //
.filter(p -> "test".equals(p.getName())) //
.findFirst().get();

NodeJSCommandInterpreter step = prj.getBuildersList().get(NodeJSCommandInterpreter.class);
Assertions.assertThat(step.getCacheLocationStrategy()).isInstanceOf(PerJobCacheLocationLocator.class);
}

/**
* Verify that the serialisation is backward compatible.
*/
@LocalData
@Test
@Issue("JENKINS-57844")
public void test_serialisation_is_compatible_with_version_1_2_x_buildWrapper() throws Exception {
FreeStyleProject prj = j.jenkins.getAllItems(hudson.model.FreeStyleProject.class) //
.stream() //
.filter(p -> "test".equals(p.getName())) //
.findFirst().get();

NodeJSBuildWrapper step = prj.getBuildWrappersList().get(NodeJSBuildWrapper.class);
Assertions.assertThat(step.getCacheLocationStrategy()).isInstanceOf(DefaultCacheLocationLocator.class);
}

/**
* Verify reloading jenkins job configuration use the saved cache strategy instead reset to default.
*/
@LocalData
@Test
@Issue("JENKINS-58029")
public void test_reloading_job_configuration_contains_saved_cache_strategy_buildWrapper() throws Exception {
FreeStyleProject prj = j.jenkins.getAllItems(hudson.model.FreeStyleProject.class) //
.stream() //
.filter(p -> "test".equals(p.getName())) //
.findFirst().get();

NodeJSBuildWrapper step = prj.getBuildWrappersList().get(NodeJSBuildWrapper.class);
Assertions.assertThat(step.getCacheLocationStrategy()).isInstanceOf(PerJobCacheLocationLocator.class);
}

}
Loading

0 comments on commit 4c8a6b7

Please sign in to comment.