You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by an...@apache.org on 2016/11/10 01:04:10 UTC

[3/6] libcloud git commit: - AWS ALB driver tests for get methods

- AWS ALB driver tests for get methods


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/3ef8d410
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/3ef8d410
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/3ef8d410

Branch: refs/heads/trunk
Commit: 3ef8d410d929cadfc88d118323b2f0e3c86efa62
Parents: be673ee
Author: Anton Kozyrev <an...@gmail.com>
Authored: Tue Nov 1 17:35:26 2016 +0200
Committer: Anthony Shaw <an...@apache.org>
Committed: Thu Nov 10 11:31:25 2016 +1100

----------------------------------------------------------------------
 libcloud/loadbalancer/drivers/alb.py            |   4 +-
 .../alb/describe_load_balancer_listeters.xml    |  27 ++++
 .../alb/describe_load_balancer_rules.xml        |  21 +++
 .../describe_load_balancer_target_groups.xml    |  29 ++++
 .../fixtures/alb/describe_load_balancers.xml    |  31 ++++
 .../loadbalancer/fixtures/alb/describe_tags.xml |  18 +++
 .../fixtures/alb/describe_target_health.xml     |  19 +++
 libcloud/test/loadbalancer/test_alb.py          | 158 +++++++++++++++++++
 8 files changed, 305 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ef8d410/libcloud/loadbalancer/drivers/alb.py
----------------------------------------------------------------------
diff --git a/libcloud/loadbalancer/drivers/alb.py b/libcloud/loadbalancer/drivers/alb.py
index 603bd7c..fb90515 100644
--- a/libcloud/loadbalancer/drivers/alb.py
+++ b/libcloud/loadbalancer/drivers/alb.py
@@ -148,7 +148,7 @@ class ApplicationLBDriver(Driver):
         return tags
 
     def _to_rule(self, el):
-        def __to_bool(val):
+        def __to_bool__(val):
             return val.lower() in ("yes", "true", "t", "1")
 
         id = findtext(element=el, xpath='RuleArn', namespace=NS)
