blob: 70b61ecfffd48e0da5bbcbb8f8075172f14f5e29 [file] [log] [blame]
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -05001from jenkinsapi.jenkins import Jenkins
2from xml.dom import minidom
3from cvp_checks import utils
4import json
5import pytest
6
7
8def test_drivetrain_services_replicas(local_salt_client):
9 salt_output = local_salt_client.cmd(
Oleksii Zhurba0aeedf72018-07-30 11:24:01 -050010 'I@gerrit:client',
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -050011 'cmd.run',
12 ['docker service ls'],
13 expr_form='compound')
14 wrong_items = []
15 for line in salt_output[salt_output.keys()[0]].split('\n'):
16 if line[line.find('/') - 1] != line[line.find('/') + 1] \
17 and 'replicated' in line:
18 wrong_items.append(line)
19 assert len(wrong_items) == 0, \
20 '''Some DriveTrain services doesn't have expected number of replicas:
21 {}'''.format(json.dumps(wrong_items, indent=4))
22
23
24def test_drivetrain_components_and_versions(local_salt_client):
25 config = utils.get_configuration()
26 version = config['drivetrain_version'] or []
27 if not version or version == '':
28 pytest.skip("drivetrain_version is not defined. Skipping")
29 salt_output = local_salt_client.cmd(
Oleksii Zhurba0aeedf72018-07-30 11:24:01 -050030 'I@gerrit:client',
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -050031 'cmd.run',
32 ['docker service ls'],
33 expr_form='compound')
34 not_found_services = ['gerrit_db', 'gerrit_server', 'jenkins_master',
35 'jenkins_slave01', 'jenkins_slave02',
36 'jenkins_slave03', 'ldap_admin', 'ldap_server']
37 version_mismatch = []
38 for line in salt_output[salt_output.keys()[0]].split('\n'):
39 for service in not_found_services:
40 if service in line:
41 not_found_services.remove(service)
42 if version != line.split()[4].split(':')[1]:
43 version_mismatch.append("{0}: expected "
44 "version is {1}, actual - {2}".format(service,version,
45 line.split()[4].split(':')[1]))
46 continue
47 assert len(not_found_services) == 0, \
48 '''Some DriveTrain components are not found:
49 {}'''.format(json.dumps(not_found_services, indent=4))
50 assert len(version_mismatch) == 0, \
51 '''Version mismatch found:
52 {}'''.format(json.dumps(version_mismatch, indent=4))
53
54
55def test_jenkins_jobs_branch(local_salt_client):
56 config = utils.get_configuration()
57 expected_version = config['drivetrain_version'] or []
58 if not expected_version or expected_version == '':
59 pytest.skip("drivetrain_version is not defined. Skipping")
60 jenkins_password = local_salt_client.cmd(
61 'jenkins:client',
62 'pillar.get',
63 ['_param:openldap_admin_password'],
64 expr_form='pillar').values()[0]
65 jenkins_port = local_salt_client.cmd(
66 'I@jenkins:client and not I@salt:master',
67 'pillar.get',
68 ['_param:haproxy_jenkins_bind_port'],
69 expr_form='compound').values()[0]
70 jenkins_address = local_salt_client.cmd(
71 'I@jenkins:client and not I@salt:master',
72 'pillar.get',
73 ['_param:haproxy_jenkins_bind_host'],
74 expr_form='compound').values()[0]
75 version_mismatch = []
76 jenkins_url = 'http://{0}:{1}'.format(jenkins_address,jenkins_port)
77 server = Jenkins(jenkins_url, username='admin', password=jenkins_password)
78 for job_name, job_instance in server.get_jobs():
79 job_config = job_instance.get_config()
80 xml_data = minidom.parseString(job_config)
81 BranchSpec = xml_data.getElementsByTagName('hudson.plugins.git.BranchSpec')
82 if BranchSpec:
83 actual_version = BranchSpec[0].getElementsByTagName('name')[0].childNodes[0].data
84 if actual_version != expected_version and 'master' not in actual_version:
85 version_mismatch.append("Job {0} has {1} branch."
86 "Expected {2}".format(job_instance.name,
87 actual_version,
88 expected_version))
89 assert len(version_mismatch) == 0, \
90 '''Some DriveTrain jobs have version/branch mismatch:
91 {}'''.format(json.dumps(version_mismatch, indent=4))