blob: 9e22eb633d996218df350239981dc7d83a8d4ddf [file] [log] [blame]
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +03001import git
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +04002import jenkins
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -05003import json
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +03004import logging
5import os
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -05006import pytest
Mikhail Kraynov351e8412018-10-04 18:27:44 +04007import time
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +03008import utils
9from builtins import range
10from ldap3 import (
11 Connection,
12 Server,
13 Reader,
14 LDIF,
15 MODIFY_ADD,
16 MODIFY_DELETE,
17 SUBTREE,
18 ALL_ATTRIBUTES)
19from ldap3.core.exceptions import LDAPException
Mikhail Kraynov351e8412018-10-04 18:27:44 +040020from pygerrit2 import GerritRestAPI, HTTPBasicAuth
21from requests import HTTPError
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +030022from xml.dom import minidom
Ievgeniia Zadorozhna4f470442020-01-09 13:22:14 +020023from collections import defaultdict
Hanna Arhipovae792be52019-02-13 13:28:11 +020024
Hanna Arhipova53268ce2020-05-15 15:22:06 +030025# ############################ FIXTURES ######################################
26user_name = 'DT_test_user'
27user_pass = 'aSecretPassw'
28
Hanna Arhipova91dc8152019-07-04 12:43:16 +030029
Mikhail Kraynov351e8412018-10-04 18:27:44 +040030def join_to_gerrit(local_salt_client, gerrit_user, gerrit_password):
Hanna Arhipova91dc8152019-07-04 12:43:16 +030031 # Workaround for issue in test_drivetrain.join_to_jenkins https://github.com/kennethreitz/requests/issues/3829
32 os.environ["PYTHONHTTPSVERIFY"] = "0"
33
34 pytest.gerrit_port = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050035 tgt='I@gerrit:client and not I@salt:master',
36 param='_param:haproxy_gerrit_bind_port',
37 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030038 pytest.gerrit_address = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050039 tgt='I@gerrit:client and not I@salt:master',
40 param='_param:haproxy_gerrit_bind_host',
41 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030042
43 pytest.gerrit_protocol = local_salt_client.pillar_get(
44 tgt='I@gerrit:client and not I@salt:master',
45 param="gerrit:client:server:protocol",
46 expr_form='compound')
47
48 gerrit_url = '{protocol}://{address}:{port}'.format(
49 protocol=pytest.gerrit_protocol,
50 address=pytest.gerrit_address,
51 port=pytest.gerrit_port)
Mikhail Kraynov351e8412018-10-04 18:27:44 +040052 auth = HTTPBasicAuth(gerrit_user, gerrit_password)
Hanna Arhipova91dc8152019-07-04 12:43:16 +030053 rest = GerritRestAPI(url=gerrit_url, auth=auth)
Mikhail Kraynov351e8412018-10-04 18:27:44 +040054 return rest
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -050055
Hanna Arhipovae792be52019-02-13 13:28:11 +020056
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +040057def join_to_jenkins(local_salt_client, jenkins_user, jenkins_password):
Hanna Arhipova91dc8152019-07-04 12:43:16 +030058
59 pytest.jenkins_port = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050060 tgt='I@jenkins:client and not I@salt:master',
61 param='_param:haproxy_jenkins_bind_port',
62 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030063 pytest.jenkins_address = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050064 tgt='I@jenkins:client and not I@salt:master',
65 param='_param:haproxy_jenkins_bind_host',
66 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030067 pytest.jenkins_protocol = local_salt_client.pillar_get(
68 tgt='I@gerrit:client and not I@salt:master',
69 param="_param:jenkins_master_protocol",
70 expr_form='compound')
71
72 jenkins_url = '{protocol}://{address}:{port}'.format(
73 protocol=pytest.jenkins_protocol,
74 address=pytest.jenkins_address,
75 port=pytest.jenkins_port)
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +040076 server = jenkins.Jenkins(jenkins_url, username=jenkins_user, password=jenkins_password)
77 return server
78
Hanna Arhipovae792be52019-02-13 13:28:11 +020079
Mikhail Kraynov351e8412018-10-04 18:27:44 +040080def get_password(local_salt_client,service):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050081 password = local_salt_client.pillar_get(
82 tgt=service,
83 param='_param:openldap_admin_password')
Mikhail Kraynov351e8412018-10-04 18:27:44 +040084 return password
85
Hanna Arhipovae792be52019-02-13 13:28:11 +020086
Hanna Arhipova745bbae2020-05-29 16:30:28 +030087@pytest.fixture(scope='class')
Hanna Arhipova53268ce2020-05-15 15:22:06 +030088def ldap_conn_from_new_admin(local_salt_client):
89 """
90 1. Create a test user 'DT_test_user' in openldap
91 2. Add the user to admin group
92
93 :return: connection to ldap with new created user
94 Finally, delete the user from admin group and openldap
95 """
96 ldap_password = get_password(local_salt_client, 'openldap:client')
97 # Check that ldap_password is exists, otherwise skip test
98 if not ldap_password:
99 pytest.skip("Openldap service or openldap:client pillar \
100 are not found on this environment.")
101 ldap_port = local_salt_client.pillar_get(
102 tgt='I@openldap:client and not I@salt:master',
103 param='_param:haproxy_openldap_bind_port',
104 expr_form='compound')
105 ldap_address = local_salt_client.pillar_get(
106 tgt='I@openldap:client and not I@salt:master',
107 param='_param:haproxy_openldap_bind_host',
108 expr_form='compound')
109 ldap_dc = local_salt_client.pillar_get(
110 tgt='openldap:client',
111 param='_param:openldap_dn')
112 ldap_admin_name = local_salt_client.pillar_get(
113 tgt='openldap:client',
114 param='openldap:client:server:auth:user')
115 ldap_admin_password = local_salt_client.pillar_get(
116 tgt='openldap:client',
117 param='openldap:client:server:auth:password')
118
119 ldap_user_name = 'cn={0},ou=people,{1}'.format(user_name, ldap_dc)
120
121 # Admins group CN
122 admin_gr_dn = 'cn=admins,ou=groups,{0}'.format(ldap_dc)
123 # List of attributes for test user
124 attrs = {
125 'cn': user_name,
126 'sn': user_name,
127 'uid': user_name,
128 'userPassword': user_pass,
129 'objectClass': ['shadowAccount', 'inetOrgPerson'],
130 'description': 'Test user for CVP DT test'
131 }
132 logging.warning("LOCALS {}".format(locals()))
133 ldap_server = Server(host=ldap_address, port=ldap_port,
134 use_ssl=False, get_info='NO_INFO')
135 admin_conn = Connection(ldap_server,
136 user=ldap_admin_name,
137 password=ldap_admin_password)
138
139 admin_conn.bind()
140 # Add new user
141 new_user = admin_conn.add(ldap_user_name, 'person', attrs)
Hanna Arhipova745bbae2020-05-29 16:30:28 +0300142 if not new_user:
143 logging.warning('new_user: {}\n error: {}'.format(new_user,
144 admin_conn.result))
Hanna Arhipova53268ce2020-05-15 15:22:06 +0300145 # Add him to admins group
146 modified_user = admin_conn.modify(admin_gr_dn,
147 {'memberUid': (MODIFY_ADD, [user_name])})
Hanna Arhipova745bbae2020-05-29 16:30:28 +0300148 if not modified_user:
149 logging.warning("added user to admins: {} \n error: {}".format(
150 modified_user,
151 admin_conn.result))
Hanna Arhipova53268ce2020-05-15 15:22:06 +0300152
153 user_conn = Connection(ldap_server,
154 user=ldap_user_name,
155 password=user_pass)
156 user_conn.bind()
157
158 # ###########################
159 yield user_conn
160 # ###########################
161 user_conn.unbind()
162 admin_conn.modify(admin_gr_dn, {
163 'memberUid': (MODIFY_DELETE, [user_name])
164 })
165 admin_conn.delete(ldap_user_name)
166 admin_conn.unbind()
167
168# ########################### TESTS ##########################################
169
170
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500171@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300172def test_drivetrain_gerrit(local_salt_client, check_cicd):
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300173
174 gerrit_password = get_password(local_salt_client, 'gerrit:client')
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400175 gerrit_error = ''
176 current_date = time.strftime("%Y%m%d-%H.%M.%S", time.localtime())
177 test_proj_name = "test-dt-{0}".format(current_date)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300178
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400179 try:
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300180 # Connecting to gerrit and check connection
181 server = join_to_gerrit(local_salt_client, 'admin', gerrit_password)
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400182 gerrit_check = server.get("/changes/?q=owner:self%20status:open")
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300183 # Check deleteproject plugin and skip test if the plugin is not installed
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400184 gerrit_plugins = server.get("/plugins/?all")
185 if 'deleteproject' not in gerrit_plugins:
186 pytest.skip("Delete-project plugin is not installed")
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300187 # Create test project and add description
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400188 server.put("/projects/"+test_proj_name)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300189 server.put("/projects/"+test_proj_name+"/description",
190 json={"description": "Test DriveTrain project", "commit_message": "Update the project description"})
Hanna Arhipova56eab942019-05-06 20:14:18 +0300191 except HTTPError as e:
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400192 gerrit_error = e
193 try:
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300194 # Create test folder and init git
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400195 repo_dir = os.path.join(os.getcwd(),test_proj_name)
196 file_name = os.path.join(repo_dir, current_date)
197 repo = git.Repo.init(repo_dir)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300198 # Add remote url for this git repo
199 origin = repo.create_remote('origin', '{http}://admin:{password}@{address}:{port}/{project}.git'.format(
200 project=test_proj_name,
201 password=gerrit_password,
202 http=pytest.gerrit_protocol,
203 address=pytest.gerrit_address,
204 port=pytest.gerrit_port))
205 # Add commit-msg hook to automatically add Change-Id to our commit
206 os.system("curl -Lo {repo}/.git/hooks/commit-msg '{http}://admin:{password}@{address}:{port}/tools/hooks/commit-msg' > /dev/null 2>&1".format(
207 repo=repo_dir,
208 password=gerrit_password,
209 address=pytest.gerrit_address,
210 http=pytest.gerrit_protocol,
211 port=pytest.gerrit_port))
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400212 os.system("chmod u+x {0}/.git/hooks/commit-msg".format(repo_dir))
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300213 # Create a test file
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400214 f = open(file_name, 'w+')
215 f.write("This is a test file for DriveTrain test")
216 f.close()
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300217 # Add file to git and commit it to Gerrit for review
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400218 repo.index.add([file_name])
219 repo.index.commit("This is a test commit for DriveTrain test")
220 repo.git.push("origin", "HEAD:refs/for/master")
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300221 # Get change id from Gerrit. Set Code-Review +2 and submit this change
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400222 changes = server.get("/changes/?q=project:{0}".format(test_proj_name))
223 last_change = changes[0].get('change_id')
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300224 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 +0400225 server.post("/changes/{0}/submit".format(last_change))
Hanna Arhipova56eab942019-05-06 20:14:18 +0300226 except HTTPError as e:
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400227 gerrit_error = e
228 finally:
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300229 # Delete test project
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400230 server.post("/projects/"+test_proj_name+"/deleteproject~delete")
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200231 assert gerrit_error == '', (
232 'There is an error during Gerrit operations:\n{}'.format(gerrit_error))
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400233
Hanna Arhipovae792be52019-02-13 13:28:11 +0200234
Hanna Arhipova745bbae2020-05-29 16:30:28 +0300235@pytest.mark.usefixtures('ldap_conn_from_new_admin')
236class TestOpenldap():
237 @pytest.mark.full
238 def test_new_user_can_connect_jenkins(self, local_salt_client, check_cicd):
239 """
240 1. Start job in jenkins from new ldap user
241 """
242 # Get a test job name from config
243 config = utils.get_configuration()
244 jenkins_cvp_job = config['jenkins_cvp_job']
245 jenkins_error = ''
246 try:
247 # Check connection between Jenkins and LDAP
248 jenkins_server = join_to_jenkins(local_salt_client, user_name, user_pass)
249 jenkins_version = jenkins_server.get_job_name(jenkins_cvp_job)
250 except jenkins.JenkinsException as e:
251 jenkins_error = e
252 assert jenkins_error == '', (
253 "Connection to Jenkins is not established:\n{}".format(jenkins_error))
Hanna Arhipova53268ce2020-05-15 15:22:06 +0300254
Hanna Arhipova745bbae2020-05-29 16:30:28 +0300255 @pytest.mark.full
256 def test_new_user_can_connect_gerrit(self, local_salt_client, check_cicd):
257 """
258 1. Add the user to devops group in Gerrit
259 2. Login to Gerrit using test_user credentials.
Hanna Arhipova53268ce2020-05-15 15:22:06 +0300260
Hanna Arhipova745bbae2020-05-29 16:30:28 +0300261 """
262 ldap_password = get_password(local_salt_client, 'openldap:client')
263 gerrit_error = ''
Hanna Arhipova53268ce2020-05-15 15:22:06 +0300264
Hanna Arhipova745bbae2020-05-29 16:30:28 +0300265 try:
266 # Check connection between Gerrit and LDAP
267 gerrit_server = join_to_gerrit(local_salt_client, 'admin', ldap_password)
268 gerrit_check = gerrit_server.get("/changes/?q=owner:self%20status:open")
Hanna Arhipova53268ce2020-05-15 15:22:06 +0300269
Hanna Arhipova745bbae2020-05-29 16:30:28 +0300270 # Add test user to devops-contrib group in Gerrit and check login
271 _link = "/groups/devops-contrib/members/{0}".format(user_name)
272 gerrit_add_user = gerrit_server.put(_link)
Hanna Arhipova53268ce2020-05-15 15:22:06 +0300273
Hanna Arhipova745bbae2020-05-29 16:30:28 +0300274 # Login to Gerrit as a user
275 gerrit_server = join_to_gerrit(local_salt_client, user_name, user_pass)
276 gerrit_result = gerrit_server.get(
277 "/changes/?q=owner:self%20status:open")
278 except HTTPError as e:
279 gerrit_error = e
Hanna Arhipova53268ce2020-05-15 15:22:06 +0300280
Hanna Arhipova745bbae2020-05-29 16:30:28 +0300281 assert gerrit_error == '', (
282 "Connection to Gerrit is not established:\n{}".format(gerrit_error))
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300283
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400284
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500285@pytest.mark.sl_dup
286#DockerService***Outage
287@pytest.mark.full
Oleksii Zhurba67aaec92019-04-15 18:05:13 -0500288def test_drivetrain_services_replicas(local_salt_client, check_cicd):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300289 """
290 # Execute ` salt -C 'I@gerrit:client' cmd.run 'docker service ls'` command to get info for each docker service like that:
291 "x5nzktxsdlm6 jenkins_slave02 replicated 0/1 docker-prod-local.artifactory.mirantis.com/mirantis/cicd/jnlp-slave:2019.2.0 "
292 # Check that each service has all replicas
293 """
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200294 # TODO: replace with rerunfalures plugin
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300295 wrong_items = []
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200296 for _ in range(4):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300297 docker_services_by_nodes = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500298 tgt='I@gerrit:client',
299 param='docker service ls',
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200300 expr_form='compound')
301 wrong_items = []
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300302 for line in docker_services_by_nodes[list(docker_services_by_nodes.keys())[0]].split('\n'):
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200303 if line[line.find('/') - 1] != line[line.find('/') + 1] \
304 and 'replicated' in line:
305 wrong_items.append(line)
306 if len(wrong_items) == 0:
307 break
308 else:
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200309 time.sleep(5)
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200310 assert len(wrong_items) == 0, (
311 "Some DriveTrain services don't have expected number of replicas:\n"
312 "{}".format(json.dumps(wrong_items, indent=4))
313 )
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500314
315
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500316@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300317def test_drivetrain_components_and_versions(local_salt_client, check_cicd):
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500318 """
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300319 1. Execute command `docker service ls --format "{{.Image}}"'` on the 'I@gerrit:client' target
320 2. Execute ` salt -C 'I@gerrit:client' pillar.get docker:client:images`
321 3. Check that list of images from step 1 is the same as a list from the step2
322 4. Check that all docker services has label that equals to mcp_version
323
324 """
Hanna Arhipova04344f12019-06-12 13:56:36 +0300325 def get_name(long_name):
326 return long_name.rsplit(':', 1)[0]
327
328 def get_tag(long_name):
329 return long_name.rsplit(':', 1)[-1]
330
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500331 table_with_docker_services = local_salt_client.cmd(tgt='I@gerrit:client',
332 param='docker service ls --format "{{.Image}}"',
Hanna Arhipovae792be52019-02-13 13:28:11 +0200333 expr_form='compound')
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300334 stack_info = local_salt_client.pillar_get(tgt='gerrit:client',
335 param='docker:client:stack')
336
Ievgeniia Zadorozhna4f470442020-01-09 13:22:14 +0200337 expected_images_list = list()
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300338 # find services in list of docker clients
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300339 for key, stack in list(stack_info.items()):
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300340 if stack.get('service'):
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300341 stack = [item.get('image') for _,item in list(stack.get('service').items()) if item.get('image')]
Ievgeniia Zadorozhna4f470442020-01-09 13:22:14 +0200342 expected_images_list += stack
343 expected_images = defaultdict(list)
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300344
Ievgeniia Zadorozhna4f470442020-01-09 13:22:14 +0200345 # collect unique tags for each image in same structure as for actual images
346 for image in expected_images_list:
347 if get_name(image) in expected_images:
348 if get_tag(image) not in expected_images[get_name(image)]:
349 expected_images[get_name(image)].append(get_tag(image))
350 else:
351 expected_images[get_name(image)].append(get_tag(image))
352
353 # collect tags for each image in same structure as for expected images
354 actual_images = defaultdict(list)
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300355 for image in set(table_with_docker_services[list(table_with_docker_services.keys())[0]].split('\n')):
Ievgeniia Zadorozhna4f470442020-01-09 13:22:14 +0200356 actual_images[get_name(image)].append(get_tag(image))
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300357
Ievgeniia Zadorozhna4f470442020-01-09 13:22:14 +0200358 # find difference between defaultdicts
359 total_diff = 0
360 for i in expected_images:
361 diff = set(expected_images[i]) - set(actual_images[i])
362 total_diff += len(diff)
363
364 assert actual_images == expected_images, (
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200365 "Some DriveTrain components do not have expected versions:\n{}".format(
Ievgeniia Zadorozhna4f470442020-01-09 13:22:14 +0200366 json.dumps(total_diff, indent=4))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200367 )
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500368
369
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500370@pytest.mark.full
Oleksii Zhurba67aaec92019-04-15 18:05:13 -0500371def test_jenkins_jobs_branch(local_salt_client, check_cicd):
Oleksii Zhurbad52b5fe2019-03-28 11:11:35 -0500372 """ This test compares Jenkins jobs versions
373 collected from the cloud vs collected from pillars.
374 """
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300375
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500376 excludes = ['upgrade-mcp-release', 'deploy-update-salt',
377 'git-mirror-downstream-mk-pipelines',
378 'git-mirror-downstream-pipeline-library']
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200379
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500380 config = utils.get_configuration()
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200381 drivetrain_version = config.get('drivetrain_version', '')
Hanna Arhipovae792be52019-02-13 13:28:11 +0200382 jenkins_password = get_password(local_salt_client, 'jenkins:client')
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500383 version_mismatch = []
Hanna Arhipovae792be52019-02-13 13:28:11 +0200384 server = join_to_jenkins(local_salt_client, 'admin', jenkins_password)
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400385 for job_instance in server.get_jobs():
386 job_name = job_instance.get('name')
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200387 if job_name in excludes:
388 continue
389
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400390 job_config = server.get_job_config(job_name)
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500391 xml_data = minidom.parseString(job_config)
392 BranchSpec = xml_data.getElementsByTagName('hudson.plugins.git.BranchSpec')
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200393
394 # We use master branch for pipeline-library in case of 'testing,stable,nighlty' versions
395 # Leave proposed version as is
396 # in other cases we get release/{drivetrain_version} (e.g release/2019.2.0)
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300397 if drivetrain_version in ['testing', 'nightly', 'stable']:
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400398 expected_version = 'master'
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200399 else:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500400 expected_version = local_salt_client.pillar_get(
401 tgt='gerrit:client',
402 param='jenkins:client:job:{}:scm:branch'.format(job_name))
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200403
404 if not BranchSpec:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300405 logging.debug("No BranchSpec has found for {} job".format(job_name))
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200406 continue
407
408 actual_version = BranchSpec[0].getElementsByTagName('name')[0].childNodes[0].data
Oleksii Zhurba075cc7a2019-05-17 14:04:28 -0500409 if expected_version and actual_version not in expected_version:
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200410 version_mismatch.append("Job {0} has {1} branch."
411 "Expected {2}".format(job_name,
412 actual_version,
413 expected_version))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200414 assert len(version_mismatch) == 0, (
415 "Some DriveTrain jobs have version/branch mismatch:\n{}".format(
416 json.dumps(version_mismatch, indent=4))
417 )
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300418
419
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500420@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300421def test_drivetrain_jenkins_job(local_salt_client, check_cicd):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300422 """
423 # Login to Jenkins on jenkins:client
424 # Read the name of jobs from configuration 'jenkins_test_job'
425 # Start job
426 # Wait till the job completed
427 # Check that job has completed with "SUCCESS" result
428 """
429 job_result = None
430
431 jenkins_password = get_password(local_salt_client, 'jenkins:client')
432 server = join_to_jenkins(local_salt_client, 'admin', jenkins_password)
433 # Getting Jenkins test job name from configuration
434 config = utils.get_configuration()
435 jenkins_test_job = config['jenkins_test_job']
436 if not server.get_job_name(jenkins_test_job):
437 server.create_job(jenkins_test_job, jenkins.EMPTY_CONFIG_XML)
438 if server.get_job_name(jenkins_test_job):
439 next_build_num = server.get_job_info(jenkins_test_job)['nextBuildNumber']
440 # If this is first build number skip building check
441 if next_build_num != 1:
442 # Check that test job is not running at this moment,
443 # Otherwise skip the test
444 last_build_num = server.get_job_info(jenkins_test_job)['lastBuild'].get('number')
445 last_build_status = server.get_build_info(jenkins_test_job, last_build_num)['building']
446 if last_build_status:
447 pytest.skip("Test job {0} is already running").format(jenkins_test_job)
448 server.build_job(jenkins_test_job)
449 timeout = 0
450 # Use job status True by default to exclude timeout between build job and start job.
451 job_status = True
452 while job_status and (timeout < 180):
453 time.sleep(10)
454 timeout += 10
455 job_status = server.get_build_info(jenkins_test_job, next_build_num)['building']
456 job_result = server.get_build_info(jenkins_test_job, next_build_num)['result']
457 else:
458 pytest.skip("The job {0} was not found").format(jenkins_test_job)
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200459 assert job_result == 'SUCCESS', (
460 "Test job '{}' build is not successful or timeout is too "
461 "small.".format(jenkins_test_job)
462 )