Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration as code tests #59

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,68 @@ That's why this example specifies a "Relative Path" of "git" (and a "Subdirector

![](docs/images/example-checkAlreadyOnPath.png)

## JcasC

Plugin can be configured using JCasC. Please find some example bellow.

They are using extra plugin [generic-tool](https://plugins.jenkins.io/generic-tool/) and [ansible](https://plugins.jenkins.io/ansible/)

```
tool:
maven:
installations:
- name: "maven-3.9.4"
properties:
- installSource:
installers:
- anyOf:
attemptsOfWholeList: 3
attemptsPerInstaller: 5
installers:
installers:
- authenticatedzip:
label: "linux"
subdir: "apache-maven-3.9.5"
url: "https://private.registry.company.org/maven/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz"
credentialsId: "private-registry-credentials-id"
- authenticatedzip:
label: "linux"
subdir: "apache-maven-3.9.5"
url: "https://archive.apache.org/dist/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz"
- stubInstaller:
failOnSubstitution: false
failTheBuild: true
label: "!linux"
message: "Unable to install on this node"
generic:
installations:
- name: "python3"
properties:
- installSource:
installers:
- findonpath:
executableName: "python3"
label: "built-in"
versionCmd:
- "python3 --version"
versionCmdString: "python3 --version"
versionMax: "3.12.0"
versionMin: "3.10.0"
versionPatternString: "Python (.*)"

ansible:
installations:
- name: "ansible"
properties:
- installSource:
installers:
- sharedDirectoryInstaller:
failOnSubstitution: true
label: "linux"
toolHome: "^${HOME}/.local/bin/ansible"

```

## See also
* [Software licence](LICENSE)
* Support and [contribution guide](CONTRIBUTING.md)
Expand Down
29 changes: 27 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

<properties>
<changelist>999999-SNAPSHOT</changelist>
<jenkins.version>2.401.3</jenkins.version>
<jenkins.version>2.414.3</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<configuration-as-code.version>1714.v09593e830cfa</configuration-as-code.version>
</properties>

<repositories>
Expand Down Expand Up @@ -58,13 +59,37 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.configuration-as-code</groupId>
<artifactId>test-harness</artifactId>
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>generic-tool</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ansible</artifactId>
<version>276.vef26df37652f</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.401.x</artifactId>
<artifactId>bom-2.414.x</artifactId>
<version>2516.v113cb_3d00317</version>
<scope>import</scope>
<type>pom</type>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package io.jenkins.plugins.extratoolinstallers.jcasc;

import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.extratoolinstallers.installers.AnyOfInstaller;
import io.jenkins.plugins.extratoolinstallers.installers.AuthenticatedZipExtractionInstaller;
import io.jenkins.plugins.extratoolinstallers.installers.IsAlreadyOnPath;
import io.jenkins.plugins.generic_tool.GenericToolInstallation;
import jenkins.model.Jenkins;

import static org.junit.Assert.assertEquals;

import org.jenkinsci.plugins.ansible.AnsibleInstallation;
import org.junit.Rule;
import org.junit.Test;

import com.synopsys.arc.jenkinsci.plugins.extratoolinstallers.installers.SharedDirectoryInstaller;
import com.synopsys.arc.jenkinsci.plugins.extratoolinstallers.installers.StubInstaller;

import hudson.tasks.Maven.MavenInstallation;
import hudson.tools.InstallSourceProperty;
import hudson.tools.ToolProperty;
import hudson.tools.ToolPropertyDescriptor;
import hudson.util.DescribableList;

public class ConfigurationAsCodeTest {

@Rule public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();

@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void should_support_configuration_as_code() throws Exception {

// Just ensure that the structure is correct for maven tool
MavenInstallation mavenInstallation = Jenkins.get().getDescriptorByType(MavenInstallation.DescriptorImpl.class).getInstallations()[0];
assertEquals("maven-3.9.4", mavenInstallation.getName());
DescribableList<ToolProperty<?>, ToolPropertyDescriptor> mavenToolInstallers = mavenInstallation.getProperties();
assertEquals(1, mavenToolInstallers.size());
InstallSourceProperty mavenInstallSourceProperty = (InstallSourceProperty)mavenToolInstallers.get(0);
assertEquals(1, mavenInstallSourceProperty.installers.size());

// AnyOfInstaller is set with 3 installers
AnyOfInstaller anyOfInstaller = (AnyOfInstaller)mavenInstallSourceProperty.installers.get(0);
assertEquals(3, anyOfInstaller.getInstallers().installers.size());

AuthenticatedZipExtractionInstaller installer1 = (AuthenticatedZipExtractionInstaller)anyOfInstaller.getInstallers().installers.get(0);
AuthenticatedZipExtractionInstaller installer2 = (AuthenticatedZipExtractionInstaller)anyOfInstaller.getInstallers().installers.get(1);
StubInstaller installer3 = (StubInstaller)anyOfInstaller.getInstallers().installers.get(2);

// Installer 1
assertEquals("linux", installer1.getLabel());
assertEquals("private-registry-credentials-id", installer1.getCredentialsId());
assertEquals("https://private.registry.company.org/maven/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz", installer1.getUrl());

// Installer 2
assertEquals("linux", installer2.getLabel());
assertEquals(null, installer2.getCredentialsId());
assertEquals("https://archive.apache.org/dist/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz", installer2.getUrl());

// Installer 3
assertEquals("!linux", installer3.getLabel());
assertEquals(false, installer3.isFailOnSubstitution());
assertEquals(true, installer3.isFailTheBuild());
assertEquals("Unable to install on this node", installer3.getMessage());

// Just ensure that the structure is correct for generic tool
GenericToolInstallation genericInstallation = Jenkins.get().getDescriptorByType(GenericToolInstallation.DescriptorImpl.class).getInstallations()[0];
assertEquals("python3", genericInstallation.getName());
DescribableList<ToolProperty<?>, ToolPropertyDescriptor> genericToolInstallers = genericInstallation.getProperties();
assertEquals(1, genericToolInstallers.size());
InstallSourceProperty genericInstallSourceProperty = (InstallSourceProperty)genericToolInstallers.get(0);
assertEquals(1, genericInstallSourceProperty.installers.size());

IsAlreadyOnPath isAlreadyOnPathProperty = (IsAlreadyOnPath)genericInstallSourceProperty.installers.get(0);

// Installer
assertEquals("linux", isAlreadyOnPathProperty.getLabel());
assertEquals("3.10.0", isAlreadyOnPathProperty.getVersionMin());
assertEquals("3.12.0", isAlreadyOnPathProperty.getVersionMax());
assertEquals("Python (.*)", isAlreadyOnPathProperty.getVersionPatternString());

// Just ensure that the structure is correct for generic tool
AnsibleInstallation ansibleInstallation = Jenkins.get().getDescriptorByType(AnsibleInstallation.DescriptorImpl.class).getInstallations()[0];
assertEquals("ansible", ansibleInstallation.getName());
DescribableList<ToolProperty<?>, ToolPropertyDescriptor> ansibleToolInstaller = ansibleInstallation.getProperties();
assertEquals(1, ansibleToolInstaller.size());
InstallSourceProperty ansibleInstallSourceProperty = (InstallSourceProperty)ansibleInstallation.getProperties().get(0);
assertEquals(1, ansibleInstallSourceProperty.installers.size());

SharedDirectoryInstaller sharedDirectoryInstaller = (SharedDirectoryInstaller)ansibleInstallSourceProperty.installers.get(0);
assertEquals("linux", sharedDirectoryInstaller.getLabel());
assertEquals(true, sharedDirectoryInstaller.isFailOnSubstitution());
assertEquals("${HOME}/.local/bin/ansible", sharedDirectoryInstaller.getToolHome());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
tool:

# anyOf
maven:
installations:
- name: "maven-3.9.4"
properties:
- installSource:
installers:
- anyOf:
attemptsOfWholeList: 3
attemptsPerInstaller: 5
installers:
installers:
- authenticatedzip:
label: "linux"
subdir: "apache-maven-3.9.5"
url: "https://private.registry.company.org/maven/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz"
credentialsId: "private-registry-credentials-id"
- authenticatedzip:
label: "linux"
subdir: "apache-maven-3.9.5"
url: "https://archive.apache.org/dist/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz"
- stubInstaller:
failOnSubstitution: false
failTheBuild: true
label: "!linux"
message: "Unable to install on this node"

# findonpath
generic:
installations:
- name: "python3"
properties:
- installSource:
installers:
- findonpath:
executableName: "python3"
label: "linux"
versionCmd:
- "python3 --version"
versionCmdString: "python3 --version"
versionMax: "3.12.0"
versionMin: "3.10.0"
versionPatternString: "Python (.*)"

# sharedDirectoryInstaller
ansible:
installations:
- name: "ansible"
properties:
- installSource:
installers:
- sharedDirectoryInstaller:
failOnSubstitution: true
label: "linux"
toolHome: "^${HOME}/.local/bin/ansible"