Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 1 | import git |
Mikhail Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 2 | import jenkins |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 3 | import json |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 4 | import logging |
| 5 | import os |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 6 | import pytest |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 7 | import time |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 8 | import utils |
| 9 | from builtins import range |
| 10 | from ldap3 import ( |
| 11 | Connection, |
| 12 | Server, |
| 13 | Reader, |
| 14 | LDIF, |
| 15 | MODIFY_ADD, |
| 16 | MODIFY_DELETE, |
| 17 | SUBTREE, |
| 18 | ALL_ATTRIBUTES) |
| 19 | from ldap3.core.exceptions import LDAPException |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 20 | from pygerrit2 import GerritRestAPI, HTTPBasicAuth |
| 21 | from requests import HTTPError |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 22 | from xml.dom import minidom |
Ievgeniia Zadorozhna | 4f47044 | 2020-01-09 13:22:14 +0200 | [diff] [blame] | 23 | from collections import defaultdict |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 24 | |
Hanna Arhipova | 53268ce | 2020-05-15 15:22:06 +0300 | [diff] [blame] | 25 | # ############################ FIXTURES ###################################### |
| 26 | user_name = 'DT_test_user' |
| 27 | user_pass = 'aSecretPassw' |
| 28 | |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 29 | |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 30 | def join_to_gerrit(local_salt_client, gerrit_user, gerrit_password): |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 31 | # 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 Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 35 | tgt='I@gerrit:client and not I@salt:master', |
| 36 | param='_param:haproxy_gerrit_bind_port', |
| 37 | expr_form='compound') |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 38 | pytest.gerrit_address = local_salt_client.pillar_get( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 39 | tgt='I@gerrit:client and not I@salt:master', |
| 40 | param='_param:haproxy_gerrit_bind_host', |
| 41 | expr_form='compound') |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 42 | |
| 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 Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 52 | auth = HTTPBasicAuth(gerrit_user, gerrit_password) |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 53 | rest = GerritRestAPI(url=gerrit_url, auth=auth) |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 54 | return rest |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 55 | |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 56 | |
Mikhail Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 57 | def join_to_jenkins(local_salt_client, jenkins_user, jenkins_password): |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 58 | |
| 59 | pytest.jenkins_port = local_salt_client.pillar_get( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 60 | tgt='I@jenkins:client and not I@salt:master', |
| 61 | param='_param:haproxy_jenkins_bind_port', |
| 62 | expr_form='compound') |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 63 | pytest.jenkins_address = local_salt_client.pillar_get( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 64 | tgt='I@jenkins:client and not I@salt:master', |
| 65 | param='_param:haproxy_jenkins_bind_host', |
| 66 | expr_form='compound') |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 67 | 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 Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 76 | server = jenkins.Jenkins(jenkins_url, username=jenkins_user, password=jenkins_password) |
| 77 | return server |
| 78 | |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 79 | |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 80 | def get_password(local_salt_client,service): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 81 | password = local_salt_client.pillar_get( |
| 82 | tgt=service, |
| 83 | param='_param:openldap_admin_password') |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 84 | return password |
| 85 | |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 86 | |
Hanna Arhipova | 745bbae | 2020-05-29 16:30:28 +0300 | [diff] [blame] | 87 | @pytest.fixture(scope='class') |
Hanna Arhipova | 53268ce | 2020-05-15 15:22:06 +0300 | [diff] [blame] | 88 | def 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 Arhipova | 745bbae | 2020-05-29 16:30:28 +0300 | [diff] [blame] | 142 | if not new_user: |
| 143 | logging.warning('new_user: {}\n error: {}'.format(new_user, |
| 144 | admin_conn.result)) |
Hanna Arhipova | 53268ce | 2020-05-15 15:22:06 +0300 | [diff] [blame] | 145 | # Add him to admins group |
| 146 | modified_user = admin_conn.modify(admin_gr_dn, |
| 147 | {'memberUid': (MODIFY_ADD, [user_name])}) |
Hanna Arhipova | 745bbae | 2020-05-29 16:30:28 +0300 | [diff] [blame] | 148 | if not modified_user: |
| 149 | logging.warning("added user to admins: {} \n error: {}".format( |
| 150 | modified_user, |
| 151 | admin_conn.result)) |
Hanna Arhipova | 53268ce | 2020-05-15 15:22:06 +0300 | [diff] [blame] | 152 | |
| 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 Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 171 | @pytest.mark.full |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 172 | def test_drivetrain_gerrit(local_salt_client, check_cicd): |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 173 | |
| 174 | gerrit_password = get_password(local_salt_client, 'gerrit:client') |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 175 | 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 Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 178 | |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 179 | try: |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 180 | # Connecting to gerrit and check connection |
| 181 | server = join_to_gerrit(local_salt_client, 'admin', gerrit_password) |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 182 | gerrit_check = server.get("/changes/?q=owner:self%20status:open") |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 183 | # Check deleteproject plugin and skip test if the plugin is not installed |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 184 | gerrit_plugins = server.get("/plugins/?all") |
| 185 | if 'deleteproject' not in gerrit_plugins: |
| 186 | pytest.skip("Delete-project plugin is not installed") |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 187 | # Create test project and add description |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 188 | server.put("/projects/"+test_proj_name) |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 189 | server.put("/projects/"+test_proj_name+"/description", |
| 190 | json={"description": "Test DriveTrain project", "commit_message": "Update the project description"}) |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 191 | except HTTPError as e: |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 192 | gerrit_error = e |
| 193 | try: |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 194 | # Create test folder and init git |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 195 | 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 Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 198 | # 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 Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 212 | os.system("chmod u+x {0}/.git/hooks/commit-msg".format(repo_dir)) |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 213 | # Create a test file |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 214 | f = open(file_name, 'w+') |
| 215 | f.write("This is a test file for DriveTrain test") |
| 216 | f.close() |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 217 | # Add file to git and commit it to Gerrit for review |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 218 | 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 Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 221 | # Get change id from Gerrit. Set Code-Review +2 and submit this change |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 222 | changes = server.get("/changes/?q=project:{0}".format(test_proj_name)) |
| 223 | last_change = changes[0].get('change_id') |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 224 | server.post("/changes/{0}/revisions/1/review".format(last_change), json={"message": "All is good","labels":{"Code-Review":"+2"}}) |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 225 | server.post("/changes/{0}/submit".format(last_change)) |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 226 | except HTTPError as e: |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 227 | gerrit_error = e |
| 228 | finally: |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 229 | # Delete test project |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 230 | server.post("/projects/"+test_proj_name+"/deleteproject~delete") |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 231 | assert gerrit_error == '', ( |
| 232 | 'There is an error during Gerrit operations:\n{}'.format(gerrit_error)) |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 233 | |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 234 | |
Hanna Arhipova | 745bbae | 2020-05-29 16:30:28 +0300 | [diff] [blame] | 235 | @pytest.mark.usefixtures('ldap_conn_from_new_admin') |
| 236 | class 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 Arhipova | 53268ce | 2020-05-15 15:22:06 +0300 | [diff] [blame] | 254 | |
Hanna Arhipova | 745bbae | 2020-05-29 16:30:28 +0300 | [diff] [blame] | 255 | @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 Arhipova | 53268ce | 2020-05-15 15:22:06 +0300 | [diff] [blame] | 260 | |
Hanna Arhipova | 745bbae | 2020-05-29 16:30:28 +0300 | [diff] [blame] | 261 | """ |
| 262 | ldap_password = get_password(local_salt_client, 'openldap:client') |
| 263 | gerrit_error = '' |
Hanna Arhipova | 53268ce | 2020-05-15 15:22:06 +0300 | [diff] [blame] | 264 | |
Hanna Arhipova | 745bbae | 2020-05-29 16:30:28 +0300 | [diff] [blame] | 265 | 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 Arhipova | 53268ce | 2020-05-15 15:22:06 +0300 | [diff] [blame] | 269 | |
Hanna Arhipova | 745bbae | 2020-05-29 16:30:28 +0300 | [diff] [blame] | 270 | # 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 Arhipova | 53268ce | 2020-05-15 15:22:06 +0300 | [diff] [blame] | 273 | |
Hanna Arhipova | 745bbae | 2020-05-29 16:30:28 +0300 | [diff] [blame] | 274 | # 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 Arhipova | 53268ce | 2020-05-15 15:22:06 +0300 | [diff] [blame] | 280 | |
Hanna Arhipova | 745bbae | 2020-05-29 16:30:28 +0300 | [diff] [blame] | 281 | assert gerrit_error == '', ( |
| 282 | "Connection to Gerrit is not established:\n{}".format(gerrit_error)) |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 283 | |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 284 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 285 | @pytest.mark.sl_dup |
| 286 | #DockerService***Outage |
| 287 | @pytest.mark.full |
Oleksii Zhurba | 67aaec9 | 2019-04-15 18:05:13 -0500 | [diff] [blame] | 288 | def test_drivetrain_services_replicas(local_salt_client, check_cicd): |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 289 | """ |
| 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 Arhipova | f2660bd | 2019-02-08 17:25:39 +0200 | [diff] [blame] | 294 | # TODO: replace with rerunfalures plugin |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 295 | wrong_items = [] |
Hanna Arhipova | f2660bd | 2019-02-08 17:25:39 +0200 | [diff] [blame] | 296 | for _ in range(4): |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 297 | docker_services_by_nodes = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 298 | tgt='I@gerrit:client', |
| 299 | param='docker service ls', |
Hanna Arhipova | f2660bd | 2019-02-08 17:25:39 +0200 | [diff] [blame] | 300 | expr_form='compound') |
| 301 | wrong_items = [] |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 302 | for line in docker_services_by_nodes[list(docker_services_by_nodes.keys())[0]].split('\n'): |
Hanna Arhipova | f2660bd | 2019-02-08 17:25:39 +0200 | [diff] [blame] | 303 | 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 Arhipova | f2660bd | 2019-02-08 17:25:39 +0200 | [diff] [blame] | 309 | time.sleep(5) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 310 | 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 Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 314 | |
| 315 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 316 | @pytest.mark.full |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 317 | def test_drivetrain_components_and_versions(local_salt_client, check_cicd): |
Oleksii Zhurba | b91c314 | 2019-03-26 16:49:44 -0500 | [diff] [blame] | 318 | """ |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 319 | 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 Arhipova | 04344f1 | 2019-06-12 13:56:36 +0300 | [diff] [blame] | 325 | 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 Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 331 | table_with_docker_services = local_salt_client.cmd(tgt='I@gerrit:client', |
| 332 | param='docker service ls --format "{{.Image}}"', |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 333 | expr_form='compound') |
Hanna Arhipova | 8cc3a98 | 2019-07-22 14:58:03 +0300 | [diff] [blame] | 334 | stack_info = local_salt_client.pillar_get(tgt='gerrit:client', |
| 335 | param='docker:client:stack') |
| 336 | |
Ievgeniia Zadorozhna | 4f47044 | 2020-01-09 13:22:14 +0200 | [diff] [blame] | 337 | expected_images_list = list() |
Hanna Arhipova | 8cc3a98 | 2019-07-22 14:58:03 +0300 | [diff] [blame] | 338 | # find services in list of docker clients |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 339 | for key, stack in list(stack_info.items()): |
Hanna Arhipova | 8cc3a98 | 2019-07-22 14:58:03 +0300 | [diff] [blame] | 340 | if stack.get('service'): |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 341 | stack = [item.get('image') for _,item in list(stack.get('service').items()) if item.get('image')] |
Ievgeniia Zadorozhna | 4f47044 | 2020-01-09 13:22:14 +0200 | [diff] [blame] | 342 | expected_images_list += stack |
| 343 | expected_images = defaultdict(list) |
Hanna Arhipova | 8cc3a98 | 2019-07-22 14:58:03 +0300 | [diff] [blame] | 344 | |
Ievgeniia Zadorozhna | 4f47044 | 2020-01-09 13:22:14 +0200 | [diff] [blame] | 345 | # 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 Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 355 | for image in set(table_with_docker_services[list(table_with_docker_services.keys())[0]].split('\n')): |
Ievgeniia Zadorozhna | 4f47044 | 2020-01-09 13:22:14 +0200 | [diff] [blame] | 356 | actual_images[get_name(image)].append(get_tag(image)) |
Hanna Arhipova | 8cc3a98 | 2019-07-22 14:58:03 +0300 | [diff] [blame] | 357 | |
Ievgeniia Zadorozhna | 4f47044 | 2020-01-09 13:22:14 +0200 | [diff] [blame] | 358 | # 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 Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 365 | "Some DriveTrain components do not have expected versions:\n{}".format( |
Ievgeniia Zadorozhna | 4f47044 | 2020-01-09 13:22:14 +0200 | [diff] [blame] | 366 | json.dumps(total_diff, indent=4)) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 367 | ) |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 368 | |
| 369 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 370 | @pytest.mark.full |
Oleksii Zhurba | 67aaec9 | 2019-04-15 18:05:13 -0500 | [diff] [blame] | 371 | def test_jenkins_jobs_branch(local_salt_client, check_cicd): |
Oleksii Zhurba | d52b5fe | 2019-03-28 11:11:35 -0500 | [diff] [blame] | 372 | """ This test compares Jenkins jobs versions |
| 373 | collected from the cloud vs collected from pillars. |
| 374 | """ |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 375 | |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 376 | excludes = ['upgrade-mcp-release', 'deploy-update-salt', |
| 377 | 'git-mirror-downstream-mk-pipelines', |
| 378 | 'git-mirror-downstream-pipeline-library'] |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 379 | |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 380 | config = utils.get_configuration() |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 381 | drivetrain_version = config.get('drivetrain_version', '') |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 382 | jenkins_password = get_password(local_salt_client, 'jenkins:client') |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 383 | version_mismatch = [] |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 384 | server = join_to_jenkins(local_salt_client, 'admin', jenkins_password) |
Mikhail Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 385 | for job_instance in server.get_jobs(): |
| 386 | job_name = job_instance.get('name') |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 387 | if job_name in excludes: |
| 388 | continue |
| 389 | |
Mikhail Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 390 | job_config = server.get_job_config(job_name) |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 391 | xml_data = minidom.parseString(job_config) |
| 392 | BranchSpec = xml_data.getElementsByTagName('hudson.plugins.git.BranchSpec') |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 393 | |
| 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 Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 397 | if drivetrain_version in ['testing', 'nightly', 'stable']: |
Mikhail Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 398 | expected_version = 'master' |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 399 | else: |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 400 | expected_version = local_salt_client.pillar_get( |
| 401 | tgt='gerrit:client', |
| 402 | param='jenkins:client:job:{}:scm:branch'.format(job_name)) |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 403 | |
| 404 | if not BranchSpec: |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 405 | logging.debug("No BranchSpec has found for {} job".format(job_name)) |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 406 | continue |
| 407 | |
| 408 | actual_version = BranchSpec[0].getElementsByTagName('name')[0].childNodes[0].data |
Oleksii Zhurba | 075cc7a | 2019-05-17 14:04:28 -0500 | [diff] [blame] | 409 | if expected_version and actual_version not in expected_version: |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 410 | version_mismatch.append("Job {0} has {1} branch." |
| 411 | "Expected {2}".format(job_name, |
| 412 | actual_version, |
| 413 | expected_version)) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 414 | assert len(version_mismatch) == 0, ( |
| 415 | "Some DriveTrain jobs have version/branch mismatch:\n{}".format( |
| 416 | json.dumps(version_mismatch, indent=4)) |
| 417 | ) |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 418 | |
| 419 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 420 | @pytest.mark.full |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 421 | def test_drivetrain_jenkins_job(local_salt_client, check_cicd): |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 422 | """ |
| 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 Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 459 | assert job_result == 'SUCCESS', ( |
| 460 | "Test job '{}' build is not successful or timeout is too " |
| 461 | "small.".format(jenkins_test_job) |
| 462 | ) |