Skip to content

Commit

Permalink
Add more test for configfile provider plugin (#1071)
Browse files Browse the repository at this point in the history
* Add more test for configfile provider plugin

Signed-off-by: Olivier Lamy <[email protected]>

* add missing needed plugin

Signed-off-by: Olivier Lamy <[email protected]>

* cleanup

Signed-off-by: Olivier Lamy <[email protected]>

* use assertThat from hamcrest

Signed-off-by: Olivier Lamy <[email protected]>

---------

Signed-off-by: Olivier Lamy <[email protected]>
  • Loading branch information
olamy authored Apr 25, 2023
1 parent 9de5afd commit ccd9f0a
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ public static void checkScript(final String script) {
public static String resolveScriptName(final String scriptName) {
return (SystemUtils.IS_OS_UNIX) ? scriptName : scriptName + "Windows";
}
}
}
142 changes: 140 additions & 2 deletions src/test/java/plugins/ConfigFileProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@
import org.jenkinsci.test.acceptance.plugins.credentials.ManagedCredentials;
import org.jenkinsci.test.acceptance.plugins.credentials.UserPwdCredential;
import org.jenkinsci.test.acceptance.po.Build;
import org.jenkinsci.test.acceptance.po.Folder;
import org.jenkinsci.test.acceptance.po.WorkflowJob;
import org.jenkinsci.test.acceptance.utils.PipelineTestUtils;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;

import org.apache.commons.lang3.SystemUtils;
import org.openqa.selenium.Alert;