@@ -165,7 +165,7 @@ class ApplicationLBDriver(Driver):
 
         rule = {
             'id': id,
-            'is_default': __to_bool(is_default),
+            'is_default': __to_bool__(is_default),
             'priority': priority,
             'target_group': target_group,
             'conditions': conditions

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ef8d410/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_listeters.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_listeters.xml b/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_listeters.xml
new file mode 100644
index 0000000..273615c
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_listeters.xml
@@ -0,0 +1,27 @@
+<DescribeListenersResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
+    <DescribeListenersResult>
+        <Listeners>
+            <member>
+                <LoadBalancerArn>arn:aws:elasticloadbalancing:us-east-1:111111111111:loadbalancer/app/Test-ALB/1111111111111111</LoadBalancerArn>
+                <Protocol>HTTPS</Protocol>
+                <Certificates>
+                    <member>
+                        <CertificateArn>arn:aws:iam::111111111111:server-certificate/test.certificate</CertificateArn>
+                    </member>
+                </Certificates>
+                <Port>1443</Port>
+                <ListenerArn>arn:aws:elasticloadbalancing:us-east-1:111111111111:listener/app/Test-ALB/1111111111111111/1111111111111111</ListenerArn>
+                <SslPolicy>ELBSecurityPolicy-2015-05</SslPolicy>
+                <DefaultActions>
+                    <member>
+                        <Type>forward</Type>
+                        <TargetGroupArn>arn:aws:elasticloadbalancing:us-east-1:111111111111:targetgroup/TEST-TARGET-GROUP1/1111111111111111</TargetGroupArn>
+                    </member>
+                </DefaultActions>
+            </member>
+        </Listeners>
+    </DescribeListenersResult>
+    <ResponseMetadata>
+        <RequestId>77834e15-a03a-11e6-a23e-41d3227189e3</RequestId>
+    </ResponseMetadata>
+</DescribeListenersResponse>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ef8d410/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_rules.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_rules.xml b/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_rules.xml
new file mode 100644
index 0000000..9282ab8
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_rules.xml
@@ -0,0 +1,21 @@
+<DescribeRulesResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
+    <DescribeRulesResult>
+        <Rules>
+            <member>
+                <IsDefault>true</IsDefault>
+                <Conditions />
+                <Priority>default</Priority>
+                <Actions>
+                    <member>
+                        <Type>forward</Type>
+                        <TargetGroupArn>arn:aws:elasticloadbalancing:us-east-1:111111111111:targetgroup/TEST-TARGET-GROUP1/1111111111111111</TargetGroupArn>
+                    </member>
+                </Actions>
+                <RuleArn>arn:aws:elasticloadbalancing:us-east-1:111111111111:listener-rule/app/Test-Develop-App-LB/1111111111111111/1111111111111111/1111111111111111</RuleArn>
+            </member>
+        </Rules>
+    </DescribeRulesResult>
+    <ResponseMetadata>
+        <RequestId>a6519d8d-a03b-11e6-90ef-9138cf7efdbc</RequestId>
+    </ResponseMetadata>
+</DescribeRulesResponse>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ef8d410/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_target_groups.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_target_groups.xml b/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_target_groups.xml
new file mode 100644
index 0000000..e2f704a
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancer_target_groups.xml
@@ -0,0 +1,29 @@
+<DescribeTargetGroupsResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
+    <DescribeTargetGroupsResult>
+        <TargetGroups>
+            <member>
+                <TargetGroupArn>arn:aws:elasticloadbalancing:us-east-1:111111111111:targetgroup/TEST-TARGET-GROUP1/1111111111111111</TargetGroupArn>
+                <HealthCheckTimeoutSeconds>5</HealthCheckTimeoutSeconds>
+                <HealthCheckPort>traffic-port</HealthCheckPort>
+                <Matcher>
+                    <HttpCode>200</HttpCode>
+                </Matcher>
+                <TargetGroupName>TEST-TARGET-GROUP1</TargetGroupName>
+                <HealthCheckProtocol>HTTPS</HealthCheckProtocol>
+                <HealthCheckPath>/apps/health</HealthCheckPath>
+                <Protocol>HTTPS</Protocol>
+                <Port>1443</Port>
+                <VpcId>vpc-11111111</VpcId>
+                <HealthyThresholdCount>5</HealthyThresholdCount>
+                <HealthCheckIntervalSeconds>30</HealthCheckIntervalSeconds>
+                <LoadBalancerArns>
+                    <member>arn:aws:elasticloadbalancing:us-east-1:111111111111:loadbalancer/app/Test-ALB/1111111111111111</member>
+                </LoadBalancerArns>
+                <UnhealthyThresholdCount>2</UnhealthyThresholdCount>
+            </member>
+        </TargetGroups>
+    </DescribeTargetGroupsResult>
+    <ResponseMetadata>
+        <RequestId>20eca3ac-a03c-11e6-a91a-7f816344a331</RequestId>
+    </ResponseMetadata>
+</DescribeTargetGroupsResponse>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ef8d410/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancers.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancers.xml b/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancers.xml
new file mode 100644
index 0000000..95ef7d2
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/alb/describe_load_balancers.xml
@@ -0,0 +1,31 @@
+<DescribeLoadBalancersResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
+  <DescribeLoadBalancersResult>
+    <LoadBalancers>
+      <member>
+        <LoadBalancerArn>arn:aws:elasticloadbalancing:us-east-1:111111111111:loadbalancer/app/Test-ALB/1111111111111111</LoadBalancerArn>
+        <Scheme>internal</Scheme>
+        <AvailabilityZones>
+          <member>
+            <SubnetId>subnet-11111111</SubnetId>
+            <ZoneName>us-east-1b</ZoneName>
+          </member>
+        </AvailabilityZones>
+        <DNSName>internal-Test-ALB-1111111111.us-east-1.elb.amazonaws.com</DNSName>
+        <Type>application</Type>
+        <LoadBalancerName>Test-ALB</LoadBalancerName>
+        <VpcId>vpc-11111111</VpcId>
+        <CanonicalHostedZoneId>11111111111111</CanonicalHostedZoneId>
+        <CreatedTime>2016-10-02T20:11:22.980Z</CreatedTime>
+        <SecurityGroups>
+          <member>sg-11111111</member>
+        </SecurityGroups>
+        <State>
+          <Code>active</Code>
+        </State>
+      </member>
+    </LoadBalancers>
+  </DescribeLoadBalancersResult>
+  <ResponseMetadata>
+    <RequestId>00904965-a037-11e6-b59a-ff82c1d23c14</RequestId>
+  </ResponseMetadata>
+</DescribeLoadBalancersResponse>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ef8d410/libcloud/test/loadbalancer/fixtures/alb/describe_tags.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/alb/describe_tags.xml b/libcloud/test/loadbalancer/fixtures/alb/describe_tags.xml
new file mode 100644
index 0000000..8b14caf
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/alb/describe_tags.xml
@@ -0,0 +1,18 @@
+<DescribeTagsResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
+    <DescribeTagsResult>
+        <TagDescriptions>
+            <member>
+                <ResourceArn>arn:aws:elasticloadbalancing:us-east-1:111111111111:loadbalancer/app/Test-ALB/111111111111</ResourceArn>
+                <Tags>
+                    <member>
+                        <Value>lima</Value>
+                        <Key>project</Key>
+                    </member>
+                </Tags>
+            </member>
+        </TagDescriptions>
+    </DescribeTagsResult>
+    <ResponseMetadata>
+        <RequestId>34cba10d-a040-11e6-9d2a-7fea45a4c556</RequestId>
+    </ResponseMetadata>
+</DescribeTagsResponse>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ef8d410/libcloud/test/loadbalancer/fixtures/alb/describe_target_health.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/fixtures/alb/describe_target_health.xml b/libcloud/test/loadbalancer/fixtures/alb/describe_target_health.xml
new file mode 100644
index 0000000..9310f97
--- /dev/null
+++ b/libcloud/test/loadbalancer/fixtures/alb/describe_target_health.xml
@@ -0,0 +1,19 @@
+<DescribeTargetHealthResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
+    <DescribeTargetHealthResult>
+        <TargetHealthDescriptions>
+            <member>
+                <HealthCheckPort>3443</HealthCheckPort>
+                <TargetHealth>
+                    <State>healthy</State>
+                </TargetHealth>
+                <Target>
+                    <Port>3443</Port>
+                    <Id>i-01111111111111111</Id>
+                </Target>
+            </member>
+        </TargetHealthDescriptions>
+    </DescribeTargetHealthResult>
+    <ResponseMetadata>
+        <RequestId>3e63f250-a03f-11e6-b07a-97abc256eba0</RequestId>
+    </ResponseMetadata>
+</DescribeTargetHealthResponse>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3ef8d410/libcloud/test/loadbalancer/test_alb.py
----------------------------------------------------------------------
diff --git a/libcloud/test/loadbalancer/test_alb.py b/libcloud/test/loadbalancer/test_alb.py
new file mode 100644
index 0000000..700c5a3
--- /dev/null
+++ b/libcloud/test/loadbalancer/test_alb.py
@@ -0,0 +1,158 @@
+# 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.
+
+import sys
+import unittest
+
+from libcloud.utils.py3 import httplib
+from libcloud.loadbalancer.drivers.alb import ApplicationLBDriver
+from libcloud.loadbalancer.types import State
+
+from libcloud.test import MockHttpTestCase
+from libcloud.test.secrets import LB_ALB_PARAMS
+from libcloud.test.file_fixtures import LoadBalancerFileFixtures
+
+
+class ApplicationLBTests(unittest.TestCase):
+    def setUp(self):
+        ApplicationLBMockHttp.test = self
+        ApplicationLBDriver.connectionCls.conn_classes = (None, ApplicationLBMockHttp)
+        ApplicationLBMockHttp.type = None
+        ApplicationLBMockHttp.use_param = 'Action'
+        self.driver = ApplicationLBDriver(*LB_ALB_PARAMS)
+
+    def test_instantiate_driver_with_token(self):
+        token = 'temporary_credentials_token'
+        driver = ApplicationLBDriver(*LB_ALB_PARAMS, **{'token': token})
+        self.assertTrue(hasattr(driver, 'token'), 'Driver has no attribute token')
+        self.assertEquals(token, driver.token, "Driver token does not match with provided token")
+
+    def test_driver_with_token_signature_version(self):
+        token = 'temporary_credentials_token'
+        driver = ApplicationLBDriver(*LB_ALB_PARAMS, **{'token': token})
+        kwargs = driver._ex_connection_class_kwargs()
+        self.assertTrue(('signature_version' in kwargs), 'Driver has no attribute signature_version')
+        self.assertEquals('4', kwargs['signature_version'], 'Signature version is not 4 with temporary credentials')
+
+    def test_list_protocols(self):
+        protocols = self.driver.list_protocols()
+        self.assertEqual(len(protocols), 2)
+        self.assertTrue('http' in protocols)
+        self.assertTrue('https' in protocols)
+
+    def test_list_balancers(self):
+        balancers = self.driver.list_balancers()
+        self.assertEqual(len(balancers), 1)
+        self.assertEqual(
+            balancers[0].id,
+            'arn:aws:elasticloadbalancing:us-east-1:111111111111:loadbalancer/app/Test-ALB/1111111111111111'
+        )
+        self.assertEqual(balancers[0].name, 'Test-ALB')
+
+    def test_get_balancer(self):
+        balancer = self.driver.get_balancer(balancer_id='Test-ALB')
+        self.assertEqual(
+            balancer.id,
+            'arn:aws:elasticloadbalancing:us-east-1:111111111111:loadbalancer/app/Test-ALB/1111111111111111'
+        )
+        self.assertEqual(balancer.name, 'Test-ALB')
+        self.assertEqual(balancer.state, State.UNKNOWN)
+
+    def test_balancer_list_members(self):
+        balancer = self.driver.get_balancer(balancer_id='Test-ALB')
+        members = balancer.list_members()
+        self.assertEqual(len(members), 1)
+        self.assertEqual(members[0].balancer, balancer)
+        self.assertEqual('i-01111111111111111', members[0].id)
+
+    def test_ex_balancer_list_listeners(self):
+        balancer = self.driver.get_balancer(balancer_id='Test-ALB')
+        self.assertTrue(('listeners' in balancer.extra), 'No listeners dict found in balancer.extra')
+        listeners = self.driver.ex_balancer_list_listeners(balancer)
+        self.assertEqual(len(listeners), 1)
+
+    def test_ex_get_balancer_tags(self):
+        balancer = self.driver.get_balancer(balancer_id='Test-ALB')
+        self.assertTrue(('tags' in balancer.extra), 'No tags dict found in balancer.extra')
+        tags = self.driver._ex_get_balancer_tags(balancer)
+        self.assertEqual(tags['project'], 'lima')
+
+    def test_ex_get_target_group_members(self):
+        target_group_members = self.driver._ex_get_target_group_members(
+            'arn:aws:elasticloadbalancing:us-east-1:111111111111:targetgroup/TEST-TARGET-GROUP1/1111111111111111'
+        )
+        self.assertEqual(len(target_group_members), 1)
+        self.assertTrue(('id' in target_group_members[0]), 'Target group member is missing "id" field')
+        self.assertTrue(('port' in target_group_members[0]), 'Target group member is missing "port" field')
+        self.assertTrue(('health' in target_group_members[0]), 'Target group member is missing "health" field')
+
+    def test_ex_get_balancer_target_groups(self):
+        balancer = self.driver.get_balancer(balancer_id='Test-ALB')
+        target_groups = self.driver._ex_get_balancer_target_groups(balancer)
+        self.assertEqual(len(target_groups), 1)
+        self.assertTrue(('id' in target_groups[0]), 'Target group is missing "id" field')
+        self.assertTrue(('name' in target_groups[0]), 'Target group is missing "port" field')
+        self.assertTrue(('members' in target_groups[0]), 'Target group is missing "members" field')
+
+    def test_ex_get_balancer_listeners(self):
+        balancer = self.driver.get_balancer(balancer_id='Test-ALB')
+        listeners = self.driver._ex_get_balancer_listeners(balancer)
+        self.assertEqual(len(listeners), 1)
+        self.assertTrue(('id' in listeners[0]), 'Listener is missing "id" field')
+        self.assertTrue(('port' in listeners[0]), 'Listener is missing "port" field')
+        self.assertTrue(('protocol' in listeners[0]), 'Listener is missing "protocol" field')
+        self.assertTrue(('rules' in listeners[0]), 'Listener is missing "rules" field')
+
+    def test_ex_get_rules_for_listener(self):
+        listener_rules = self.driver._ex_get_rules_for_listener(
+            'arn:aws:elasticloadbalancing:us-east-1:111111111111:listener/app/Test-ALB/1111111111111111/1111111111111111'
+        )
+        self.assertEqual(len(listener_rules), 1)
+        self.assertTrue(('id' in listener_rules[0]), 'Rule is missing "id" field')
+        self.assertTrue(('is_default' in listener_rules[0]), 'Rule is missing "port" field')
+        self.assertTrue(('priority' in listener_rules[0]), 'Rule is missing "priority" field')
+        self.assertTrue(('target_group' in listener_rules[0]), 'Rule is missing "target_group" field')
+        self.assertTrue(('conditions' in listener_rules[0]), 'Rule is missing "conditions" field')
+
+class ApplicationLBMockHttp(MockHttpTestCase):
+    fixtures = LoadBalancerFileFixtures('alb')
+
+    def _2015_12_01_DescribeLoadBalancers(self, method, url, body, headers):
+        body = self.fixtures.load('describe_load_balancers.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _2015_12_01_DescribeListeners(self, method, url, body, headers):
+        body = self.fixtures.load('describe_load_balancer_listeters.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _2015_12_01_DescribeRules(self, method, url, body, headers):
+        body = self.fixtures.load('describe_load_balancer_rules.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _2015_12_01_DescribeTargetGroups(self, method, url, body, headers):
+        body = self.fixtures.load('describe_load_balancer_target_groups.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _2015_12_01_DescribeTargetHealth(self, method, url, body, headers):
+        body = self.fixtures.load('describe_target_health.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def _2015_12_01_DescribeTags(self, method, url, body, headers):
+        body = self.fixtures.load('describe_tags.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+
+if __name__ == "__main__":
+    sys.exit(unittest.main())