forked from BobBuildTool/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_jenkins.py
191 lines (158 loc) · 6.14 KB
/
test_jenkins.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
# 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 bob.state import BobState
from unittest.mock import patch
from mocks.jenkins_mock import JenkinsMock
from bob.cmds.jenkins import doJenkins
from bob.state import finalize
class TestJenkinsPush(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):
self.oldCwd = os.getcwd()
self.jenkinsMock = JenkinsMock()
self.jenkinsMock.start_mock_server()
self.jenkinsMock.getServerData()
self.cwd = tempfile.mkdtemp()
os.chdir(self.cwd)
def testSimplePush(self):
RECIPE = """
root: True
checkoutSCM:
-
scm: git
url: [email protected]
branch: test
checkoutScript: |
TestCheckoutScript
buildScript: |
TestBuildScript
packageScript: |
TestPackageScript
"""
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()))
# throw away server data (but there shouldn't by any...
assert(len(self.jenkinsMock.getServerData()) == 0)
self.executeBobJenkinsCmd("push myTestJenkins -q")
send = self.jenkinsMock.getServerData()
assert(len(send) == 2)
assert( 'createItem?name=test' in send[0][0])
jobconfig = ElementTree.fromstring(send[0][1])
self.assertEqual( jobconfig.tag, 'project' )
# test GitSCM
for scm in jobconfig.iter('scm'):
if ('git' in scm.attrib.get('class')):
assert ( '[email protected]' in [ url.text for url in scm.iter('url') ])
for branch in scm.iter('branches'):
assert ( 'refs/heads/test' in [ name.text for name in branch.iter('name') ])
found = False
for cmd in jobconfig.iter('command'):
if (('TestCheckoutScript' in cmd.text) and
('TestBuildScript' in cmd.text) and
('TestPackageScript' in cmd.text)):
found = True
self.assertEqual(found, True)
self.assertEqual('/job/test/build', send[1][0])
self.executeBobJenkinsCmd("prune -q myTestJenkins")
send = self.jenkinsMock.getServerData()
assert(len(send) == 1)
assert( '/job/test/doDelete' == send[0][0])
self.executeBobJenkinsCmd("rm myTestJenkins")
assert(len(self.jenkinsMock.getServerData()) == 0)
def testStableJobConfig(self):
# This test generates the following jobs with it's dependencies:
# --> app1 -> lib-a
# root -|--> app2 -> lib-b
# --> app3 -> lib-c
# Afterwards the app* jobs are modified. In this case the lib-* jobs shouldn't
# be modified or triggered.
RECIPE_LIB="""
buildScript:
echo 'hello bob'
multiPackage:
a:
packageScript: '1'
b:
packageScript: '2'
c:
packageScript: '3'
"""
RECIPE_APP="""
depends:
- {DEPENDS}
buildScript: |
{SCRIPT}
"""
ROOT_RECIPE="""
root: True
depends:
- app1
- app2
- app3
buildScript: |
true
"""
os.mkdir("recipes")
with open(os.path.join("recipes", "root.yaml"), "w") as f:
print(ROOT_RECIPE, file=f)
with open(os.path.join("recipes", "lib.yaml"), "w") as f:
print(RECIPE_LIB, file=f)
with open(os.path.join("recipes", "app1.yaml"), "w") as f:
print(RECIPE_APP.format(SCRIPT='test', DEPENDS='lib-a'), file=f)
with open(os.path.join("recipes", "app2.yaml"), "w") as f:
print(RECIPE_APP.format(SCRIPT='test1', DEPENDS='lib-b'), file=f)
with open(os.path.join("recipes", "app3.yaml"), "w") as f:
print(RECIPE_APP.format(SCRIPT='test2', DEPENDS='lib-c'), file=f)
# do bob jenkins add
self.executeBobJenkinsCmd("add myTestJenkins http://localhost:{} -r root"
.format(self.jenkinsMock.getServerPort()))
# throw away server data (but there shouldn't by any...
assert(len(self.jenkinsMock.getServerData()) == 0)
self.executeBobJenkinsCmd("push -q myTestJenkins")
send = self.jenkinsMock.getServerData()
self.assertEqual(len(send), 10) # 5 jobs, create + schedule
# bob will try to receive the old job config. Put it on the server...
for data in send:
created = re.match(r"/createItem\?name=(.*)$", data[0])
if created:
self.jenkinsMock.addServerData('/job/{}/config.xml'.format(created.group(1)), data[1])
if 'lib' in created.group(1):
oldTestConfig = data[1]
testrun = 0
while testrun < 3:
testrun += 1
with open(os.path.join("recipes", "app{}.yaml".format(testrun)), "w") as f:
print(RECIPE_APP.format(DEPENDS='lib-{}'.format(chr(ord('a')-1+testrun)),
SCRIPT='test_'+str(testrun)), file=f)
self.executeBobJenkinsCmd("push myTestJenkins -q")
send = self.jenkinsMock.getServerData()
# one of the app's were changed.
# jenkins has to reconfigure the app and the root job but not the lib job
configsChanged = 0
for data in send:
if 'job' in data[0] and 'config.xml' in data[0]:
configsChanged +=1
assert(configsChanged == 2)
self.executeBobJenkinsCmd("prune -q myTestJenkins")
send = self.jenkinsMock.getServerData()
assert(len(send) == 5) # deleted 5 Jobs
self.executeBobJenkinsCmd("rm myTestJenkins")
assert(len(self.jenkinsMock.getServerData()) == 0)