libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject [1/5] GCE Loadbalancer Support and improvements in the compute
Date Fri, 04 Oct 2013 16:02:57 GMT
Updated Branches:
  refs/heads/trunk 0d057251a -> bb65ab986


http://git-wip-us.apache.org/repos/asf/libcloud/blob/2115f9f6/libcloud/test/compute/test_gce.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_gce.py b/libcloud/test/compute/test_gce.py
index 9146d6a..747faef 100644
--- a/libcloud/test/compute/test_gce.py
+++ b/libcloud/test/compute/test_gce.py
@@ -22,13 +22,15 @@ import datetime
 from libcloud.utils.py3 import httplib
 from libcloud.compute.drivers.gce import (GCENodeDriver, API_VERSION,
                                           timestamp_to_datetime,
-                                          GCEAddress, GCEFirewall, GCENetwork,
-                                          GCENodeSize, GCEProject, GCEZone,
-                                          GCEError, ResourceExistsError,
-                                          QuotaExceededError)
+                                          GCEAddress, GCEHealthCheck,
+                                          GCEFirewall, GCEForwardingRule,
+                                          GCENetwork, GCENodeSize, GCEProject,
+                                          GCERegion, GCETargetPool, GCEZone)
 from libcloud.common.google import (GoogleBaseAuthConnection,
                                     GoogleInstalledAppAuthConnection,
-                                    GoogleBaseConnection)
+                                    GoogleBaseConnection,
+                                    ResourceNotFoundError, ResourceExistsError,
+                                    QuotaExceededError)
 from libcloud.test.common.test_google import GoogleAuthMockHttp
 from libcloud.compute.base import (Node, NodeImage, NodeSize, NodeLocation,
                                    StorageVolume)
@@ -71,15 +73,26 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         datetime2 = datetime.datetime(2013, 6, 26, 17, 43, 15)
         self.assertEqual(timestamp_to_datetime(timestamp2), datetime2)
 
-    def test_find_zone(self):
-        zone1 = self.driver._find_zone('libcloud-demo-np-node', 'instances')
-        self.assertEqual(zone1, 'us-central1-a')
-        zone2 = self.driver._find_zone('libcloud-demo-europe-np-node',
-                                       'instances')
-        self.assertEqual(zone2, 'europe-west1-a')
-        region = self.driver._find_zone('libcloud-demo-address', 'addresses',
-                                        region=True)
-        self.assertEqual(region, 'us-central1')
+    def test_get_region_from_zone(self):
+        zone1 = self.driver.ex_get_zone('us-central1-a')
+        expected_region1 = 'us-central1'
+        region1 = self.driver._get_region_from_zone(zone1)
+        self.assertEqual(region1.name, expected_region1)
+        zone2 = self.driver.ex_get_zone('europe-west1-b')
+        expected_region2 = 'europe-west1'
+        region2 = self.driver._get_region_from_zone(zone2)
+        self.assertEqual(region2.name, expected_region2)
+
+    def test_find_zone_or_region(self):
+        zone1 = self.driver._find_zone_or_region('libcloud-demo-np-node',
+                                                 'instances')
+        self.assertEqual(zone1.name, 'us-central1-a')
+        zone2 = self.driver._find_zone_or_region(
+            'libcloud-demo-europe-np-node', 'instances')
+        self.assertEqual(zone2.name, 'europe-west1-a')
+        region = self.driver._find_zone_or_region('libcloud-demo-address',
+                                                  'addresses', region=True)
+        self.assertEqual(region.name, 'us-central1')
 
     def test_match_images(self):
         project = 'debian-cloud'
@@ -96,13 +109,31 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertEqual(len(address_list_all), 4)
         self.assertEqual(address_list[0].name, 'libcloud-demo-address')
         self.assertEqual(address_list_uc1[0].name, 'libcloud-demo-address')
-        #self.assertEqual(address_list_all[0].name, 'lcaddress')
+        names = [a.name for a in address_list_all]
+        self.assertTrue('libcloud-demo-address' in names)
+
+    def test_ex_list_healthchecks(self):
+        healthchecks = self.driver.ex_list_healthchecks()
+        self.assertEqual(len(healthchecks), 2)
+        self.assertEqual(healthchecks[0].name, 'basic-check')
 
     def test_ex_list_firewalls(self):
         firewalls = self.driver.ex_list_firewalls()
         self.assertEqual(len(firewalls), 4)
         self.assertEqual(firewalls[0].name, 'default-allow-internal')
 
