blob: 8358e544d5d39e0aae95bb2ffb605f4babcd2234 [file] [log] [blame]
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +04001import jenkins
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -05002from xml.dom import minidom
3from cvp_checks import utils
4import json
5import pytest
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +04006from time import sleep
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -05007
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +04008def join_to_jenkins(local_salt_client, jenkins_user, jenkins_password):
9 jenkins_port = local_salt_client.cmd(
10 'I@jenkins:client and not I@salt:master',
11 'pillar.get',
12 ['_param:haproxy_jenkins_bind_port'],
13 expr_form='compound').values()[0]
14 jenkins_address = local_salt_client.cmd(
15 'I@jenkins:client and not I@salt:master',
16 'pillar.get',
17 ['_param:haproxy_jenkins_bind_host'],
18 expr_form='compound').values()[0]
19 jenkins_url = 'http://{0}:{1}'.format(jenkins_address,jenkins_port)
20 server = jenkins.Jenkins(jenkins_url, username=jenkins_user, password=jenkins_password)
21 return server
22
23def test_drivetrain_jenkins_job(local_salt_client):
24 jenkins_password = local_salt_client.cmd(
25 'jenkins:client',
26 'pillar.get',
27 ['_param:openldap_admin_password'],
28 expr_form='pillar').values()[0]
29 server = join_to_jenkins(local_salt_client,'admin',jenkins_password)
30 #Getting Jenkins test job name from configuration
31 config = utils.get_configuration()
32 jenkins_test_job = config['jenkins_test_job']
33 if not jenkins_test_job or jenkins_test_job == '':
34 jenkins_test_job = 'git-mirror-downstream-mk-pipelines'
35 if server.get_job_name(jenkins_test_job):
36 next_build_num = server.get_job_info(jenkins_test_job)['nextBuildNumber']
37 #If this is first build number skip building check
38 if next_build_num != 1:
39 #Check that test job is not running at this moment,
40 #Otherwise skip the test
41 last_build_num = server.get_job_info(jenkins_test_job)['lastBuild'].get('number')
42 last_build_status = server.get_build_info(jenkins_test_job,last_build_num)['building']
43 if last_build_status:
44 pytest.skip("Test job {0} is already running").format(jenkins_test_job)
45 #This jenkins module doesn't work with build_job function without parameters
46 #Just send some fake parameters. All others will be used from default values
47 param_dict = {'foo':'bar'}
48 server.build_job(jenkins_test_job, param_dict)
49 timeout = 0
50 #Use job status True by default to exclude timeout between build job and start job.
51 job_status = True
52 while job_status and ( timeout < 180 ):
53 sleep(10)
54 timeout += 10
55 job_status = server.get_build_info(jenkins_test_job,next_build_num)['building']
56 job_result = server.get_build_info(jenkins_test_job,next_build_num)['result']
57 else:
58 pytest.skip("The job {0} was not found").format(test_job_name)
59 assert job_result == 'SUCCESS', \
60 '''Test job '{0}' build was not successfull or timeout is too small
61 '''.format(jenkins_test_job)
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -050062
63def test_drivetrain_services_replicas(local_salt_client):
64 salt_output = local_salt_client.cmd(
Oleksii Zhurba0aeedf72018-07-30 11:24:01 -050065 'I@gerrit:client',
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -050066 'cmd.run',
67 ['docker service ls'],
68 expr_form='compound')
69 wrong_items = []
70 for line in salt_output[salt_output.keys()[0]].split('\n'):
71 if line[line.find('/') - 1] != line[line.find('/') + 1] \
72 and 'replicated' in line:
73 wrong_items.append(line)
74 assert len(wrong_items) == 0, \
75 '''Some DriveTrain services doesn't have expected number of replicas:
76 {}'''.format(json.dumps(wrong_items, indent=4))
77
78
79def test_drivetrain_components_and_versions(local_salt_client):
80 config = utils.get_configuration()
81 version = config['drivetrain_version'] or []
82 if not version or version == '':
83 pytest.skip("drivetrain_version is not defined. Skipping")
84 salt_output = local_salt_client.cmd(
Oleksii Zhurba0aeedf72018-07-30 11:24:01 -050085 'I@gerrit:client',
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -050086 'cmd.run',
87 ['docker service ls'],
88 expr_form='compound')
89 not_found_services = ['gerrit_db', 'gerrit_server', 'jenkins_master',
90 'jenkins_slave01', 'jenkins_slave02',
91 'jenkins_slave03', 'ldap_admin', 'ldap_server']
92 version_mismatch = []
93 for line in salt_output[salt_output.keys()[0]].split('\n'):
94 for service in not_found_services:
95 if service in line:
96 not_found_services.remove(service)
97 if version != line.split()[4].split(':')[1]:
98 version_mismatch.append("{0}: expected "
99 "version is {1}, actual - {2}".format(service,version,
100 line.split()[4].split(':')[1]))
101 continue
102 assert len(not_found_services) == 0, \
103 '''Some DriveTrain components are not found:
104 {}'''.format(json.dumps(not_found_services, indent=4))
105 assert len(version_mismatch) == 0, \
106 '''Version mismatch found:
107 {}'''.format(json.dumps(version_mismatch, indent=4))
108
109
110def test_jenkins_jobs_branch(local_salt_client):
111 config = utils.get_configuration()
112 expected_version = config['drivetrain_version'] or []
113 if not expected_version or expected_version == '':
114 pytest.skip("drivetrain_version is not defined. Skipping")
115 jenkins_password = local_salt_client.cmd(
116 'jenkins:client',
117 'pillar.get',
118 ['_param:openldap_admin_password'],
119 expr_form='pillar').values()[0]
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500120 version_mismatch = []
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400121 server = join_to_jenkins(local_salt_client,'admin',jenkins_password)
122 for job_instance in server.get_jobs():
123 job_name = job_instance.get('name')
124 job_config = server.get_job_config(job_name)
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500125 xml_data = minidom.parseString(job_config)
126 BranchSpec = xml_data.getElementsByTagName('hudson.plugins.git.BranchSpec')
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400127 #We use master branch for pipeline-library in case of 'testing,stable,nighlty' versions
128 if expected_version in ['testing','nightly','stable']:
129 expected_version = 'master'
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500130 if BranchSpec:
131 actual_version = BranchSpec[0].getElementsByTagName('name')[0].childNodes[0].data
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400132 if ( actual_version != expected_version ) and ( job_name not in ['cvp-func','cvp-ha','cvp-perf'] ) :
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500133 version_mismatch.append("Job {0} has {1} branch."
Mikhail Kraynove5cc81b2018-10-03 13:01:06 +0400134 "Expected {2}".format(job_name,
Oleksii Zhurbaa25984b2018-06-15 15:30:41 -0500135 actual_version,
136 expected_version))
137 assert len(version_mismatch) == 0, \
138 '''Some DriveTrain jobs have version/branch mismatch:
139 {}'''.format(json.dumps(version_mismatch, indent=4))