blob: 6741380cb34cd70d84b4cf3964046046952cfbeb [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")
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200133 assert gerrit_error == '', (
134 'There is an error during Gerrit operations:\n{}'.format(gerrit_error))
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400135
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()
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200226 assert ldap_error == '', (
227 "There is an error with connection to LDAP:\n{}".format(e))
228 assert jenkins_error == '', (
229 "Connection to Jenkins is not established:\n{}".format(e))
230 assert gerrit_error == '', (
231 "Connection to Gerrit is not established:\n{}".format(e))
232 assert ldap_result != [], "Test user {} is not found".format(ldap_result)
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400233
Hanna Arhipovae792be52019-02-13 13:28:11 +0200234
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500235@pytest.mark.sl_dup
236#DockerService***Outage
237@pytest.mark.full
Oleksii Zhurba67aaec92019-04-15 18:05:13 -0500238def test_drivetrain_services_replicas(local_salt_client, check_cicd):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300239 """
240 # Execute ` salt -C 'I@gerrit:client' cmd.run 'docker service ls'` command to get info for each docker service like that:
241 "x5nzktxsdlm6 jenkins_slave02 replicated 0/1 docker-prod-local.artifactory.mirantis.com/mirantis/cicd/jnlp-slave:2019.2.0 "
242 # Check that each service has all replicas
243 """
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200244 # TODO: replace with rerunfalures plugin
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300245 wrong_items = []
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200246 for _ in range(4):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300247 docker_services_by_nodes = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500248 tgt='I@gerrit:client',
249 param='docker service ls',
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200250 expr_form='compound')
251 wrong_items = []
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300252 for line in docker_services_by_nodes[docker_services_by_nodes.keys()[0]].split('\n'):
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200253 if line[line.find('/') - 1] != line[line.find('/') + 1] \
254 and 'replicated' in line:
255 wrong_items.append(line)
256 if len(wrong_items) == 0:
257 break
258 else:
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200259 time.sleep(5)
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200260 assert len(wrong_items) == 0, (
261 "Some DriveTrain services don't have expected number of replicas:\n"
262 "{}".format(json.dumps(wrong_items, indent=4))
263 )
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500264
265
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500266@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300267def test_drivetrain_components_and_versions(local_salt_client, check_cicd):
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500268 """
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300269 1. Execute command `docker service ls --format "{{.Image}}"'` on the 'I@gerrit:client' target
270 2. Execute ` salt -C 'I@gerrit:client' pillar.get docker:client:images`
271 3. Check that list of images from step 1 is the same as a list from the step2
272 4. Check that all docker services has label that equals to mcp_version
273
274 """
Hanna Arhipova04344f12019-06-12 13:56:36 +0300275 def get_name(long_name):
276 return long_name.rsplit(':', 1)[0]
277
278 def get_tag(long_name):
279 return long_name.rsplit(':', 1)[-1]
280
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500281 table_with_docker_services = local_salt_client.cmd(tgt='I@gerrit:client',
282 param='docker service ls --format "{{.Image}}"',
Hanna Arhipovae792be52019-02-13 13:28:11 +0200283 expr_form='compound')
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300284 stack_info = local_salt_client.pillar_get(tgt='gerrit:client',
285 param='docker:client:stack')
286
287 expected_images = list()
288 # find services in list of docker clients
289 for key, stack in stack_info.items():
290 if stack.get('service'):
291 stack = [item.get('image') for _,item in stack.get('service').items() if item.get('image')]
292 expected_images += stack
293
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500294 mismatch = {}
295 actual_images = {}
296 for image in set(table_with_docker_services[table_with_docker_services.keys()[0]].split('\n')):
Hanna Arhipova04344f12019-06-12 13:56:36 +0300297 actual_images[get_name(image)] = get_tag(image)
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300298
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500299 for image in set(expected_images):
Hanna Arhipova04344f12019-06-12 13:56:36 +0300300 im_name = get_name(image)
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500301 if im_name not in actual_images:
302 mismatch[im_name] = 'not found on env'
Hanna Arhipova04344f12019-06-12 13:56:36 +0300303 elif get_tag(image) != actual_images[im_name]:
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500304 mismatch[im_name] = 'has {actual} version instead of {expected}'.format(
Hanna Arhipova04344f12019-06-12 13:56:36 +0300305 actual=actual_images[im_name], expected=get_tag(image))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200306 assert len(mismatch) == 0, (
307 "Some DriveTrain components do not have expected versions:\n{}".format(
308 json.dumps(mismatch, indent=4))
309 )
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500310
311
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500312@pytest.mark.full
Oleksii Zhurba67aaec92019-04-15 18:05:13 -0500313def test_jenkins_jobs_branch(local_salt_client, check_cicd):
Oleksii Zhurbad52b5fe2019-03-28 11:11:35 -0500314 """ This test compares Jenkins jobs versions
315 collected from the cloud vs collected from pillars.
316 """
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300317
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500318 excludes = ['upgrade-mcp-release', 'deploy-update-salt',
319 'git-mirror-downstream-mk-pipelines',
320 'git-mirror-downstream-pipeline-library']
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200321
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500322 config = utils.get_configuration()
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200323 drivetrain_version = config.get('drivetrain_version', '')
Hanna Arhipovae792be52019-02-13 13:28:11 +0200324 jenkins_password = get_password(local_salt_client, 'jenkins:client')
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500325 version_mismatch = []
Hanna Arhipovae792be52019-02-13 13:28:11 +0200326 server = join_to_jenkins(local_salt_client, 'admin', jenkins_password)
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400327 for job_instance in server.get_jobs():
328 job_name = job_instance.get('name')
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200329 if job_name in excludes:
330 continue
331
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400332 job_config = server.get_job_config(job_name)
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500333 xml_data = minidom.parseString(job_config)
334 BranchSpec = xml_data.getElementsByTagName('hudson.plugins.git.BranchSpec')
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200335
336 # We use master branch for pipeline-library in case of 'testing,stable,nighlty' versions
337 # Leave proposed version as is
338 # in other cases we get release/{drivetrain_version} (e.g release/2019.2.0)
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300339 if drivetrain_version in ['testing', 'nightly', 'stable']:
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400340 expected_version = 'master'
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200341 else:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500342 expected_version = local_salt_client.pillar_get(
343 tgt='gerrit:client',
344 param='jenkins:client:job:{}:scm:branch'.format(job_name))
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200345
346 if not BranchSpec:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300347 logging.debug("No BranchSpec has found for {} job".format(job_name))
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200348 continue
349
350 actual_version = BranchSpec[0].getElementsByTagName('name')[0].childNodes[0].data
Oleksii Zhurba075cc7a2019-05-17 14:04:28 -0500351 if expected_version and actual_version not in expected_version:
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200352 version_mismatch.append("Job {0} has {1} branch."
353 "Expected {2}".format(job_name,
354 actual_version,
355 expected_version))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200356 assert len(version_mismatch) == 0, (
357 "Some DriveTrain jobs have version/branch mismatch:\n{}".format(
358 json.dumps(version_mismatch, indent=4))
359 )
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300360
361
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500362@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300363def test_drivetrain_jenkins_job(local_salt_client, check_cicd):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300364 """
365 # Login to Jenkins on jenkins:client
366 # Read the name of jobs from configuration 'jenkins_test_job'
367 # Start job
368 # Wait till the job completed
369 # Check that job has completed with "SUCCESS" result
370 """
371 job_result = None
372
373 jenkins_password = get_password(local_salt_client, 'jenkins:client')
374 server = join_to_jenkins(local_salt_client, 'admin', jenkins_password)
375 # Getting Jenkins test job name from configuration
376 config = utils.get_configuration()
377 jenkins_test_job = config['jenkins_test_job']
378 if not server.get_job_name(jenkins_test_job):
379 server.create_job(jenkins_test_job, jenkins.EMPTY_CONFIG_XML)
380 if server.get_job_name(jenkins_test_job):
381 next_build_num = server.get_job_info(jenkins_test_job)['nextBuildNumber']
382 # If this is first build number skip building check
383 if next_build_num != 1:
384 # Check that test job is not running at this moment,
385 # Otherwise skip the test
386 last_build_num = server.get_job_info(jenkins_test_job)['lastBuild'].get('number')
387 last_build_status = server.get_build_info(jenkins_test_job, last_build_num)['building']
388 if last_build_status:
389 pytest.skip("Test job {0} is already running").format(jenkins_test_job)
390 server.build_job(jenkins_test_job)
391 timeout = 0
392 # Use job status True by default to exclude timeout between build job and start job.
393 job_status = True
394 while job_status and (timeout < 180):
395 time.sleep(10)
396 timeout += 10
397 job_status = server.get_build_info(jenkins_test_job, next_build_num)['building']
398 job_result = server.get_build_info(jenkins_test_job, next_build_num)['result']
399 else:
400 pytest.skip("The job {0} was not found").format(jenkins_test_job)
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200401 assert job_result == 'SUCCESS', (
402 "Test job '{}' build is not successful or timeout is too "
403 "small.".format(jenkins_test_job)
404 )