forked from BobBuildTool/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_jenkins_set_options.py
247 lines (211 loc) · 8.55 KB
/
test_jenkins_set_options.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Bob build tool
# Copyright (C) 2016 BobBuildToolTeam
#
# SPDX-License-Identifier: GPL-3.0-or-later
import os, re
import tempfile
from xml.etree import ElementTree
from unittest import TestCase
from bob.utils import removePath
from bob.errors import BuildError
from mocks.jenkins_mock import JenkinsMock
from bob.cmds.jenkins import doJenkins
from bob.state import finalize
class TestJenkinsSetOptions(TestCase):
def executeBobJenkinsCmd(self, arg):
doJenkins(arg.split(' '), self.cwd)
def tearDown(self):
self.jenkinsMock.stop_mock_server()
finalize()
os.chdir(self.oldCwd)
removePath(self.cwd)
def setUp(self):
RECIPE = """
root: True
checkoutSCM:
-
scm: git
url: [email protected]
branch: test
buildScript: |
echo 'build'
packageScript: |
echo 'package'
"""
self.oldCwd = os.getcwd()
self.cwd = tempfile.mkdtemp()
self.jenkinsMock = JenkinsMock()
self.jenkinsMock.start_mock_server()
self.jenkinsMock.getServerData()
os.chdir(self.cwd)
os.mkdir("recipes")
with open(os.path.join("recipes", "test.yaml"), "w") as f:
print(RECIPE, file=f)
# do bob jenkins add
self.executeBobJenkinsCmd("add myTestJenkins http://localhost:{} -r test"
.format(self.jenkinsMock.getServerPort()))
self.executeBobJenkinsCmd("push -q myTestJenkins")
self.jenkinsMock.getServerData()
def createComplexRecipes(self):
ROOT = """
root: True
depends:
- dependency-one
- dependency-two
checkoutSCM:
-
scm: git
url: [email protected]/root.git
branch: test
buildScript: |
echo 'build'
packageScript: |
echo 'package'
"""
DEPENDENCYONE = """
depends:
- dependency-two
checkoutSCM:
-
scm: git
url: [email protected]/dependency-one.git
branch: test
buildScript: |
echo 'build'
packageScript: |
echo 'package'
"""
DEPENDENCYTWO = """
checkoutSCM:
-
scm: git
url: [email protected]/dependency-two.git
branch: test
buildScript: |
echo 'build'
packageScript: |
echo 'package'
"""
with open(os.path.join("recipes", "root.yaml"), "w") as f:
print(ROOT, file=f)
with open(os.path.join("recipes", "dependency-one.yaml"), "w") as f:
print(DEPENDENCYONE, file=f)
with open(os.path.join("recipes", "dependency-two.yaml"), "w") as f:
print(DEPENDENCYTWO, file=f)
self.executeBobJenkinsCmd("add myTestJenkinsComplex http://localhost:{} -r root"
.format(self.jenkinsMock.getServerPort()))
def testSetNode(self):
self.executeBobJenkinsCmd("set-options -n testSlave myTestJenkins")
self.executeBobJenkinsCmd("push -q myTestJenkins")
send = self.jenkinsMock.getServerData()
assert('<assignedNode>testSlave</assignedNode>' in send[0][1].decode('utf-8'))
def testUpDownload(self):
DEFAULT="""
archive:
backend: http
url: "http://localhost:8001/upload"
"""
with open("default.yaml", "w") as f:
print(DEFAULT, file=f)
self.executeBobJenkinsCmd("set-options --download myTestJenkins")
self.executeBobJenkinsCmd("push -q myTestJenkins")
send = self.jenkinsMock.getServerData()
assert('BOB_DOWNLOAD_URL=http://localhost:8001/upload' in send[0][1].decode('utf-8'))
self.executeBobJenkinsCmd("set-options --reset --add-root test myTestJenkins")
self.executeBobJenkinsCmd("push -q myTestJenkins")
send = self.jenkinsMock.getServerData()
assert('BOB_DOWNLOAD_URL=http://localhost:8001/upload' not in send[0][1].decode('utf-8'))
self.executeBobJenkinsCmd("set-options --upload myTestJenkins")
self.executeBobJenkinsCmd("push -q myTestJenkins")
send = self.jenkinsMock.getServerData()
assert('BOB_UPLOAD_URL=http://localhost:8001/upload' in send[0][1].decode('utf-8'))
def testSetURL(self):
self.newJenkinsMock = JenkinsMock()
self.newJenkinsMock.start_mock_server()
self.executeBobJenkinsCmd("set-url myTestJenkins http://localhost:{}"
.format(self.newJenkinsMock.getServerPort()))
self.executeBobJenkinsCmd("push -q myTestJenkins")
send = self.jenkinsMock.getServerData()
sendNew = self.newJenkinsMock.getServerData()
assert(len(send) == 0)
assert(len(sendNew) != 0)
self.newJenkinsMock.stop_mock_server()
def testSetGitShallowClone(self):
self.executeBobJenkinsCmd("set-options -o scm.git.shallow=42 myTestJenkins")
self.executeBobJenkinsCmd("push -q myTestJenkins")
send = self.jenkinsMock.getServerData()
config = ElementTree.fromstring(send[0][1])
for clone in config.iter('hudson.plugins.git.extensions.impl.CloneOption'):
found = 0
for a in clone.iter():
if a.tag == 'shallow':
assert(a.text == 'true')
found += 1
if a.tag == 'depth':
assert(a.text == '42')
found += 1
assert(found == 2)
self.executeBobJenkinsCmd("set-options -o scm.git.shallow=-1 myTestJenkins")
with self.assertRaises(Exception) as c:
self.executeBobJenkinsCmd("push -q myTestJenkins")
assert(type(c.exception) == BuildError)
def testSetGitTimeoutClone(self):
self.executeBobJenkinsCmd("set-options -o scm.git.timeout=42 myTestJenkins")
self.executeBobJenkinsCmd("push -q myTestJenkins")
send = self.jenkinsMock.getServerData()
config = ElementTree.fromstring(send[0][1])
for clone in config.iter('hudson.plugins.git.extensions.impl.CloneOption'):
found = 0
for a in clone.iter():
if a.tag == 'timeout':
assert(a.text == '42')
found += 1
assert(found == 1)
self.executeBobJenkinsCmd("set-options -o scm.git.timeout=-10 myTestJenkins")
with self.assertRaises(Exception) as c:
self.executeBobJenkinsCmd("push -q myTestJenkins")
assert(type(c.exception) == BuildError)
def testSetPrefix(self):
self.executeBobJenkinsCmd("set-options -p abcde- myTestJenkins")
self.executeBobJenkinsCmd("push -q myTestJenkins")
send = self.jenkinsMock.getServerData()
assert(send[0][0] == '/createItem?name=abcde-test')
assert(send[1][0] == '/job/test/doDelete')
assert(send[2][0] == '/job/abcde-test/build')
def testDelRoot(self):
self.executeBobJenkinsCmd("set-options --del-root test myTestJenkins")
self.executeBobJenkinsCmd("push -q myTestJenkins")
send = self.jenkinsMock.getServerData()
assert(send[0][0] == '/job/test/doDelete')
def testShortDescription(self):
self.createComplexRecipes()
self.executeBobJenkinsCmd("set-options --shortdescription myTestJenkinsComplex")
self.executeBobJenkinsCmd("push -q myTestJenkinsComplex")
send = self.jenkinsMock.getServerData()
result_set = set()
try:
for i in send:
if i[0] == '/createItem?name=dependency-two':
for items in ElementTree.fromstring(i[1]).iter('description'):
for line in [x for x in items.itertext()][0].splitlines():
if line.startswith('<li>') and line.endswith('</li>'):
result_set.add(line[4:-5])
except:
print("Malformed Data Recieved")
self.assertEqual(result_set, {'root/dependency-one/dependency-two'})
def testLongDescription(self):
self.createComplexRecipes()
self.executeBobJenkinsCmd("set-options --longdescription myTestJenkinsComplex")
self.executeBobJenkinsCmd("push -q myTestJenkinsComplex")
send = self.jenkinsMock.getServerData()
result_set = set()
try:
for i in send:
if i[0] == '/createItem?name=dependency-two':
for items in ElementTree.fromstring(i[1]).iter('description'):
for line in [x for x in items.itertext()][0].splitlines():
if line.startswith('<li>') and line.endswith('</li>'):
result_set.add(line[4:-5])
except:
print("Malformed Data Recieved")
self.assertEqual(result_set, {'root/dependency-two', 'root/dependency-one/dependency-two'})