Search in sources :

Example 36 with Jenkins

use of jenkins.model.Jenkins in project configuration-as-code-plugin by jenkinsci.

the class ActiveDirectoryTest method configure_active_directory.

@Test
@ConfiguredWithCode(value = "ActiveDirectoryTest.yml")
public void configure_active_directory() throws Exception {
    final Jenkins jenkins = Jenkins.getInstance();
    final ActiveDirectorySecurityRealm realm = (ActiveDirectorySecurityRealm) jenkins.getSecurityRealm();
    assertEquals(1, realm.domains.size());
    final ActiveDirectoryDomain domain = realm.domains.get(0);
    assertEquals("acme", domain.name);
    assertEquals("admin", domain.bindName);
    assertEquals("ADMIN123", domain.bindPassword.getPlainText());
    assertEquals("ad1.acme.com:123,ad2.acme.com:456", domain.servers);
    assertTrue(realm.removeIrrelevantGroups);
    assertTrue(realm.startTls);
}
Also used : Jenkins(jenkins.model.Jenkins) ActiveDirectorySecurityRealm(hudson.plugins.active_directory.ActiveDirectorySecurityRealm) ActiveDirectoryDomain(hudson.plugins.active_directory.ActiveDirectoryDomain) Test(org.junit.Test) ConfiguredWithCode(org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)

Example 37 with Jenkins

use of jenkins.model.Jenkins in project configuration-as-code-plugin by jenkinsci.

the class AgentProtocolsTest method configure_agent_protocols.

@Test
@ConfiguredWithCode(value = "AgentProtocolsTest.yml")
public void configure_agent_protocols() throws Exception {
    final Jenkins jenkins = Jenkins.getInstance();
    final Set<String> agentProtocols = Arrays.stream(new String[] { "JNLP4-connect", "Ping" }).collect(Collectors.toSet());
    assertEquals(agentProtocols, jenkins.getAgentProtocols());
}
Also used : Jenkins(jenkins.model.Jenkins) Test(org.junit.Test) ConfiguredWithCode(org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)

Example 38 with Jenkins

use of jenkins.model.Jenkins in project configuration-as-code-plugin by jenkinsci.

the class Configurator method lookup.

/**
 * Looks for a configurator for exact type.
 * @param type Type
 * @return Configurator or {@code null} if it is not found
 */
@CheckForNull
public static Configurator lookup(Type type) {
    Class clazz = Types.erasure(type);
    final Jenkins jenkins = Jenkins.getInstance();
    final ExtensionList<Configurator> l = jenkins.getExtensionList(Configurator.class);
    for (Configurator c : l) {
        if (clazz == c.getTarget()) {
            // this type has a dedicated Configurator implementation
            return c;
        }
    }
    if (Collection.class.isAssignableFrom(clazz)) {
        // TODO: Only try to cast if we can actually get the parameterized type
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            Type actualType = pt.getActualTypeArguments()[0];
            if (actualType instanceof WildcardType) {
                actualType = ((WildcardType) actualType).getUpperBounds()[0];
            }
            if (!(actualType instanceof Class)) {
                throw new IllegalStateException("Can't handle " + type);
            }
            return lookup(actualType);
        }
    }
    if (Descriptor.class.isAssignableFrom(clazz)) {
        return new DescriptorConfigurator((Descriptor) jenkins.getExtensionList(clazz).get(0));
    }
    if (getDataBoundConstructor(clazz) != null) {
        return new DataBoundConfigurator(clazz);
    }
    if (Modifier.isAbstract(clazz.getModifiers()) && Describable.class.isAssignableFrom(clazz)) {
        // this is a jenkins Describable component, with various implementations
        return new HeteroDescribableConfigurator(clazz);
    }
    if (TopLevelItem.class.isAssignableFrom(clazz)) {
        return new TopLevelItemConfigurator(clazz);
    }
    if (Extension.class.isAssignableFrom(clazz)) {
        return new ExtensionConfigurator(clazz);
    }
    if (Stapler.lookupConverter(clazz) != null) {
        return new PrimitiveConfigurator(clazz);
    }
    logger.warning("Configuration-as-Code can't handle type " + type);
    return null;
}
Also used : Describable(hudson.model.Describable) Jenkins(jenkins.model.Jenkins) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) CheckForNull(javax.annotation.CheckForNull)

