From 3f432261ecb669ff3b51fee11f552844d9e040d2 Mon Sep 17 00:00:00 2001 From: VenuMurthy Date: Thu, 28 Aug 2014 18:14:40 +0530 Subject: [PATCH] Venu | refactored tests and added one for power_off --- ec2driver_config.py | 7 +-- test/tests.py | 97 ------------------------------------- {test => tests}/__init__.py | 0 tests/test_destroy.py | 62 ++++++++++++++++++++++++ tests/test_power_off.py | 54 +++++++++++++++++++++ tests/test_spawn.py | 50 +++++++++++++++++++ 6 files changed, 168 insertions(+), 102 deletions(-) delete mode 100644 test/tests.py rename {test => tests}/__init__.py (100%) create mode 100644 tests/test_destroy.py create mode 100644 tests/test_power_off.py create mode 100644 tests/test_spawn.py diff --git a/ec2driver_config.py b/ec2driver_config.py index 255b23c..7a83122 100644 --- a/ec2driver_config.py +++ b/ec2driver_config.py @@ -22,8 +22,5 @@ aws_secret_access_key = "FMld6m8kok9jpxBkORST5xfbZSod7mVm9ChDgttS" aws_ami = "ami-864d84ee" instance_type = "t2.micro" -flavor_map={ 'm1.tiny':'t2.micro', - 'm1.small':'t2.small', - 'm1.medium':'t2.medium', - 'm1.large':'c3.xlarge', - 'm1.xlarge':'c3.2xlarge'} +flavor_map = {'m1.tiny': 't2.micro', 'm1.small': 't2.small', 'm1.medium': 't2.medium', 'm1.large': 'c3.xlarge', + 'm1.xlarge': 'c3.2xlarge'} diff --git a/test/tests.py b/test/tests.py deleted file mode 100644 index 2cef280..0000000 --- a/test/tests.py +++ /dev/null @@ -1,97 +0,0 @@ -import unittest -import requests -import json -import time - -from novaclient.v1_1 import client -from ..credentials import get_nova_creds - -from boto import ec2 -from ..ec2driver_config import * - - -class TestSpawn(unittest.TestCase): - - def setUp(self): - print "Establishing connection with AWS" - self.ec2_conn = ec2.connect_to_region(aws_region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) - self.creds = get_nova_creds() - - # @unittest.skip("For fun") - def test_spawn(self): - print "Spawning an instance" - nova = client.Client(**self.creds) - image = nova.images.find(name="cirros-0.3.1-x86_64-uec") - flavor = nova.flavors.find(name="m1.tiny") - self.server = nova.servers.create(name = "cirros-test", - image = image.id, - flavor = flavor.id) - instance = nova.servers.get(self.server.id) - while instance.status != 'ACTIVE': - time.sleep(10) - instance = nova.servers.get(self.server.id) - - instance = self.ec2_conn.get_only_instances(instance_ids=[self.server.metadata['ec2_id']], filters=None, dry_run=False, max_results=None) - - self.assertTrue(len(instance) == 1) - - def tearDown(self): - print "Cleanup: Destroying the instance used for testing" - ec2_id = self.server.metadata['ec2_id'] - ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, max_results=None) - # EC2 statecode: 16->Running, 32->Shutting Down - while ec2_instance[0].state_code != 16: - time.sleep(10) - ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, max_results=None) - print ec2_instance[0].state, ec2_instance[0].state_code - self.server.delete() - -class TestDestroy(unittest.TestCase): - def setUp(self): - print "Establishing connection with AWS" - self.ec2_conn = ec2.connect_to_region(aws_region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) - self.creds = get_nova_creds() - - # @unittest.skip("For fun") - def test_destroy(self): - print "Spawning an instance" - nova = client.Client(**self.creds) - image = nova.images.find(name="cirros-0.3.1-x86_64-uec") - flavor = nova.flavors.find(name="m1.tiny") - server = nova.servers.create(name = "cirros-test", - image = image.id, - flavor = flavor.id) - - instance = nova.servers.get(server.id) - while instance.status != 'ACTIVE': - time.sleep(10) - instance = nova.servers.get(server.id) - - instance = nova.servers.get(server.id) - print instance.status - ec2_id = instance.metadata['ec2_id'] - - ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, max_results=None) - # EC2 statecode: 16->Running, 32->Shutting Down - while ec2_instance[0].state_code != 16: - time.sleep(10) - ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, max_results=None) - print ec2_instance[0].state, ec2_instance[0].state_code - - instance.delete() - - ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, max_results=None) - # EC2 statecode: 16->Running, 32->Shutting Down - while ec2_instance[0].state_code != 32: - time.sleep(10) - ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, max_results=None) - print ec2_instance[0].state, ec2_instance[0].state_code - - - ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, max_results=None) - - shutting_down_state_code = 32 - self.assertEquals(ec2_instance[0].state_code, shutting_down_state_code) - -if __name__ == '__main__': - unittest.main() \ No newline at end of file diff --git a/test/__init__.py b/tests/__init__.py similarity index 100% rename from test/__init__.py rename to tests/__init__.py diff --git a/tests/test_destroy.py b/tests/test_destroy.py new file mode 100644 index 0000000..ef68f13 --- /dev/null +++ b/tests/test_destroy.py @@ -0,0 +1,62 @@ +import unittest +import time + +from novaclient.v1_1 import client +from ..credentials import get_nova_creds + +from boto import ec2 +from ..ec2driver_config import * + + +class TestDestroy(unittest.TestCase): + def setUp(self): + print "Establishing connection with AWS" + self.ec2_conn = ec2.connect_to_region(aws_region, aws_access_key_id=aws_access_key_id, + aws_secret_access_key=aws_secret_access_key) + self.creds = get_nova_creds() + + # @unittest.skip("For fun") + def test_destroy(self): + print "Spawning an instance" + nova = client.Client(**self.creds) + image = nova.images.find(name="cirros-0.3.1-x86_64-uec") + flavor = nova.flavors.find(name="m1.tiny") + server = nova.servers.create(name="cirros-test", image=image.id, flavor=flavor.id) + + instance = nova.servers.get(server.id) + while instance.status != 'ACTIVE': + time.sleep(10) + instance = nova.servers.get(server.id) + + instance = nova.servers.get(server.id) + print instance.status + ec2_id = instance.metadata['ec2_id'] + + ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, + max_results=None) + # EC2 statecode: 16->Running, 32->Shutting Down + while ec2_instance[0].state_code != 16: + time.sleep(10) + ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, + max_results=None) + print ec2_instance[0].state, ec2_instance[0].state_code + + instance.delete() + + ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, + max_results=None) + # EC2 statecode: 16->Running, 32->Shutting Down + while ec2_instance[0].state_code != 32: + time.sleep(10) + ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, + max_results=None) + print ec2_instance[0].state, ec2_instance[0].state_code + + ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, + max_results=None) + + shutting_down_state_code = 32 + self.assertEquals(ec2_instance[0].state_code, shutting_down_state_code) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/test_power_off.py b/tests/test_power_off.py new file mode 100644 index 0000000..e871954 --- /dev/null +++ b/tests/test_power_off.py @@ -0,0 +1,54 @@ +import unittest +import time + +from novaclient.v1_1 import client +from ..credentials import get_nova_creds + +from boto import ec2 +from ..ec2driver_config import * + + +class TestPowerOff(unittest.TestCase): + + def setUp(self): + print "Establishing connection with AWS" + self.ec2_conn = ec2.connect_to_region(aws_region, aws_access_key_id=aws_access_key_id, + aws_secret_access_key=aws_secret_access_key) + self.creds = get_nova_creds() + + # @unittest.skip("For fun") + def test_power_off(self): + print "Spawning an instance" + nova = client.Client(**self.creds) + image = nova.images.find(name="cirros-0.3.1-x86_64-uec") + flavor = nova.flavors.find(name="m1.tiny") + self.server = nova.servers.create(name="cirros-test", image=image.id, flavor=flavor.id) + instance = nova.servers.get(self.server.id) + while instance.status != 'ACTIVE': + time.sleep(10) + instance = nova.servers.get(self.server.id) + + instance = self.ec2_conn.get_only_instances(instance_ids=[self.server.metadata['ec2_id']], filters=None, + dry_run=False, max_results=None) + + #Send poweroff to the instance + nova.server.stop() + + #assert power off + self.assertTrue(ec2.get_state(instance) == 32) + + def tearDown(self): + print "Cleanup: Destroying the instance used for testing" + ec2_id = self.server.metadata['ec2_id'] + ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, + max_results=None) + # EC2 statecode: 16->Running, 32->Shutting Down + while ec2_instance[0].state_code != 16: + time.sleep(10) + ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, + max_results=None) + print ec2_instance[0].state, ec2_instance[0].state_code + self.server.delete() + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/test_spawn.py b/tests/test_spawn.py new file mode 100644 index 0000000..8f2b217 --- /dev/null +++ b/tests/test_spawn.py @@ -0,0 +1,50 @@ +import unittest +import time + +from novaclient.v1_1 import client +from ..credentials import get_nova_creds + +from boto import ec2 +from ..ec2driver_config import * + + +class TestSpawn(unittest.TestCase): + + def setUp(self): + print "Establishing connection with AWS" + self.ec2_conn = ec2.connect_to_region(aws_region, aws_access_key_id=aws_access_key_id, + aws_secret_access_key=aws_secret_access_key) + self.creds = get_nova_creds() + + # @unittest.skip("For fun") + def test_spawn(self): + print "Spawning an instance" + nova = client.Client(**self.creds) + image = nova.images.find(name="cirros-0.3.1-x86_64-uec") + flavor = nova.flavors.find(name="m1.tiny") + self.server = nova.servers.create(name="cirros-test", image=image.id, flavor=flavor.id) + instance = nova.servers.get(self.server.id) + while instance.status != 'ACTIVE': + time.sleep(10) + instance = nova.servers.get(self.server.id) + + instance = self.ec2_conn.get_only_instances(instance_ids=[self.server.metadata['ec2_id']], filters=None, + dry_run=False, max_results=None) + + self.assertTrue(len(instance) == 1) + + def tearDown(self): + print "Cleanup: Destroying the instance used for testing" + ec2_id = self.server.metadata['ec2_id'] + ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, + max_results=None) + # EC2 statecode: 16->Running, 32->Shutting Down + while ec2_instance[0].state_code != 16: + time.sleep(10) + ec2_instance = self.ec2_conn.get_only_instances(instance_ids=[ec2_id], filters=None, dry_run=False, + max_results=None) + print ec2_instance[0].state, ec2_instance[0].state_code + self.server.delete() + +if __name__ == '__main__': + unittest.main() \ No newline at end of file