Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 0cc6f63

Browse files
committed
Refactor JenkinsFile
1 parent 9411642 commit 0cc6f63

File tree

1 file changed

+45
-80
lines changed

1 file changed

+45
-80
lines changed

Jenkinsfile

Lines changed: 45 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,48 @@
1-
// Define the test routine for different python versions
2-
3-
def test_python_basic(pythonVersion)
4-
{
5-
node {
6-
// Unstash the source on this node
7-
unstash name: 'source'
8-
// Set up the environment and test
9-
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'clientlibs-test', usernameVariable: 'DB_USER', passwordVariable: 'DB_PASSWORD']]) {
10-
try {
11-
sh """ virtualenv tmp -p /usr/local/lib/python${pythonVersion}/bin/${pythonVersion.startsWith('3') ? "python3" : "python"}
12-
. ./tmp/bin/activate
13-
echo \$DB_USER
14-
export RUN_CLOUDANT_TESTS=1
15-
export RUN_BASIC_AUTH_TESTS=1
16-
export CLOUDANT_ACCOUNT=\$DB_USER
17-
# Temporarily disable the _db_updates tests pending resolution of case 71610
18-
export SKIP_DB_UPDATES=1
19-
pip install -r requirements.txt
20-
pip install -r test-requirements.txt
21-
pylint ./src/cloudant
22-
nosetests -w ./tests/unit --with-xunit"""
23-
} finally {
24-
// Load the test results
25-
junit 'nosetests.xml'
26-
}
27-
}
28-
}
29-
}
30-
31-
32-
def test_python_cookie(pythonVersion)
33-
{
34-
node {
35-
// Unstash the source on this node
36-
unstash name: 'source'
37-
// Set up the environment and test
38-
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'clientlibs-test', usernameVariable: 'DB_USER', passwordVariable: 'DB_PASSWORD']]) {
39-
try {
40-
sh """ virtualenv tmp -p /usr/local/lib/python${pythonVersion}/bin/${pythonVersion.startsWith('3') ? "python3" : "python"}
41-
. ./tmp/bin/activate
42-
echo \$DB_USER
43-
export RUN_CLOUDANT_TESTS=1
44-
export CLOUDANT_ACCOUNT=\$DB_USER
45-
# Temporarily disable the _db_updates tests pending resolution of case 71610
46-
export SKIP_DB_UPDATES=1
47-
pip install -r requirements.txt
48-
pip install -r test-requirements.txt
49-
pylint ./src/cloudant
50-
nosetests -w ./tests/unit --with-xunit"""
51-
} finally {
52-
// Load the test results
53-
junit 'nosetests.xml'
54-
}
55-
}
1+
def getEnvForSuite(suiteName) {
2+
// Base environment variables
3+
def envVars = [
4+
"CLOUDANT_ACCOUNT=$DB_USER",
5+
"RUN_CLOUDANT_TESTS=1",
6+
"SKIP_DB_UPDATES=1" // Disable pending resolution of case 71610
7+
]
8+
// Add test suite specific environment variables
9+
switch(suiteName) {
10+
case 'basic':
11+
envVars.add("RUN_BASIC_AUTH_TESTS=1")
12+
break
13+
case 'cookie':
14+
break
15+
case 'iam':
16+
// Setting IAM_API_KEY forces tests to run using an IAM enabled client.
17+
envVars.add("IAM_API_KEY=$DB_IAM_API_KEY")
18+
break
19+
default:
20+
error("Unknown test suite environment ${suiteName}")
5621
}
22+
return envVars
5723
}
5824

59-
def test_python_iam(pythonVersion)
60-
{
25+
def setupPythonAndTest(pythonVersion, testSuite) {
6126
node {
6227
// Unstash the source on this node
6328
unstash name: 'source'
6429
// Set up the environment and test
65-
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'iam-testy023', usernameVariable: 'DB_USER', passwordVariable: 'IAM_API_KEY']]) {
66-
try {
67-
sh """ virtualenv tmp -p /usr/local/lib/python${pythonVersion}/bin/${pythonVersion.startsWith('3') ? "python3" : "python"}
68-
. ./tmp/bin/activate
69-
echo \$DB_USER
70-
export RUN_CLOUDANT_TESTS=1
71-
export CLOUDANT_ACCOUNT=\$DB_USER
72-
# Temporarily disable the _db_updates tests pending resolution of case 71610
73-
export SKIP_DB_UPDATES=1
74-
pip install -r requirements.txt
75-
pip install -r test-requirements.txt
76-
pylint ./src/cloudant
77-
nosetests -w ./tests/unit --with-xunit"""
78-
} finally {
79-
// Load the test results
80-
junit 'nosetests.xml'
30+
withCredentials([usernamePassword(credentialsId: 'clientlibs-test', usernameVariable: 'DB_USER', passwordVariable: 'DB_PASSWORD'),
31+
string(credentialsId: 'clientlibs-test-iam', variable: 'DB_IAM_API_KEY')]) {
32+
withEnv(getEnvForSuite("${testSuite}")) {
33+
try {
34+
sh """
35+
virtualenv tmp -p /usr/local/lib/python${pythonVersion}/bin/${pythonVersion.startsWith('3') ? "python3" : "python"}
36+
. ./tmp/bin/activate
37+
pip install -r requirements.txt
38+
pip install -r test-requirements.txt
39+
pylint ./src/cloudant
40+
nosetests -w ./tests/unit --with-xunit
41+
"""
42+
} finally {
43+
// Load the test results
44+
junit 'nosetests.xml'
45+
}
8146
}
8247
}
8348
}
@@ -91,14 +56,14 @@ stage('Checkout'){
9156
stash name: 'source'
9257
}
9358
}
59+
9460
stage('Test'){
95-
// Run tests in parallel for multiple python versions
9661
parallel(
97-
'Python2-BASIC': {test_python_basic('2.7.12')},
98-
'Python3-BASIC': {test_python_basic('3.5.2')},
99-
'Python2-COOKIE': {test_python_cookie('2.7.12')},
100-
'Python3-COOKIE': {test_python_cookie('3.5.2')},
101-
'Python2-IAM': {test_python_iam('2.7.12')},
102-
'Python3-IAM': {test_python_iam('3.5.2')}
62+
'Python2-BASIC': { setupPythonAndTest('2.7.12', 'basic') },
63+
'Python3-BASIC': { setupPythonAndTest('3.5.2', 'basic') },
64+
'Python2-COOKIE': { setupPythonAndTest('2.7.12', 'cookie') },
65+
'Python3-COOKIE': { setupPythonAndTest('3.5.2', 'cookie') },
66+
'Python2-IAM': { setupPythonAndTest('2.7.12', 'iam') },
67+
'Python3-IAM': { setupPythonAndTest('3.5.2', 'iam') }
10368
)
10469
}

0 commit comments

Comments
 (0)