Mikhail Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 1 | import jenkins |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 2 | from xml.dom import minidom |
Hanna Arhipova | 16e93fb | 2019-01-23 19:03:01 +0200 | [diff] [blame] | 3 | import utils |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 4 | import json |
| 5 | import pytest |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 6 | import time |
| 7 | import os |
| 8 | from pygerrit2 import GerritRestAPI, HTTPBasicAuth |
| 9 | from requests import HTTPError |
| 10 | import git |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 11 | import ldap |
| 12 | import ldap.modlist as modlist |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 13 | import logging |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 14 | |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 15 | |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 16 | def join_to_gerrit(local_salt_client, gerrit_user, gerrit_password): |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 17 | # 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 Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 21 | tgt='I@gerrit:client and not I@salt:master', |
| 22 | param='_param:haproxy_gerrit_bind_port', |
| 23 | expr_form='compound') |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 24 | pytest.gerrit_address = local_salt_client.pillar_get( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 25 | tgt='I@gerrit:client and not I@salt:master', |
| 26 | param='_param:haproxy_gerrit_bind_host', |
| 27 | expr_form='compound') |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 28 | |
| 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 Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 38 | auth = HTTPBasicAuth(gerrit_user, gerrit_password) |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 39 | rest = GerritRestAPI(url=gerrit_url, auth=auth) |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 40 | return rest |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 41 | |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 42 | |
Mikhail Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 43 | def join_to_jenkins(local_salt_client, jenkins_user, jenkins_password): |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 44 | |
| 45 | pytest.jenkins_port = local_salt_client.pillar_get( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 46 | tgt='I@jenkins:client and not I@salt:master', |
| 47 | param='_param:haproxy_jenkins_bind_port', |
| 48 | expr_form='compound') |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 49 | pytest.jenkins_address = local_salt_client.pillar_get( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 50 | tgt='I@jenkins:client and not I@salt:master', |
| 51 | param='_param:haproxy_jenkins_bind_host', |
| 52 | expr_form='compound') |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 53 | 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 Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 62 | server = jenkins.Jenkins(jenkins_url, username=jenkins_user, password=jenkins_password) |
| 63 | return server |
| 64 | |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 65 | |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 66 | def get_password(local_salt_client,service): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 67 | password = local_salt_client.pillar_get( |
| 68 | tgt=service, |
| 69 | param='_param:openldap_admin_password') |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 70 | return password |
| 71 | |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 72 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 73 | @pytest.mark.full |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 74 | def test_drivetrain_gerrit(local_salt_client, check_cicd): |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 75 | |
| 76 | gerrit_password = get_password(local_salt_client, 'gerrit:client') |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 77 | 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 Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 80 | |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 81 | try: |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 82 | # Connecting to gerrit and check connection |
| 83 | server = join_to_gerrit(local_salt_client, 'admin', gerrit_password) |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 84 | gerrit_check = server.get("/changes/?q=owner:self%20status:open") |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 85 | # Check deleteproject plugin and skip test if the plugin is not installed |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 86 | gerrit_plugins = server.get("/plugins/?all") |
| 87 | if 'deleteproject' not in gerrit_plugins: |
| 88 | pytest.skip("Delete-project plugin is not installed") |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 89 | # Create test project and add description |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 90 | server.put("/projects/"+test_proj_name) |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 91 | server.put("/projects/"+test_proj_name+"/description", |
| 92 | json={"description": "Test DriveTrain project", "commit_message": "Update the project description"}) |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 93 | except HTTPError as e: |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 94 | gerrit_error = e |
| 95 | try: |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 96 | # Create test folder and init git |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 97 | 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 Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 100 | # 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 Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 114 | 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] | 115 | # Create a test file |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 116 | f = open(file_name, 'w+') |
| 117 | f.write("This is a test file for DriveTrain test") |
| 118 | f.close() |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 119 | # Add file to git and commit it to Gerrit for review |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 120 | 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 Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 123 | # 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] | 124 | changes = server.get("/changes/?q=project:{0}".format(test_proj_name)) |
| 125 | last_change = changes[0].get('change_id') |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 126 | 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] | 127 | server.post("/changes/{0}/submit".format(last_change)) |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 128 | except HTTPError as e: |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 129 | gerrit_error = e |
| 130 | finally: |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 131 | # Delete test project |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 132 | server.post("/projects/"+test_proj_name+"/deleteproject~delete") |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 133 | assert gerrit_error == '', ( |
| 134 | 'There is an error during Gerrit operations:\n{}'.format(gerrit_error)) |
Mikhail Kraynov | 351e841 | 2018-10-04 18:27:44 +0400 | [diff] [blame] | 135 | |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 136 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 137 | @pytest.mark.full |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 138 | def test_drivetrain_openldap(local_salt_client, check_cicd): |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 139 | """ |
| 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 Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 153 | ldap_password = get_password(local_salt_client, 'openldap:client') |
| 154 | # Check that ldap_password is exists, otherwise skip test |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 155 | if not ldap_password: |
| 156 | pytest.skip("Openldap service or openldap:client pillar \ |
| 157 | are not found on this environment.") |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 158 | 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') |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 172 | 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 Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 178 | # Test user's CN |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 179 | test_user_name = 'DT_test_user' |
| 180 | test_user = 'cn={0},ou=people,{1}'.format(test_user_name,ldap_dc) |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 181 | # Admins group CN |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 182 | admin_gr_dn = 'cn=admins,ou=groups,{0}'.format(ldap_dc) |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 183 | # List of attributes for test user |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 184 | attrs = {} |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 185 | attrs['objectclass'] = ['organizationalRole', 'simpleSecurityObject', 'shadowAccount'] |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 186 | 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 Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 191 | # Get a test job name from config |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 192 | config = utils.get_configuration() |
mkraynov | 058ee12 | 2018-11-30 13:15:49 +0400 | [diff] [blame] | 193 | jenkins_cvp_job = config['jenkins_cvp_job'] |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 194 | # Open connection to ldap and creating test user in admins group |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 195 | 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 Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 199 | 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 |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 202 | searchScope = ldap.SCOPE_SUBTREE |
| 203 | ldap_result = ldap_server.search_s(ldap_dc, searchScope, searchFilter) |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 204 | except ldap.LDAPError as e: |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 205 | ldap_error = e |
| 206 | try: |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 207 | # Check connection between Jenkins and LDAP |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 208 | jenkins_server = join_to_jenkins(local_salt_client,test_user_name,'aSecretPassw') |
mkraynov | 058ee12 | 2018-11-30 13:15:49 +0400 | [diff] [blame] | 209 | jenkins_version = jenkins_server.get_job_name(jenkins_cvp_job) |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 210 | # Check connection between Gerrit and LDAP |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 211 | gerrit_server = join_to_gerrit(local_salt_client,'admin',ldap_password) |
| 212 | gerrit_check = gerrit_server.get("/changes/?q=owner:self%20status:open") |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 213 | # Add test user to devops-contrib group in Gerrit and check login |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 214 | _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 Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 218 | except HTTPError as e: |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 219 | gerrit_error = e |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 220 | except jenkins.JenkinsException as e: |
mkraynov | 360c30d | 2018-09-27 17:02:45 +0400 | [diff] [blame] | 221 | 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 Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 226 | 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 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 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 235 | @pytest.mark.sl_dup |
| 236 | #DockerService***Outage |
| 237 | @pytest.mark.full |
Oleksii Zhurba | 67aaec9 | 2019-04-15 18:05:13 -0500 | [diff] [blame] | 238 | def test_drivetrain_services_replicas(local_salt_client, check_cicd): |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 239 | """ |
| 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 Arhipova | f2660bd | 2019-02-08 17:25:39 +0200 | [diff] [blame] | 244 | # TODO: replace with rerunfalures plugin |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 245 | wrong_items = [] |
Hanna Arhipova | f2660bd | 2019-02-08 17:25:39 +0200 | [diff] [blame] | 246 | for _ in range(4): |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 247 | docker_services_by_nodes = local_salt_client.cmd( |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 248 | tgt='I@gerrit:client', |
| 249 | param='docker service ls', |
Hanna Arhipova | f2660bd | 2019-02-08 17:25:39 +0200 | [diff] [blame] | 250 | expr_form='compound') |
| 251 | wrong_items = [] |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 252 | for line in docker_services_by_nodes[docker_services_by_nodes.keys()[0]].split('\n'): |
Hanna Arhipova | f2660bd | 2019-02-08 17:25:39 +0200 | [diff] [blame] | 253 | 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 Arhipova | f2660bd | 2019-02-08 17:25:39 +0200 | [diff] [blame] | 259 | time.sleep(5) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 260 | 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 Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 264 | |
| 265 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 266 | @pytest.mark.full |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 267 | def test_drivetrain_components_and_versions(local_salt_client, check_cicd): |
Oleksii Zhurba | b91c314 | 2019-03-26 16:49:44 -0500 | [diff] [blame] | 268 | """ |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 269 | 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 Arhipova | 04344f1 | 2019-06-12 13:56:36 +0300 | [diff] [blame] | 275 | 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 Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 281 | table_with_docker_services = local_salt_client.cmd(tgt='I@gerrit:client', |
| 282 | param='docker service ls --format "{{.Image}}"', |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 283 | expr_form='compound') |
Hanna Arhipova | 8cc3a98 | 2019-07-22 14:58:03 +0300 | [diff] [blame] | 284 | 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 Zhurba | b91c314 | 2019-03-26 16:49:44 -0500 | [diff] [blame] | 294 | mismatch = {} |
| 295 | actual_images = {} |
| 296 | for image in set(table_with_docker_services[table_with_docker_services.keys()[0]].split('\n')): |
Hanna Arhipova | 04344f1 | 2019-06-12 13:56:36 +0300 | [diff] [blame] | 297 | actual_images[get_name(image)] = get_tag(image) |
Hanna Arhipova | 8cc3a98 | 2019-07-22 14:58:03 +0300 | [diff] [blame] | 298 | |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 299 | for image in set(expected_images): |
Hanna Arhipova | 04344f1 | 2019-06-12 13:56:36 +0300 | [diff] [blame] | 300 | im_name = get_name(image) |
Oleksii Zhurba | b91c314 | 2019-03-26 16:49:44 -0500 | [diff] [blame] | 301 | if im_name not in actual_images: |
| 302 | mismatch[im_name] = 'not found on env' |
Hanna Arhipova | 04344f1 | 2019-06-12 13:56:36 +0300 | [diff] [blame] | 303 | elif get_tag(image) != actual_images[im_name]: |
Oleksii Zhurba | b91c314 | 2019-03-26 16:49:44 -0500 | [diff] [blame] | 304 | mismatch[im_name] = 'has {actual} version instead of {expected}'.format( |
Hanna Arhipova | 04344f1 | 2019-06-12 13:56:36 +0300 | [diff] [blame] | 305 | actual=actual_images[im_name], expected=get_tag(image)) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 306 | assert len(mismatch) == 0, ( |
| 307 | "Some DriveTrain components do not have expected versions:\n{}".format( |
| 308 | json.dumps(mismatch, indent=4)) |
| 309 | ) |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 310 | |
| 311 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 312 | @pytest.mark.full |
Oleksii Zhurba | 67aaec9 | 2019-04-15 18:05:13 -0500 | [diff] [blame] | 313 | def test_jenkins_jobs_branch(local_salt_client, check_cicd): |
Oleksii Zhurba | d52b5fe | 2019-03-28 11:11:35 -0500 | [diff] [blame] | 314 | """ This test compares Jenkins jobs versions |
| 315 | collected from the cloud vs collected from pillars. |
| 316 | """ |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 317 | |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 318 | excludes = ['upgrade-mcp-release', 'deploy-update-salt', |
| 319 | 'git-mirror-downstream-mk-pipelines', |
| 320 | 'git-mirror-downstream-pipeline-library'] |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 321 | |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 322 | config = utils.get_configuration() |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 323 | drivetrain_version = config.get('drivetrain_version', '') |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 324 | jenkins_password = get_password(local_salt_client, 'jenkins:client') |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 325 | version_mismatch = [] |
Hanna Arhipova | e792be5 | 2019-02-13 13:28:11 +0200 | [diff] [blame] | 326 | server = join_to_jenkins(local_salt_client, 'admin', jenkins_password) |
Mikhail Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 327 | for job_instance in server.get_jobs(): |
| 328 | job_name = job_instance.get('name') |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 329 | if job_name in excludes: |
| 330 | continue |
| 331 | |
Mikhail Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 332 | job_config = server.get_job_config(job_name) |
Oleksii Zhurba | a25984b | 2018-06-15 15:30:41 -0500 | [diff] [blame] | 333 | xml_data = minidom.parseString(job_config) |
| 334 | BranchSpec = xml_data.getElementsByTagName('hudson.plugins.git.BranchSpec') |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 335 | |
| 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 Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 339 | if drivetrain_version in ['testing', 'nightly', 'stable']: |
Mikhail Kraynov | e5cc81b | 2018-10-03 13:01:06 +0400 | [diff] [blame] | 340 | expected_version = 'master' |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 341 | else: |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 342 | expected_version = local_salt_client.pillar_get( |
| 343 | tgt='gerrit:client', |
| 344 | param='jenkins:client:job:{}:scm:branch'.format(job_name)) |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 345 | |
| 346 | if not BranchSpec: |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 347 | logging.debug("No BranchSpec has found for {} job".format(job_name)) |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 348 | continue |
| 349 | |
| 350 | actual_version = BranchSpec[0].getElementsByTagName('name')[0].childNodes[0].data |
Oleksii Zhurba | 075cc7a | 2019-05-17 14:04:28 -0500 | [diff] [blame] | 351 | if expected_version and actual_version not in expected_version: |
Hanna Arhipova | 6f34fbb | 2019-02-08 11:19:41 +0200 | [diff] [blame] | 352 | version_mismatch.append("Job {0} has {1} branch." |
| 353 | "Expected {2}".format(job_name, |
| 354 | actual_version, |
| 355 | expected_version)) |
Dmitriy Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 356 | assert len(version_mismatch) == 0, ( |
| 357 | "Some DriveTrain jobs have version/branch mismatch:\n{}".format( |
| 358 | json.dumps(version_mismatch, indent=4)) |
| 359 | ) |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 360 | |
| 361 | |
Oleksii Zhurba | 5b15b9b | 2019-05-09 18:53:40 -0500 | [diff] [blame] | 362 | @pytest.mark.full |
Hanna Arhipova | b7e866c | 2019-04-10 13:49:56 +0300 | [diff] [blame] | 363 | def test_drivetrain_jenkins_job(local_salt_client, check_cicd): |
Hanna Arhipova | 16a8f41 | 2019-04-08 17:10:38 +0300 | [diff] [blame] | 364 | """ |
| 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 Kruglov | a34a304 | 2019-08-20 11:45:35 +0200 | [diff] [blame] | 401 | assert job_result == 'SUCCESS', ( |
| 402 | "Test job '{}' build is not successful or timeout is too " |
| 403 | "small.".format(jenkins_test_job) |
| 404 | ) |