/**
* Tests config-file-provider plugin inside a Pipeline.
*/
@WithPlugins({"config-file-provider", "workflow-job", "workflow-cps", "workflow-basic-steps", "workflow-durable-task-step"})
@WithPlugins({"config-file-provider", "workflow-job", "workflow-cps", "workflow-basic-steps", "workflow-durable-task-step", "cloudbees-folder"})
public class ConfigFileProviderTest extends AbstractJUnitTest {

private static final String CRED_ID = "credId";
Expand All @@ -30,6 +35,11 @@ public class ConfigFileProviderTest extends AbstractJUnitTest {
private static final String SERVER_ID = "fakeServer";
private static final String CUSTOM_CONF_CONTENT = "test_custom_content for custom file";

private static final String ANOTHER_SERVER_ID = "anotherServer";
private static final String CUSTOM_CONF_EXTRA_CONTENT = "extra content for custom";

private static final String MANAGED_FILE_NOT_FOUND_ERROR = "not able to provide the file [ManagedFile: id=%s";


@Before
public void setup() {
Expand All @@ -51,7 +61,6 @@ public void testMavenSettingsConfigFile() {

assertThat(jobLog, containsString(SERVER_ID));
assertThat(jobLog, containsString(CRED_USR));
assertThat(jobLog, containsString(CRED_PWD));
}


Expand Down Expand Up @@ -129,4 +138,133 @@ private String createPipelineAndGetConsole(final CustomConfig customConfig) {
final Build b = job.startBuild().shouldSucceed();
return b.getConsole();
}

@Test
public void testConfigFileProviderWithFolders() {
final MavenSettingsConfig mvnConfig = this.createMavenSettingsConfig(SERVER_ID, ANOTHER_SERVER_ID, CRED_ID);

final Folder f = jenkins.jobs.create(Folder.class);
final CustomConfig customConf = this.createCustomConfigInFolder(f, CUSTOM_CONF_CONTENT);
final WorkflowJob job = createPipelineJobInFolderWithScript(f, scriptForPipelineWithParameters(mvnConfig.id(), customConf.id()));

String jobLog = this.buildJobAndGetConsole(job, true);

assertThat(jobLog, containsString(SERVER_ID));
assertThat(jobLog, containsString(CRED_USR));
assertThat(jobLog, containsString(ANOTHER_SERVER_ID));
assertThat(jobLog, containsString(CUSTOM_CONF_CONTENT));

this.makeChangesInConfigurations(mvnConfig, customConf);
jobLog = this.buildJobAndGetConsole(job, true);

assertThat(jobLog, containsString(SERVER_ID));
assertThat(jobLog, containsString(CRED_USR));
assertThat(jobLog, not(containsString(ANOTHER_SERVER_ID)));
assertThat(jobLog, containsString(CUSTOM_CONF_CONTENT + CUSTOM_CONF_EXTRA_CONTENT));

// We want to delete the config file and re-run the job to see it fail
jenkins.visit("configfiles");
driver.findElement(
by.xpath("//td/code['%s']/parent::td/parent::tr/td[1]/a[2]", mvnConfig.id()) // this won't age well
).click();
handleAlert(Alert::accept);

jobLog = this.buildJobAndGetConsole(job, false);

assertThat(jobLog, containsString(String.format(MANAGED_FILE_NOT_FOUND_ERROR, mvnConfig.id())));
}

private MavenSettingsConfig createMavenSettingsConfig(final String serverWithCreds, final String serverWithoutCreds, final String credId) {
final MavenSettingsConfig mvnConfig = new ConfigFileProvider(jenkins).addFile(MavenSettingsConfig.class);

mvnConfig.replaceAll(false);
mvnConfig.content(String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<settings xmlns=\"http://maven.apache.org/SETTINGS/1.0.0\" \n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" +
" xsi:schemaLocation=\"http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd\">\n" +
" \n" +
" <servers>\n" +
" <server>\n" +
" <id>%s</id>\n" +
" </server>\n" +
" <server>\n" +
" <id>%s</id>\n" +
" </server>\n" +
" </servers>\n" +
"\n" +
"</settings>", serverWithCreds, serverWithoutCreds));

final ServerCredentialMapping serverCred = mvnConfig.addServerCredentialMapping();
serverCred.serverId(serverWithCreds);
serverCred.credentialsId(credId);

mvnConfig.save();
return mvnConfig;
}

private CustomConfig createCustomConfigInFolder(final Folder f, final String content) {
final CustomConfig customConf = new ConfigFileProvider(f).addFile(CustomConfig.class);

customConf.content(content);

customConf.save();
return customConf;
}

private String buildJobAndGetConsole(final WorkflowJob job, final boolean expectedSuccess) {
final Build b = job.startBuild();

if (expectedSuccess) {
b.shouldSucceed();
} else {
b.shouldFail();
}

return b.getConsole();
}

private void makeChangesInConfigurations(final MavenSettingsConfig mvnConfig, final CustomConfig customConf) {
mvnConfig.open();
mvnConfig.replaceAll(true);
mvnConfig.save();

customConf.open();
customConf.content(CUSTOM_CONF_CONTENT + CUSTOM_CONF_EXTRA_CONTENT);
customConf.save();
}


public String scriptForPipeline() {
if (SystemUtils.IS_OS_UNIX) {
return "node {\n" +
" configFileProvider(\n" +
" [configFile(fileId: '%s', variable: 'MAVEN_SETTINGS'),\n" +
" configFile(fileId: '%s', variable: 'CUSTOM_SETTINGS')]) {\n" +
" sh 'cat $MAVEN_SETTINGS '\n" +
" sh 'cat $CUSTOM_SETTINGS '\n" +
" }\n" +
"}";
} else {
return "node {\n" +
" configFileProvider(\n" +
" [configFile(fileId: '%s', variable: 'MAVEN_SETTINGS'),\n" +
" configFile(fileId: '%s', variable: 'CUSTOM_SETTINGS')]) {\n" +
" bat '@type %%MAVEN_SETTINGS%% '\n" +
" bat '@type %%CUSTOM_SETTINGS%% '\n" +
" }\n" +
"}";
}
}

public WorkflowJob createPipelineJobInFolderWithScript(final Folder f, final String script) {
return PipelineTestUtils.createPipelineJobWithScript(f.getJobs(), script);
}

public String scriptForPipelineWithParameters(final String... scriptParameters) {
final String script = this.scriptForPipeline();
PipelineTestUtils.checkScript(script);

return String.format(script, scriptParameters);
}

}

0 comments on commit ccd9f0a

Please sign in to comment.