Merge "Fix ips in ceph prx"
diff --git a/tcp_tests/managers/saltmanager.py b/tcp_tests/managers/saltmanager.py
index 8ef817a..1e1640f 100644
--- a/tcp_tests/managers/saltmanager.py
+++ b/tcp_tests/managers/saltmanager.py
@@ -167,6 +167,24 @@
         result = self.local(tgt=tgt, fun='pillar.get', args=pillar)
         return result['return']
 
+    def get_single_pillar(self, tgt, pillar):
+        """Get a scalar value from a single node
+
+        :return: pillar value
+        """
+
+        result = self.get_pillar(tgt, pillar)
+        nodes = result[0].keys()
+
+        if not nodes:
+            raise LookupError("No minions selected "
+                              "for the target '{0}'".format(tgt))
+        if len(nodes) > 1:
+            raise LookupError("Too many minions selected "
+                              "for the target '{0}' , expected one: {1}"
+                              .format(tgt, nodes))
+        return result[0][nodes[0]]
+
     def get_grains(self, tgt, grains):
         result = self.local(tgt=tgt, fun='grains.get', args=grains)
         return result['return']
diff --git a/tcp_tests/tests/system/test_cvp_pipelines.py b/tcp_tests/tests/system/test_cvp_pipelines.py
new file mode 100644
index 0000000..5d2e060
--- /dev/null
+++ b/tcp_tests/tests/system/test_cvp_pipelines.py
@@ -0,0 +1,149 @@
+#    Copyright 2016 Mirantis, Inc.
+#
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+import pytest
+
+from tcp_tests import logger
+from tcp_tests import settings
+from tcp_tests.utils import run_jenkins_job
+from tcp_tests.utils import get_jenkins_job_stages
+
+LOG = logger.logger
+
+
+class TestCvpPipelines(object):
+    """Test class for running Cloud Validation Pipelines
+
+    Requires environment variables:
+      ENV_NAME
+      LAB_CONFIG_NAME
+      TESTS_CONFIGS
+    """
+
+    @pytest.mark.grab_versions
+    @pytest.mark.parametrize("_", [settings.ENV_NAME])
+    @pytest.mark.run_cvp_func_smoke
+    def test_run_cvp_func_smoke(self, salt_actions, show_step, _):
+        """Runner for Pipeline CVP - Functional tests
+
+        Scenario:
+            1. Get CICD Jenkins access credentials from salt
+            2. Run job cvp-func
+            3. Get passed stages from cvp-func
+        """
+        salt = salt_actions
+        show_step(1)
+
+        tgt = 'I@docker:client:stack:jenkins and cid01*'
+        jenkins_host = salt.get_single_pillar(
+            tgt=tgt, pillar="jenkins:client:master:host")
+        jenkins_port = salt.get_single_pillar(
+            tgt=tgt, pillar="jenkins:client:master:port")
+        jenkins_url = 'http://{0}:{1}'.format(jenkins_host, jenkins_port)
+        jenkins_user = salt.get_single_pillar(
+            tgt=tgt, pillar="jenkins:client:master:username")
+        jenkins_pass = salt.get_single_pillar(
+            tgt=tgt, pillar="jenkins:client:master:password")
+        jenkins_start_timeout = 60
+        jenkins_build_timeout = 1800
+
+        job_name = 'cvp-func'
+        job_parameters = {
+            'TARGET_NODE': 'gtw01*',
+            'TEMPEST_ENDPOINT_TYPE': 'internalURL',
+            'TEMPEST_TEST_PATTERN': 'set=smoke',
+        }
+        show_step(2)
+        cvp_func_smoke_result = run_jenkins_job.run_job(
+            host=jenkins_url,
+            username=jenkins_user,
+            password=jenkins_pass,
+            start_timeout=jenkins_start_timeout,
+            build_timeout=jenkins_build_timeout,
+            verbose=True,
+            job_name=job_name,
+            job_parameters=job_parameters,
+            job_output_prefix='[ cvp-func/{build_number}:platform {time} ] ')
+
+        show_step(3)
+        (description, stages) = get_jenkins_job_stages.get_deployment_result(
+            host=jenkins_url,
+            username=jenkins_user,
+            password=jenkins_pass,
+            job_name=job_name,
+            build_number='lastBuild')
+
+        LOG.info(description)
+        LOG.info('\n'.join(stages))
+
+        assert cvp_func_smoke_result == 'SUCCESS', "{0}\n{1}".format(
+            description, '\n'.join(stages))
+
+    @pytest.mark.grab_versions
+    @pytest.mark.parametrize("_", [settings.ENV_NAME])
+    @pytest.mark.run_cvp_func_sanity
+    def test_run_cvp_func_sanity(self, salt_actions, show_step, _):
+        """Runner for Pipeline CVP - Functional tests
+
+        Scenario:
+            1. Get CICD Jenkins access credentials from salt
+            2. Run job cvp-sanity
+            3. Get passed stages from cvp-sanity
+        """
+        salt = salt_actions
+        show_step(1)
+
+        tgt = 'I@docker:client:stack:jenkins and cid01*'
+        jenkins_host = salt.get_single_pillar(
+            tgt=tgt, pillar="jenkins:client:master:host")
+        jenkins_port = salt.get_single_pillar(
+            tgt=tgt, pillar="jenkins:client:master:port")
+        jenkins_url = 'http://{0}:{1}'.format(jenkins_host, jenkins_port)
+        jenkins_user = salt.get_single_pillar(
+            tgt=tgt, pillar="jenkins:client:master:username")
+        jenkins_pass = salt.get_single_pillar(
+            tgt=tgt, pillar="jenkins:client:master:password")
+        jenkins_start_timeout = 60
+        jenkins_build_timeout = 1800
+
+        job_name = 'cvp-sanity'
+        job_parameters = {
+            'TEST_SET': '/var/lib/cvp-sanity/cvp_checks/tests/',
+        }
+
+        show_step(2)
+        cvp_func_sanity_result = run_jenkins_job.run_job(
+            host=jenkins_url,
+            username=jenkins_user,
+            password=jenkins_pass,
+            start_timeout=jenkins_start_timeout,
+            build_timeout=jenkins_build_timeout,
+            verbose=True,
+            job_name=job_name,
+            job_parameters=job_parameters,
+            job_output_prefix='[ cvp-func/{build_number}:platform {time} ] ')
+
+        show_step(3)
+        (description, stages) = get_jenkins_job_stages.get_deployment_result(
+            host=jenkins_url,
+            username=jenkins_user,
+            password=jenkins_pass,
+            job_name=job_name,
+            build_number='lastBuild')
+
+        LOG.info(description)
+        LOG.info('\n'.join(stages))
+
+        assert cvp_func_sanity_result == 'SUCCESS', "{0}\n{1}".format(
+            description, '\n'.join(stages))
diff --git a/tcp_tests/utils/get_jenkins_job_stages.py b/tcp_tests/utils/get_jenkins_job_stages.py
index 883494f..b28e1d2 100755
--- a/tcp_tests/utils/get_jenkins_job_stages.py
+++ b/tcp_tests/utils/get_jenkins_job_stages.py
@@ -69,15 +69,15 @@
     return parser
 
 