+    def test_ex_list_forwarding_rules(self):
+        forwarding_rules = self.driver.ex_list_forwarding_rules()
+        forwarding_rules_all = self.driver.ex_list_forwarding_rules('all')
+        forwarding_rules_uc1 = self.driver.ex_list_forwarding_rules(
+            'us-central1')
+        self.assertEqual(len(forwarding_rules), 2)
+        self.assertEqual(len(forwarding_rules_all), 2)
+        self.assertEqual(forwarding_rules[0].name, 'lcforwardingrule')
+        self.assertEqual(forwarding_rules_uc1[0].name, 'lcforwardingrule')
+        names = [f.name for f in forwarding_rules_all]
+        self.assertTrue('lcforwardingrule' in names)
+
     def test_list_images(self):
         local_images = self.driver.list_images()
         debian_images = self.driver.list_images(ex_project='debian-cloud')
@@ -129,7 +160,25 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertEqual(len(nodes_uc1a), 5)
         self.assertEqual(nodes[0].name, 'node-name')
         self.assertEqual(nodes_uc1a[0].name, 'node-name')
-        #self.assertEqual(nodes_all[0].name, 'libcloud-demo-persist-node')
+        names = [n.name for n in nodes_all]
+        self.assertTrue('node-name' in names)
+
+    def test_ex_list_regions(self):
+        regions = self.driver.ex_list_regions()
+        self.assertEqual(len(regions), 3)
+        self.assertEqual(regions[0].name, 'europe-west1')
+
+    def ex_list_targetpools(self):
+        target_pools = self.driver.ex_list_targetpools()
+        target_pools_all = self.driver.ex_list_targetpools('all')
+        target_pools_uc1 = self.driver.ex_list_targetpools('us-central1')
+        self.assertEqual(len(target_pools), 3)
+        self.assertEqual(len(target_pools_all), 4)
+        self.assertEqual(len(target_pools_uc1), 3)
+        self.assertEqual(target_pools[0].name, 'www-pool')
+        self.assertEqual(target_pools_uc1[0].name, 'www-pool')
+        names = [t.name for t in target_pools_all]
+        self.assertTrue('www-pool' in names)
 
     def test_list_sizes(self):
         sizes = self.driver.list_sizes()
@@ -138,8 +187,8 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertEqual(len(sizes_all), 100)
         self.assertEqual(sizes[0].name, 'f1-micro')
         self.assertEqual(sizes[0].extra['zone'].name, 'us-central1-a')
-        #self.assertEqual(sizes_all[0].name, 'n1-highmem-8')
-        #self.assertEqual(sizes_all[0].extra['zone'].name, 'us-central1-a')
+        names = [s.name for s in sizes_all]
+        self.assertEqual(names.count('n1-standard-1'), 5)
 
     def test_list_volumes(self):
         volumes = self.driver.list_volumes()
@@ -149,8 +198,9 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertEqual(len(volumes_all), 3)
         self.assertEqual(len(volumes_uc1a), 3)
         self.assertEqual(volumes[0].name, 'lcdisk')
-        #self.assertEqual(volumes_all[0].name, 'test-disk')
         self.assertEqual(volumes_uc1a[0].name, 'lcdisk')
+        names = [v.name for v in volumes_all]
+        self.assertTrue('test-disk' in names)
 
     def test_ex_list_zones(self):
         zones = self.driver.ex_list_zones()
@@ -163,6 +213,22 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertTrue(isinstance(address, GCEAddress))
         self.assertEqual(address.name, address_name)
 
+    def test_ex_create_healthcheck(self):
+        healthcheck_name = 'lchealthcheck'
+        kwargs = {'host': 'lchost',
+                  'path': '/lc',
+                  'port': 8000,
+                  'interval': 10,
+                  'timeout': 10,
+                  'unhealthy_threshold': 4,
+                  'healthy_threshold': 3}
+        hc = self.driver.ex_create_healthcheck(healthcheck_name, **kwargs)
+        self.assertTrue(isinstance(hc, GCEHealthCheck))
+        self.assertEqual(hc.name, healthcheck_name)
+        self.assertEqual(hc.path, '/lc')
+        self.assertEqual(hc.port, 8000)
+        self.assertEqual(hc.interval, 10)
+
     def test_ex_create_firewall(self):
         firewall_name = 'lcfirewall'
         allowed = [{'IPProtocol': 'tcp', 'ports': ['4567']}]
