Merge pull request #111 from lanwen/tests
move all init logic to special rule @ConfiguredWithCode
This commit is contained in:
commit
40b2c83fca
|
|
@ -3,9 +3,12 @@ package org.jenkinsci.plugins.casc;
|
|||
import hudson.plugins.active_directory.ActiveDirectoryDomain;
|
||||
import hudson.plugins.active_directory.ActiveDirectorySecurityRealm;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.EnvVarsRule;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
import org.junit.rules.RuleChain;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
|
@ -15,16 +18,14 @@ import static org.junit.Assert.assertTrue;
|
|||
*/
|
||||
public class ActiveDirectoryTest {
|
||||
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public RuleChain chain = RuleChain.outerRule(new EnvVarsRule()
|
||||
.env("BIND_PASSWORD", "ADMIN123"))
|
||||
.around(new JenkinsConfiguredWithCodeRule());
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode(value = "ActiveDirectoryTest.yml")
|
||||
public void configure_active_directory() throws Exception {
|
||||
System.setProperty("BIND_PASSWORD", "ADMIN123");
|
||||
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("ActiveDirectoryTest.yml"));
|
||||
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
final ActiveDirectorySecurityRealm realm = (ActiveDirectorySecurityRealm) jenkins.getSecurityRealm();
|
||||
assertEquals(1, realm.domains.size());
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
package org.jenkinsci.plugins.casc;
|
||||
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
|
@ -18,12 +18,11 @@ import static org.junit.Assert.assertEquals;
|
|||
public class AgentProtocolsTest {
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode(value = "AgentProtocolsTest.yml")
|
||||
public void configure_agent_protocols() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("AgentProtocolsTest.yml"));
|
||||
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
final Set<String> agentProtocols =
|
||||
Arrays.stream(new String[]{"JNLP4-connect", "Ping"}).collect(Collectors.toSet());
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package org.jenkinsci.plugins.casc;
|
||||
|
||||
import hudson.model.AbstractDescribableImpl;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
|
@ -11,7 +10,9 @@ import javax.annotation.PostConstruct;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
|
||||
|
|
@ -33,7 +34,7 @@ public class DataBoundConfiguratorTest {
|
|||
assertEquals(true, configured.bar);
|
||||
assertEquals(123, configured.qix);
|
||||
assertEquals("DataBoundSetter", configured.zot);
|
||||
assertTrue( configured.intialized);
|
||||
assertThat(configured.intialized, is(true));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -4,9 +4,11 @@ import com.nirima.jenkins.plugins.docker.DockerCloud;
|
|||
import com.nirima.jenkins.plugins.docker.DockerTemplate;
|
||||
import hudson.model.Label;
|
||||
import io.jenkins.docker.connector.DockerComputerAttachConnector;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.jenkinsci.plugins.casc.misc.TestConfiguration;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
|
@ -16,14 +18,12 @@ import static org.junit.Assert.assertNotNull;
|
|||
*/
|
||||
public class DockerCloudTest {
|
||||
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("DockerCloudTest.yml")
|
||||
public void configure_docker_cloud() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("DockerCloudTest.yml"));
|
||||
|
||||
final DockerCloud docker = DockerCloud.getCloudByName("docker");
|
||||
assertNotNull(docker);
|
||||
assertNotNull(docker.getDockerApi());
|
||||
|
|
@ -34,10 +34,8 @@ public class DockerCloudTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("DockerCloudTest/update_docker_cloud/DockerCloudTest1.yml")
|
||||
public void update_docker_cloud() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream(
|
||||
"DockerCloudTest/update_docker_cloud/DockerCloudTest1.yml"));
|
||||
|
||||
DockerCloud docker = DockerCloud.getCloudByName("docker");
|
||||
assertNotNull(docker);
|
||||
assertNotNull(docker.getDockerApi());
|
||||
|
|
@ -47,8 +45,7 @@ public class DockerCloudTest {
|
|||
DockerTemplate template = docker.getTemplate(Label.get("docker-agent"));
|
||||
checkTemplate(template, "docker-agent", "jenkins", "/home/jenkins/agent", "10");
|
||||
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream(
|
||||
"DockerCloudTest/update_docker_cloud/DockerCloudTest2.yml"));
|
||||
TestConfiguration.withCode("DockerCloudTest/update_docker_cloud/DockerCloudTest2.yml").configure(getClass());
|
||||
|
||||
docker = DockerCloud.getCloudByName("docker");
|
||||
assertNotNull(docker);
|
||||
|
|
@ -65,7 +62,7 @@ public class DockerCloudTest {
|
|||
}
|
||||
|
||||
private void checkTemplate(DockerTemplate template, String labelString, String user, String remoteFs,
|
||||
String instanceCapStr){
|
||||
String instanceCapStr) {
|
||||
assertNotNull(template);
|
||||
assertEquals(labelString, template.getLabelString());
|
||||
assertEquals(user, ((DockerComputerAttachConnector) template.getConnector()).getUser());
|
||||
|
|
|
|||
|
|
@ -2,9 +2,10 @@ package org.jenkinsci.plugins.casc;
|
|||
|
||||
import hudson.plugins.git.GitTool;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
|
@ -13,14 +14,12 @@ import static org.junit.Assert.assertEquals;
|
|||
*/
|
||||
public class GitToolInstallationTest {
|
||||
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("GitToolInstallationTest.yml")
|
||||
public void configure_git_installations() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("GitToolInstallationTest.yml"));
|
||||
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
final GitTool.DescriptorImpl descriptor = (GitTool.DescriptorImpl) jenkins.getDescriptor(GitTool.class);
|
||||
assertEquals(2, descriptor.getInstallations().length);
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
package org.jenkinsci.plugins.casc;
|
||||
|
||||
import com.nirima.jenkins.plugins.docker.DockerCloud;
|
||||
import com.nirima.jenkins.plugins.docker.DockerTemplate;
|
||||
import hudson.model.TopLevelItem;
|
||||
import jenkins.branch.OrganizationFolder;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
|
@ -19,15 +17,14 @@ import static org.junit.Assert.assertTrue;
|
|||
*/
|
||||
public class GithubOrganisationFolderTest {
|
||||
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
|
||||
// @Test
|
||||
// Fails as Items do override submit() with manual data-binding implementation
|
||||
@ConfiguredWithCode("GithubOrganisationFolderTest.yml")
|
||||
public void configure_github_organisation_folder_seed_job() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("GithubOrganisationFolderTest.yml"));
|
||||
|
||||
final TopLevelItem job = Jenkins.getInstance().getItem("ndeloof");
|
||||
assertNotNull(job);
|
||||
assertTrue(job instanceof OrganizationFolder);
|
||||
|
|
|
|||
|
|
@ -1,34 +1,27 @@
|
|||
package org.jenkinsci.plugins.casc;
|
||||
|
||||
import com.nirima.jenkins.plugins.docker.DockerCloud;
|
||||
import com.nirima.jenkins.plugins.docker.DockerTemplate;
|
||||
import io.jenkins.docker.connector.DockerComputerAttachConnector;
|
||||
import jenkins.plugins.git.GitSCMSource;
|
||||
import jenkins.scm.api.SCMSource;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries;
|
||||
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
|
||||
import org.jenkinsci.plugins.workflow.libs.LibraryRetriever;
|
||||
import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
|
||||
*/
|
||||
public class GlobalLibrariesTest {
|
||||
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("GlobalLibrariesTest.yml")
|
||||
public void configure_global_library() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("GlobalLibrariesTest.yml"));
|
||||
|
||||
assertEquals(1, GlobalLibraries.get().getLibraries().size());
|
||||
final LibraryConfiguration library = GlobalLibraries.get().getLibraries().get(0);
|
||||
assertEquals("awesome-lib", library.getName());
|
||||
|
|
|
|||
|
|
@ -5,12 +5,17 @@ import hudson.security.FullControlOnceLoggedInAuthorizationStrategy;
|
|||
import hudson.security.HudsonPrivateSecurityRealm;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.Issue;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
|
||||
|
|
@ -18,21 +23,19 @@ import static org.junit.Assert.*;
|
|||
public class JenkinsConfiguratorTest {
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("Primitives.yml")
|
||||
public void jenkins_primitive_attributes() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("Primitives.yml"));
|
||||
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
assertEquals(6666, jenkins.getSlaveAgentPort());
|
||||
assertEquals(false, jenkins.isUsageStatisticsCollected());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("HeteroDescribable.yml")
|
||||
public void jenkins_abstract_describable_attributes() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("HeteroDescribable.yml"));
|
||||
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
assertTrue(jenkins.getSecurityRealm() instanceof HudsonPrivateSecurityRealm);
|
||||
assertTrue(jenkins.getAuthorizationStrategy() instanceof FullControlOnceLoggedInAuthorizationStrategy);
|
||||
|
|
|
|||
|
|
@ -2,12 +2,13 @@ package org.jenkinsci.plugins.casc;
|
|||
|
||||
import javaposse.jobdsl.plugin.GlobalJobDslSecurityConfiguration;
|
||||
import jenkins.model.GlobalConfiguration;
|
||||
import org.jenkinsci.plugins.casc.misc.TestConfiguration;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Created by odavid on 23/12/2017.
|
||||
|
|
@ -19,15 +20,14 @@ public class JobDslGlobaSecurityConfigurationTest {
|
|||
|
||||
@Test
|
||||
public void global_dsl_security() throws Exception {
|
||||
final GlobalJobDslSecurityConfiguration dslSecurity =
|
||||
GlobalConfiguration.all().get(GlobalJobDslSecurityConfiguration.class);
|
||||
final GlobalJobDslSecurityConfiguration dslSecurity = GlobalConfiguration.all()
|
||||
.get(GlobalJobDslSecurityConfiguration.class);
|
||||
|
||||
dslSecurity.setUseScriptSecurity(true);
|
||||
assertTrue(dslSecurity.isUseScriptSecurity());
|
||||
assertThat("ScriptSecurity", dslSecurity.isUseScriptSecurity(), is(true));
|
||||
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("JobDslGlobaSecurityConfigurationTest.yml"));
|
||||
|
||||
assertFalse(dslSecurity.isUseScriptSecurity());
|
||||
TestConfiguration.withCode("JobDslGlobaSecurityConfigurationTest.yml").configure(getClass());
|
||||
assertThat("ScriptSecurity", dslSecurity.isUseScriptSecurity(), is(false));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
package org.jenkinsci.plugins.casc;
|
||||
|
||||
import hudson.security.LDAPSecurityRealm;
|
||||
import hudson.security.SecurityRealm;
|
||||
import hudson.tasks.Mailer;
|
||||
import jenkins.model.IdStrategy;
|
||||
import jenkins.model.Jenkins;
|
||||
import jenkins.security.plugins.ldap.LDAPConfiguration;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.EnvVarsRule;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
import org.junit.rules.RuleChain;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
|
@ -18,15 +19,14 @@ import static org.junit.Assert.assertTrue;
|
|||
*/
|
||||
public class LDAPSecurityRealmTest {
|
||||
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public RuleChain chain = RuleChain.outerRule(new EnvVarsRule()
|
||||
.env("LDAP_PASSWORD", "SECRET"))
|
||||
.around(new JenkinsConfiguredWithCodeRule());
|
||||
|
||||
@Test
|
||||
public void configure_securityRealm() throws Exception {
|
||||
System.setProperty("LDAP_PASSWORD", "SECRET");
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("LDAPSecurityRealmTest.yml"));
|
||||
|
||||
@ConfiguredWithCode("LDAPSecurityRealmTest.yml")
|
||||
public void configure_securityRealm() {
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
final LDAPSecurityRealm securityRealm = (LDAPSecurityRealm) jenkins.getSecurityRealm();
|
||||
assertEquals(1, securityRealm.getConfigurations().size());
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
package org.jenkinsci.plugins.casc;
|
||||
|
||||
import hudson.plugins.git.GitTool;
|
||||
import hudson.tasks.Mailer;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
|
@ -14,14 +14,12 @@ import static org.junit.Assert.assertEquals;
|
|||
*/
|
||||
public class MailerTest {
|
||||
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("MailerTest.yml")
|
||||
public void configure_mailer() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("MailerTest.yml"));
|
||||
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
final Mailer.DescriptorImpl descriptor = (Mailer.DescriptorImpl) jenkins.getDescriptor(Mailer.class);
|
||||
assertEquals("4441", descriptor.getSmtpPort());
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ import org.junit.Test;
|
|||
import org.junit.contrib.java.lang.system.EnvironmentVariables;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
|
||||
|
|
|
|||
|
|
@ -2,10 +2,11 @@ package org.jenkinsci.plugins.casc;
|
|||
|
||||
import hudson.model.TopLevelItem;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
|
@ -16,11 +17,11 @@ import static org.junit.Assert.assertTrue;
|
|||
public class SeedJobTest {
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("SeedJobTest.yml")
|
||||
public void configure_seed_job() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("SeedJobTest.yml"));
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
final TopLevelItem test = jenkins.getItem("configuration-as-code");
|
||||
assertNotNull(test);
|
||||
|
|
|
|||
|
|
@ -5,47 +5,53 @@ import com.cloudbees.plugins.credentials.CredentialsProvider;
|
|||
import com.cloudbees.plugins.credentials.common.CertificateCredentials;
|
||||
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
|
||||
import hudson.security.ACL;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.EnvVarsRule;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
import org.junit.rules.RuleChain;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
|
||||
*/
|
||||
public class SystemCredentialsTest {
|
||||
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public RuleChain chain = RuleChain.outerRule(new EnvVarsRule()
|
||||
.env("SUDO_PASSWORD", "1234")
|
||||
.env("SSH_PRIVATE_KEY", "s3cr3t")
|
||||
.env("SSH_KEY_PASSWORD", "ABCD"))
|
||||
.around(new JenkinsConfiguredWithCodeRule());
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("SystemCredentialsTest.yml")
|
||||
public void configure_system_credentials() throws Exception {
|
||||
System.setProperty("SUDO_PASSWORD", "1234");
|
||||
System.setProperty("SSH_KEY_PASSWORD", "ABCD");
|
||||
System.setProperty("SSH_PRIVATE_KEY", "s3cr3t");
|
||||
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("SystemCredentialsTest.yml"));
|
||||
|
||||
List<UsernamePasswordCredentials> ups = CredentialsProvider.lookupCredentials(UsernamePasswordCredentials.class, j.jenkins, ACL.SYSTEM, Collections.EMPTY_LIST);
|
||||
assertEquals(1, ups.size());
|
||||
final UsernamePasswordCredentials up = ups.get(0);
|
||||
assertEquals("1234", up.getPassword().getPlainText());
|
||||
|
||||
List<CertificateCredentials> certs = CredentialsProvider.lookupCredentials(CertificateCredentials.class, j.jenkins, ACL.SYSTEM, Collections.EMPTY_LIST);
|
||||
assertEquals(1, certs.size());
|
||||
final CertificateCredentials cert = certs.get(0);
|
||||
assertEquals("ABCD", cert.getPassword().getPlainText());
|
||||
|
||||
List<BasicSSHUserPrivateKey> sshPrivateKeys = CredentialsProvider.lookupCredentials(BasicSSHUserPrivateKey.class, j.jenkins, ACL.SYSTEM, Collections.EMPTY_LIST);
|
||||
assertEquals(2, sshPrivateKeys.size());
|
||||
final BasicSSHUserPrivateKey sshUserPrivateKey = sshPrivateKeys.get(0);
|
||||
assertEquals("ABCD", sshUserPrivateKey.getPassphrase().getPlainText());
|
||||
Jenkins jenkins = Jenkins.getInstance();
|
||||
List<UsernamePasswordCredentials> ups = CredentialsProvider.lookupCredentials(
|
||||
UsernamePasswordCredentials.class, jenkins, ACL.SYSTEM, Collections.emptyList()
|
||||
);
|
||||
assertThat(ups, hasSize(1));
|
||||
assertThat(ups.get(0).getPassword().getPlainText(), equalTo("1234"));
|
||||
|
||||
List<CertificateCredentials> certs = CredentialsProvider.lookupCredentials(
|
||||
CertificateCredentials.class, jenkins, ACL.SYSTEM, Collections.emptyList()
|
||||
);
|
||||
assertThat(certs, hasSize(1));
|
||||
assertThat(certs.get(0).getPassword().getPlainText(), equalTo("ABCD"));
|
||||
|
||||
List<BasicSSHUserPrivateKey> sshPrivateKeys = CredentialsProvider.lookupCredentials(
|
||||
BasicSSHUserPrivateKey.class, jenkins, ACL.SYSTEM, Collections.emptyList()
|
||||
);
|
||||
assertThat(sshPrivateKeys, hasSize(2));
|
||||
assertThat(sshPrivateKeys.get(0).getPassphrase().getPlainText(), equalTo("ABCD"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,25 +22,24 @@ package org.jenkinsci.plugins.casc.core;
|
|||
|
||||
import jenkins.model.Jenkins;
|
||||
import jenkins.security.s2m.AdminWhitelistRule;
|
||||
import org.jenkinsci.plugins.casc.ConfigurationAsCode;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Assert;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.For;
|
||||
import org.jvnet.hudson.test.Issue;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
@For(AdminWhitelistRule.class)
|
||||
public class AdminWhitelistRuleConfiguratorTest {
|
||||
|
||||
@ClassRule
|
||||
public static JenkinsRule j = new JenkinsRule();
|
||||
@Rule
|
||||
public JenkinsConfiguredWithCodeRule config = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
@Issue("Issue #28")
|
||||
@ConfiguredWithCode("AdminWhitelistRuleConfigurator/Slave2MasterSecurityKillSwitch_enabled.yml")
|
||||
public void checkM2SSecurityKillSwitch_enabled() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("AdminWhitelistRuleConfigurator/Slave2MasterSecurityKillSwitch_enabled.yml"));
|
||||
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
AdminWhitelistRule rule = jenkins.getInjector().getInstance(AdminWhitelistRule.class);
|
||||
Assert.assertTrue("MasterToSlave Security should be enabled", rule.getMasterKillSwitch());
|
||||
|
|
@ -48,9 +47,8 @@ public class AdminWhitelistRuleConfiguratorTest {
|
|||
|
||||
@Test
|
||||
@Issue("Issue #28")
|
||||
@ConfiguredWithCode("AdminWhitelistRuleConfigurator/Slave2MasterSecurityKillSwitch_disabled.yml")
|
||||
public void checkM2SSecurityKillSwitch_disabled() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("AdminWhitelistRuleConfigurator/Slave2MasterSecurityKillSwitch_disabled.yml"));
|
||||
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
AdminWhitelistRule rule = jenkins.getInjector().getInstance(AdminWhitelistRule.class);
|
||||
Assert.assertFalse("MasterToSlave Security should be disabled", rule.getMasterKillSwitch());
|
||||
|
|
|
|||
|
|
@ -1,16 +1,17 @@
|
|||
package org.jenkinsci.plugins.casc.core;
|
||||
|
||||
import hudson.model.User;
|
||||
import hudson.security.AuthorizationStrategy;
|
||||
import hudson.security.FullControlOnceLoggedInAuthorizationStrategy;
|
||||
import hudson.security.HudsonPrivateSecurityRealm;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.plugins.casc.ConfigurationAsCode;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
|
||||
|
|
@ -18,12 +19,11 @@ import static org.junit.Assert.*;
|
|||
public class HudsonPrivateSecurityRealmConfiguratorTest {
|
||||
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("HudsonPrivateSecurityRealmConfiguratorTest.yml")
|
||||
public void configure_local_security_and_admin_user() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("HudsonPrivateSecurityRealmConfiguratorTest.yml"));
|
||||
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
final HudsonPrivateSecurityRealm securityRealm = (HudsonPrivateSecurityRealm) jenkins.getSecurityRealm();
|
||||
assertFalse(securityRealm.allowsSignup());
|
||||
|
|
|
|||
|
|
@ -1,23 +1,23 @@
|
|||
package org.jenkinsci.plugins.casc.core;
|
||||
|
||||
import hudson.security.AuthorizationStrategy;
|
||||
import org.jenkinsci.plugins.casc.ConfigurationAsCode;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
/**
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class UnsecuredAuthorizationStrategyConfiguratorTest {
|
||||
@Rule
|
||||
public JenkinsRule j = new JenkinsRule();
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("UnsecuredAuthorizationStrategyConfiguratorTest.yml")
|
||||
public void unsecured() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("UnsecuredAuthorizationStrategyConfiguratorTest.yml"));
|
||||
assertSame(AuthorizationStrategy.UNSECURED, j.jenkins.getAuthorizationStrategy());
|
||||
}
|
||||
}
|
||||
|
|
@ -3,11 +3,11 @@ package org.jenkinsci.plugins.casc.integrations.globalmatrixauth;
|
|||
import hudson.security.AuthorizationStrategy;
|
||||
import hudson.security.GlobalMatrixAuthorizationStrategy;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.plugins.casc.ConfigurationAsCode;
|
||||
import org.jenkinsci.plugins.casc.Configurator;
|
||||
import org.junit.ClassRule;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
|
@ -21,8 +21,9 @@ import static org.junit.Assert.assertNotNull;
|
|||
*/
|
||||
public class GlobalMatrixAuthorizationTest {
|
||||
|
||||
@ClassRule
|
||||
public static JenkinsRule j = new JenkinsRule();
|
||||
@Rule
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldReturnCustomConfigurator() {
|
||||
|
|
@ -40,10 +41,10 @@ public class GlobalMatrixAuthorizationTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@ConfiguredWithCode("GlobalMatrixStrategy.yml")
|
||||
public void checkCorrectlyConfiguredPermissions() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("GlobalMatrixStrategy.yml"));
|
||||
assertEquals("The configured instance must use the Global Matrix Authentication Strategy", GlobalMatrixAuthorizationStrategy.class, Jenkins.getInstance().getAuthorizationStrategy().getClass());
|
||||
GlobalMatrixAuthorizationStrategy gms = (GlobalMatrixAuthorizationStrategy)Jenkins.getInstance().getAuthorizationStrategy();
|
||||
GlobalMatrixAuthorizationStrategy gms = (GlobalMatrixAuthorizationStrategy) Jenkins.getInstance().getAuthorizationStrategy();
|
||||
|
||||
List<String> adminPermission = new ArrayList<>(gms.getGrantedPermissions().get(Jenkins.ADMINISTER));
|
||||
assertEquals("authenticated", adminPermission.get(0));
|
||||
|
|
|
|||
|
|
@ -3,20 +3,19 @@ package org.jenkinsci.plugins.casc.integrations.rolebasedauth;
|
|||
import com.michelin.cio.hudson.plugins.rolestrategy.Role;
|
||||
import com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy;
|
||||
import hudson.security.AuthorizationStrategy;
|
||||
import hudson.security.Permission;
|
||||
import jenkins.model.Jenkins;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import org.jenkinsci.plugins.casc.ConfigurationAsCode;
|
||||
import org.jenkinsci.plugins.casc.Configurator;
|
||||
import org.jenkinsci.plugins.casc.integrations.rolebasedauth.RoleBasedAuthorizationStrategyConfigurator;
|
||||
import org.junit.ClassRule;
|
||||
import org.jenkinsci.plugins.casc.misc.ConfiguredWithCode;
|
||||
import org.jenkinsci.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.jvnet.hudson.test.Issue;
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
|
@ -27,8 +26,8 @@ import static org.junit.Assert.assertThat;
|
|||
*/
|
||||
public class RoleStrategyTest {
|
||||
|
||||
@ClassRule
|
||||
public static JenkinsRule j = new JenkinsRule();
|
||||
@Rule
|
||||
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();
|
||||
|
||||
@Test
|
||||
public void shouldReturnCustomConfigurator() {
|
||||
|
|
@ -49,15 +48,14 @@ public class RoleStrategyTest {
|
|||
|
||||
@Test
|
||||
@Issue("Issue #48")
|
||||
@ConfiguredWithCode("RoleStrategy1.yml")
|
||||
public void shouldReadRolesCorrectly() throws Exception {
|
||||
ConfigurationAsCode.configure(getClass().getResourceAsStream("RoleStrategy1.yml"));
|
||||
|
||||
final Jenkins jenkins = Jenkins.getInstance();
|
||||
|
||||
AuthorizationStrategy s = jenkins.getAuthorizationStrategy();
|
||||
assertThat("Authorization Strategy has been read incorrectly",
|
||||
s, instanceOf(RoleBasedAuthorizationStrategy.class));
|
||||
RoleBasedAuthorizationStrategy rbas = (RoleBasedAuthorizationStrategy)s;
|
||||
RoleBasedAuthorizationStrategy rbas = (RoleBasedAuthorizationStrategy) s;
|
||||
|
||||
Map<Role, Set<String>> globalRoles = rbas.getGrantedRoles(RoleBasedAuthorizationStrategy.GLOBAL);
|
||||
assertThat(globalRoles.size(), equalTo(2));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
package org.jenkinsci.plugins.casc.misc;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* To load specified config with plugin
|
||||
*/
|
||||
@Target(METHOD)
|
||||
@Retention(RUNTIME)
|
||||
public @interface ConfiguredWithCode {
|
||||
|
||||
/**
|
||||
* resource path in classpath
|
||||
*/
|
||||
String value();
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package org.jenkinsci.plugins.casc.misc;
|
||||
|
||||
import org.junit.contrib.java.lang.system.EnvironmentVariables;
|
||||
|
||||
/**
|
||||
* @author lanwen (Kirill Merkushev)
|
||||
*/
|
||||
public class EnvVarsRule extends EnvironmentVariables {
|
||||
public EnvVarsRule env(String name, String value) {
|
||||
set(name, value);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package org.jenkinsci.plugins.casc.misc;
|
||||
|
||||
import org.jvnet.hudson.test.JenkinsRule;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author lanwen (Kirill Merkushev)
|
||||
*/
|
||||
public class JenkinsConfiguredWithCodeRule extends JenkinsRule {
|
||||
@Override
|
||||
public void before() throws Throwable {
|
||||
super.before();
|
||||
ConfiguredWithCode configuredWithCode = env.description().getAnnotation(ConfiguredWithCode.class);
|
||||
|
||||
if (Objects.nonNull(configuredWithCode)) {
|
||||
TestConfiguration.withCode(configuredWithCode.value()).configure(env.description().getTestClass());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package org.jenkinsci.plugins.casc.misc;
|
||||
|
||||
import org.jenkinsci.plugins.casc.ConfigurationAsCode;
|
||||
|
||||
/**
|
||||
* Loads resource as configuration-as-code
|
||||
*/
|
||||
public class TestConfiguration {
|
||||
private final String resource;
|
||||
|
||||
private TestConfiguration(String resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
public static TestConfiguration withCode(String resource) {
|
||||
return new TestConfiguration(resource);
|
||||
}
|
||||
|
||||
public void configure(Class<?> clazz) {
|
||||
try {
|
||||
ConfigurationAsCode.configure(clazz.getResourceAsStream(resource));
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Can't configure test with " + resource, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue