Merge "Add proper skip mechanism for Stacklight component"
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..546cf31
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,22 @@
+FROM ubuntu:16.04
+
+USER root
+
+WORKDIR /var/lib/
+
+RUN mkdir -p cvp-sanity-checks/
+
+COPY . cvp-sanity-checks/
+
+RUN apt-get update && \
+    apt-get install -y python-pip git curl wget vim inetutils-ping && \
+    python -m pip install --upgrade pip && \
+    pip install -r cvp-sanity-checks/requirements.txt && \
+    apt-get -y autoremove; apt-get -y clean
+
+RUN rm -rf /root/.cache && \
+    rm -rf /var/lib/apt/lists/* && \
+    rm -rf /tmp/* && \
+    rm -rf /var/tmp/*
+
+ENTRYPOINT ["/bin/bash"]
diff --git a/cvp_checks/global_config.yaml b/cvp_checks/global_config.yaml
index 72e6980..10a3d75 100644
--- a/cvp_checks/global_config.yaml
+++ b/cvp_checks/global_config.yaml
@@ -65,8 +65,11 @@
     "skipped_ifaces": ["bonding_masters", "lo", "veth", "tap", "cali", "qv", "qb", "br-int", "vxlan"]}
 # mask for interfaces to skip
 
+# specify what mcp version (tag) is deployed
 drivetrain_version: ''
-jenkins_test_job: ''
+
+# jenkins job to run during the test
+jenkins_test_job: 'git-mirror-downstream-mk-pipelines'
 
 # 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 8358e54..ae83d57 100644
--- a/cvp_checks/tests/test_drivetrain.py
+++ b/cvp_checks/tests/test_drivetrain.py
@@ -3,7 +3,27 @@
 from cvp_checks import utils
 import json
 import pytest
-from time import sleep
+import time
+import os
+from pygerrit2 import GerritRestAPI, HTTPBasicAuth
+from requests import HTTPError
+import git
+
+def join_to_gerrit(local_salt_client, gerrit_user, gerrit_password):
+    gerrit_port = local_salt_client.cmd(
+        'I@gerrit:client and not I@salt:master',
+        'pillar.get',
+        ['_param:haproxy_gerrit_bind_port'],
+        expr_form='compound').values()[0]
+    gerrit_address = local_salt_client.cmd(
+        'I@gerrit:client and not I@salt:master',
+        'pillar.get',
+        ['_param:haproxy_gerrit_bind_host'],
+        expr_form='compound').values()[0]
+    url = 'http://{0}:{1}'.format(gerrit_address,gerrit_port)
+    auth = HTTPBasicAuth(gerrit_user, gerrit_password)
+    rest = GerritRestAPI(url=url, auth=auth)
+    return rest
 
 def join_to_jenkins(local_salt_client, jenkins_user, jenkins_password):
     jenkins_port = local_salt_client.cmd(
@@ -20,12 +40,76 @@
     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',
+def get_password(local_salt_client,service):
+    password = local_salt_client.cmd(
+        service,
         'pillar.get',
         ['_param:openldap_admin_password'],
         expr_form='pillar').values()[0]
+    return password
+
+def test_drivetrain_gerrit(local_salt_client):
+    gerrit_password = get_password(local_salt_client,'gerrit:client')
+    gerrit_error = ''
+    current_date = time.strftime("%Y%m%d-%H.%M.%S", time.localtime())
+    test_proj_name = "test-dt-{0}".format(current_date)
+    gerrit_port = local_salt_client.cmd(
+        'I@gerrit:client and not I@salt:master',
+        'pillar.get',
+        ['_param:haproxy_gerrit_bind_port'],
+        expr_form='compound').values()[0]
+    gerrit_address = local_salt_client.cmd(
+        'I@gerrit:client and not I@salt:master',
+        'pillar.get',
+        ['_param:haproxy_gerrit_bind_host'],
+        expr_form='compound').values()[0]
+    try:
+        #Connecting to gerrit and check connection
+        server = join_to_gerrit(local_salt_client,'admin',gerrit_password)
+        gerrit_check = server.get("/changes/?q=owner:self%20status:open")
+        #Check deleteproject plugin and skip test if the plugin is not installed
+        gerrit_plugins = server.get("/plugins/?all")
+        if 'deleteproject' not in gerrit_plugins:
+            pytest.skip("Delete-project plugin is not installed")
+        #Create test project and add description
+        server.put("/projects/"+test_proj_name)
+        server.put("/projects/"+test_proj_name+"/description",json={"description":"Test DriveTrain project","commit_message": "Update the project description"})
+    except HTTPError, e:
+        gerrit_error = e
+    try:
+        #Create test folder and init git
+        repo_dir = os.path.join(os.getcwd(),test_proj_name)
+        file_name = os.path.join(repo_dir, current_date)
+        repo = git.Repo.init(repo_dir)
+        #Add remote url for this git repo
+        origin = repo.create_remote('origin', 'http://admin:{1}@{2}:{3}/{0}.git'.format(test_proj_name,gerrit_password,gerrit_address,gerrit_port))
+        #Add commit-msg hook to automatically add Change-Id to our commit
+        os.system("curl -Lo {0}/.git/hooks/commit-msg 'http://admin:{1}@{2}:{3}/tools/hooks/commit-msg' > /dev/null 2>&1".format(repo_dir,gerrit_password,gerrit_address,gerrit_port))
+        os.system("chmod u+x {0}/.git/hooks/commit-msg".format(repo_dir))
+        #Create a test file
+        f = open(file_name, 'w+')
+        f.write("This is a test file for DriveTrain test")
+        f.close()
+        #Add file to git and commit it to Gerrit for review
+        repo.index.add([file_name])
+        repo.index.commit("This is a test commit for DriveTrain test")
+        repo.git.push("origin", "HEAD:refs/for/master")
+        #Get change id from Gerrit. Set Code-Review +2 and submit this change
+        changes = server.get("/changes/?q=project:{0}".format(test_proj_name))
+        last_change = changes[0].get('change_id')
+        server.post("/changes/{0}/revisions/1/review".format(last_change),json={"message":"All is good","labels":{"Code-Review":"+2"}})
+        server.post("/changes/{0}/submit".format(last_change))
+    except HTTPError, e:
+        gerrit_error = e
+    finally:
+        #Delete test project
+        server.post("/projects/"+test_proj_name+"/deleteproject~delete")
+    assert gerrit_error == '',\
+        'Something is wrong with Gerrit'.format(gerrit_error)
+
+
+def test_drivetrain_jenkins_job(local_salt_client):
+    jenkins_password = get_password(local_salt_client,'jenkins:client')
     server = join_to_jenkins(local_salt_client,'admin',jenkins_password)
     #Getting Jenkins test job name from configuration
     config = utils.get_configuration()
@@ -50,7 +134,7 @@
         #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)
+            time.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']
@@ -112,11 +196,7 @@
     expected_version = config['drivetrain_version'] or []
     if not expected_version or expected_version == '':
         pytest.skip("drivetrain_version is not defined. Skipping")
-    jenkins_password = local_salt_client.cmd(
-        'jenkins:client',
-        'pillar.get',
-        ['_param:openldap_admin_password'],
-        expr_form='pillar').values()[0]
+    jenkins_password = get_password(local_salt_client,'jenkins:client')
     version_mismatch = []
     server = join_to_jenkins(local_salt_client,'admin',jenkins_password)
     for job_instance in server.get_jobs():
diff --git a/cvp_checks/tests/test_ui_addresses.py b/cvp_checks/tests/test_ui_addresses.py
index ee02232..15c9068 100644
--- a/cvp_checks/tests/test_ui_addresses.py
+++ b/cvp_checks/tests/test_ui_addresses.py
@@ -61,7 +61,7 @@
 
 @pytest.mark.usefixtures('check_grafana')
 def test_ui_grafana(local_salt_client):
-    IP = utils.get_monitoring_ip('cluster_public_host')
+    IP = utils.get_monitoring_ip('stacklight_monitor_address')
     result = local_salt_client.cmd(
         'keystone:server',
         'cmd.run',
diff --git a/requirements.txt b/requirements.txt
index 6d5d004..38b0da6 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,5 +1,7 @@
 pytest==3.0.6
-requests
+requests==2.10.0
 flake8
 PyYAML
 python-jenkins==0.4.11
+pygerrit2==2.0.6
+gitpython