@@ -172,6 +238,16 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertTrue(isinstance(firewall, GCEFirewall))
         self.assertEqual(firewall.name, firewall_name)
 
+    def test_ex_create_forwarding_rule(self):
+        fwr_name = 'lcforwardingrule'
+        targetpool = 'lctargetpool'
+        region = 'us-central1'
+        fwr = self.driver.ex_create_forwarding_rule(fwr_name, targetpool,
+                                                    region=region,
+                                                    port_range='8000-8500')
+        self.assertTrue(isinstance(fwr, GCEForwardingRule))
+        self.assertEqual(fwr.name, fwr_name)
+
     def test_ex_create_network(self):
         network_name = 'lcnetwork'
         cidr = '10.11.0.0/16'
@@ -227,6 +303,22 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertEqual(nodes[0].name, '%s-000' % base_name)
         self.assertEqual(nodes[1].name, '%s-001' % base_name)
 
+    def test_ex_create_targetpool(self):
+        targetpool_name = 'lctargetpool'
+        region = 'us-central1'
+        healthchecks = ['libcloud-lb-demo-healthcheck']
+        node1 = self.driver.ex_get_node('libcloud-lb-demo-www-000',
+                                        'us-central1-b')
+        node2 = self.driver.ex_get_node('libcloud-lb-demo-www-001',
+                                        'us-central1-b')
+        nodes = [node1, node2]
+        targetpool = self.driver.ex_create_targetpool(
+            targetpool_name, region=region, healthchecks=healthchecks,
+            nodes=nodes)
+        self.assertEqual(targetpool.name, targetpool_name)
+        self.assertEqual(len(targetpool.nodes), len(nodes))
+        self.assertEqual(targetpool.region.name, region)
+
     def test_create_volume(self):
         volume_name = 'lcdisk'
         size = 1
@@ -234,6 +326,13 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertTrue(isinstance(volume, StorageVolume))
         self.assertEqual(volume.name, volume_name)
 
+    def test_ex_update_healthcheck(self):
+        healthcheck_name = 'lchealthcheck'
+        healthcheck = self.driver.ex_get_healthcheck(healthcheck_name)
+        healthcheck.port = 9000
+        healthcheck2 = self.driver.ex_update_healthcheck(healthcheck)
+        self.assertTrue(isinstance(healthcheck2, GCEHealthCheck))
+
     def test_ex_update_firewall(self):
         firewall_name = 'lcfirewall'
         firewall = self.driver.ex_get_firewall(firewall_name)
@@ -242,6 +341,32 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         firewall2 = self.driver.ex_update_firewall(firewall)
         self.assertTrue(isinstance(firewall2, GCEFirewall))
 
+    def test_ex_targetpool_remove_add_node(self):
+        targetpool = self.driver.ex_get_targetpool('lctargetpool')
+        node = self.driver.ex_get_node('libcloud-lb-demo-www-001',
+                                       'us-central1-b')
+        remove_node = self.driver.ex_targetpool_remove_node(targetpool, node)
+        self.assertTrue(remove_node)
+        self.assertEqual(len(targetpool.nodes), 1)
+
+        add_node = self.driver.ex_targetpool_add_node(targetpool, node)
+        self.assertTrue(add_node)
+        self.assertEqual(len(targetpool.nodes), 2)
+
+    def test_ex_targetpool_remove_add_healthcheck(self):
+        targetpool = self.driver.ex_get_targetpool('lctargetpool')
+        healthcheck = self.driver.ex_get_healthcheck(
+            'libcloud-lb-demo-healthcheck')
+        remove_healthcheck = self.driver.ex_targetpool_remove_healthcheck(
+            targetpool, healthcheck)
+        self.assertTrue(remove_healthcheck)
+        self.assertEqual(len(targetpool.healthchecks), 0)
+
+        add_healthcheck = self.driver.ex_targetpool_add_healthcheck(
+            targetpool, healthcheck)
+        self.assertTrue(add_healthcheck)
+        self.assertEqual(len(targetpool.healthchecks), 1)
+
     def test_reboot_node(self):
         node = self.driver.ex_get_node('node-name')
         reboot = self.driver.reboot_node(node)
@@ -274,11 +399,21 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         destroyed = address.destroy()
         self.assertTrue(destroyed)
 
+    def test_ex_destroy_healthcheck(self):
+        hc = self.driver.ex_get_healthcheck('lchealthcheck')
+        destroyed = hc.destroy()
+        self.assertTrue(destroyed)
+
     def test_ex_destroy_firewall(self):
         firewall = self.driver.ex_get_firewall('lcfirewall')
         destroyed = firewall.destroy()
         self.assertTrue(destroyed)
 
