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

MPL-32 Added working implementation of the JenkinsRule unit tests #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
108 changes: 91 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jenkins.version>2.204.2</jenkins.version>
<jenkins-core.version>${jenkins.version}</jenkins-core.version>
<jenkins-war.version>${jenkins.version}</jenkins-war.version>
<jenkins-test-harness.version>2.62</jenkins-test-harness.version>
<workflow-cps.version>2.80</workflow-cps.version>
<argLine>-Xms768M -Xmx768M -Djava.awt.headless=true -XX:+HeapDumpOnOutOfMemoryError -XX:+TieredCompilation -XX:TieredStopAtLevel=1</argLine>
<slf4jVersion>1.7.25</slf4jVersion>
</properties>

<repositories>
Expand All @@ -46,17 +53,30 @@
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-core</artifactId>
<version>2.73.3</version>
<version>${jenkins-core.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps-global-lib</artifactId>
<version>2.8</version>
<artifactId>workflow-cps</artifactId>
<version>${workflow-cps.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.44</version>
<artifactId>workflow-cps-global-lib</artifactId>
<version>2.15</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.14.0</version>
<scope>test</scope>
</dependency>

<dependency>
Expand All @@ -65,12 +85,63 @@
<version>1.1</version>
<scope>test</scope>
</dependency>

<!-- Tests JenkinsRule base class -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-test-harness</artifactId>
<version>${jenkins-test-harness.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-war</artifactId>
<version>${jenkins-war.version}</version>
<type>war</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.36</version>
<scope>test</scope>
</dependency>

<!-- Unit tests additional requirements -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>2.19</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
<scope>test</scope>
<!-- mark the API as optional so it is not packaged in the HPI but available during compile -->
<optional>true</optional>
</dependency>
<!-- make slf4j bindings to java.util.logging available during tests -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4jVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4jVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4jVersion}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -103,19 +174,21 @@
</executions>
</plugin>

<!-- Tests -->
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.7.1</version>
<version>1.8.1</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<!--goal>addSources</goal-->
<!--goal>addTestSources</goal-->
<!--goal>generateStubs</goal-->
<goal>generateTestStubs</goal>
<goal>compile</goal>
<goal>compileTests</goal>
<!--goal>removeStubs</goal-->
<goal>removeTestStubs</goal>
<goal>groovydoc</goal>
<goal>groovydocTests</goal>
</goals>
Expand All @@ -124,9 +197,9 @@
<configuration>
<sources>
<source>
<directory>${project.basedir}</directory>
<directory>${project.basedir}/src</directory>
<includes>
<include>src/**/*.groovy</include>
<include>**/*.groovy</include>
</includes>
</source>
<source>
Expand All @@ -144,12 +217,13 @@
</source-->
</sources>
<testSources>
<source>
<testSource>
<directory>${project.basedir}/test</directory>
<includes>
<include>**/*.groovy</include>
<include>**/*.java</include>
</includes>
</source>
</testSource>
</testSources>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// Copyright (c) 2020 Grid Dynamics International, Inc. All Rights Reserved
// https://www.griddynamics.com
//
// Classification level: Public
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id: $
// @Project: MPL
// @Description: Shared Jenkins Modular Pipeline Library
//

import org.junit.Before
import org.junit.Test

import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
import static com.lesfurets.jenkins.unit.global.lib.LocalSource.localSource

import static org.assertj.core.api.Assertions.assertThat

import com.griddynamics.devops.mpl.Helper
import com.griddynamics.devops.mpl.testing.MPLTestBaseJenkinsRule

/**
* Same tests using JenkinsRule
*
* @author Sergei Parshev <[email protected]>
*/
public class BuildJenkinsRuleTest extends MPLTestBaseJenkinsRule {
@Override
@Before
void setUp() throws Exception {
//setScriptRoots([ 'vars' ] as String[])
//setScriptExtension('groovy')

super.setUp()

String sharedLibs = this.class.getResource('.').getFile()
helper.registerSharedLibrary(library()
.name('mpl')
.allowOverride(false)
.retriever(localSource(sharedLibs))
.targetPath(sharedLibs)
.defaultVersion('snapshot')
.implicit(true)
.build()
)

binding.setVariable('env', [:])

// Shared lib requirements
helper.registerAllowedMethod('MPLModule', [], null)
helper.registerAllowedMethod('MPLModule', [String.class], null)
helper.registerAllowedMethod('MPLModule', [String.class, Object.class], null)
helper.registerAllowedMethod('call', [String.class, Object.class], null)

// Test requirements
helper.registerAllowedMethod('fileExists', [String.class], { return false })
helper.registerAllowedMethod('tool', [String.class], { name -> "${name}_HOME" })
helper.registerAllowedMethod('withEnv', [List.class, Closure.class], null)
helper.registerAllowedMethod('sh', [String.class], {})
}


@Test
void default_run() {
runMPLModule('Build')

printCallStack()

assertThat(helper.callStack)
.filteredOn { c -> c.methodName == 'tool' }
.filteredOn { c -> c.argsToString().contains('Maven 3') }
.as('Maven 3 tool used')
.isNotEmpty()

assertThat(helper.callStack)
.filteredOn { c -> c.methodName == 'sh' }
.filteredOn { c -> c.argsToString().startsWith('mvn') }
.filteredOn { c -> c.argsToString().contains('clean install') }
.as('Shell execution should contain mvn command and default clean install')
.isNotEmpty()

assertThat(helper.callStack)
.filteredOn { c -> c.methodName == 'sh' }
.filteredOn { c -> c.argsToString().startsWith('mvn') }
.filteredOn { c -> ! c.argsToString().contains('-s ') }
.as('Default mvn run without settings provided')
.isNotEmpty()

assertJobStatusSuccess()
}

@Test
void change_tool() {
runMPLModule('Build', [
maven: [
tool_version: 'Maven 2',
],
])

printCallStack()

assertThat(helper.callStack)
.filteredOn { c -> c.methodName == 'tool' }
.filteredOn { c -> c.argsToString().contains('Maven 2') }
.as('Changing maven tool name')
.isNotEmpty()

assertJobStatusSuccess()
}

@Test
void change_settings() {
runMPLModule('Build', [
maven: [
settings_path: '/test-settings.xml',
],
])

printCallStack()

assertThat(helper.callStack)
.filteredOn { c -> c.methodName == 'sh' }
.filteredOn { c -> c.argsToString().contains("-s '/test-settings.xml'") }
.as('Providing settings file should set the maven operation')
.isNotEmpty()

assertJobStatusSuccess()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Copyright (c) 2020 Grid Dynamics International, Inc. All Rights Reserved
// https://www.griddynamics.com
//
// Classification level: Public
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id: $
// @Project: MPL
// @Description: Shared Jenkins Modular Pipeline Library
//

package com.griddynamics.devops.mpl.testing

import com.cloudbees.groovy.cps.sandbox.DefaultInvoker
import com.griddynamics.devops.mpl.testing.MPLInterceptor

/**
* Default invoker override for intercepting the methodCall execution
*
* @author Sergei Parshev <[email protected]>
*/
public class MPLDefaultInvoker extends DefaultInvoker {
@Override
public Object methodCall(Object receiver, String method, Object[] args) throws Throwable {
return MPLInterceptor.instance.processMethodCall(super.&doMethodCall, 'Default', receiver, method, args)
}

public Object doMethodCall(Object receiver, String method, Object[] args) throws Throwable {
return super.methodCall(receiver, method, args)
}
}
Loading