blob: 3c2344bd04a00fa1b5d7cf623305a9dbdc3c4d1a [file] [log] [blame]
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +04001import jenkins
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -05002from xml.dom import minidom
Hanna Arhipova16e93fb2019-01-23 19:03:01 +02003import utils
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -05004import json
5import pytest
Mikhail Kraynov351e8412018-10-04 18:27:44 +04006import time
7import os
8from pygerrit2 import GerritRestAPI, HTTPBasicAuth
9from requests import HTTPError
10import git
mkraynov360c30d2018-09-27 17:02:45 +040011import ldap
12import ldap.modlist as modlist
Hanna Arhipova56eab942019-05-06 20:14:18 +030013import logging
Hanna Arhipovae792be52019-02-13 13:28:11 +020014
Hanna Arhipova91dc8152019-07-04 12:43:16 +030015
Mikhail Kraynov351e8412018-10-04 18:27:44 +040016def join_to_gerrit(local_salt_client, gerrit_user, gerrit_password):
Hanna Arhipova91dc8152019-07-04 12:43:16 +030017 # Workaround for issue in test_drivetrain.join_to_jenkins https://github.com/kennethreitz/requests/issues/3829
18 os.environ["PYTHONHTTPSVERIFY"] = "0"
19
20 pytest.gerrit_port = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050021 tgt='I@gerrit:client and not I@salt:master',
22 param='_param:haproxy_gerrit_bind_port',
23 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030024 pytest.gerrit_address = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050025 tgt='I@gerrit:client and not I@salt:master',
26 param='_param:haproxy_gerrit_bind_host',
27 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030028
29 pytest.gerrit_protocol = local_salt_client.pillar_get(
30 tgt='I@gerrit:client and not I@salt:master',
31 param="gerrit:client:server:protocol",
32 expr_form='compound')
33
34 gerrit_url = '{protocol}://{address}:{port}'.format(
35 protocol=pytest.gerrit_protocol,
36 address=pytest.gerrit_address,
37 port=pytest.gerrit_port)
Mikhail Kraynov351e8412018-10-04 18:27:44 +040038 auth = HTTPBasicAuth(gerrit_user, gerrit_password)
Hanna Arhipova91dc8152019-07-04 12:43:16 +030039 rest = GerritRestAPI(url=gerrit_url, auth=auth)
Mikhail Kraynov351e8412018-10-04 18:27:44 +040040 return rest
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -050041
Hanna Arhipovae792be52019-02-13 13:28:11 +020042
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +040043def join_to_jenkins(local_salt_client, jenkins_user, jenkins_password):
Hanna Arhipova91dc8152019-07-04 12:43:16 +030044
45 pytest.jenkins_port = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050046 tgt='I@jenkins:client and not I@salt:master',
47 param='_param:haproxy_jenkins_bind_port',
48 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030049 pytest.jenkins_address = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050050 tgt='I@jenkins:client and not I@salt:master',
51 param='_param:haproxy_jenkins_bind_host',
52 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030053 pytest.jenkins_protocol = local_salt_client.pillar_get(
54 tgt='I@gerrit:client and not I@salt:master',
55 param="_param:jenkins_master_protocol",
56 expr_form='compound')
57
58 jenkins_url = '{protocol}://{address}:{port}'.format(
59 protocol=pytest.jenkins_protocol,
60 address=pytest.jenkins_address,
61 port=pytest.jenkins_port)
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +040062 server = jenkins.Jenkins(jenkins_url, username=jenkins_user, password=jenkins_password)
63 return server
64
Hanna Arhipovae792be52019-02-13 13:28:11 +020065
Mikhail Kraynov351e8412018-10-04 18:27:44 +040066def get_password(local_salt_client,service):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050067 password = local_salt_client.pillar_get(
68 tgt=service,
69 param='_param:openldap_admin_password')
Mikhail Kraynov351e8412018-10-04 18:27:44 +040070 return password
71
Hanna Arhipovae792be52019-02-13 13:28:11 +020072
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -050073@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +030074def test_drivetrain_gerrit(local_salt_client, check_cicd):
Hanna Arhipova91dc8152019-07-04 12:43:16 +030075
76 gerrit_password = get_password(local_salt_client, 'gerrit:client')
Mikhail Kraynov351e8412018-10-04 18:27:44 +040077 gerrit_error = ''
78 current_date = time.strftime("%Y%m%d-%H.%M.%S", time.localtime())
79 test_proj_name = "test-dt-{0}".format(current_date)
Hanna Arhipova91dc8152019-07-04 12:43:16 +030080
Mikhail Kraynov351e8412018-10-04 18:27:44 +040081 try:
Hanna Arhipova91dc8152019-07-04 12:43:16 +030082 # Connecting to gerrit and check connection
83 server = join_to_gerrit(local_salt_client, 'admin', gerrit_password)
Mikhail Kraynov351e8412018-10-04 18:27:44 +040084 gerrit_check = server.get("/changes/?q=owner:self%20status:open")
Hanna Arhipova91dc8152019-07-04 12:43:16 +030085 # Check deleteproject plugin and skip test if the plugin is not installed
Mikhail Kraynov351e8412018-10-04 18:27:44 +040086 gerrit_plugins = server.get("/plugins/?all")
87 if 'deleteproject' not in gerrit_plugins:
88 pytest.skip("Delete-project plugin is not installed")
Hanna Arhipova91dc8152019-07-04 12:43:16 +030089 # Create test project and add description
Mikhail Kraynov351e8412018-10-04 18:27:44 +040090 server.put("/projects/"+test_proj_name)
Hanna Arhipova91dc8152019-07-04 12:43:16 +030091 server.put("/projects/"+test_proj_name+"/description",
92 json={"description": "Test DriveTrain project", "commit_message": "Update the project description"})
Hanna Arhipova56eab942019-05-06 20:14:18 +030093 except HTTPError as e:
Mikhail Kraynov351e8412018-10-04 18:27:44 +040094 gerrit_error = e
95 try:
Hanna Arhipova91dc8152019-07-04 12:43:16 +030096 # Create test folder and init git
Mikhail Kraynov351e8412018-10-04 18:27:44 +040097 repo_dir = os.path.join(os.getcwd(),test_proj_name)
98 file_name = os.path.join(repo_dir, current_date)
99 repo = git.Repo.init(repo_dir)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300100 # Add remote url for this git repo
101 origin = repo.create_remote('origin', '{http}://admin:{password}@{address}:{port}/{project}.git'.format(
102 project=test_proj_name,
103 password=gerrit_password,
104 http=pytest.gerrit_protocol,
105 address=pytest.gerrit_address,
106 port=pytest.gerrit_port))
107 # Add commit-msg hook to automatically add Change-Id to our commit
108 os.system("curl -Lo {repo}/.git/hooks/commit-msg '{http}://admin:{password}@{address}:{port}/tools/hooks/commit-msg' > /dev/null 2>&1".format(
109 repo=repo_dir,
110 password=gerrit_password,
111 address=pytest.gerrit_address,
112 http=pytest.gerrit_protocol,
113 port=pytest.gerrit_port))
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400114 os.system("chmod u+x {0}/.git/hooks/commit-msg".format(repo_dir))
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300115 # Create a test file
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400116 f = open(file_name, 'w+')
117 f.write("This is a test file for DriveTrain test")
118 f.close()
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300119 # Add file to git and commit it to Gerrit for review
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400120 repo.index.add([file_name])
121 repo.index.commit("This is a test commit for DriveTrain test")
122 repo.git.push("origin", "HEAD:refs/for/master")
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300123 # Get change id from Gerrit. Set Code-Review +2 and submit this change
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400124 changes = server.get("/changes/?q=project:{0}".format(test_proj_name))
125 last_change = changes[0].get('change_id')
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300126 server.post("/changes/{0}/revisions/1/review".format(last_change), json={"message": "All is good","labels":{"Code-Review":"+2"}})
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400127 server.post("/changes/{0}/submit".format(last_change))
Hanna Arhipova56eab942019-05-06 20:14:18 +0300128 except HTTPError as e:
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400129 gerrit_error = e
130 finally:
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300131 # Delete test project
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400132 server.post("/projects/"+test_proj_name+"/deleteproject~delete")
133 assert gerrit_error == '',\
134 'Something is wrong with Gerrit'.format(gerrit_error)
135
Hanna Arhipovae792be52019-02-13 13:28:11 +0200136
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500137@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300138def test_drivetrain_openldap(local_salt_client, check_cicd):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300139 """
140 1. Create a test user 'DT_test_user' in openldap
141 2. Add the user to admin group
142 3. Login using the user to Jenkins
143 4. Check that no error occurred
144 5. Add the user to devops group in Gerrit and then login to Gerrit
145 using test_user credentials.
146 6 Start job in jenkins from this user
147 7. Get info from gerrit from this user
148 6. Finally, delete the user from admin
149 group and openldap
150 """
151
152 # TODO split to several test cases. One check - per one test method. Make the login process in fixture
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300153 ldap_password = get_password(local_salt_client, 'openldap:client')
154 # Check that ldap_password is exists, otherwise skip test
mkraynov360c30d2018-09-27 17:02:45 +0400155 if not ldap_password:
156 pytest.skip("Openldap service or openldap:client pillar \
157 are not found on this environment.")
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500158 ldap_port = local_salt_client.pillar_get(
159 tgt='I@openldap:client and not I@salt:master',
160 param='_param:haproxy_openldap_bind_port',
161 expr_form='compound')
162 ldap_address = local_salt_client.pillar_get(
163 tgt='I@openldap:client and not I@salt:master',
164 param='_param:haproxy_openldap_bind_host',
165 expr_form='compound')
166 ldap_dc = local_salt_client.pillar_get(
167 tgt='openldap:client',
168 param='_param:openldap_dn')
169 ldap_con_admin = local_salt_client.pillar_get(
170 tgt='openldap:client',
171 param='openldap:client:server:auth:user')
mkraynov360c30d2018-09-27 17:02:45 +0400172 ldap_url = 'ldap://{0}:{1}'.format(ldap_address,ldap_port)
173 ldap_error = ''
174 ldap_result = ''
175 gerrit_result = ''
176 gerrit_error = ''
177 jenkins_error = ''
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300178 # Test user's CN
mkraynov360c30d2018-09-27 17:02:45 +0400179 test_user_name = 'DT_test_user'
180 test_user = 'cn={0},ou=people,{1}'.format(test_user_name,ldap_dc)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300181 # Admins group CN
mkraynov360c30d2018-09-27 17:02:45 +0400182 admin_gr_dn = 'cn=admins,ou=groups,{0}'.format(ldap_dc)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300183 # List of attributes for test user
mkraynov360c30d2018-09-27 17:02:45 +0400184 attrs = {}
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300185 attrs['objectclass'] = ['organizationalRole', 'simpleSecurityObject', 'shadowAccount']
mkraynov360c30d2018-09-27 17:02:45 +0400186 attrs['cn'] = test_user_name
187 attrs['uid'] = test_user_name
188 attrs['userPassword'] = 'aSecretPassw'
189 attrs['description'] = 'Test user for CVP DT test'
190 searchFilter = 'cn={0}'.format(test_user_name)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300191 # Get a test job name from config
mkraynov360c30d2018-09-27 17:02:45 +0400192 config = utils.get_configuration()
mkraynov058ee122018-11-30 13:15:49 +0400193 jenkins_cvp_job = config['jenkins_cvp_job']
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300194 # Open connection to ldap and creating test user in admins group
mkraynov360c30d2018-09-27 17:02:45 +0400195 try:
196 ldap_server = ldap.initialize(ldap_url)
197 ldap_server.simple_bind_s(ldap_con_admin,ldap_password)
198 ldif = modlist.addModlist(attrs)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300199 ldap_server.add_s(test_user, ldif)
200 ldap_server.modify_s(admin_gr_dn, [(ldap.MOD_ADD, 'memberUid', [test_user_name],)],)
201 # Check search test user in LDAP
mkraynov360c30d2018-09-27 17:02:45 +0400202 searchScope = ldap.SCOPE_SUBTREE
203 ldap_result = ldap_server.search_s(ldap_dc, searchScope, searchFilter)
Hanna Arhipova56eab942019-05-06 20:14:18 +0300204 except ldap.LDAPError as e:
mkraynov360c30d2018-09-27 17:02:45 +0400205 ldap_error = e
206 try:
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300207 # Check connection between Jenkins and LDAP
mkraynov360c30d2018-09-27 17:02:45 +0400208 jenkins_server = join_to_jenkins(local_salt_client,test_user_name,'aSecretPassw')
mkraynov058ee122018-11-30 13:15:49 +0400209 jenkins_version = jenkins_server.get_job_name(jenkins_cvp_job)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300210 # Check connection between Gerrit and LDAP
mkraynov360c30d2018-09-27 17:02:45 +0400211 gerrit_server = join_to_gerrit(local_salt_client,'admin',ldap_password)
212 gerrit_check = gerrit_server.get("/changes/?q=owner:self%20status:open")
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300213 # Add test user to devops-contrib group in Gerrit and check login
mkraynov360c30d2018-09-27 17:02:45 +0400214 _link = "/groups/devops-contrib/members/{0}".format(test_user_name)
215 gerrit_add_user = gerrit_server.put(_link)
216 gerrit_server = join_to_gerrit(local_salt_client,test_user_name,'aSecretPassw')
217 gerrit_result = gerrit_server.get("/changes/?q=owner:self%20status:open")
Hanna Arhipova56eab942019-05-06 20:14:18 +0300218 except HTTPError as e:
mkraynov360c30d2018-09-27 17:02:45 +0400219 gerrit_error = e
Hanna Arhipova56eab942019-05-06 20:14:18 +0300220 except jenkins.JenkinsException as e:
mkraynov360c30d2018-09-27 17:02:45 +0400221 jenkins_error = e
222 finally:
223 ldap_server.modify_s(admin_gr_dn,[(ldap.MOD_DELETE, 'memberUid', [test_user_name],)],)
224 ldap_server.delete_s(test_user)
225 ldap_server.unbind_s()
226 assert ldap_error == '', \
227 '''Something is wrong with connection to LDAP:
228 {0}'''.format(e)
229 assert jenkins_error == '', \
230 '''Connection to Jenkins was not established:
231 {0}'''.format(e)
232 assert gerrit_error == '', \
233 '''Connection to Gerrit was not established:
234 {0}'''.format(e)
235 assert ldap_result !=[], \
236 '''Test user was not found'''
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400237
Hanna Arhipovae792be52019-02-13 13:28:11 +0200238
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500239@pytest.mark.sl_dup
240#DockerService***Outage
241@pytest.mark.full
Oleksii Zhurba67aaec92019-04-15 18:05:13 -0500242def test_drivetrain_services_replicas(local_salt_client, check_cicd):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300243 """
244 # Execute ` salt -C 'I@gerrit:client' cmd.run 'docker service ls'` command to get info for each docker service like that:
245 "x5nzktxsdlm6 jenkins_slave02 replicated 0/1 docker-prod-local.artifactory.mirantis.com/mirantis/cicd/jnlp-slave:2019.2.0 "
246 # Check that each service has all replicas
247 """
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200248 # TODO: replace with rerunfalures plugin
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300249 wrong_items = []
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200250 for _ in range(4):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300251 docker_services_by_nodes = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500252 tgt='I@gerrit:client',
253 param='docker service ls',
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200254 expr_form='compound')
255 wrong_items = []
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300256 for line in docker_services_by_nodes[docker_services_by_nodes.keys()[0]].split('\n'):
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200257 if line[line.find('/') - 1] != line[line.find('/') + 1] \
258 and 'replicated' in line:
259 wrong_items.append(line)
260 if len(wrong_items) == 0:
261 break
262 else:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300263 logging.error('''Some DriveTrain services doesn't have expected number of replicas:
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200264 {}\n'''.format(json.dumps(wrong_items, indent=4)))
265 time.sleep(5)
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300266 assert len(wrong_items) == 0
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500267
268
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500269@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300270def test_drivetrain_components_and_versions(local_salt_client, check_cicd):
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500271 """
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300272 1. Execute command `docker service ls --format "{{.Image}}"'` on the 'I@gerrit:client' target
273 2. Execute ` salt -C 'I@gerrit:client' pillar.get docker:client:images`
274 3. Check that list of images from step 1 is the same as a list from the step2
275 4. Check that all docker services has label that equals to mcp_version
276
277 """
Hanna Arhipova04344f12019-06-12 13:56:36 +0300278 def get_name(long_name):
279 return long_name.rsplit(':', 1)[0]
280
281 def get_tag(long_name):
282 return long_name.rsplit(':', 1)[-1]
283
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500284 table_with_docker_services = local_salt_client.cmd(tgt='I@gerrit:client',
285 param='docker service ls --format "{{.Image}}"',
Hanna Arhipovae792be52019-02-13 13:28:11 +0200286 expr_form='compound')
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500287 expected_images = local_salt_client.pillar_get(tgt='gerrit:client',
288 param='docker:client:images')
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500289 mismatch = {}
290 actual_images = {}
291 for image in set(table_with_docker_services[table_with_docker_services.keys()[0]].split('\n')):
Hanna Arhipova04344f12019-06-12 13:56:36 +0300292 actual_images[get_name(image)] = get_tag(image)
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500293 for image in set(expected_images):
Hanna Arhipova04344f12019-06-12 13:56:36 +0300294 im_name = get_name(image)
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500295 if im_name not in actual_images:
296 mismatch[im_name] = 'not found on env'
Hanna Arhipova04344f12019-06-12 13:56:36 +0300297 elif get_tag(image) != actual_images[im_name]:
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500298 mismatch[im_name] = 'has {actual} version instead of {expected}'.format(
Hanna Arhipova04344f12019-06-12 13:56:36 +0300299 actual=actual_images[im_name], expected=get_tag(image))
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500300 assert len(mismatch) == 0, \
301 '''Some DriveTrain components do not have expected versions:
302 {}'''.format(json.dumps(mismatch, indent=4))
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500303
304
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500305@pytest.mark.full
Oleksii Zhurba67aaec92019-04-15 18:05:13 -0500306def test_jenkins_jobs_branch(local_salt_client, check_cicd):
Oleksii Zhurbad52b5fe2019-03-28 11:11:35 -0500307 """ This test compares Jenkins jobs versions
308 collected from the cloud vs collected from pillars.
309 """
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300310
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500311 excludes = ['upgrade-mcp-release', 'deploy-update-salt',
312 'git-mirror-downstream-mk-pipelines',
313 'git-mirror-downstream-pipeline-library']
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200314
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500315 config = utils.get_configuration()
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200316 drivetrain_version = config.get('drivetrain_version', '')
Hanna Arhipovae792be52019-02-13 13:28:11 +0200317 jenkins_password = get_password(local_salt_client, 'jenkins:client')
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500318 version_mismatch = []
Hanna Arhipovae792be52019-02-13 13:28:11 +0200319 server = join_to_jenkins(local_salt_client, 'admin', jenkins_password)
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400320 for job_instance in server.get_jobs():
321 job_name = job_instance.get('name')
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200322 if job_name in excludes:
323 continue
324
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400325 job_config = server.get_job_config(job_name)
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500326 xml_data = minidom.parseString(job_config)
327 BranchSpec = xml_data.getElementsByTagName('hudson.plugins.git.BranchSpec')
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200328
329 # We use master branch for pipeline-library in case of 'testing,stable,nighlty' versions
330 # Leave proposed version as is
331 # in other cases we get release/{drivetrain_version} (e.g release/2019.2.0)
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300332 if drivetrain_version in ['testing', 'nightly', 'stable']:
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400333 expected_version = 'master'
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200334 else:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500335 expected_version = local_salt_client.pillar_get(
336 tgt='gerrit:client',
337 param='jenkins:client:job:{}:scm:branch'.format(job_name))
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200338
339 if not BranchSpec:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300340 logging.debug("No BranchSpec has found for {} job".format(job_name))
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200341 continue
342
343 actual_version = BranchSpec[0].getElementsByTagName('name')[0].childNodes[0].data
Oleksii Zhurba075cc7a2019-05-17 14:04:28 -0500344 if expected_version and actual_version not in expected_version:
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200345 version_mismatch.append("Job {0} has {1} branch."
346 "Expected {2}".format(job_name,
347 actual_version,
348 expected_version))
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500349 assert len(version_mismatch) == 0, \
350 '''Some DriveTrain jobs have version/branch mismatch:
351 {}'''.format(json.dumps(version_mismatch, indent=4))
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300352
353
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500354@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300355def test_drivetrain_jenkins_job(local_salt_client, check_cicd):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300356 """
357 # Login to Jenkins on jenkins:client
358 # Read the name of jobs from configuration 'jenkins_test_job'
359 # Start job
360 # Wait till the job completed
361 # Check that job has completed with "SUCCESS" result
362 """
363 job_result = None
364
365 jenkins_password = get_password(local_salt_client, 'jenkins:client')
366 server = join_to_jenkins(local_salt_client, 'admin', jenkins_password)
367 # Getting Jenkins test job name from configuration
368 config = utils.get_configuration()
369 jenkins_test_job = config['jenkins_test_job']
370 if not server.get_job_name(jenkins_test_job):
371 server.create_job(jenkins_test_job, jenkins.EMPTY_CONFIG_XML)
372 if server.get_job_name(jenkins_test_job):
373 next_build_num = server.get_job_info(jenkins_test_job)['nextBuildNumber']
374 # If this is first build number skip building check
375 if next_build_num != 1:
376 # Check that test job is not running at this moment,
377 # Otherwise skip the test
378 last_build_num = server.get_job_info(jenkins_test_job)['lastBuild'].get('number')
379 last_build_status = server.get_build_info(jenkins_test_job, last_build_num)['building']
380 if last_build_status:
381 pytest.skip("Test job {0} is already running").format(jenkins_test_job)
382 server.build_job(jenkins_test_job)
383 timeout = 0
384 # Use job status True by default to exclude timeout between build job and start job.
385 job_status = True
386 while job_status and (timeout < 180):
387 time.sleep(10)
388 timeout += 10
389 job_status = server.get_build_info(jenkins_test_job, next_build_num)['building']
390 job_result = server.get_build_info(jenkins_test_job, next_build_num)['result']
391 else:
392 pytest.skip("The job {0} was not found").format(jenkins_test_job)
393 assert job_result == 'SUCCESS', \
394 '''Test job '{0}' build was not successful or timeout is too small
395 '''.format(jenkins_test_job)