+    def test_ex_destroy_forwarding_rule(self):
+        fwr = self.driver.ex_get_forwarding_rule('lcforwardingrule')
+        destroyed = fwr.destroy()
+        self.assertTrue(destroyed)
+
     def test_ex_destroy_network(self):
         network = self.driver.ex_get_network('lcnetwork')
         destroyed = network.destroy()
@@ -297,9 +432,14 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         for d in destroyed:
             self.assertTrue(d)
 
+    def test_destroy_targetpool(self):
+        targetpool = self.driver.ex_get_targetpool('lctargetpool')
+        destroyed = targetpool.destroy()
+        self.assertTrue(destroyed)
+
     def test_destroy_volume(self):
-        address = self.driver.ex_get_address('lcaddress')
-        destroyed = address.destroy()
+        disk = self.driver.ex_get_volume('lcdisk')
+        destroyed = disk.destroy()
         self.assertTrue(destroyed)
 
     def test_ex_get_address(self):
@@ -307,9 +447,16 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         address = self.driver.ex_get_address(address_name)
         self.assertEqual(address.name, address_name)
         self.assertEqual(address.address, '173.255.113.20')
-        self.assertEqual(address.region, 'us-central1')
+        self.assertEqual(address.region.name, 'us-central1')
         self.assertEqual(address.extra['status'], 'RESERVED')
 
+    def test_ex_get_healthcheck(self):
+        healthcheck_name = 'lchealthcheck'
+        healthcheck = self.driver.ex_get_healthcheck(healthcheck_name)
+        self.assertEqual(healthcheck.name, healthcheck_name)
+        self.assertEqual(healthcheck.port, 8000)
+        self.assertEqual(healthcheck.path, '/lc')
+
     def test_ex_get_firewall(self):
         firewall_name = 'lcfirewall'
         firewall = self.driver.ex_get_firewall(firewall_name)
@@ -317,6 +464,14 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertEqual(firewall.network.name, 'default')
         self.assertEqual(firewall.source_tags, ['libcloud'])
 
+    def test_ex_get_forwarding_rule(self):
+        fwr_name = 'lcforwardingrule'
+        fwr = self.driver.ex_get_forwarding_rule(fwr_name)
+        self.assertEqual(fwr.name, fwr_name)
+        self.assertEqual(fwr.extra['portRange'], '8000-8500')
+        self.assertEqual(fwr.targetpool.name, 'lctargetpool')
+        self.assertEqual(fwr.protocol, 'TCP')
+
     def test_ex_get_image(self):
         partial_name = 'debian-7'
         image = self.driver.ex_get_image(partial_name)
@@ -336,6 +491,19 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertEqual(network.cidr, '10.11.0.0/16')
         self.assertEqual(network.extra['gatewayIPv4'], '10.11.0.1')
 
+    def test_ex_get_node(self):
+        node_name = 'node-name'
+        zone = 'us-central1-a'
+        node = self.driver.ex_get_node(node_name, zone)
+        self.assertEqual(node.name, node_name)
+        self.assertEqual(node.size, 'n1-standard-1')
+        removed_node = 'libcloud-lb-demo-www-002'
+        self.assertRaises(ResourceNotFoundError, self.driver.ex_get_node,
+                          removed_node, 'us-central1-b')
+        missing_node = 'dummy-node'
+        self.assertRaises(ResourceNotFoundError, self.driver.ex_get_node,
+                          missing_node, 'all')
+
     def test_ex_get_project(self):
         project = self.driver.ex_get_project()
         self.assertEqual(project.name, 'project_name')
@@ -343,6 +511,13 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertEqual(instances_quota['usage'], 7.0)
         self.assertEqual(instances_quota['limit'], 8.0)
 
+    def test_ex_get_region(self):
+        region_name = 'us-central1'
+        region = self.driver.ex_get_region(region_name)
+        self.assertEqual(region.name, region_name)
+        self.assertEqual(region.status, 'UP')
+        self.assertEqual(region.zones[0].name, 'us-central1-a')
+
     def test_ex_get_size(self):
         size_name = 'n1-standard-1'
         size = self.driver.ex_get_size(size_name)
@@ -352,6 +527,13 @@ class GCENodeDriverTest(LibcloudTestCase, TestCaseMixin):
         self.assertEqual(size.ram, 3840)
         self.assertEqual(size.extra['guestCpus'], 1)
 
