Network load balancer testing

Support and test cases for network load balancer testing were missing.
I have added support for load balancer for JSON.
Till now I have added test cases of virtual IPs (vips) and some test
cases for pool.

Change-Id: I6ab258ce8a14ad0a16befbbdb06d7abab62ad004
diff --git a/tempest/api/network/base.py b/tempest/api/network/base.py
index 19c5f84..3ae718c 100644
--- a/tempest/api/network/base.py
+++ b/tempest/api/network/base.py
@@ -56,9 +56,15 @@
         cls.networks = []
         cls.subnets = []
         cls.ports = []
+        cls.pools = []
+        cls.vips = []
 
     @classmethod
     def tearDownClass(cls):
+        for vip in cls.vips:
+            cls.client.delete_vip(vip['id'])
+        for pool in cls.pools:
+            cls.client.delete_pool(pool['id'])
         for port in cls.ports:
             cls.client.delete_port(port['id'])
         for subnet in cls.subnets:
@@ -111,3 +117,21 @@
         port = body['port']
         cls.ports.append(port)
         return port
+
+    @classmethod
+    def create_pool(cls, name, lb_method, protocol, subnet):
+        """Wrapper utility that returns a test pool."""
+        resp, body = cls.client.create_pool(name, lb_method, protocol,
+                                            subnet['id'])
+        pool = body['pool']
+        cls.pools.append(pool)
+        return pool
+
+    @classmethod
+    def create_vip(cls, name, protocol, protocol_port, subnet, pool):
+        """Wrapper utility that returns a test vip."""
+        resp, body = cls.client.create_vip(name, protocol, protocol_port,
+                                           subnet['id'], pool['id'])
+        vip = body['vip']
+        cls.vips.append(vip)
+        return vip
diff --git a/tempest/api/network/test_load_balancer.py b/tempest/api/network/test_load_balancer.py
new file mode 100644
index 0000000..1c8c355
--- /dev/null
+++ b/tempest/api/network/test_load_balancer.py
@@ -0,0 +1,102 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2013 OpenStack, LLC
+# 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.api.network import base
+from tempest.common.utils.data_utils import rand_name
+from tempest.test import attr
+
+
+class LoadBalancerJSON(base.BaseNetworkTest):
+    _interface = 'json'
+
+    """
+    Tests the following operations in the Neutron API using the REST client for
+    Neutron:
+
+        create vIP, and Pool
+        show vIP
+        list vIP
+        update vIP
+        delete vIP
+        update pool
+        delete pool
+    """
+
+    @classmethod
+    def setUpClass(cls):
+        super(LoadBalancerJSON, cls).setUpClass()
+        cls.network = cls.create_network()
+        cls.name = cls.network['name']
+        cls.subnet = cls.create_subnet(cls.network)
+        pool_name = rand_name('pool-')
+        vip_name = rand_name('vip-')
+        cls.pool = cls.create_pool(pool_name, "ROUND_ROBIN",
+                                   "HTTP", cls.subnet)
+        cls.vip = cls.create_vip(vip_name, "HTTP", 80, cls.subnet, cls.pool)
+
+    @attr(type='smoke')
+    def test_list_vips(self):
+        # Verify the vIP exists in the list of all vIPs
+        resp, body = self.client.list_vips()
+        self.assertEqual('200', resp['status'])
+        vips = body['vips']
+        found = None
+        for n in vips:
+            if (n['id'] == self.vip['id']):
+                found = n['id']
+        msg = "vIPs list doesn't contain created vip"
+        self.assertIsNotNone(found, msg)
+
+    def test_create_update_delete_pool_vip(self):
+        # Creates a vip
+        name = rand_name('vip-')
+        resp, body = self.client.create_pool(rand_name("pool-"),
+                                             "ROUND_ROBIN", "HTTP",
+                                             self.subnet['id'])
+        pool = body['pool']
+        resp, body = self.client.create_vip(name, "HTTP", 80,
+                                            self.subnet['id'], pool['id'])
+        self.assertEqual('201', resp['status'])
+        vip = body['vip']
+        vip_id = vip['id']
+        # Verification of vip update
+        new_name = "New_vip"
+        resp, body = self.client.update_vip(vip_id, new_name)
+        self.assertEqual('200', resp['status'])
+        updated_vip = body['vip']
+        self.assertEqual(updated_vip['name'], new_name)
+        # Verification of vip delete
+        resp, body = self.client.delete_vip(vip['id'])
+        self.assertEqual('204', resp['status'])
+        # Verification of pool update
+        new_name = "New_pool"
+        resp, body = self.client.update_pool(pool['id'], new_name)
+        self.assertEqual('200', resp['status'])
+        updated_pool = body['pool']
+        self.assertEqual(updated_pool['name'], new_name)
+        # Verification of pool delete
+        resp, body = self.client.delete_pool(pool['id'])
+        self.assertEqual('204', resp['status'])
+
+    @attr(type='smoke')
+    def test_show_vip(self):
+        # Verifies the details of a vip
+        resp, body = self.client.show_vip(self.vip['id'])
+        self.assertEqual('200', resp['status'])
+        vip = body['vip']
+        self.assertEqual(self.vip['id'], vip['id'])
+        self.assertEqual(self.vip['name'], vip['name'])
diff --git a/tempest/services/network/json/network_client.py b/tempest/services/network/json/network_client.py
index 10e64f8..bc0a6cf 100644
--- a/tempest/services/network/json/network_client.py
+++ b/tempest/services/network/json/network_client.py
@@ -401,3 +401,80 @@
         resp, body = self.post(uri, headers=self.headers, body=body)
         body = json.loads(body)
         return resp, body
+
+    def list_vips(self):
+        uri = '%s/lb/vips' % (self.uri_prefix)
+        resp, body = self.get(uri, self.headers)
+        body = json.loads(body)
+        return resp, body
+
+    def create_vip(self, name, protocol, protocol_port, subnet_id, pool_id):
+        post_body = {
+            "vip": {
+                "protocol": protocol,
+                "name": name,
+                "subnet_id": subnet_id,
+                "pool_id": pool_id,
+                "protocol_port": protocol_port
+            }
+        }
+        body = json.dumps(post_body)
+        uri = '%s/lb/vips' % (self.uri_prefix)
+        resp, body = self.post(uri, headers=self.headers, body=body)
+        body = json.loads(body)
+        return resp, body
+
+    def create_pool(self, name, lb_method, protocol, subnet_id):
+        post_body = {
+            "pool": {
+                "protocol": protocol,
+                "name": name,
+                "subnet_id": subnet_id,
+                "lb_method": lb_method
+            }
+        }
+        body = json.dumps(post_body)
+        uri = '%s/lb/pools' % (self.uri_prefix)
+        resp, body = self.post(uri, headers=self.headers, body=body)
+        body = json.loads(body)
+        return resp, body
+
+    def show_vip(self, uuid):
+        uri = '%s/lb/vips/%s' % (self.uri_prefix, uuid)
+        resp, body = self.get(uri, self.headers)
+        body = json.loads(body)
+        return resp, body
+
+    def delete_vip(self, uuid):
+        uri = '%s/lb/vips/%s' % (self.uri_prefix, uuid)
+        resp, body = self.delete(uri, self.headers)
+        return resp, body
+
+    def delete_pool(self, uuid):
+        uri = '%s/lb/pools/%s' % (self.uri_prefix, uuid)
+        resp, body = self.delete(uri, self.headers)
+        return resp, body
+
+    def update_vip(self, vip_id, new_name):
+        put_body = {
+            "vip": {
+                "name": new_name,
+            }
+        }
+        body = json.dumps(put_body)
+        uri = '%s/lb/vips/%s' % (self.uri_prefix, vip_id)
+        resp, body = self.put(uri, body=body, headers=self.headers)
+        body = json.loads(body)
+        return resp, body
+
+    def update_pool(self, pool_id, new_name):
+        put_body = {
+            "pool": {
+                "name": new_name,
+            }
+        }
+        body = json.dumps(put_body)
+        uri = '%s/lb/pools/%s' % (self.uri_prefix, pool_id)
+        resp, body = self.put(uri, body=body, headers=self.headers)
+        body = json.loads(body)
+        return resp, body