Example 39 with Jenkins

use of jenkins.model.Jenkins in project configuration-as-code-plugin by jenkinsci.

the class JenkinsConfigurator method describe.

@Override
public Set<Attribute> describe() {
    final Set<Attribute> attributes = super.describe();
    final Jenkins jenkins = Jenkins.getInstance();
    attributes.add(new PersistedListAttribute<Cloud>("clouds", jenkins.clouds, Cloud.class));
    attributes.add(new Attribute<String>("jobs", String.class).multiple(true).setter((target, attribute, value) -> {
        JenkinsJobManagement mng = new JenkinsJobManagement(System.out, new EnvVars(), null, null, LookupStrategy.JENKINS_ROOT);
        for (String script : (List<String>) value) {
            new JenkinsDslScriptLoader(mng).runScript(script);
        }
    }));
    // Check for unclassified Descriptors
    final ExtensionList<Descriptor> descriptors = jenkins.getExtensionList(Descriptor.class);
    for (Descriptor descriptor : descriptors) {
        if (descriptor.getGlobalConfigPage() != null && descriptor.getCategory() instanceof GlobalConfigurationCategory.Unclassified) {
            final DescriptorConfigurator configurator = new DescriptorConfigurator(descriptor);
            attributes.add(new Attribute(configurator.getName(), configurator.getTarget()).setter(NOOP));
        }
    }
    // Add remoting security, all legwork will be done by a configurator
    attributes.add(new Attribute("remotingSecurity", AdminWhitelistRule.class).setter(NOOP));
    return attributes;
}
Also used : JenkinsDslScriptLoader(javaposse.jobdsl.plugin.JenkinsDslScriptLoader) JenkinsJobManagement(javaposse.jobdsl.plugin.JenkinsJobManagement) Descriptor(hudson.model.Descriptor) Jenkins(jenkins.model.Jenkins) AdminWhitelistRule(jenkins.security.s2m.AdminWhitelistRule) Set(java.util.Set) Logger(java.util.logging.Logger) Level(java.util.logging.Level) List(java.util.List) Map(java.util.Map) Extension(hudson.Extension) LookupStrategy(javaposse.jobdsl.plugin.LookupStrategy) EnvVars(hudson.EnvVars) Cloud(hudson.slaves.Cloud) ExtensionList(hudson.ExtensionList) GlobalConfigurationCategory(jenkins.model.GlobalConfigurationCategory) JenkinsJobManagement(javaposse.jobdsl.plugin.JenkinsJobManagement) JenkinsDslScriptLoader(javaposse.jobdsl.plugin.JenkinsDslScriptLoader) Jenkins(jenkins.model.Jenkins) EnvVars(hudson.EnvVars) Cloud(hudson.slaves.Cloud) Descriptor(hudson.model.Descriptor) GlobalConfigurationCategory(jenkins.model.GlobalConfigurationCategory)

Example 40 with Jenkins

use of jenkins.model.Jenkins in project configuration-as-code-plugin by jenkinsci.

the class SeedJobTest method configure_seed_job.

@Test
@ConfiguredWithCode("SeedJobTest.yml")
public void configure_seed_job() throws Exception {
    final Jenkins jenkins = Jenkins.getInstance();
    final TopLevelItem test = jenkins.getItem("configuration-as-code");
    assertNotNull(test);
    assertTrue(test instanceof WorkflowMultiBranchProject);
}
Also used : Jenkins(jenkins.model.Jenkins) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) TopLevelItem(hudson.model.TopLevelItem) Test(org.junit.Test) ConfiguredWithCode(org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)

Aggregations

Jenkins (jenkins.model.Jenkins)73 Test (org.junit.Test)22 ConfiguredWithCode (org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)13 IOException (java.io.IOException)10 File (java.io.File)9 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)9 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 Statement (org.junit.runners.model.Statement)7 CheckForNull (javax.annotation.CheckForNull)6 FilePath (hudson.FilePath)5 Computer (hudson.model.Computer)5 URL (java.net.URL)5 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)5 Issue (org.jvnet.hudson.test.Issue)5 Item (hudson.model.Item)4 Node (hudson.model.Node)4 Date (java.util.Date)4