+    def test_ex_get_targetpool(self):
+        targetpool_name = 'lctargetpool'
+        targetpool = self.driver.ex_get_targetpool(targetpool_name)
+        self.assertEqual(targetpool.name, targetpool_name)
+        self.assertEqual(len(targetpool.nodes), 2)
+        self.assertEqual(targetpool.region.name, 'us-central1')
+
     def test_ex_get_volume(self):
         volume_name = 'lcdisk'
         volume = self.driver.ex_get_volume(volume_name)
@@ -397,6 +579,10 @@ class GCEMockHttp(MockHttpTestCase):
         body = self.fixtures.load('aggregated_disks.json')
         return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
 
+    def _aggregated_forwardingRules(self, method, url, body, headers):
+        body = self.fixtures.load('aggregated_forwardingRules.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
     def _aggregated_instances(self, method, url, body, headers):
         body = self.fixtures.load('aggregated_instances.json')
         return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
@@ -405,6 +591,36 @@ class GCEMockHttp(MockHttpTestCase):
         body = self.fixtures.load('aggregated_machineTypes.json')
         return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
 
+    def _global_httpHealthChecks(self, method, url, body, headers):
+        if method == 'POST':
+            body = self.fixtures.load('global_httpHealthChecks_post.json')
+        else:
+            body = self.fixtures.load('global_httpHealthChecks.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _global_httpHealthChecks_basic_check(self, method, url, body, headers):
+        body = self.fixtures.load('global_httpHealthChecks_basic-check.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _global_httpHealthChecks_libcloud_lb_demo_healthcheck(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'global_httpHealthChecks_libcloud-lb-demo-healthcheck.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _global_httpHealthChecks_lchealthcheck(self, method, url, body,
+                                               headers):
+        if method == 'DELETE':
+            body = self.fixtures.load(
+                'global_httpHealthChecks_lchealthcheck_delete.json')
+        elif method == 'PUT':
+            body = self.fixtures.load(
+                'global_httpHealthChecks_lchealthcheck_put.json')
+        else:
+            body = self.fixtures.load(
+                'global_httpHealthChecks_lchealthcheck.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
     def _global_firewalls(self, method, url, body, headers):
         if method == 'POST':
             body = self.fixtures.load('global_firewalls_post.json')
@@ -455,6 +671,24 @@ class GCEMockHttp(MockHttpTestCase):
             body = self.fixtures.load('global_networks_lcnetwork.json')
         return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
 
+    def _global_operations_operation_global_httpHealthChecks_lchealthcheck_delete(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_global_httpHealthChecks_lchealthcheck_delete.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _global_operations_operation_global_httpHealthChecks_lchealthcheck_put(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_global_httpHealthChecks_lchealthcheck_delete.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _global_operations_operation_global_httpHealthChecks_post(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_global_httpHealthChecks_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
     def _global_operations_operation_global_firewalls_lcfirewall_delete(
             self, method, url, body, headers):
         body = self.fixtures.load(
@@ -497,6 +731,54 @@ class GCEMockHttp(MockHttpTestCase):
             'operations_operation_regions_us-central1_addresses_post.json')
         return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
 
+    def _regions_us_central1_operations_operation_regions_us_central1_forwardingRules_post(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_regions_us-central1_forwardingRules_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_operations_operation_regions_us_central1_forwardingRules_lcforwardingrule_delete(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_regions_us-central1_forwardingRules_lcforwardingrule_delete.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_operations_operation_regions_us_central1_targetPools_post(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_regions_us-central1_targetPools_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_operations_operation_regions_us_central1_targetPools_lctargetpool_delete(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_regions_us-central1_targetPools_lctargetpool_delete.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_operations_operation_regions_us_central1_targetPools_lctargetpool_removeHealthCheck_post(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_regions_us-central1_targetPools_lctargetpool_removeHealthCheck_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_operations_operation_regions_us_central1_targetPools_lctargetpool_addHealthCheck_post(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_regions_us-central1_targetPools_lctargetpool_addHealthCheck_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_operations_operation_regions_us_central1_targetPools_lctargetpool_removeInstance_post(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_regions_us-central1_targetPools_lctargetpool_removeInstance_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_operations_operation_regions_us_central1_targetPools_lctargetpool_addInstance_post(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'operations_operation_regions_us-central1_targetPools_lctargetpool_addInstance_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
     def _zones_us_central1_a_operations_operation_zones_us_central1_a_disks_lcdisk_delete(
             self, method, url, body, headers):
         body = self.fixtures.load(
@@ -571,6 +853,11 @@ class GCEMockHttp(MockHttpTestCase):
         body = self.fixtures.load('projects_debian-cloud_global_images.json')
         return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
 
+    def _regions(self, method, url, body, headers):
+        body = self.fixtures.load(
+            'regions.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
     def _regions_us_central1_addresses(self, method, url, body, headers):
         if method == 'POST':
             body = self.fixtures.load(
@@ -589,6 +876,79 @@ class GCEMockHttp(MockHttpTestCase):
                 'regions_us-central1_addresses_lcaddress.json')
         return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
 
+    def _regions_us_central1_forwardingRules(self, method, url, body, headers):
+        if method == 'POST':
+            body = self.fixtures.load(
+                'regions_us-central1_forwardingRules_post.json')
+        else:
+            body = self.fixtures.load(
+                'regions_us-central1_forwardingRules.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_forwardingRules_libcloud_lb_demo_lb(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'regions_us-central1_forwardingRules_libcloud-lb-demo-lb.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_forwardingRules_lcforwardingrule(
+            self, method, url, body, headers):
+        if method == 'DELETE':
+            body = self.fixtures.load(
+                'regions_us-central1_forwardingRules_lcforwardingrule_delete.json')
+        else:
+            body = self.fixtures.load(
+                'regions_us-central1_forwardingRules_lcforwardingrule.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_targetPools(self, method, url, body, headers):
+        if method == 'POST':
+            body = self.fixtures.load(
+                'regions_us-central1_targetPools_post.json')
+        else:
+            body = self.fixtures.load('regions_us-central1_targetPools.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_targetPools_lctargetpool(self, method, url,
+                                                      body, headers):
+        if method == 'DELETE':
+            body = self.fixtures.load(
+                'regions_us-central1_targetPools_lctargetpool_delete.json')
+        else:
+            body = self.fixtures.load(
+                'regions_us-central1_targetPools_lctargetpool.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_targetPools_libcloud_lb_demo_lb_tp(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'regions_us-central1_targetPools_libcloud-lb-demo-lb-tp.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_targetPools_lctargetpool_removeHealthCheck(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'regions_us-central1_targetPools_lctargetpool_removeHealthCheck_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_targetPools_lctargetpool_addHealthCheck(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'regions_us-central1_targetPools_lctargetpool_addHealthCheck_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_targetPools_lctargetpool_removeInstance(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'regions_us-central1_targetPools_lctargetpool_removeInstance_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _regions_us_central1_targetPools_lctargetpool_addInstance(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'regions_us-central1_targetPools_lctargetpool_addInstance_post.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
     def _zones(self, method, url, body, headers):
         body = self.fixtures.load('zones.json')
         return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
@@ -678,6 +1038,25 @@ class GCEMockHttp(MockHttpTestCase):
                 'zones_us-central1-a_instances_lcnode-001.json')
         return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
 
+    def _zones_us_central1_b_instances_libcloud_lb_demo_www_000(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'zones_us-central1-b_instances_libcloud-lb-demo-www-000.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _zones_us_central1_b_instances_libcloud_lb_demo_www_001(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'zones_us-central1-b_instances_libcloud-lb-demo-www-001.json')
+        return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])
+
+    def _zones_us_central1_b_instances_libcloud_lb_demo_www_002(
+            self, method, url, body, headers):
+        body = self.fixtures.load(
+            'zones_us-central1-b_instances_libcloud-lb-demo-www-002.json')
+        return (httplib.NOT_FOUND, body, self.json_hdr,
+                httplib.responses[httplib.NOT_FOUND])
+
     def _zones_us_central1_a(self, method, url, body, headers):
         body = self.fixtures.load('zones_us-central1-a.json')
         return (httplib.OK, body, self.json_hdr, httplib.responses[httplib.OK])

http://git-wip-us.apache.org/repos/asf/libcloud/blob/2115f9f6/libcloud/test/loadbalancer/test_gce.py
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/test_gce.py b/libcloud/test/loadbalancer/test_gce.py
new file mode 100644
index 0000000..b60977f
--- /dev/null
+++ b/libcloud/test/loadbalancer/test_gce.py
@@ -0,0 +1,207 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+"""
+Tests for Google Compute Engine Load Balancer Driver
+"""
+import sys
+import unittest
+
+from libcloud.common.google import (GoogleBaseAuthConnection,
+                                    GoogleInstalledAppAuthConnection,
+                                    GoogleBaseConnection)
+from libcloud.compute.drivers.gce import (GCENodeDriver)
+from libcloud.loadbalancer.drivers.gce import (GCELBDriver)
+from libcloud.test.common.test_google import GoogleAuthMockHttp
+from libcloud.test.compute.test_gce import GCEMockHttp
+
+from libcloud.test import MockHttpTestCase, LibcloudTestCase
+
+from libcloud.test.secrets import GCE_PARAMS, GCE_KEYWORD_PARAMS
+
+
+class GCELoadBalancerTest(LibcloudTestCase):
+    GoogleBaseConnection._get_token_info_from_file = lambda x: None
+    GoogleBaseConnection._write_token_info_to_file = lambda x: None
+    GoogleInstalledAppAuthConnection.get_code = lambda x: '1234'
+    datacenter = 'us-central1-a'
+
+    def setUp(self):
+        GCEMockHttp.test = self
+        GCELBDriver.connectionCls.conn_classes = (GCEMockHttp, GCEMockHttp)
+        GCENodeDriver.connectionCls.conn_classes = (GCEMockHttp, GCEMockHttp)
+        GoogleBaseAuthConnection.conn_classes = (GoogleAuthMockHttp,
+                                                 GoogleAuthMockHttp)
+        GCEMockHttp.type = None
+        kwargs = GCE_KEYWORD_PARAMS.copy()
+        kwargs['auth_type'] = 'IA'
+        kwargs['datacenter'] = self.datacenter
+        self.driver = GCELBDriver(*GCE_PARAMS, **kwargs)
+
+    def test_get_node_from_ip(self):
+        ip = '173.255.115.146'
+        expected_name = 'node-name'
+        node = self.driver._get_node_from_ip(ip)
+        self.assertEqual(node.name, expected_name)
+
+        dummy_ip = '8.8.8.8'
+        node = self.driver._get_node_from_ip(dummy_ip)
+        self.assertTrue(node is None)
+
+    def test_list_protocols(self):
+        expected_protocols = ['TCP', 'UDP']
+        protocols = self.driver.list_protocols()
+        self.assertEqual(protocols, expected_protocols)
+
+    def test_list_balancers(self):
+        balancers = self.driver.list_balancers()
+        balancers_all = self.driver.list_balancers(ex_region='all')
+        balancer_name = 'lcforwardingrule'
+        self.assertEqual(len(balancers), 2)
+        self.assertEqual(len(balancers_all), 2)
+        self.assertEqual(balancers[0].name, balancer_name)
+
+    def test_create_balancer(self):
+        balancer_name = 'libcloud-lb-demo-lb'
+        tp_name = '%s-tp' % (balancer_name)
+        port = '80'
+        protocol = 'tcp'
+        algorithm = None
+        node0 = self.driver.gce.ex_get_node('libcloud-lb-demo-www-000',
+                                            'us-central1-b')
+        node1 = self.driver.gce.ex_get_node('libcloud-lb-demo-www-001',
+                                            'us-central1-b')
+        members = [node0, node1]
+        balancer = self.driver.create_balancer(balancer_name, port, protocol,
+                                               algorithm, members)
+        self.assertEqual(balancer.name, balancer_name)
+        self.assertEqual(balancer.extra['targetpool'].name, tp_name)
+        self.assertEqual(len(balancer.list_members()), 3)
+
+    def test_destory_balancer(self):
+        balancer_name = 'lcforwardingrule'
+        balancer = self.driver.get_balancer(balancer_name)
+        destroyed = balancer.destroy()
+        self.assertTrue(destroyed)
+
+    def test_get_balancer(self):
+        balancer_name = 'lcforwardingrule'
+        tp_name = 'lctargetpool'
+        balancer_ip = '173.255.119.224'
+        balancer = self.driver.get_balancer(balancer_name)
+        self.assertEqual(balancer.name, balancer_name)
+        self.assertEqual(balancer.extra['forwarding_rule'].name, balancer_name)
+        self.assertEqual(balancer.ip, balancer_ip)
+        self.assertEqual(balancer.extra['targetpool'].name, tp_name)
+
+    def test_attach_compute_node(self):
+        node = self.driver.gce.ex_get_node('libcloud-lb-demo-www-001',
+                                           'us-central1-b')
+        balancer = self.driver.get_balancer('lcforwardingrule')
+        member = self.driver._node_to_member(node, balancer)
+        # Detach member first
+        detach_member = balancer.detach_member(member)
+        self.assertEqual(len(balancer.list_members()), 1)
+        # Attach Node
+        attach_node = balancer.attach_compute_node(node)
+        self.assertEqual(len(balancer.list_members()), 2)
+
+    def test_detach_attach_member(self):
+        node = self.driver.gce.ex_get_node('libcloud-lb-demo-www-001',
+                                           'us-central1-b')
+        balancer = self.driver.get_balancer('lcforwardingrule')
+        member = self.driver._node_to_member(node, balancer)
+
+        # Check that balancer has 2 members
+        self.assertEqual(len(balancer.list_members()), 2)
+
+        # Remove a member and check that it now has 1 member
+        detach_member = balancer.detach_member(member)
+        self.assertEqual(len(balancer.list_members()), 1)
+
+        # Reattach member and check that it has 2 members again
+        attach_member = balancer.attach_member(member)
+        self.assertEqual(len(balancer.list_members()), 2)
+
+    def test_balancer_list_members(self):
+        balancer = self.driver.get_balancer('lcforwardingrule')
+        members = balancer.list_members()
+        self.assertEqual(len(members), 2)
+        member_ips = [m.ip for m in members]
+        self.assertTrue('173.255.113.234' in member_ips)
+
+    def test_ex_create_healthcheck(self):
+        healthcheck_name = 'lchealthcheck'
+        kwargs = {'host': 'lchost',
+                  'path': '/lc',
+                  'port': 8000,
+                  'interval': 10,
+                  'timeout': 10,
+                  'unhealthy_threshold': 4,
+                  'healthy_threshold': 3}
+        hc = self.driver.ex_create_healthcheck(healthcheck_name, **kwargs)
+        self.assertEqual(hc.name, healthcheck_name)
+        self.assertEqual(hc.path, '/lc')
+        self.assertEqual(hc.port, 8000)
+        self.assertEqual(hc.interval, 10)
+
+    def test_ex_list_healthchecks(self):
+        healthchecks = self.driver.ex_list_healthchecks()
+        self.assertEqual(len(healthchecks), 2)
+        self.assertEqual(healthchecks[0].name, 'basic-check')
+
+    def test_ex_balancer_detach_attach_healthcheck(self):
+        healthcheck = self.driver.gce.ex_get_healthcheck(
+            'libcloud-lb-demo-healthcheck')
+        balancer = self.driver.get_balancer('lcforwardingrule')
+
+        healthchecks = self.driver.ex_balancer_list_healthchecks(balancer)
+        self.assertEqual(len(healthchecks), 1)
+        # Detach Healthcheck
+        detach_healthcheck = self.driver.ex_balancer_detach_healthcheck(
+            balancer, healthcheck)
+        self.assertTrue(detach_healthcheck)
+        healthchecks = self.driver.ex_balancer_list_healthchecks(balancer)
+        self.assertEqual(len(healthchecks), 0)
+
+        # Reattach Healthcheck
+        attach_healthcheck = self.driver.ex_balancer_attach_healthcheck(
+            balancer, healthcheck)
+        self.assertTrue(attach_healthcheck)
+        healthchecks = self.driver.ex_balancer_list_healthchecks(balancer)
+        self.assertEqual(len(healthchecks), 1)
+
+    def test_ex_balancer_list_healthchecks(self):
+        balancer = self.driver.get_balancer('lcforwardingrule')
+        healthchecks = self.driver.ex_balancer_list_healthchecks(balancer)
+        self.assertEqual(healthchecks[0].name, 'libcloud-lb-demo-healthcheck')
+
+    def test_node_to_member(self):
+        node = self.driver.gce.ex_get_node('libcloud-lb-demo-www-001',
+                                           'us-central1-b')
+        balancer = self.driver.get_balancer('lcforwardingrule')
+        member = self.driver._node_to_member(node, balancer)
+        self.assertEqual(member.ip, node.public_ips[0])
+        self.assertEqual(member.id, node.name)
+        self.assertEqual(member.port, balancer.port)
+
+    def test_forwarding_rule_to_loadbalancer(self):
+        fwr = self.driver.gce.ex_get_forwarding_rule('lcforwardingrule')
+        balancer = self.driver._forwarding_rule_to_loadbalancer(fwr)
+        self.assertEqual(fwr.name, balancer.name)
+        self.assertEqual(fwr.address, balancer.ip)
+        self.assertEqual(fwr.extra['portRange'], balancer.port)
+
+if __name__ == '__main__':
+    sys.exit(unittest.main())


Mime
View raw message