def test_DescribeAddresses(self): self.cluster_manager.elasticip.user.list.return_value = [ {'vm_id': 1, 'ip': '192.168.0.1'}, {'vm_id': None, 'ip': '192.168.0.2'}, ] action = Action({'Action': 'DescribeAddresses'}, self.cluster_manager) response = action.execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <DescribeAddressesResponse xmlns="http://ec2.amazonaws.com/doc/2012-03-01/"> <requestId>a13c7c2e-4106-459d-a728-9d38bcbab9bf</requestId> <addressesSet> <item> <publicIp>192.168.0.1</publicIp> <domain>standard</domain> <instanceId>1</instanceId> </item> <item> <publicIp>192.168.0.2</publicIp> <domain>standard</domain> <instanceId>None</instanceId> </item> </addressesSet> </DescribeAddressesResponse> """ )
def test_AssociateAddress(self): self.cluster_manager.elasticip.user.assign.return_value = None self.cluster_manager.elasticip.user.list.return_value = [ {'ip_id': 1,'vm_id': 1, 'ip': '1.1.0.1'}, {'ip_id': 2,'vm_id': None, 'ip': '1.1.0.2'}, ] action = Action({ 'Action': 'AssociateAddress', 'InstanceId': 2, 'PublicIp': '1.1.0.2', }, self.cluster_manager) response = action.execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <AssociateAddressResponse xmlns="http://ec2.amazonaws.com/doc/2012-03-01/"> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <return>true</return> </AssociateAddressResponse> """ ) with self.assertRaises(AuthFailure): action = Action({ 'Action': 'AssociateAddress', 'InstanceId': 2, 'PublicIp': '1.1.0.3' }, self.cluster_manager) response = action.execute()
def test_ReleaseAddress(self): self.cluster_manager.elasticip.user.list.return_value = [ {'ip_id': 1,'vm_id': 1, 'ip': '1.1.0.1'}, {'ip_id': 2,'vm_id': None, 'ip': '1.1.0.2'}, ] self.cluster_manager.elasticip.user.release.return_value = { 'status': 'ok', 'data': None } response = Action({ 'Action': 'ReleaseAddress', 'PublicIp': '1.1.0.1' }, self.cluster_manager).execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <ReleaseAddressResponse xmlns="http://ec2.amazonaws.com/doc/2012-03-01/"> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <return>true</return> </ReleaseAddressResponse> """ ) with self.assertRaises(AuthFailure): response = Action({ 'Action': 'ReleaseAddress', 'PublicIp': '1.1.0.3' }, self.cluster_manager).execute()
def test_DisassociateAddress(self): self.cluster_manager.elasticip.user.unassign.return_value = None self.cluster_manager.elasticip.user.list.return_value = [ {'ip_id': 1,'vm_id': 1, 'ip': '1.1.0.1'}, {'ip_id': 2,'vm_id': None, 'ip': '1.1.0.2'}, ] action = Action({ 'Action': 'DisassociateAddress', 'PublicIp': '1.1.0.1' }, self.cluster_manager) response = action.execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <DisassociateAddressResponse xmlns="http://ec2.amazonaws.com/doc/2012-03-01/"> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <return>true</return> </DisassociateAddressResponse> """ ) with self.assertRaises(UndefinedError): response = Action({ 'Action': 'DisassociateAddress', 'PublicIp': '1.1.0.2' }, self.cluster_manager).execute() with self.assertRaises(UndefinedError): response = Action({ 'Action': 'DisassociateAddress', 'PublicIp': '1.1.0.3' }, self.cluster_manager).execute()
def test_DescribeImages(self): self.cluster_manager.image.user.list.side_effect = [ [{ 'description': 'Fake image 01.', 'id': 123, 'name': 'image 01', 'platform': 2, 'state': 0, 'user_id': 1, }], [{ 'description': 'Fake image 02.', 'id': 456, 'name': 'image 02', 'platform': 2, 'state': 0, 'user_id': 1, }], ] action = Action({'Action': 'DescribeImages'}, self.cluster_manager) response = action.execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2012-03-01/"> <requestId>833f0802-7b29-4641-b953-6f6b78908b22</requestId> <imagesSet> <item> <architecture>x86_64</architecture> <description>Fake image 01.</description> <imageId>123</imageId> <imageLocation>ami-123</imageLocation> <imageState>available</imageState> <imageOwnerId>1</imageOwnerId> <isPublic>false</isPublic> <name>image 01</name> <platform></platform> </item> <item> <architecture>x86_64</architecture> <description>Fake image 02.</description> <imageId>456</imageId> <imageLocation>ami-456</imageLocation> <imageState>available</imageState> <imageOwnerId>1</imageOwnerId> <isPublic>true</isPublic> <name>image 02</name> <platform></platform> </item> </imagesSet> </DescribeImagesResponse> """ )
def test_DescribeImages(self): self.cluster_manager.image.user.list.side_effect = [ [{ 'description': 'Fake image 01.', 'id': 123, 'name': 'image 01', 'platform': 2, 'state': 0, 'user_id': 1, }], [{ 'description': 'Fake image 02.', 'id': 456, 'name': 'image 02', 'platform': 2, 'state': 0, 'user_id': 1, }], ] action = Action({'Action': 'DescribeImages'}, self.cluster_manager) response = action.execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2012-03-01/"> <requestId>833f0802-7b29-4641-b953-6f6b78908b22</requestId> <imagesSet> <item> <architecture>x86_64</architecture> <description>Fake image 01.</description> <imageId>123</imageId> <imageLocation>ami-123</imageLocation> <imageState>available</imageState> <imageOwnerId>1</imageOwnerId> <isPublic>false</isPublic> <name>image 01</name> <platform></platform> </item> <item> <architecture>x86_64</architecture> <description>Fake image 02.</description> <imageId>456</imageId> <imageLocation>ami-456</imageLocation> <imageState>available</imageState> <imageOwnerId>1</imageOwnerId> <isPublic>true</isPublic> <name>image 02</name> <platform></platform> </item> </imagesSet> </DescribeImagesResponse> """)
def test_ImportKeyPair(self): self.cluster_manager.key.user.add.return_value = None self.cluster_manager.key.user.get.return_value = { 'fingerprint': 'test fingerprint', } # 1. Test success. response = Action( { 'Action': 'ImportKeyPair', 'KeyName': 'test key name', 'PublicKeyMaterial': 'test key material', }, self.cluster_manager).execute() self.cluster_manager.key.user.add.assert_called_once_with({ 'name': 'test key name', 'key': 'test key material' }) self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <ImportKeyPairResponse xmlns="http://ec2.amazonaws.com/doc/2012-04-01/"> <requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId> <keyName>test key name</keyName> <keyFingerprint>test fingerprint</keyFingerprint> </ImportKeyPairResponse> """) # 2. Test `MissingParameter`. with self.assertRaises(MissingParameter): response = Action({ 'Action': 'ImportKeyPair', }, self.cluster_manager).execute() with self.assertRaises(MissingParameter): response = Action( { 'Action': 'ImportKeyPair', 'KeyName': 'test key name' }, self.cluster_manager).execute() with self.assertRaises(MissingParameter): response = Action( { 'Action': 'ImportKeyPair', 'PublicKeyMaterial': 'test key material', }, self.cluster_manager).execute()
def test_DescribeKeyPairs(self): self.cluster_manager.key.user.list.return_value = [ { 'name': 'key01', 'fingerprint': 'fp01' }, { 'name': 'key02', 'fingerprint': 'fp02' }, ] # 1. Test success. response = Action({ 'Action': 'DescribeKeyPairs', }, self.cluster_manager).execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <DescribeKeyPairsResponse xmlns="http://ec2.amazonaws.com/doc/2012-04-01/"> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <keySet> <item> <keyName>key01</keyName> <keyFingerprint>fp01</keyFingerprint> </item> <item> <keyName>key02</keyName> <keyFingerprint>fp02</keyFingerprint> </item> </keySet> </DescribeKeyPairsResponse> """)
def test_DescribeAddresses(self): self.cluster_manager.cloud_manager.cluster_managers.return_value = [ ClusterManager(1, 'ifj', self.cluster_manager), ClusterManager(2, 'agh', self.cluster_manager), ] response = Action( { 'Action': 'DescribeRegions', 'Endpoint': 'ifj.ec2.localhost', }, self.cluster_manager).execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <DescribeRegionsResponse xmlns="http://ec2.amazonaws.com/doc/2012-04-01/"> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <regionInfo> <item> <regionName>ifj</regionName> <regionEndpoint>ifj.ec2.localhost</regionEndpoint> </item> <item> <regionName>agh</regionName> <regionEndpoint>agh.ec2.localhost</regionEndpoint> </item> </regionInfo> </DescribeRegionsResponse> """)
def test_AllocateAddresses(self): self.cluster_manager.elasticip.user.request.return_value = { 'ip': '1.1.0.1', } action = Action({'Action': 'AllocateAddress'}, self.cluster_manager) response = action.execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <AllocateAddressResponse xmlns="http://ec2.amazonaws.com/doc/2012-03-01/"> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <publicIp>1.1.0.1</publicIp> <domain>standard</domain> </AllocateAddressResponse> """ )
def test_CreateKeyPair(self): self.cluster_manager.key.user.gen.return_value = 'key data' self.cluster_manager.key.user.get.return_value = { 'fingerprint': 'fingerprint data', } # 1. Test success. response = Action( { 'Action': 'CreateKeyPair', 'KeyName': 'test key pair', }, self.cluster_manager).execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <CreateKeyPairResponse xmlns="http://ec2.amazonaws.com/doc/2012-04-01/"> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <keyName>test key pair</keyName> <keyFingerprint>fingerprint data</keyFingerprint> <keyMaterial>key data</keyMaterial> </CreateKeyPairResponse> """) # 2. Test `MissingParameter`. with self.assertRaises(MissingParameter): response = Action({ 'Action': 'CreateKeyPair', }, self.cluster_manager).execute() self.cluster_manager.key.user.gen.return_value = None # 3. Test `InvalidKeyPair.Duplicate` exception. self.cluster_manager.key.user.gen.side_effect = \ CLMException('ssh_key_already_exist', 'key.user.gen') with self.assertRaises(InvalidKeyPair.Duplicate): response = Action( { 'Action': 'CreateKeyPair', 'KeyName': 'test key pair', }, self.cluster_manager).execute()
def test_DeleteKeyPair(self): self.cluster_manager.key.user.delete.return_value = None # 1. Test success. response = Action( { 'Action': 'DeleteKeyPair', 'KeyName': 'test key pair', }, self.cluster_manager).execute() self.assertMultiLineEqual( response, """<?xml version="1.0" encoding="UTF-8"?> <DeleteKeyPairResponse xmlns="http://ec2.amazonaws.com/doc/2012-04-01/"> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <return>true</return> </DeleteKeyPairResponse> """) # 2. Test `MissingParameter`. with self.assertRaises(MissingParameter): response = Action({ 'Action': 'DeleteKeyPair', }, self.cluster_manager).execute()
return response # ============================ END OF S3 ================================= cloud_manager = CloudManager( CLM_ADDRESS, aws_key=parameters.get('AWSAccessKeyId'), parameters=parameters, signature=parameters.get('Signature'), ) cluster_manager_name = environ['HTTP_HOST'].split('.')[0] cluster_manager = cloud_manager.get_cluster_manager( by_name=cluster_manager_name) try: if not cluster_manager: raise InvalidZone.NotFound(zone_name=cluster_manager_name) action = Action(parameters, cluster_manager) response = action.execute() start_response('200 OK', [('Content-Type', 'text/xml;charset=UTF-8')]) except EC2Exception, error: http_code = '400 Bad Request' if isinstance(error, AuthFailure): http_code = '401 Unauthorized' response = error.to_xml() start_response(http_code, [('Content-Type', 'text/xml;charset=UTF-8')]) # TODO: # * respect 'Accept-Encoding' HTTP header, i.e. gzip response, etc. return validate(response) def application(environ, start_response): try:
# ============================ END OF S3 ================================= cloud_manager = CloudManager( CLM_ADDRESS, aws_key=parameters.get('AWSAccessKeyId'), parameters=parameters, signature=parameters.get('Signature'), ) cluster_manager_name = environ['HTTP_HOST'].split('.')[0] cluster_manager = cloud_manager.get_cluster_manager( by_name=cluster_manager_name ) try: if not cluster_manager: raise InvalidZone.NotFound(zone_name=cluster_manager_name) action = Action(parameters, cluster_manager) response = action.execute() start_response('200 OK', [('Content-Type', 'text/xml;charset=UTF-8')]) except EC2Exception, error: http_code = '400 Bad Request' if isinstance(error, AuthFailure): http_code = '401 Unauthorized' response = error.to_xml() start_response(http_code, [('Content-Type', 'text/xml;charset=UTF-8')]) # TODO: # * respect 'Accept-Encoding' HTTP header, i.e. gzip response, etc. return validate(response) def application(environ, start_response): try: