blob: 06245361c8f7d426c9128d9b3751ac32a9453836 [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
Hanna Arhipovae792be52019-02-13 13:28:11 +020023
Hanna Arhipova91dc8152019-07-04 12:43:16 +030024
Mikhail Kraynov351e8412018-10-04 18:27:44 +040025def join_to_gerrit(local_salt_client, gerrit_user, gerrit_password):
Hanna Arhipova91dc8152019-07-04 12:43:16 +030026 # Workaround for issue in test_drivetrain.join_to_jenkins https://github.com/kennethreitz/requests/issues/3829
27 os.environ["PYTHONHTTPSVERIFY"] = "0"
28
29 pytest.gerrit_port = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050030 tgt='I@gerrit:client and not I@salt:master',
31 param='_param:haproxy_gerrit_bind_port',
32 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030033 pytest.gerrit_address = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050034 tgt='I@gerrit:client and not I@salt:master',
35 param='_param:haproxy_gerrit_bind_host',
36 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030037
38 pytest.gerrit_protocol = local_salt_client.pillar_get(
39 tgt='I@gerrit:client and not I@salt:master',
40 param="gerrit:client:server:protocol",
41 expr_form='compound')
42
43 gerrit_url = '{protocol}://{address}:{port}'.format(
44 protocol=pytest.gerrit_protocol,
45 address=pytest.gerrit_address,
46 port=pytest.gerrit_port)
Mikhail Kraynov351e8412018-10-04 18:27:44 +040047 auth = HTTPBasicAuth(gerrit_user, gerrit_password)
Hanna Arhipova91dc8152019-07-04 12:43:16 +030048 rest = GerritRestAPI(url=gerrit_url, auth=auth)
Mikhail Kraynov351e8412018-10-04 18:27:44 +040049 return rest
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -050050
Hanna Arhipovae792be52019-02-13 13:28:11 +020051
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +040052def join_to_jenkins(local_salt_client, jenkins_user, jenkins_password):
Hanna Arhipova91dc8152019-07-04 12:43:16 +030053
54 pytest.jenkins_port = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050055 tgt='I@jenkins:client and not I@salt:master',
56 param='_param:haproxy_jenkins_bind_port',
57 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030058 pytest.jenkins_address = local_salt_client.pillar_get(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050059 tgt='I@jenkins:client and not I@salt:master',
60 param='_param:haproxy_jenkins_bind_host',
61 expr_form='compound')
Hanna Arhipova91dc8152019-07-04 12:43:16 +030062 pytest.jenkins_protocol = local_salt_client.pillar_get(
63 tgt='I@gerrit:client and not I@salt:master',
64 param="_param:jenkins_master_protocol",
65 expr_form='compound')
66
67 jenkins_url = '{protocol}://{address}:{port}'.format(
68 protocol=pytest.jenkins_protocol,
69 address=pytest.jenkins_address,
70 port=pytest.jenkins_port)
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +040071 server = jenkins.Jenkins(jenkins_url, username=jenkins_user, password=jenkins_password)
72 return server
73
Hanna Arhipovae792be52019-02-13 13:28:11 +020074
Mikhail Kraynov351e8412018-10-04 18:27:44 +040075def get_password(local_salt_client,service):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050076 password = local_salt_client.pillar_get(
77 tgt=service,
78 param='_param:openldap_admin_password')
Mikhail Kraynov351e8412018-10-04 18:27:44 +040079 return password
80
Hanna Arhipovae792be52019-02-13 13:28:11 +020081
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -050082@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +030083def test_drivetrain_gerrit(local_salt_client, check_cicd):
Hanna Arhipova91dc8152019-07-04 12:43:16 +030084
85 gerrit_password = get_password(local_salt_client, 'gerrit:client')
Mikhail Kraynov351e8412018-10-04 18:27:44 +040086 gerrit_error = ''
87 current_date = time.strftime("%Y%m%d-%H.%M.%S", time.localtime())
88 test_proj_name = "test-dt-{0}".format(current_date)
Hanna Arhipova91dc8152019-07-04 12:43:16 +030089
Mikhail Kraynov351e8412018-10-04 18:27:44 +040090 try:
Hanna Arhipova91dc8152019-07-04 12:43:16 +030091 # Connecting to gerrit and check connection
92 server = join_to_gerrit(local_salt_client, 'admin', gerrit_password)
Mikhail Kraynov351e8412018-10-04 18:27:44 +040093 gerrit_check = server.get("/changes/?q=owner:self%20status:open")
Hanna Arhipova91dc8152019-07-04 12:43:16 +030094 # Check deleteproject plugin and skip test if the plugin is not installed
Mikhail Kraynov351e8412018-10-04 18:27:44 +040095 gerrit_plugins = server.get("/plugins/?all")
96 if 'deleteproject' not in gerrit_plugins:
97 pytest.skip("Delete-project plugin is not installed")
Hanna Arhipova91dc8152019-07-04 12:43:16 +030098 # Create test project and add description
Mikhail Kraynov351e8412018-10-04 18:27:44 +040099 server.put("/projects/"+test_proj_name)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300100 server.put("/projects/"+test_proj_name+"/description",
101 json={"description": "Test DriveTrain project", "commit_message": "Update the project description"})
Hanna Arhipova56eab942019-05-06 20:14:18 +0300102 except HTTPError as e:
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400103 gerrit_error = e
104 try:
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300105 # Create test folder and init git
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400106 repo_dir = os.path.join(os.getcwd(),test_proj_name)
107 file_name = os.path.join(repo_dir, current_date)
108 repo = git.Repo.init(repo_dir)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300109 # Add remote url for this git repo
110 origin = repo.create_remote('origin', '{http}://admin:{password}@{address}:{port}/{project}.git'.format(
111 project=test_proj_name,
112 password=gerrit_password,
113 http=pytest.gerrit_protocol,
114 address=pytest.gerrit_address,
115 port=pytest.gerrit_port))
116 # Add commit-msg hook to automatically add Change-Id to our commit
117 os.system("curl -Lo {repo}/.git/hooks/commit-msg '{http}://admin:{password}@{address}:{port}/tools/hooks/commit-msg' > /dev/null 2>&1".format(
118 repo=repo_dir,
119 password=gerrit_password,
120 address=pytest.gerrit_address,
121 http=pytest.gerrit_protocol,
122 port=pytest.gerrit_port))
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400123 os.system("chmod u+x {0}/.git/hooks/commit-msg".format(repo_dir))
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300124 # Create a test file
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400125 f = open(file_name, 'w+')
126 f.write("This is a test file for DriveTrain test")
127 f.close()
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300128 # Add file to git and commit it to Gerrit for review
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400129 repo.index.add([file_name])
130 repo.index.commit("This is a test commit for DriveTrain test")
131 repo.git.push("origin", "HEAD:refs/for/master")
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300132 # Get change id from Gerrit. Set Code-Review +2 and submit this change
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400133 changes = server.get("/changes/?q=project:{0}".format(test_proj_name))
134 last_change = changes[0].get('change_id')
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300135 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 +0400136 server.post("/changes/{0}/submit".format(last_change))
Hanna Arhipova56eab942019-05-06 20:14:18 +0300137 except HTTPError as e:
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400138 gerrit_error = e
139 finally:
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300140 # Delete test project
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400141 server.post("/projects/"+test_proj_name+"/deleteproject~delete")
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200142 assert gerrit_error == '', (
143 'There is an error during Gerrit operations:\n{}'.format(gerrit_error))
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400144
Hanna Arhipovae792be52019-02-13 13:28:11 +0200145
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500146@pytest.mark.full
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300147@pytest.mark.skip
148# Temporary skipped, ldap3 package add\search user is not working
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300149def test_drivetrain_openldap(local_salt_client, check_cicd):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300150 """
151 1. Create a test user 'DT_test_user' in openldap
152 2. Add the user to admin group
153 3. Login using the user to Jenkins
154 4. Check that no error occurred
155 5. Add the user to devops group in Gerrit and then login to Gerrit
156 using test_user credentials.
157 6 Start job in jenkins from this user
158 7. Get info from gerrit from this user
159 6. Finally, delete the user from admin
160 group and openldap
161 """
162
163 # 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 +0300164 ldap_password = get_password(local_salt_client, 'openldap:client')
165 # Check that ldap_password is exists, otherwise skip test
mkraynov360c30d2018-09-27 17:02:45 +0400166 if not ldap_password:
167 pytest.skip("Openldap service or openldap:client pillar \
168 are not found on this environment.")
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500169 ldap_port = local_salt_client.pillar_get(
170 tgt='I@openldap:client and not I@salt:master',
171 param='_param:haproxy_openldap_bind_port',
172 expr_form='compound')
173 ldap_address = local_salt_client.pillar_get(
174 tgt='I@openldap:client and not I@salt:master',
175 param='_param:haproxy_openldap_bind_host',
176 expr_form='compound')
177 ldap_dc = local_salt_client.pillar_get(
178 tgt='openldap:client',
179 param='_param:openldap_dn')
180 ldap_con_admin = local_salt_client.pillar_get(
181 tgt='openldap:client',
182 param='openldap:client:server:auth:user')
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300183 ldap_url = 'ldap://{0}:{1}'.format(ldap_address, ldap_port)
mkraynov360c30d2018-09-27 17:02:45 +0400184 ldap_error = ''
185 ldap_result = ''
186 gerrit_result = ''
187 gerrit_error = ''
188 jenkins_error = ''
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300189 # Test user's CN
mkraynov360c30d2018-09-27 17:02:45 +0400190 test_user_name = 'DT_test_user'
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300191 test_user = 'cn={0},ou=people,{1}'.format(test_user_name, ldap_dc)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300192 # Admins group CN
mkraynov360c30d2018-09-27 17:02:45 +0400193 admin_gr_dn = 'cn=admins,ou=groups,{0}'.format(ldap_dc)
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300194 user_pass = 'aSecretPassw'
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300195 # List of attributes for test user
mkraynov360c30d2018-09-27 17:02:45 +0400196 attrs = {}
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300197 attrs['objectclass'] = ['organizationalRole', 'simpleSecurityObject', 'shadowAccount']
mkraynov360c30d2018-09-27 17:02:45 +0400198 attrs['cn'] = test_user_name
199 attrs['uid'] = test_user_name
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300200 attrs['userPassword'] = user_pass
mkraynov360c30d2018-09-27 17:02:45 +0400201 attrs['description'] = 'Test user for CVP DT test'
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300202 # search_filter = '(cn={0})'.format(test_user_name)
203 search_filter = '(cn={})'.format(test_user_name)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300204 # Get a test job name from config
mkraynov360c30d2018-09-27 17:02:45 +0400205 config = utils.get_configuration()
mkraynov058ee122018-11-30 13:15:49 +0400206 jenkins_cvp_job = config['jenkins_cvp_job']
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300207 logging.warning('test_user: {}'.format(test_user))
208 logging.warning('ldap_address: {}'.format(ldap_address))
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300209 # Open connection to ldap and creating test user in admins group
mkraynov360c30d2018-09-27 17:02:45 +0400210 try:
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300211 ldap_server = Server(host=ldap_address, port=ldap_port,
212 use_ssl=False, get_info='NO_INFO')
213 conn = Connection(ldap_server, client_strategy=LDIF)
214 conn.bind()
215 new_user = conn.add(test_user, test_user_name, attrs)
216 logging.warning('new_user: {}'.format(new_user))
217 conn.modify(admin_gr_dn,
218 {'memberUid': (MODIFY_ADD, [test_user_name])
219 })
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300220 # Check search test user in LDAP
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300221 conn2 = Connection(ldap_server)
222 conn2.bind()
223 ldap_result = conn2.search(search_base='dc=heat-cicd-queens-contrail41-sl,dc=local',
224 search_filter=search_filter, search_scope='SUBTREE', attributes=['cn'])
225 logging.warning('ldap_result: {}'.format(ldap_result))
226 logging.warning('conn2.entries.: {}'.format(conn2.entries))
227 except LDAPException as e:
mkraynov360c30d2018-09-27 17:02:45 +0400228 ldap_error = e
229 try:
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300230 # Check if user is created before connect from Jenkins
231 assert ldap_result, "Test user {} is not found".format(ldap_result)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300232 # Check connection between Jenkins and LDAP
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300233 jenkins_server = join_to_jenkins(local_salt_client, test_user_name, user_pass)
mkraynov058ee122018-11-30 13:15:49 +0400234 jenkins_version = jenkins_server.get_job_name(jenkins_cvp_job)
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300235 # Check connection between Gerrit and LDAP
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300236 gerrit_server = join_to_gerrit(local_salt_client, 'admin', ldap_password)
mkraynov360c30d2018-09-27 17:02:45 +0400237 gerrit_check = gerrit_server.get("/changes/?q=owner:self%20status:open")
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300238 # Add test user to devops-contrib group in Gerrit and check login
mkraynov360c30d2018-09-27 17:02:45 +0400239 _link = "/groups/devops-contrib/members/{0}".format(test_user_name)
240 gerrit_add_user = gerrit_server.put(_link)
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300241 gerrit_server = join_to_gerrit(local_salt_client, test_user_name, user_pass)
mkraynov360c30d2018-09-27 17:02:45 +0400242 gerrit_result = gerrit_server.get("/changes/?q=owner:self%20status:open")
Hanna Arhipova56eab942019-05-06 20:14:18 +0300243 except HTTPError as e:
mkraynov360c30d2018-09-27 17:02:45 +0400244 gerrit_error = e
Hanna Arhipova56eab942019-05-06 20:14:18 +0300245 except jenkins.JenkinsException as e:
mkraynov360c30d2018-09-27 17:02:45 +0400246 jenkins_error = e
247 finally:
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300248 conn.modify(admin_gr_dn,
249 {'memberUid': (MODIFY_DELETE, [test_user_name])
250 })
251 conn.delete(test_user)
252 conn.unbind()
253 conn2.unbind()
254
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200255 assert ldap_error == '', (
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300256 "There is an error with connection to LDAP:\n{}".format(ldap_error))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200257 assert jenkins_error == '', (
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300258 "Connection to Jenkins is not established:\n{}".format(jenkins_error))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200259 assert gerrit_error == '', (
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300260 "Connection to Gerrit is not established:\n{}".format(gerrit_error))
261
Mikhail Kraynov351e8412018-10-04 18:27:44 +0400262
Hanna Arhipovae792be52019-02-13 13:28:11 +0200263
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500264@pytest.mark.sl_dup
265#DockerService***Outage
266@pytest.mark.full
Oleksii Zhurba67aaec92019-04-15 18:05:13 -0500267def test_drivetrain_services_replicas(local_salt_client, check_cicd):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300268 """
269 # Execute ` salt -C 'I@gerrit:client' cmd.run 'docker service ls'` command to get info for each docker service like that:
270 "x5nzktxsdlm6 jenkins_slave02 replicated 0/1 docker-prod-local.artifactory.mirantis.com/mirantis/cicd/jnlp-slave:2019.2.0 "
271 # Check that each service has all replicas
272 """
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200273 # TODO: replace with rerunfalures plugin
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300274 wrong_items = []
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200275 for _ in range(4):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300276 docker_services_by_nodes = local_salt_client.cmd(
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500277 tgt='I@gerrit:client',
278 param='docker service ls',
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200279 expr_form='compound')
280 wrong_items = []
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300281 for line in docker_services_by_nodes[list(docker_services_by_nodes.keys())[0]].split('\n'):
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200282 if line[line.find('/') - 1] != line[line.find('/') + 1] \
283 and 'replicated' in line:
284 wrong_items.append(line)
285 if len(wrong_items) == 0:
286 break
287 else:
Hanna Arhipovaf2660bd2019-02-08 17:25:39 +0200288 time.sleep(5)
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200289 assert len(wrong_items) == 0, (
290 "Some DriveTrain services don't have expected number of replicas:\n"
291 "{}".format(json.dumps(wrong_items, indent=4))
292 )
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500293
294
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500295@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300296def test_drivetrain_components_and_versions(local_salt_client, check_cicd):
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500297 """
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300298 1. Execute command `docker service ls --format "{{.Image}}"'` on the 'I@gerrit:client' target
299 2. Execute ` salt -C 'I@gerrit:client' pillar.get docker:client:images`
300 3. Check that list of images from step 1 is the same as a list from the step2
301 4. Check that all docker services has label that equals to mcp_version
302
303 """
Hanna Arhipova04344f12019-06-12 13:56:36 +0300304 def get_name(long_name):
305 return long_name.rsplit(':', 1)[0]
306
307 def get_tag(long_name):
308 return long_name.rsplit(':', 1)[-1]
309
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500310 table_with_docker_services = local_salt_client.cmd(tgt='I@gerrit:client',
311 param='docker service ls --format "{{.Image}}"',
Hanna Arhipovae792be52019-02-13 13:28:11 +0200312 expr_form='compound')
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300313 stack_info = local_salt_client.pillar_get(tgt='gerrit:client',
314 param='docker:client:stack')
315
316 expected_images = list()
317 # find services in list of docker clients
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300318 for key, stack in list(stack_info.items()):
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300319 if stack.get('service'):
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300320 stack = [item.get('image') for _,item in list(stack.get('service').items()) if item.get('image')]
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300321 expected_images += stack
322
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500323 mismatch = {}
324 actual_images = {}
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300325 for image in set(table_with_docker_services[list(table_with_docker_services.keys())[0]].split('\n')):
Hanna Arhipova04344f12019-06-12 13:56:36 +0300326 actual_images[get_name(image)] = get_tag(image)
Hanna Arhipova8cc3a982019-07-22 14:58:03 +0300327
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500328 for image in set(expected_images):
Hanna Arhipova04344f12019-06-12 13:56:36 +0300329 im_name = get_name(image)
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500330 if im_name not in actual_images:
331 mismatch[im_name] = 'not found on env'
Hanna Arhipova04344f12019-06-12 13:56:36 +0300332 elif get_tag(image) != actual_images[im_name]:
Oleksii Zhurbab91c3142019-03-26 16:49:44 -0500333 mismatch[im_name] = 'has {actual} version instead of {expected}'.format(
Hanna Arhipova04344f12019-06-12 13:56:36 +0300334 actual=actual_images[im_name], expected=get_tag(image))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200335 assert len(mismatch) == 0, (
336 "Some DriveTrain components do not have expected versions:\n{}".format(
337 json.dumps(mismatch, indent=4))
338 )
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500339
340
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500341@pytest.mark.full
Oleksii Zhurba67aaec92019-04-15 18:05:13 -0500342def test_jenkins_jobs_branch(local_salt_client, check_cicd):
Oleksii Zhurbad52b5fe2019-03-28 11:11:35 -0500343 """ This test compares Jenkins jobs versions
344 collected from the cloud vs collected from pillars.
345 """
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300346
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500347 excludes = ['upgrade-mcp-release', 'deploy-update-salt',
348 'git-mirror-downstream-mk-pipelines',
349 'git-mirror-downstream-pipeline-library']
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200350
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500351 config = utils.get_configuration()
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200352 drivetrain_version = config.get('drivetrain_version', '')
Hanna Arhipovae792be52019-02-13 13:28:11 +0200353 jenkins_password = get_password(local_salt_client, 'jenkins:client')
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500354 version_mismatch = []
Hanna Arhipovae792be52019-02-13 13:28:11 +0200355 server = join_to_jenkins(local_salt_client, 'admin', jenkins_password)
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400356 for job_instance in server.get_jobs():
357 job_name = job_instance.get('name')
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200358 if job_name in excludes:
359 continue
360
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400361 job_config = server.get_job_config(job_name)
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500362 xml_data = minidom.parseString(job_config)
363 BranchSpec = xml_data.getElementsByTagName('hudson.plugins.git.BranchSpec')
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200364
365 # We use master branch for pipeline-library in case of 'testing,stable,nighlty' versions
366 # Leave proposed version as is
367 # in other cases we get release/{drivetrain_version} (e.g release/2019.2.0)
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300368 if drivetrain_version in ['testing', 'nightly', 'stable']:
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400369 expected_version = 'master'
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200370 else:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500371 expected_version = local_salt_client.pillar_get(
372 tgt='gerrit:client',
373 param='jenkins:client:job:{}:scm:branch'.format(job_name))
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200374
375 if not BranchSpec:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300376 logging.debug("No BranchSpec has found for {} job".format(job_name))
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200377 continue
378
379 actual_version = BranchSpec[0].getElementsByTagName('name')[0].childNodes[0].data
Oleksii Zhurba075cc7a2019-05-17 14:04:28 -0500380 if expected_version and actual_version not in expected_version:
Hanna Arhipova6f34fbb2019-02-08 11:19:41 +0200381 version_mismatch.append("Job {0} has {1} branch."
382 "Expected {2}".format(job_name,
383 actual_version,
384 expected_version))
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200385 assert len(version_mismatch) == 0, (
386 "Some DriveTrain jobs have version/branch mismatch:\n{}".format(
387 json.dumps(version_mismatch, indent=4))
388 )
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300389
390
Oleksii Zhurba5b15b9b2019-05-09 18:53:40 -0500391@pytest.mark.full
Hanna Arhipovab7e866c2019-04-10 13:49:56 +0300392def test_drivetrain_jenkins_job(local_salt_client, check_cicd):
Hanna Arhipova16a8f412019-04-08 17:10:38 +0300393 """
394 # Login to Jenkins on jenkins:client
395 # Read the name of jobs from configuration 'jenkins_test_job'
396 # Start job
397 # Wait till the job completed
398 # Check that job has completed with "SUCCESS" result
399 """
400 job_result = None
401
402 jenkins_password = get_password(local_salt_client, 'jenkins:client')
403 server = join_to_jenkins(local_salt_client, 'admin', jenkins_password)
404 # Getting Jenkins test job name from configuration
405 config = utils.get_configuration()
406 jenkins_test_job = config['jenkins_test_job']
407 if not server.get_job_name(jenkins_test_job):
408 server.create_job(jenkins_test_job, jenkins.EMPTY_CONFIG_XML)
409 if server.get_job_name(jenkins_test_job):
410 next_build_num = server.get_job_info(jenkins_test_job)['nextBuildNumber']
411 # If this is first build number skip building check
412 if next_build_num != 1:
413 # Check that test job is not running at this moment,
414 # Otherwise skip the test
415 last_build_num = server.get_job_info(jenkins_test_job)['lastBuild'].get('number')
416 last_build_status = server.get_build_info(jenkins_test_job, last_build_num)['building']
417 if last_build_status:
418 pytest.skip("Test job {0} is already running").format(jenkins_test_job)
419 server.build_job(jenkins_test_job)
420 timeout = 0
421 # Use job status True by default to exclude timeout between build job and start job.
422 job_status = True
423 while job_status and (timeout < 180):
424 time.sleep(10)
425 timeout += 10
426 job_status = server.get_build_info(jenkins_test_job, next_build_num)['building']
427 job_result = server.get_build_info(jenkins_test_job, next_build_num)['result']
428 else:
429 pytest.skip("The job {0} was not found").format(jenkins_test_job)
Dmitriy Kruglova34a3042019-08-20 11:45:35 +0200430 assert job_result == 'SUCCESS', (
431 "Test job '{}' build is not successful or timeout is too "
432 "small.".format(jenkins_test_job)
433 )