Skip to content

Commit

Permalink
[LIBCLOUD-869] - AWS ALB driver tests for get methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lrvan committed Nov 1, 2016
1 parent c53331f commit fc883f8
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libcloud/loadbalancer/drivers/alb.py
Expand Up @@ -148,7 +148,7 @@ def _to_tags(self, data):
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)
Expand All @@ -165,7 +165,7 @@ def __to_bool(val):

rule = {
'id': id,
'is_default': __to_bool(is_default),
'is_default': __to_bool__(is_default),
'priority': priority,
'target_group': target_group,
'conditions': conditions
Expand Down
@@ -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>
@@ -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>
@@ -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>
@@ -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>
18 changes: 18 additions & 0 deletions 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>
19 changes: 19 additions & 0 deletions 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>
158 changes: 158 additions & 0 deletions 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())

0 comments on commit fc883f8

Please sign in to comment.