Skip to content

Commit a557d57

Browse files
author
Sergei Parshev
committed
MPL-32 Added a way to execute experimental CPS tests
1 parent 807fc47 commit a557d57

File tree

7 files changed

+328
-51
lines changed

7 files changed

+328
-51
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//
2+
// Copyright (c) 2019 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.MPLTestBaseCPS
34+
35+
class BuildCPSTest extends MPLTestBaseCPS {
36+
def script = null
37+
38+
@Override
39+
@Before
40+
void setUp() throws Exception {
41+
String sharedLibs = this.class.getResource('.').getFile()
42+
43+
helper.registerSharedLibrary(library()
44+
.name('mpl')
45+
.allowOverride(false)
46+
.retriever(localSource(sharedLibs))
47+
.targetPath(sharedLibs)
48+
.defaultVersion('snapshot')
49+
.implicit(true)
50+
.build()
51+
)
52+
53+
setScriptRoots([ 'vars' ] as String[])
54+
setScriptExtension('groovy')
55+
56+
super.setUp()
57+
58+
binding.setVariable('env', [:])
59+
60+
helper.registerAllowedMethod('fileExists', [String.class], null)
61+
helper.registerAllowedMethod('tool', [String.class], { name -> "${name}_HOME" })
62+
helper.registerAllowedMethod('withEnv', [List.class, Closure.class], null)
63+
64+
script = loadScript('MPLModule.groovy')
65+
}
66+
67+
@Test
68+
void default_run() throws Exception {
69+
script.call('Build')
70+
71+
printCallStack()
72+
73+
assertThat(helper.callStack)
74+
.filteredOn { c -> c.methodName == 'tool' }
75+
.filteredOn { c -> c.argsToString().contains('Maven 3') }
76+
.as('Maven 3 tool used')
77+
.isNotEmpty()
78+
79+
assertThat(helper.callStack)
80+
.filteredOn { c -> c.methodName == 'sh' }
81+
.filteredOn { c -> c.argsToString().startsWith('mvn') }
82+
.filteredOn { c -> c.argsToString().contains('clean install') }
83+
.as('Shell execution should contain mvn command and default clean install')
84+
.isNotEmpty()
85+
86+
assertThat(helper.callStack)
87+
.filteredOn { c -> c.methodName == 'sh' }
88+
.filteredOn { c -> c.argsToString().startsWith('mvn') }
89+
.filteredOn { c -> ! c.argsToString().contains('-s ') }
90+
.as('Default mvn run without settings provided')
91+
.isNotEmpty()
92+
93+
assertJobStatusSuccess()
94+
}
95+
96+
@Test
97+
void change_tool() throws Exception {
98+
script.call('Build', [
99+
maven: [
100+
tool_version: 'Maven 2',
101+
],
102+
])
103+
104+
printCallStack()
105+
106+
assertThat(helper.callStack)
107+
.filteredOn { c -> c.methodName == 'tool' }
108+
.filteredOn { c -> c.argsToString().contains('Maven 2') }
109+
.as('Changing maven tool name')
110+
.isNotEmpty()
111+
112+
assertJobStatusSuccess()
113+
}
114+
115+
@Test
116+
void change_settings() throws Exception {
117+
script.call('Build', [
118+
maven: [
119+
settings_path: '/test-settings.xml',
120+
],
121+
])
122+
123+
printCallStack()
124+
125+
assertThat(helper.callStack)
126+
.filteredOn { c -> c.methodName == 'sh' }
127+
.filteredOn { c -> c.argsToString().contains("-s '/test-settings.xml'") }
128+
.as('Providing setings file should set the maven opetion')
129+
.isNotEmpty()
130+
131+
assertJobStatusSuccess()
132+
}
133+
}

