Merge "attempt to get to flake8/hacking plugins"
diff --git a/etc/logging.conf.sample b/etc/logging.conf.sample
new file mode 100644
index 0000000..5c1ea5f
--- /dev/null
+++ b/etc/logging.conf.sample
@@ -0,0 +1,30 @@
+[loggers]
+keys=root
+
+[formatters]
+keys=normal,debug
+
+[handlers]
+keys=file,devel
+
+[logger_root]
+level=NOTSET
+handlers=file
+
+[handler_file]
+class=FileHandler
+level=DEBUG
+formatter=normal
+args=('tempest.log', 'w')
+
+[handler_devel]
+class=StreamHandler
+level=DEBUG
+formatter=debug
+args=(sys.stdout,)
+
+[formatter_normal]
+format=%(asctime)s %(levelname)s %(message)s
+
+[formatter_debug]
+format=%(asctime)s %(levelname)s %(module)s %(funcName)s %(message)s
diff --git a/run_tests.sh b/run_tests.sh
index 25b9729..6fcdd90 100755
--- a/run_tests.sh
+++ b/run_tests.sh
@@ -17,6 +17,8 @@
   echo "  -h, --help               Print this usage message"
   echo "  -d, --debug              Debug this script -- set -o xtrace"
   echo "  -S, --stdout             Don't capture stdout"
+  echo "  -l, --logging            Enable logging"
+  echo "  -L, --logging-config     Logging config file location.  Default is etc/logging.conf"
   echo "  -- [NOSEOPTIONS]         After the first '--' you can pass arbitrary arguments to nosetests "
 }
 
@@ -32,8 +34,10 @@
 nova_coverage=0
 config_file=""
 update=0
+logging=0
+logging_config=etc/logging.conf
 
-if ! options=$(getopt -o VNnfuswcphdSC: -l virtual-env,no-virtual-env,no-site-packages,force,update,smoke,whitebox,nova-coverage,pep8,help,debug,stdout,config: -- "$@")
+if ! options=$(getopt -o VNnfuswcphdSC:lL: -l virtual-env,no-virtual-env,no-site-packages,force,update,smoke,whitebox,nova-coverage,pep8,help,debug,stdout,config:,logging,logging-config: -- "$@")
 then
     # parse error
     usage
@@ -57,6 +61,8 @@
     -s|--smoke) noseargs="$noseargs --attr=type=smoke";;
     -w|--whitebox) noseargs="$noseargs --attr=type=whitebox";;
     -S|--stdout) noseargs="$noseargs -s";;
+    -l|--logging) logging=1;;
+    -L|--logging-config) logging_config=$2; shift;;
     --) [ "yes" == "$first_uu" ] || noseargs="$noseargs $1"; first_uu=no  ;;
     *) noseargs="$noseargs $1"
   esac
@@ -78,6 +84,14 @@
 export NOSE_OPENSTACK_SHOW_ELAPSED=1
 export NOSE_OPENSTACK_STDOUT=1
 
+if [ $logging -eq 1 ]; then
+    if [ ! -f "$logging_config" ]; then
+        echo "No such logging config file: $logging_config"
+        exit
+    fi
+    noseargs="$noseargs --logging-config=$logging_config"
+fi
+
 if [ $no_site_packages -eq 1 ]; then
   installvenvopts="--no-site-packages"
 fi
diff --git a/tempest/clients.py b/tempest/clients.py
index 38334d7..8f8d5e6 100644
--- a/tempest/clients.py
+++ b/tempest/clients.py
@@ -20,6 +20,8 @@
 from tempest import config
 from tempest import exceptions
 from tempest.services import botoclients
+from tempest.services.compute.json.aggregates_client import \
+    AggregatesClientJSON
 from tempest.services.compute.json.extensions_client import \
     ExtensionsClientJSON
 from tempest.services.compute.json.fixed_ips_client import FixedIPsClientJSON
@@ -251,6 +253,7 @@
         self.custom_object_client = ObjectClientCustomizedHeader(*client_args)
         self.custom_account_client = \
             AccountClientCustomizedHeader(*client_args)
+        self.aggregates_client = AggregatesClientJSON(*client_args)
 
 
 class AltManager(Manager):
