Add DT Jenkins job test

Change-Id: I60d65c7347a91dd3bb24f6278507f4948320a299
Related-PROD: PROD-23242
diff --git a/cvp_checks/global_config.yaml b/cvp_checks/global_config.yaml
index 40303d6..72e6980 100644
--- a/cvp_checks/global_config.yaml
+++ b/cvp_checks/global_config.yaml
@@ -66,6 +66,7 @@
 # mask for interfaces to skip
 
 drivetrain_version: ''
+jenkins_test_job: ''
 
 # ntp test setting
 # this test may skip specific node (use fqdn)
diff --git a/cvp_checks/tests/test_drivetrain.py b/cvp_checks/tests/test_drivetrain.py
index 70b61ec..8358e54 100644
--- a/cvp_checks/tests/test_drivetrain.py
+++ b/cvp_checks/tests/test_drivetrain.py
@@ -1,9 +1,64 @@
-from jenkinsapi.jenkins import Jenkins
+import jenkins
 from xml.dom import minidom
 from cvp_checks import utils
 import json
 import pytest
+from time import sleep
 
+def join_to_jenkins(local_salt_client, jenkins_user, jenkins_password):
+    jenkins_port = local_salt_client.cmd(
+        'I@jenkins:client and not I@salt:master',
+        'pillar.get',
+        ['_param:haproxy_jenkins_bind_port'],
+        expr_form='compound').values()[0]
+    jenkins_address = local_salt_client.cmd(
+        'I@jenkins:client and not I@salt:master',
+        'pillar.get',
+        ['_param:haproxy_jenkins_bind_host'],
+        expr_form='compound').values()[0]
+    jenkins_url = 'http://{0}:{1}'.format(jenkins_address,jenkins_port)
+    server = jenkins.Jenkins(jenkins_url, username=jenkins_user, password=jenkins_password)
+    return server
+
+def test_drivetrain_jenkins_job(local_salt_client):
+    jenkins_password = local_salt_client.cmd(
+        'jenkins:client',
+        'pillar.get',
+        ['_param:openldap_admin_password'],
+        expr_form='pillar').values()[0]
+    server = join_to_jenkins(local_salt_client,'admin',jenkins_password)
+    #Getting Jenkins test job name from configuration
+    config = utils.get_configuration()
+    jenkins_test_job = config['jenkins_test_job']
+    if not jenkins_test_job or jenkins_test_job == '':
+        jenkins_test_job = 'git-mirror-downstream-mk-pipelines'
+    if server.get_job_name(jenkins_test_job):
+        next_build_num = server.get_job_info(jenkins_test_job)['nextBuildNumber']
+        #If this is first build number skip building check
+        if next_build_num != 1:
+            #Check that test job is not running at this moment,
+            #Otherwise skip the test
+            last_build_num = server.get_job_info(jenkins_test_job)['lastBuild'].get('number')
+            last_build_status = server.get_build_info(jenkins_test_job,last_build_num)['building']
+            if last_build_status:
+                pytest.skip("Test job {0} is already running").format(jenkins_test_job)
+        #This jenkins module doesn't work with build_job function without parameters
+        #Just send some fake parameters. All others will be used from default values
+        param_dict = {'foo':'bar'}
+        server.build_job(jenkins_test_job, param_dict)
+        timeout = 0
+        #Use job status True by default to exclude timeout between build job and start job.
+        job_status = True
+        while job_status and ( timeout < 180 ):
+            sleep(10)
+            timeout += 10
+            job_status = server.get_build_info(jenkins_test_job,next_build_num)['building']
+        job_result = server.get_build_info(jenkins_test_job,next_build_num)['result']
+    else:
+        pytest.skip("The job {0} was not found").format(test_job_name)
+    assert job_result == 'SUCCESS', \
+        '''Test job '{0}' build was not successfull or timeout is too small
+         '''.format(jenkins_test_job)
 
 def test_drivetrain_services_replicas(local_salt_client):
     salt_output = local_salt_client.cmd(
@@ -62,28 +117,21 @@
         'pillar.get',
         ['_param:openldap_admin_password'],
         expr_form='pillar').values()[0]
-    jenkins_port = local_salt_client.cmd(
-        'I@jenkins:client and not I@salt:master',
-        'pillar.get',
-        ['_param:haproxy_jenkins_bind_port'],
-        expr_form='compound').values()[0]
-    jenkins_address = local_salt_client.cmd(
-        'I@jenkins:client and not I@salt:master',
-        'pillar.get',
-        ['_param:haproxy_jenkins_bind_host'],
-        expr_form='compound').values()[0]
     version_mismatch = []
-    jenkins_url = 'http://{0}:{1}'.format(jenkins_address,jenkins_port)
-    server = Jenkins(jenkins_url, username='admin', password=jenkins_password)
-    for job_name, job_instance in server.get_jobs():
-        job_config = job_instance.get_config()
+    server = join_to_jenkins(local_salt_client,'admin',jenkins_password)
+    for job_instance in server.get_jobs():
+        job_name = job_instance.get('name')
+        job_config = server.get_job_config(job_name)
         xml_data = minidom.parseString(job_config)
         BranchSpec = xml_data.getElementsByTagName('hudson.plugins.git.BranchSpec')
+        #We use master branch for pipeline-library in case of 'testing,stable,nighlty' versions
+        if expected_version in ['testing','nightly','stable']:
+            expected_version = 'master'
         if BranchSpec:
             actual_version = BranchSpec[0].getElementsByTagName('name')[0].childNodes[0].data
-            if actual_version != expected_version and 'master' not in actual_version:
+            if ( actual_version != expected_version ) and ( job_name not in ['cvp-func','cvp-ha','cvp-perf'] ) :
                 version_mismatch.append("Job {0} has {1} branch."
-                                        "Expected {2}".format(job_instance.name,
+                                        "Expected {2}".format(job_name,
                                                               actual_version,
                                                               expected_version))
     assert len(version_mismatch) == 0, \
diff --git a/requirements.txt b/requirements.txt
index 3c11ee2..6d5d004 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,4 +2,4 @@
 requests
 flake8
 PyYAML
-jenkinsapi
+python-jenkins==0.4.11