Skip to content

Commit

Permalink
JENKINS-38872 add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
imod committed Oct 12, 2016
1 parent d966f46 commit f8765dc
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 20 deletions.
Expand Up @@ -41,9 +41,9 @@ of this software and associated documentation files (the "Software"), to deal

/**
* Defines the contract for actions called by jelly
*
*
* @author domi
*
*
*/
public interface ConfigFilesUIContract {

Expand All @@ -56,8 +56,6 @@ public interface ConfigFilesUIContract {

public List<ConfigProvider> getProviders();

public Collection<Config> getConfigs();

/**
* Insert or update
* @param req
Expand All @@ -69,7 +67,7 @@ public interface ConfigFilesUIContract {

/**
* Loads the config by its id and forwards the request to "edit.jelly".
*
*
* @param req
* request
* @param rsp
Expand All @@ -83,7 +81,7 @@ public interface ConfigFilesUIContract {

/**
* Requests a new config object from provider (defined by the given id) and forwards the request to "edit.jelly".
*
*
* @param req
* request
* @param rsp
Expand All @@ -99,7 +97,7 @@ public interface ConfigFilesUIContract {

/**
* Removes a script from the config and filesystem.
*
*
* @param res
* response
* @param rsp
Expand Down
Expand Up @@ -81,7 +81,9 @@ public static Map<ManagedFile, FilePath> provisionConfigFiles(List<ManagedFile>
Config configFile = Config.getByIdOrNull(build, managedFile.fileId);

if (configFile == null) {
throw new AbortException("not able to provide the following file, can't be resolved by any provider - maybe it got deleted by an administrator: " + managedFile);
String message = "not able to provide the file " + managedFile + ", can't be resolved by any provider - maybe it got deleted by an administrator?";
listener.getLogger().println(message);
throw new AbortException(message);
}

FilePath workDir = tempDir(workspace);
Expand Down
Expand Up @@ -65,7 +65,7 @@ public ContentType getContentTypeForProvider(String providerId) {

@Override
public Map<ConfigProvider, Collection<Config>> getGroupedConfigs() {
ConfigFileStore store = getStore(folder);
ConfigFileStore store = getStore();
Map<ConfigProvider, Collection<Config>> groupedConfigs = store.getGroupedConfigs();
return groupedConfigs;
}
Expand All @@ -75,11 +75,6 @@ public List<ConfigProvider> getProviders() {
return ConfigProvider.all();
}

@Override
public Collection<Config> getConfigs() {
return null;
}

@Override
public HttpResponse doSaveConfig(StaplerRequest req) throws IOException, ServletException {
checkPermission(Hudson.ADMINISTER);
Expand All @@ -88,7 +83,7 @@ public HttpResponse doSaveConfig(StaplerRequest req) throws IOException, Servlet
JSONObject json = req.getSubmittedForm().getJSONObject("config");
Config config = req.bindJSON(Config.class, json);

ConfigFileStore store = getStore(folder);
ConfigFileStore store = getStore();
// potentially replace existing
store.remove(config.id);
store.save(config);
Expand All @@ -99,7 +94,7 @@ public HttpResponse doSaveConfig(StaplerRequest req) throws IOException, Servlet
return new HttpRedirect("index");
}

private ConfigFileStore getStore(Folder folder) {
ConfigFileStore getStore() {
// TODO only add property when its really needed (eg. don't add it if there is no config to be saved)
FolderConfigFileProperty folderConfigFileProperty = folder.getProperties().get(FolderConfigFileProperty.class);
if(folderConfigFileProperty == null) {
Expand All @@ -116,7 +111,7 @@ private ConfigFileStore getStore(Folder folder) {
@Override
public void doShow(StaplerRequest req, StaplerResponse rsp, @QueryParameter("id") String confgiId) throws IOException, ServletException {

Config config = getStore(folder).getById(confgiId);
Config config = getStore().getById(confgiId);
req.setAttribute("contentType", config.getProvider().getContentType());
req.setAttribute("config", config);
req.getView(this, "show.jelly").forward(req, rsp);
Expand All @@ -127,7 +122,7 @@ public void doShow(StaplerRequest req, StaplerResponse rsp, @QueryParameter("id"
public void doEditConfig(StaplerRequest req, StaplerResponse rsp, @QueryParameter("id") String confgiId) throws IOException, ServletException {
checkPermission(Hudson.ADMINISTER);

Config config = getStore(folder).getById(confgiId);
Config config = getStore().getById(confgiId);
req.setAttribute("contentType", config.getProvider().getContentType());
req.setAttribute("config", config);
req.setAttribute("provider", config.getProvider());
Expand Down Expand Up @@ -186,7 +181,7 @@ public void doSelectProvider(StaplerRequest req, StaplerResponse rsp) throws IOE
public HttpResponse doRemoveConfig(StaplerRequest res, StaplerResponse rsp, @QueryParameter("id") String configId) throws IOException {
checkPermission(Hudson.ADMINISTER);

getStore(folder).remove(configId);
getStore().remove(configId);

return new HttpRedirect("index");
}
Expand All @@ -197,7 +192,7 @@ public FormValidation doCheckConfigId(@QueryParameter("configId") String configI
return FormValidation.warning(Messages.ConfigFilesManagement_configIdCannotBeEmpty());
}

Config config = getStore(folder).getById(configId);
Config config = getStore().getById(configId);
if (config == null) {
return FormValidation.ok();
} else {
Expand Down
@@ -0,0 +1,115 @@
package org.jenkinsci.plugins.configfiles.folder;

import com.cloudbees.hudson.plugins.folder.Folder;
import hudson.ExtensionList;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.ConfigFileStore;
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles;
import org.jenkinsci.plugins.configfiles.maven.MavenSettingsConfig;
import org.jenkinsci.plugins.configfiles.xml.XmlConfig;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

/**
* Created by domi on 11/10/16.
*/
public class FolderConfigFileActionTest {

@Rule
public JenkinsRule r = new JenkinsRule();
@Rule
public BuildWatcher buildWatcher = new BuildWatcher();

@Test
public void foldersHaveTheirOwnListOfConfigs() throws Exception {
Folder f1 = createFolder();
getStore(f1).save(newXmlFile());

Map<ConfigProvider, Collection<Config>> groupedConfigs1 = getStore(f1).getGroupedConfigs();
assertNotNull(groupedConfigs1);
Folder f2 = createFolder();
getStore(f2).save(newXmlFile());

Map<ConfigProvider, Collection<Config>> groupedConfigs2 = getStore(f2).getGroupedConfigs();
assertNotNull(groupedConfigs2);
System.out.println(groupedConfigs1 + " - " + groupedConfigs2);
assertNotEquals(groupedConfigs1, groupedConfigs2);
}


/*
* see details to this test: https://gist.github.com/cyrille-leclerc/5571fdc443d6f7bff4e5ec10d614a15d
*/
@Test
public void accessGlobalFilesFromWithinFolder() throws Exception {
GlobalConfigFiles globalConfigFiles = r.jenkins.getExtensionList(ConfigFileStore.class).get(GlobalConfigFiles.class);
Collection<Config> configs = globalConfigFiles.getConfigs();
assertNotNull(configs);
assertTrue(configs.isEmpty());
globalConfigFiles.save(newMvnSettings("my-maven-settings"));

WorkflowJob jobInRoot = r.jenkins.createProject(WorkflowJob.class, "p");
jobInRoot.setDefinition(getNewJobDefinition());

Folder folder1 = createFolder();
getStore(folder1).save(newMvnSettings("my-maven-settings"));

WorkflowJob jobInFolder1 = folder1.createProject(WorkflowJob.class, "p");
jobInFolder1.setDefinition(getNewJobDefinition());

Folder folder2 = createFolder();


WorkflowJob jobInFolder2 = folder2.createProject(WorkflowJob.class, "p");
jobInFolder2.setDefinition(getNewJobDefinition());

WorkflowRun b0 = r.assertBuildStatusSuccess(jobInRoot.scheduleBuild2(0));
WorkflowRun b1 = r.assertBuildStatusSuccess(jobInFolder1.scheduleBuild2(0));
WorkflowRun b2 = r.assertBuildStatusSuccess(jobInFolder2.scheduleBuild2(0));
}

private CpsFlowDefinition getNewJobDefinition() {
return new CpsFlowDefinition(""
+ "node {\n" +
" configFileProvider([configFile(fileId: 'my-maven-settings', variable: 'MY_MAVEN_SETTINGS')]) {\n" +
" sh '''\n" +
" ls -al $MY_MAVEN_SETTINGS\n" +
" cat $MY_MAVEN_SETTINGS\n" +
" '''\n" +
" }\n" +
"}", true);
}


private ConfigFileStore getStore(Folder f) {
FolderConfigFileAction action = f.getAction(FolderConfigFileAction.class);
return action.getStore();
}

private Folder createFolder() throws IOException {
return r.jenkins.createProject(Folder.class, "folder" + r.jenkins.getItems().size());
}

private Config newXmlFile() {
XmlConfig.XmlConfigProvider configProvider = r.jenkins.getExtensionList(ConfigProvider.class).get(XmlConfig.XmlConfigProvider.class);
return configProvider.newConfig("xml_" + System.currentTimeMillis());
}

private Config newMvnSettings(String settingsId) {
MavenSettingsConfig.MavenSettingsConfigProvider configProvider = r.jenkins.getExtensionList(ConfigProvider.class).get(MavenSettingsConfig.MavenSettingsConfigProvider.class);
return configProvider.newConfig(settingsId);
}
}

0 comments on commit f8765dc

Please sign in to comment.