# 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.
# libcloud.org 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 unittest
from libcloud.drivers.ec2 import EC2NodeDriver
from libcloud.base import Node, NodeImage, NodeSize
from test import MockHttp, TestCaseMixin
import httplib
from secrets import EC2_ACCESS_ID, EC2_SECRET
class EC2Tests(unittest.TestCase, TestCaseMixin):
def setUp(self):
EC2NodeDriver.connectionCls.conn_classes = (None, EC2MockHttp)
EC2MockHttp.use_param = 'Action'
self.driver = EC2NodeDriver(EC2_ACCESS_ID, EC2_SECRET)
def test_create_node(self):
image = NodeImage(id='ami-be3adfd7',
name='ec2-public-images/fedora-8-i386-base-v1.04.manifest.xml',
driver=self.driver)
size = NodeSize('m1.small', 'Small Instance', None, None, None, None, driver=self.driver)
node = self.driver.create_node(name='foo', image=image, size=size)
self.assertEqual(node.id, 'i-2ba64342')
def test_list_nodes(self):
node = self.driver.list_nodes()[0]
self.assertEqual(node.id, 'i-4382922a')
def test_reboot_node(self):
node = Node('i-4382922a', None, None, None, None, self.driver)
ret = self.driver.reboot_node(node)
self.assertTrue(ret)
def test_destroy_node(self):
node = Node('i-4382922a', None, None, None, None, self.driver)
ret = self.driver.destroy_node(node)
self.assertTrue(ret)
def test_list_sizes(self):
sizes = self.driver.list_sizes()
self.assertEqual(len(sizes), 7)
self.assertTrue('m1.small' in [ s.id for s in sizes])
self.assertTrue('m1.large' in [ s.id for s in sizes])
self.assertTrue('m1.xlarge' in [ s.id for s in sizes])
self.assertTrue('m2.2xlarge' in [ s.id for s in sizes])
def test_list_images(self):
images = self.driver.list_images()
image = images[0]
self.assertEqual(len(images), 1)
self.assertEqual(image.name, 'ec2-public-images/fedora-8-i386-base-v1.04.manifest.xml')
self.assertEqual(image.id, 'ami-be3adfd7')
class EC2MockHttp(MockHttp):
def _DescribeInstances(self, method, url, body, headers):
body = """
56d0fffa-8819-4658-bdd7-548f143a86d2
-
r-07adf66e
822272953071
-
default
-
i-4382922a
ami-0d57b264
0
pending
0
m1.small
2009-08-07T05:47:04.000Z
us-east-1a
disabled
"""
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
def _RebootInstances(self, method, url, body, headers):
body = """
76dabb7a-fb39-4ed1-b5e0-31a4a0fdf5c0
true
"""
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
def _DescribeImages(self, method, url, body, headers):
body = """
-
ami-be3adfd7
ec2-public-images/fedora-8-i386-base-v1.04.manifest.xml
available
206029621532
false
i386
machine
aki-4438dd2d
ari-4538dd2c
"""
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
def _RunInstances(self, method, url, body, headers):
body = """
r-47a5402e
AIDADH4IGTRXXKCD
-
default
-
i-2ba64342
ami-be3adfd7
0
pending
example-key-name
0
m1.small
2007-08-07T11:51:50.000Z
us-east-1b
true
"""
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
def _TerminateInstances(self, method, url, body, headers):
body = """
fa63083d-e0f7-4933-b31a-f266643bdee8
-
i-4382922a
32
shutting-down
16
running
"""
return (httplib.OK, body, {}, httplib.responses[httplib.OK])