test/groovy/com/griddynamics/devops/mpl/modules/Build/BuildTest.groovy

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,21 @@ class BuildTest extends MPLTestBase {
7373
assertThat(helper.callStack)
7474
.filteredOn { c -> c.methodName == 'tool' }
7575
.filteredOn { c -> c.argsToString().contains('Maven 3') }
76+
.as('Maven 3 tool used')
7677
.isNotEmpty()
7778

7879
assertThat(helper.callStack)
79-
.as('Shell execution should contain mvn command and default clean install')
8080
.filteredOn { c -> c.methodName == 'sh' }
8181
.filteredOn { c -> c.argsToString().startsWith('mvn') }
8282
.filteredOn { c -> c.argsToString().contains('clean install') }
83+
.as('Shell execution should contain mvn command and default clean install')
8384
.isNotEmpty()
8485

8586
assertThat(helper.callStack)
86-
.as('Default mvn run without settings provided')
8787
.filteredOn { c -> c.methodName == 'sh' }
8888
.filteredOn { c -> c.argsToString().startsWith('mvn') }
8989
.filteredOn { c -> ! c.argsToString().contains('-s ') }
90+
.as('Default mvn run without settings provided')
9091
.isNotEmpty()
9192

9293
assertJobStatusSuccess()
@@ -103,9 +104,9 @@ class BuildTest extends MPLTestBase {
103104
printCallStack()
104105

105106
assertThat(helper.callStack)
106-
.as('Changing maven tool name')
107107
.filteredOn { c -> c.methodName == 'tool' }
108108
.filteredOn { c -> c.argsToString().contains('Maven 2') }
109+
.as('Changing maven tool name')
109110
.isNotEmpty()
110111

111112
assertJobStatusSuccess()
@@ -122,9 +123,9 @@ class BuildTest extends MPLTestBase {
122123
printCallStack()
123124

124125
assertThat(helper.callStack)
125-
.as('Providing setings file should set the maven opetion')
126126
.filteredOn { c -> c.methodName == 'sh' }
127127
.filteredOn { c -> c.argsToString().contains("-s '/test-settings.xml'") }
128+
.as('Providing setings file should set the maven opetion')
128129
.isNotEmpty()
129130

130131
assertJobStatusSuccess()

test/groovy/com/griddynamics/devops/mpl/testing/MPLTestBase.groovy

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,10 @@
2323

2424
package com.griddynamics.devops.mpl.testing
2525

26+
import com.griddynamics.devops.mpl.testing.MPLTestBaseSetup
2627
import com.lesfurets.jenkins.unit.BasePipelineTest
2728

28-
import com.griddynamics.devops.mpl.MPLConfig
29-
import com.griddynamics.devops.mpl.MPLManager
30-
import com.griddynamics.devops.mpl.Helper
31-
32-
import java.security.AccessController
33-
import java.security.PrivilegedAction
34-
import org.codehaus.groovy.runtime.InvokerHelper
29+
import com.griddynamics.devops.mpl.testing.MPLTestHelper
3530

3631
abstract class MPLTestBase extends BasePipelineTest {
3732
MPLTestBase() {
@@ -40,40 +35,6 @@ abstract class MPLTestBase extends BasePipelineTest {
4035

4136
void setUp() throws Exception {
4237
super.setUp()
43-
44-
// Overriding Helper to find the right resources in the loaded libs
45-
Helper.metaClass.static.getModulesList = { String path ->
46-
def modules = []
47-
def resourcesFolder = helper.getLibraryClassLoader().getResource('.').getFile()
48-
MPLManager.instance.getModulesLoadPaths().each { modulesPath ->
49-
def libPath = modulesPath + '/' + path
50-
helper.getLibraryClassLoader().getResources(libPath).each { res ->
51-
def libname = res.getFile().substring(resourcesFolder.length())
52-
libname = libname.substring(0, Math.max(libname.indexOf('@'), 0))
53-
modules += [[libname + '/' + libPath, res.text]]
54-
}
55-
}
56-
return modules
57-
}
58-
59-
// Replacing runModule function to mock it
60-
Helper.metaClass.static.runModule = { String source, String path, Map vars = [:] ->
61-
def binding = new Binding()
62-
this.binding.variables.each { k, v -> binding.setVariable(k, v) }
63-
vars.each { k, v -> binding.setVariable(k, v) }
64-
def loader = AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>() {
65-
public GroovyClassLoader run() {
66-
return new GroovyClassLoader(helper.getLibraryClassLoader(), helper.getLibraryConfig())
67-
}
68-
})
69-
def script = InvokerHelper.createScript(loader.parseClass(new GroovyCodeSource(source, path, '/mpl/modules'), false), binding)
70-
script.metaClass.invokeMethod = helper.getMethodInterceptor()
71-
script.metaClass.static.invokeMethod = helper.getMethodInterceptor()
72-
script.metaClass.methodMissing = helper.getMethodMissingInterceptor()
73-
script.run()
74-
}
75-
76-
// Show the dump of the configuration during unit tests execution
77-
Helper.metaClass.static.configEntrySet = { Map config -> config.entrySet() }
38+
MPLTestBaseSetup.setUp(helper, this.binding)
7839
}
7940
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Copyright (c) 2018 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.griddynamics.devops.mpl.testing.MPLTestBaseSetup
27+
import com.lesfurets.jenkins.unit.cps.BasePipelineTestCPS
28+
29+
import com.griddynamics.devops.mpl.testing.MPLTestHelperCPS
30+
31+
abstract class MPLTestBaseCPS extends BasePipelineTestCPS {
32+
MPLTestBaseCPS() {
33+
helper = new MPLTestHelperCPS()
34+
}
35+
36+
void setUp() throws Exception {
37+
super.setUp()
38+
MPLTestBaseSetup.setUp(helper, this.binding)
39+
}
40+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Copyright (c) 2019 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.griddynamics.devops.mpl.MPLManager
27+
import com.griddynamics.devops.mpl.Helper
28+
29+
import org.codehaus.groovy.runtime.InvokerHelper
30+
31+
abstract class MPLTestBaseSetup {
32+
static void setUp(helper, context) throws Exception {
33+
// Overriding Helper to find the right resources in the loaded libs
34+
Helper.metaClass.static.getModulesList = { String path ->
35+
def modules = []
36+
def resourcesFolder = helper.getLibraryClassLoader().getResource('.').getFile()
37+
MPLManager.instance.getModulesLoadPaths().each { modulesPath ->
38+
def libPath = modulesPath + '/' + path
39+
helper.getLibraryClassLoader().getResources(libPath).each { res ->
40+
def libname = res.getFile().substring(resourcesFolder.length())
41+
libname = libname.substring(0, Math.max(libname.indexOf('@'), 0))
42+
modules += [[libname + '/' + libPath, res.text]]
43+
}
44+
}
45+
return modules
46+
}
47+
48+
// Replacing runModule function to mock it
49+
Helper.metaClass.static.runModule = { String source, String path, Map vars = [:] ->
50+
def binding = new Binding()
51+
context.variables.each { k, v -> binding.setVariable(k, v) }
52+
vars.each { k, v -> binding.setVariable(k, v) }
53+
def script = InvokerHelper.createScript(helper.getLibraryClassLoader().parseClass(new GroovyCodeSource(source, path, '/mpl/modules'), false), binding)
54+
script.metaClass.invokeMethod = helper.getMethodInterceptor()
55+
script.metaClass.static.invokeMethod = helper.getMethodInterceptor()
56+
script.metaClass.methodMissing = helper.getMethodMissingInterceptor()
57+
script.run()
58+
}
59+
60+
// Show the dump of the configuration during unit tests execution
61+
Helper.metaClass.static.configEntrySet = { Map config -> config.entrySet() }
62+
}
63+
}

0 commit comments

Comments
 (0)