-def get_deployment_result(opts):
+def get_deployment_result(host, username, password, job_name, build_number):
     """Get the pipeline job result from Jenkins
 
     Get all the stages resutls from the specified job,
     show error message if present.
     """
-    jenkins = client.JenkinsClient(host=opts.host,
-                                   username=opts.username,
-                                   password=opts.password)
+    jenkins = client.JenkinsClient(host=host,
+                                   username=username,
+                                   password=password)
 
     def get_stages(nodes, indent=0, show_status=True):
         res = []
@@ -90,15 +90,15 @@
                 res.append(msg)
 
             if node['status'] != 'SUCCESS':
-                wf = jenkins.get_workflow(opts.job_name, opts.build_number,
+                wf = jenkins.get_workflow(job_name, build_number,
                                           int(node['id']))
                 if wf is not None:
                     if 'stageFlowNodes' in wf:
                         res += get_stages(wf['stageFlowNodes'], indent + 2,
                                           show_status=False)
                     elif '_links' in wf and 'log' in wf['_links']:
-                        log = jenkins.get_workflow(opts.job_name,
-                                                   opts.build_number,
+                        log = jenkins.get_workflow(job_name,
+                                                   build_number,
                                                    int(node['id']),
                                                    mode='log')
                         if "text" in log:
@@ -109,8 +109,8 @@
         return res
 
     for _ in range(3):
-        wf = jenkins.get_workflow(opts.job_name, opts.build_number)
-        info = jenkins.build_info(opts.job_name, int(wf['id']))
+        wf = jenkins.get_workflow(job_name, build_number)
+        info = jenkins.build_info(job_name, int(wf['id']))
         if info.get('result'):
             break
         time.sleep(3)
@@ -120,7 +120,7 @@
     stages = get_stages(wf['stages'], 0)
     if not stages:
         msg = wf['status'] + ":\n\n"
-        stages = [msg + jenkins.get_build_output(opts.job_name, int(wf['id']))]
+        stages = [msg + jenkins.get_build_output(job_name, int(wf['id']))]
     return (build_description, stages)
 
 
@@ -133,7 +133,12 @@
         parser.print_help()
         return 10
     else:
-        (build_description, stages) = get_deployment_result(opts)
+        (build_description, stages) = get_deployment_result(
+            opts.host,
+            opts.username,
+            opts.password,
+            opts.job_name,
+            opts.build_number)
         print(build_description)
         print('\n'.join(stages))
 
diff --git a/tcp_tests/utils/run_jenkins_job.py b/tcp_tests/utils/run_jenkins_job.py
index acc2e9f..e0e7f69 100755
--- a/tcp_tests/utils/run_jenkins_job.py
+++ b/tcp_tests/utils/run_jenkins_job.py
@@ -97,10 +97,10 @@
     return parser
 
 
-def print_build_header(build, job_params, opts):
+def print_build_header(build, job_params, build_timeout):
     print('\n#############################################################')
     print('##### Building job [{0}] #{1} (timeout={2}) with the following '
-          'parameters:'.format(build[0], build[1], opts.build_timeout))
+          'parameters:'.format(build[0], build[1], build_timeout))
     print('##### ' + '\n##### '.join(
         [str(key) + ": " + str(val) for key, val in job_params.iteritems()]
     ))
@@ -114,41 +114,44 @@
     print('#############################################################\n')
 
 
-def run_job(opts):
+def run_job(host, username, password,
+            job_name, job_parameters=None, job_output_prefix='',
+            start_timeout=1800, build_timeout=3600 * 4, verbose=False):
 
     jenkins = JenkinsClient(
-        host=opts.host,
-        username=opts.username,
-        password=opts.password)
+        host=host,
+        username=username,
+        password=password)
 
