Skip to content

Commit dbbdf58

Browse files
author
Sergei Parshev
committed
MPL-32 Initial working implementation of the JenkinsRule base class (#32)
1 parent f16492d commit dbbdf58

File tree

11 files changed

+1005
-17
lines changed

11 files changed

+1005
-17
lines changed

pom.xml

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
<maven.compiler.source>1.8</maven.compiler.source>
2929
<maven.compiler.target>1.8</maven.compiler.target>
3030
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31+
<jenkins.version>2.204.2</jenkins.version>
32+
<jenkins-core.version>${jenkins.version}</jenkins-core.version>
33+
<jenkins-war.version>${jenkins.version}</jenkins-war.version>
34+
<jenkins-test-harness.version>2.62</jenkins-test-harness.version>
35+
<workflow-cps.version>2.80</workflow-cps.version>
36+
<argLine>-Xms768M -Xmx768M -Djava.awt.headless=true -XX:+HeapDumpOnOutOfMemoryError -XX:+TieredCompilation -XX:TieredStopAtLevel=1</argLine>
37+
<slf4jVersion>1.7.25</slf4jVersion>
3138
</properties>
3239

3340
<repositories>
@@ -46,17 +53,30 @@
4653
<dependency>
4754
<groupId>org.jenkins-ci.main</groupId>
4855
<artifactId>jenkins-core</artifactId>
49-
<version>2.73.3</version>
56+
<version>${jenkins-core.version}</version>
5057
</dependency>
5158
<dependency>
5259
<groupId>org.jenkins-ci.plugins.workflow</groupId>
53-
<artifactId>workflow-cps-global-lib</artifactId>
54-
<version>2.8</version>
60+
<artifactId>workflow-cps</artifactId>
61+
<version>${workflow-cps.version}</version>
5562
</dependency>
5663
<dependency>
5764
<groupId>org.jenkins-ci.plugins.workflow</groupId>
58-
<artifactId>workflow-cps</artifactId>
59-
<version>2.44</version>
65+
<artifactId>workflow-cps-global-lib</artifactId>
66+
<version>2.15</version>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>junit</groupId>
71+
<artifactId>junit</artifactId>
72+
<version>4.12</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.assertj</groupId>
77+
<artifactId>assertj-core</artifactId>
78+
<version>3.14.0</version>
79+
<scope>test</scope>
6080
</dependency>
6181

6282
<dependency>
@@ -65,12 +85,63 @@
6585
<version>1.1</version>
6686
<scope>test</scope>
6787
</dependency>
88+
89+
<!-- Tests JenkinsRule base class -->
6890
<dependency>
69-
<groupId>junit</groupId>
70-
<artifactId>junit</artifactId>
71-
<version>4.12</version>
91+
<groupId>org.jenkins-ci.main</groupId>
92+
<artifactId>jenkins-test-harness</artifactId>
93+
<version>${jenkins-test-harness.version}</version>
94+
<scope>test</scope>
95+
</dependency>
96+
<dependency>
97+
<groupId>org.jenkins-ci.main</groupId>
98+
<artifactId>jenkins-war</artifactId>
99+
<version>${jenkins-war.version}</version>
100+
<type>war</type>
101+
<scope>test</scope>
102+
</dependency>
103+
<dependency>
104+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
105+
<artifactId>workflow-job</artifactId>
106+
<version>2.36</version>
72107
<scope>test</scope>
73108
</dependency>
109+
110+
<!-- Unit tests additional requirements -->
111+
<dependency>
112+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
113+
<artifactId>workflow-basic-steps</artifactId>
114+
<version>2.19</version>
115+
<scope>test</scope>
116+
</dependency>
117+
118+
<dependency>
119+
<groupId>org.slf4j</groupId>
120+
<artifactId>slf4j-api</artifactId>
121+
<version>${slf4jVersion}</version>
122+
<scope>test</scope>
123+
<!-- mark the API as optional so it is not packaged in the HPI but available during compile -->
124+
<optional>true</optional>
125+
</dependency>
126+
<!-- make slf4j bindings to java.util.logging available during tests -->
127+
<dependency>
128+
<groupId>org.slf4j</groupId>
129+
<artifactId>log4j-over-slf4j</artifactId>
130+
<version>${slf4jVersion}</version>
131+
<scope>test</scope>
132+
</dependency>
133+
<dependency>
134+
<groupId>org.slf4j</groupId>
135+
<artifactId>jcl-over-slf4j</artifactId>
136+
<version>${slf4jVersion}</version>
137+
<scope>test</scope>
138+
</dependency>
139+
<dependency>
140+
<groupId>org.slf4j</groupId>
141+
<artifactId>slf4j-jdk14</artifactId>
142+
<version>${slf4jVersion}</version>
143+
<scope>test</scope>
144+
</dependency>
74145
</dependencies>
75146

76147
<build>
@@ -103,19 +174,21 @@
103174
</executions>
104175
</plugin>
105176

106-
<!-- Tests -->
107177
<plugin>
108178
<groupId>org.codehaus.gmavenplus</groupId>
109179
<artifactId>gmavenplus-plugin</artifactId>
110-
<version>1.7.1</version>
180+
<version>1.8.1</version>
111181
<executions>
112182
<execution>
113183
<goals>
114-
<goal>addSources</goal>
115-
<goal>addTestSources</goal>
116-
<goal>generateStubs</goal>
184+
<!--goal>addSources</goal-->
185+
<!--goal>addTestSources</goal-->
186+
<!--goal>generateStubs</goal-->
187+
<goal>generateTestStubs</goal>
117188
<goal>compile</goal>
118189
<goal>compileTests</goal>
190+
<!--goal>removeStubs</goal-->
191+
<goal>removeTestStubs</goal>
119192
<goal>groovydoc</goal>
120193
<goal>groovydocTests</goal>
121194
</goals>
@@ -124,9 +197,9 @@
124197
<configuration>
125198
<sources>
126199
<source>
127-
<directory>${project.basedir}</directory>
200+
<directory>${project.basedir}/src</directory>
128201
<includes>
129-
<include>src/**/*.groovy</include>
202+
<include>**/*.groovy</include>
130203
</includes>
131204
</source>
132205
<source>
@@ -144,12 +217,13 @@
144217
</source-->
145218
</sources>
146219
<testSources>
147-
<source>
220+
<testSource>
148221
<directory>${project.basedir}/test</directory>
149222
<includes>
150223
<include>**/*.groovy</include>
224+
<include>**/*.java</include>
151225
</includes>
152-
</source>
226+
</testSource>
153227
</testSources>
154228
</configuration>
155229
</plugin>
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//
2+
// Copyright (c) 2020 Grid Dynamics International, Inc. All Rights Reserved
3+
// https://www.griddynamics.com
4+
//
5+
// Classification level: Public
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
// $Id: $
20+
// @Project: MPL
21+
// @Description: Shared Jenkins Modular Pipeline Library
22+
//
23+
24+
import org.junit.Before
25+
import org.junit.Test
26+
27+
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
28+
import static com.lesfurets.jenkins.unit.global.lib.LocalSource.localSource
29+
30+
import static org.assertj.core.api.Assertions.assertThat
31+
32+
import com.griddynamics.devops.mpl.Helper
33+
import com.griddynamics.devops.mpl.testing.MPLTestBaseJenkinsRule
34+
35+
/**
36+
* Same tests using JenkinsRule
37+
*
38+
* @author Sergei Parshev <[email protected]>
39+
*/
40+
public class BuildJenkinsRuleTest extends MPLTestBaseJenkinsRule {
41+
@Override
42+
@Before
43+
void setUp() throws Exception {
44+
//setScriptRoots([ 'vars' ] as String[])
45+
//setScriptExtension('groovy')
46+
47+
super.setUp()
48+
49+
String sharedLibs = this.class.getResource('.').getFile()
50+
helper.registerSharedLibrary(library()
51+
.name('mpl')
52+
.allowOverride(false)
53+
.retriever(localSource(sharedLibs))
54+
.targetPath(sharedLibs)
55+
.defaultVersion('snapshot')
56+
.implicit(true)
57+
.build()
58+
)
59+
60+
binding.setVariable('env', [:])
61+
62+
// Shared lib requirements
63+
helper.registerAllowedMethod('MPLModule', [], null)
64+
helper.registerAllowedMethod('MPLModule', [String.class], null)
65+
helper.registerAllowedMethod('MPLModule', [String.class, Object.class], null)
66+
helper.registerAllowedMethod('call', [String.class, Object.class], null)
67+
68+
// Test requirements
69+
helper.registerAllowedMethod('fileExists', [String.class], { return false })
70+
helper.registerAllowedMethod('tool', [String.class], { name -> "${name}_HOME" })
71+
helper.registerAllowedMethod('withEnv', [List.class, Closure.class], null)
72+
helper.registerAllowedMethod('sh', [String.class], {})
73+
}
74+
75+
76+
@Test
77+
void default_run() {
78+
runMPLModule('Build')
79+
80+
printCallStack()
81+
82+
assertThat(helper.callStack)
83+
.filteredOn { c -> c.methodName == 'tool' }
84+
.filteredOn { c -> c.argsToString().contains('Maven 3') }
85+
.as('Maven 3 tool used')
86+
.isNotEmpty()
87+
88+
assertThat(helper.callStack)
89+
.filteredOn { c -> c.methodName == 'sh' }
90+
.filteredOn { c -> c.argsToString().startsWith('mvn') }
91+
.filteredOn { c -> c.argsToString().contains('clean install') }
92+
.as('Shell execution should contain mvn command and default clean install')
93+
.isNotEmpty()
94+
95+
assertThat(helper.callStack)
96+
.filteredOn { c -> c.methodName == 'sh' }
97+
.filteredOn { c -> c.argsToString().startsWith('mvn') }
98+
.filteredOn { c -> ! c.argsToString().contains('-s ') }
99+
.as('Default mvn run without settings provided')
100+
.isNotEmpty()
101+
102+
assertJobStatusSuccess()
103+
}
104+
105+
@Test
106+
void change_tool() {
107+
runMPLModule('Build', [
108+
maven: [
109+
tool_version: 'Maven 2',
110+
],
111+
])
112+
113+
printCallStack()
114+
115+
assertThat(helper.callStack)
116+
.filteredOn { c -> c.methodName == 'tool' }
117+
.filteredOn { c -> c.argsToString().contains('Maven 2') }
118+
.as('Changing maven tool name')
119+
.isNotEmpty()
120+
121+
assertJobStatusSuccess()
122+
}
123+
124+
@Test
125+
void change_settings() {
126+
runMPLModule('Build', [
127+
maven: [
128+
settings_path: '/test-settings.xml',
129+
],
130+
])
131+
132+
printCallStack()
133+
134+
assertThat(helper.callStack)
135+
.filteredOn { c -> c.methodName == 'sh' }
136+
.filteredOn { c -> c.argsToString().contains("-s '/test-settings.xml'") }
137+
.as('Providing settings file should set the maven operation')
138+
.isNotEmpty()
139+
140+
assertJobStatusSuccess()
141+
}
142+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright (c) 2020 Grid Dynamics International, Inc. All Rights Reserved
3+
// https://www.griddynamics.com
4+
//
5+
// Classification level: Public
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
// $Id: $
20+
// @Project: MPL
21+
// @Description: Shared Jenkins Modular Pipeline Library
22+
//
23+
24+
package com.griddynamics.devops.mpl.testing
25+
26+
import com.cloudbees.groovy.cps.sandbox.DefaultInvoker
27+
import com.griddynamics.devops.mpl.testing.MPLInterceptor
28+
29+
/**
30+
* Default invoker override for intercepting the methodCall execution
31+
*
32+
* @author Sergei Parshev <[email protected]>
33+
*/
34+
public class MPLDefaultInvoker extends DefaultInvoker {
35+
@Override
36+
public Object methodCall(Object receiver, String method, Object[] args) throws Throwable {
37+
return MPLInterceptor.instance.processMethodCall(super.&doMethodCall, 'Default', receiver, method, args)
38+
}
39+
40+
public Object doMethodCall(Object receiver, String method, Object[] args) throws Throwable {
41+
return super.methodCall(receiver, method, args)
42+
}
43+
}

0 commit comments

Comments
 (0)