diff --git a/tempest/services/compute/json/aggregates_client.py b/tempest/services/compute/json/aggregates_client.py
new file mode 100644
index 0000000..7ae1eee
--- /dev/null
+++ b/tempest/services/compute/json/aggregates_client.py
@@ -0,0 +1,86 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 NEC Corporation.
+# All Rights Reserved.
+#
+#    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 json
+
+from tempest.common.rest_client import RestClient
+from tempest import exceptions
+
+
+class AggregatesClientJSON(RestClient):
+
+    def __init__(self, config, username, password, auth_url, tenant_name=None):
+        super(AggregatesClientJSON, self).__init__(config, username, password,
+                                                   auth_url, tenant_name)
+        self.service = self.config.compute.catalog_type
+
+    def list_aggregates(self):
+        """Get aggregate list."""
+        resp, body = self.get("os-aggregates")
+        body = json.loads(body)
+        return resp, body['aggregates']
+
+    def get_aggregate(self, aggregate_id):
+        """Get details of the given aggregate."""
+        resp, body = self.get("os-aggregates/%s" % str(aggregate_id))
+        body = json.loads(body)
+        return resp, body['aggregate']
+
+    def create_aggregate(self, name, availability_zone=None):
+        """Creates a new aggregate."""
+        post_body = {
+            'name': name,
+            'availability_zone': availability_zone,
+        }
+        post_body = json.dumps({'aggregate': post_body})
+        resp, body = self.post('os-aggregates', post_body, self.headers)
+
+        body = json.loads(body)
+        return resp, body['aggregate']
+
+    def delete_aggregate(self, aggregate_id):
+        """Deletes the given aggregate."""
+        return self.delete("os-aggregates/%s" % str(aggregate_id))
+
+    def is_resource_deleted(self, id):
+        try:
+            self.get_aggregate(id)
+        except exceptions.NotFound:
+            return True
+        return False
+
+    def add_host(self, aggregate_id, host):
+        """Adds a host to the given aggregate."""
+        post_body = {
+            'host': host,
+        }
+        post_body = json.dumps({'add_host': post_body})
+        resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
+                               post_body, self.headers)
+        body = json.loads(body)
+        return resp, body['aggregate']
+
+    def remove_host(self, aggregate_id, host):
+        """Removes a host from the given aggregate."""
+        post_body = {
+            'host': host,
+        }
+        post_body = json.dumps({'remove_host': post_body})
+        resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
+                               post_body, self.headers)
+        body = json.loads(body)
+        return resp, body['aggregate']
diff --git a/tempest/tests/compute/admin/test_aggregates.py b/tempest/tests/compute/admin/test_aggregates.py
new file mode 100644
index 0000000..06acc41
--- /dev/null
+++ b/tempest/tests/compute/admin/test_aggregates.py
@@ -0,0 +1,256 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 NEC Corporation.
+# All Rights Reserved.
+#
+#    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.
+
+from tempest.common.utils.data_utils import rand_name
+from tempest import exceptions
+from tempest.test import attr
+from tempest.tests.compute import base
+
+
+class AggregatesAdminTestJSON(base.BaseComputeAdminTest):
+
+    """
+    Tests Aggregates API that require admin privileges
+    """
+
+    _interface = 'json'
+
+    @classmethod
+    def setUpClass(cls):
+        super(AggregatesAdminTestJSON, cls).setUpClass()
+        cls.client = cls.os_adm.aggregates_client
+        cls.user_client = cls.os.aggregates_client
+        cls.aggregate_name_prefix = 'test_aggregate_'
+        cls.az_name_prefix = 'test_az_'
+
+        resp, hosts_all = cls.os_adm.hosts_client.list_hosts()
+        hosts = map(lambda x: x['host_name'],
+                    filter(lambda y: y['service'] == 'compute', hosts_all))
+        cls.host = hosts[0]
+
+    @attr(type='positive')
+    def test_aggregate_create_delete(self):
+        # Create and delete an aggregate.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.assertEquals(200, resp.status)
+        self.assertEquals(aggregate_name, aggregate['name'])
+        self.assertEquals(None, aggregate['availability_zone'])
+
+        resp, _ = self.client.delete_aggregate(aggregate['id'])
+        self.assertEquals(200, resp.status)
+        self.client.wait_for_resource_deletion(aggregate['id'])
+
+    @attr(type='positive')
+    def test_aggregate_create_delete_with_az(self):
+        # Create and delete an aggregate.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        az_name = rand_name(self.az_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name, az_name)
+        self.assertEquals(200, resp.status)
+        self.assertEquals(aggregate_name, aggregate['name'])
+        self.assertEquals(az_name, aggregate['availability_zone'])
+
+        resp, _ = self.client.delete_aggregate(aggregate['id'])
+        self.assertEquals(200, resp.status)
+        self.client.wait_for_resource_deletion(aggregate['id'])
+
+    @attr(type='positive')
+    def test_aggregate_create_verify_entry_in_list(self):
+        # Create an aggregate and ensure it is listed.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+
+        resp, aggregates = self.client.list_aggregates()
+        self.assertEquals(200, resp.status)
+        self.assertIn((aggregate['id'], aggregate['availability_zone']),
+                      map(lambda x: (x['id'], x['availability_zone']),
+                          aggregates))
+
+    @attr(type='positive')
+    def test_aggregate_create_get_details(self):
+        # Create an aggregate and ensure its details are returned.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+
+        resp, body = self.client.get_aggregate(aggregate['id'])
+        self.assertEquals(200, resp.status)
+        self.assertEquals(aggregate['name'], body['name'])
+        self.assertEquals(aggregate['availability_zone'],
+                          body['availability_zone'])
+
+    @attr(type='negative')
+    def test_aggregate_create_as_user(self):
+        # Regular user is not allowed to create an aggregate.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        self.assertRaises(exceptions.Unauthorized,
+                          self.user_client.create_aggregate,
+                          aggregate_name)
+
+    @attr(type='negative')
+    def test_aggregate_delete_as_user(self):
+        # Regular user is not allowed to delete an aggregate.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+
+        self.assertRaises(exceptions.Unauthorized,
+                          self.user_client.delete_aggregate,
+                          aggregate['id'])
+
+    @attr(type='negative')
+    def test_aggregate_list_as_user(self):
+        # Regular user is not allowed to list aggregates.
+        self.assertRaises(exceptions.Unauthorized,
+                          self.user_client.list_aggregates)
+
+    @attr(type='negative')
+    def test_aggregate_get_details_as_user(self):
+        # Regular user is not allowed to get aggregate details.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+
+        self.assertRaises(exceptions.Unauthorized,
+                          self.user_client.get_aggregate,
+                          aggregate['id'])
+
+    @attr(type='negative')
+    def test_aggregate_delete_with_invalid_id(self):
+        # Delete an aggregate with invalid id should raise exceptions.
+        self.assertRaises(exceptions.NotFound,
+                          self.client.delete_aggregate, -1)
+
+    @attr(type='negative')
+    def test_aggregate_get_details_with_invalid_id(self):
+        # Get aggregate details with invalid id should raise exceptions.
+        self.assertRaises(exceptions.NotFound,
+                          self.client.get_aggregate, -1)
+
+    @attr(type='positive')
+    def test_aggregate_add_remove_host(self):
+        # Add an host to the given aggregate and remove.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+
+        resp, body = self.client.add_host(aggregate['id'], self.host)
+        self.assertEquals(200, resp.status)
+        self.assertEquals(aggregate_name, body['name'])
+        self.assertEquals(aggregate['availability_zone'],
+                          body['availability_zone'])
+        self.assertIn(self.host, body['hosts'])
+
+        resp, body = self.client.remove_host(aggregate['id'], self.host)
+        self.assertEquals(200, resp.status)
+        self.assertEquals(aggregate_name, body['name'])
+        self.assertEquals(aggregate['availability_zone'],
+                          body['availability_zone'])
+        self.assertNotIn(self.host, body['hosts'])
+
+    @attr(type='positive')
+    def test_aggregate_add_host_list(self):
+        # Add an host to the given aggregate and list.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+        self.client.add_host(aggregate['id'], self.host)
+        self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
+
+        resp, aggregates = self.client.list_aggregates()
+        aggs = filter(lambda x: x['id'] == aggregate['id'], aggregates)
+        self.assertEquals(1, len(aggs))
+        agg = aggs[0]
+        self.assertEquals(aggregate_name, agg['name'])
+        self.assertEquals(None, agg['availability_zone'])
+        self.assertIn(self.host, agg['hosts'])
+
+    @attr(type='positive')
+    def test_aggregate_add_host_get_details(self):
+        # Add an host to the given aggregate and get details.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+        self.client.add_host(aggregate['id'], self.host)
+        self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
+
+        resp, body = self.client.get_aggregate(aggregate['id'])
+        self.assertEquals(aggregate_name, body['name'])
+        self.assertEquals(None, body['availability_zone'])
+        self.assertIn(self.host, body['hosts'])
+
+    @attr(type='positive')
+    def test_aggregate_add_host_create_server_with_az(self):
+        # Add an host to the given aggregate and create a server.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        az_name = rand_name(self.az_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name, az_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+        self.client.add_host(aggregate['id'], self.host)
+        self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
+
+        server_name = rand_name('test_server_')
+        servers_client = self.servers_client
+        admin_servers_client = self.os_adm.servers_client
+        resp, server = self.create_server(name=server_name,
+                                          availability_zone=az_name)
+        servers_client.wait_for_server_status(server['id'], 'ACTIVE')
+        resp, body = admin_servers_client.get_server(server['id'])
+        self.assertEqual(self.host, body['OS-EXT-SRV-ATTR:host'])
+
+    @attr(type='negative')
+    def test_aggregate_add_non_exist_host(self):
+        # Adding a non-exist host to an aggregate should raise exceptions.
+        resp, hosts_all = self.os_adm.hosts_client.list_hosts()
+        hosts = map(lambda x: x['host_name'], hosts_all)
+        while True:
+            non_exist_host = rand_name('nonexist_host_')
+            if non_exist_host not in hosts:
+                break
+
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+
+        self.assertRaises(exceptions.NotFound, self.client.add_host,
+                          aggregate['id'], non_exist_host)
+
+    @attr(type='negative')
+    def test_aggregate_add_host_as_user(self):
+        # Regular user is not allowed to add a host to an aggregate.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+
+        self.assertRaises(exceptions.Unauthorized,
+                          self.user_client.add_host,
+                          aggregate['id'], self.host)
+
+    @attr(type='negative')
+    def test_aggregate_remove_host_as_user(self):
+        # Regular user is not allowed to remove a host from an aggregate.
+        aggregate_name = rand_name(self.aggregate_name_prefix)
+        resp, aggregate = self.client.create_aggregate(aggregate_name)
+        self.addCleanup(self.client.delete_aggregate, aggregate['id'])
+        self.client.add_host(aggregate['id'], self.host)
+        self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
+
+        self.assertRaises(exceptions.Unauthorized,
+                          self.user_client.remove_host,
+                          aggregate['id'], self.host)