-    job_params = jenkins.make_defults_params(opts.job_name)
-    job_params.update(opts.job_parameters)
+    job_params = jenkins.make_defults_params(job_name)
+    if job_parameters is not None:  # job_parameters = {}
+        job_params.update(job_parameters)
 
-    build = jenkins.run_build(opts.job_name,
+    build = jenkins.run_build(job_name,
                               job_params,
-                              verbose=opts.verbose,
-                              timeout=opts.start_timeout)
-    if opts.verbose:
-        print_build_header(build, job_params, opts)
+                              verbose=verbose,
+                              timeout=start_timeout)
+    if verbose:
+        print_build_header(build, job_params, build_timeout)
 
     try:
         jenkins.wait_end_of_build(
             name=build[0],
             build_id=build[1],
-            timeout=opts.build_timeout,
+            timeout=build_timeout,
             interval=1,
-            verbose=opts.verbose,
-            job_output_prefix=opts.job_output_prefix)
+            verbose=verbose,
+            job_output_prefix=job_output_prefix)
     except Exception as e:
         print(str(e))
         raise
 
     result = jenkins.build_info(name=build[0],
                                 build_id=build[1])['result']
-    if opts.verbose:
-        print_build_footer(build, result, opts.host)
+    if verbose:
+        print_build_footer(build, result, host)
 
-    return EXIT_CODES.get(result, 2)
+    return result
 
 
 def main(args=None):
@@ -160,8 +163,17 @@
         parser.print_help()
         return 10
     else:
-        exit_code = run_job(opts)
-        return exit_code
+        result = run_job(
+            opts.host,
+            opts.username,
+            opts.password,
+            opts.job_name,
+            opts.job_parameters,
+            opts.job_output_prefix,
+            opts.start_timeout,
+            opts.build_timeout,
+            opts.verbose)
+        return EXIT_CODES.get(result, 2)
 
 
 if __name__ == "__main__":