Add autoscaling scenario in tempest tests

This change runs the gabbi integration tests with a tempest plugin.

Depends-On: I453b63972ec06e5d86535c4a84080e9f5dfe9196
Change-Id: If8410f8fe53f1dd29018d019d6549f45cc4ae681
diff --git a/ceilometer/tests/integration/hooks/post_test_hook.sh b/ceilometer/tests/integration/hooks/post_test_hook.sh
index fbc69a7..b6822af 100755
--- a/ceilometer/tests/integration/hooks/post_test_hook.sh
+++ b/ceilometer/tests/integration/hooks/post_test_hook.sh
@@ -46,10 +46,29 @@
 export GLANCE_IMAGE_NAME=$(openstack image list | awk '/ cirros.*uec /{print $4}')
 export ADMIN_TOKEN=$(openstack token issue -c id -f value)
 
-# Run tests
+if [ -d $BASE/new/devstack ]; then
+    # NOTE(sileht): on swift job permissions are wrong, I don't known why
+    sudo chown -R tempest:stack $BASE/new/tempest
+    sudo chown -R tempest:stack $BASE/data/tempest
+
+    # Run tests with tempest
+    cd $BASE/new/tempest
+    set +e
+    sudo -H -u tempest OS_TEST_TIMEOUT=$TEMPEST_OS_TEST_TIMEOUT tox -eall-plugin -- ceilometer.tests.tempest.scenario.test_autoscaling --concurrency=$TEMPEST_CONCURRENCY
+    TEMPEST_EXIT_CODE=$?
+    set -e
+    if [[ $TEMPEST_EXIT_CODE != 0 ]]; then
+        # Collect and parse result
+        generate_testr_results
+        exit $TEMPEST_EXIT_CODE
+    fi
+
+    cd $CEILOMETER_DIR
+fi
+
+# Run tests with gabbi
 echo "Running telemetry integration test suite"
 set +e
-
 sudo -E -H -u ${STACK_USER:-${USER}} tox -eintegration
 EXIT_CODE=$?
 
diff --git a/ceilometer/tests/tempest/scenario/test_autoscaling.py b/ceilometer/tests/tempest/scenario/test_autoscaling.py
new file mode 100644
index 0000000..f609363
--- /dev/null
+++ b/ceilometer/tests/tempest/scenario/test_autoscaling.py
@@ -0,0 +1,109 @@
+#    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 os
+import unittest
+
+from gabbi import driver
+from tempest import config
+from tempest import test
+
+from ceilometer.tests.tempest.service import client
+
+
+class ClientManager(client.Manager):
+    load_clients = [
+        'image_client_v2',
+    ]
+
+
+class TestAutoscalingGabbi(test.BaseTestCase):
+    credentials = ['admin']
+    client_manager = ClientManager
+
+    @classmethod
+    def skip_checks(cls):
+        super(TestAutoscalingGabbi, cls).skip_checks()
+        for name in ["aodh_plugin", "gnocchi", "nova", "heat",
+                     "ceilometer", "glance"]:
+            cls._check_service(name)
+
+    @classmethod
+    def _check_service(cls, name):
+        if not getattr(config.CONF.service_available, name, False):
+            raise cls.skipException("%s support is required" %
+                                    name.capitalize())
+
+    @classmethod
+    def resource_setup(cls):
+        super(TestAutoscalingGabbi, cls).resource_setup()
+        test_dir = os.path.join(os.path.dirname(__file__), '..', '..',
+                                'integration', 'gabbi', 'gabbits-live')
+        cls.tests = driver.build_tests(
+            test_dir, unittest.TestLoader(),
+            host='localhost', port='13245',
+            test_loader_name='tempest.scenario.telemetry-autoscaling.test')
+
+        auth = cls.os_admin.auth_provider.get_auth()
+        os.environ["ADMIN_TOKEN"] = auth[0]
+        os.environ["AODH_SERVICE_URL"] = cls._get_endpoint_for(
+            auth, "alarming_plugin")
+        os.environ["GNOCCHI_SERVICE_URL"] = cls._get_endpoint_for(
+            auth, "metric")
+        os.environ["HEAT_SERVICE_URL"] = cls._get_endpoint_for(
+            auth, "orchestration")
+        os.environ["NOVA_SERVICE_URL"] = cls._get_endpoint_for(auth, "compute")
+        os.environ["GLANCE_SERVICE_URL"] = cls._get_endpoint_for(auth, "image")
+        images = cls.os_admin.image_client_v2.list_images()["images"]
+        for img in images:
+            name = img["name"]
+            if name.startswith("cirros") and name.endswith("-uec"):
+                os.environ["GLANCE_IMAGE_NAME"] = name
+                break
+        else:
+            cls.skipException("A cirros-.*-uec image is required")
+
+    @staticmethod
+    def clear_credentials():
+        # FIXME(sileht): We don't want the token to be invalided, but
+        # for some obcurs reason, clear_credentials is called before/during run
+        # So, make the one used by tearDropClass a dump, and call it manually
+        # in run()
+        pass
+
+    def run(self, result=None):
+        self.setUp()
+        try:
+            self.tests.run(result)
+        finally:
+            super(TestAutoscalingGabbi, self).clear_credentials()
+            self.tearDown()
+
+    @staticmethod
+    def _get_endpoint_for(auth, service):
+        opt_section = getattr(config.CONF, service)
+        endpoint_type = opt_section.endpoint_type
+        if not endpoint_type.endswith("URL"):
+            endpoint_type += "URL"
+
+        endpoints = [e for e in auth[1]['serviceCatalog']
+                     if e['type'] == opt_section.catalog_type]
+        if not endpoints:
+            raise Exception("%s endpoint not found" %
+                            config.CONF.metric.catalog_type)
+        return endpoints[0]['endpoints'][0][endpoint_type]
+
+    @staticmethod
+    def test_fake():
+        # NOTE(sileht): A fake test is needed to have the class loaded
+        # by the test runner
+        pass