Skip to content

Commit

Permalink
Add test for using an external ansible master node
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Dec 16, 2015
1 parent e559b86 commit 745abb6
Showing 1 changed file with 90 additions and 19 deletions.
109 changes: 90 additions & 19 deletions test/TestIM.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,32 @@ class TestIM(unittest.TestCase):

server = None
auth_data = None
inf_id = 0
inf_id = None

@classmethod
def setUpClass(cls):
cls.server = xmlrpclib.ServerProxy("http://" + HOSTNAME + ":" + str(TEST_PORT),allow_none=True)
cls.auth_data = Authentication.read_auth_data(AUTH_FILE)
cls.inf_id = 0

@classmethod
def tearDownClass(cls):
# Assure that the infrastructure is destroyed
try:
cls.server.DestroyInfrastructure(cls.inf_id, cls.auth_data)
if cls.inf_id:
if isinstance(cls.inf_id, list):
for inf_id in cls.inf_id:
cls.server.DestroyInfrastructure(inf_id, cls.auth_data)
else:
cls.server.DestroyInfrastructure(cls.inf_id, cls.auth_data)
except Exception:
pass

def wait_inf_state(self, state, timeout, incorrect_states = [], vm_ids = None):
def wait_inf_state(self, inf_id, state, timeout, incorrect_states = [], vm_ids = None):
"""
Wait for an infrastructure to have a specific state
"""
if not vm_ids:
(success, vm_ids) = self.server.GetInfrastructureInfo(self.inf_id, self.auth_data)
(success, vm_ids) = self.server.GetInfrastructureInfo(inf_id, self.auth_data)
self.assertTrue(success, msg="ERROR calling the GetInfrastructureInfo function:" + str(vm_ids))

err_states = [VirtualMachine.FAILED, VirtualMachine.OFF, VirtualMachine.UNCONFIGURED]
Expand All @@ -75,11 +79,11 @@ def wait_inf_state(self, state, timeout, incorrect_states = [], vm_ids = None):
while not all_ok and wait < timeout:
all_ok = True
for vm_id in vm_ids:
(success, vm_state) = self.server.GetVMProperty(self.inf_id, vm_id, "state", self.auth_data)
(success, vm_state) = self.server.GetVMProperty(inf_id, vm_id, "state", self.auth_data)
self.assertTrue(success, msg="ERROR getting VM info:" + str(vm_state))

if vm_state == VirtualMachine.UNCONFIGURED:
self.server.GetVMContMsg(self.inf_id, vm_id, self.auth_data)
self.server.GetVMContMsg(inf_id, vm_id, self.auth_data)

self.assertFalse(vm_state in err_states, msg="ERROR waiting for a state. '" + vm_state + "' was obtained in the VM: " + str(vm_id) + " err_states = " + str(err_states))

Expand Down Expand Up @@ -115,7 +119,7 @@ def test_11_create(self):
self.assertTrue(success, msg="ERROR calling CreateInfrastructure: " + str(inf_id))
self.__class__.inf_id = inf_id

all_configured = self.wait_inf_state(VirtualMachine.CONFIGURED, 900)
all_configured = self.wait_inf_state(inf_id, VirtualMachine.CONFIGURED, 900)
self.assertTrue(all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")

def test_12_getradl(self):
Expand Down Expand Up @@ -201,7 +205,7 @@ def test_19_addresource(self):
self.assertTrue(success, msg="ERROR calling GetInfrastructureInfo:" + str(vm_ids))
self.assertEqual(len(vm_ids), 4, msg="ERROR getting infrastructure info: Incorrect number of VMs(" + str(len(vm_ids)) + "). It must be 4")

all_configured = self.wait_inf_state(VirtualMachine.CONFIGURED, 900)
all_configured = self.wait_inf_state(self.inf_id, VirtualMachine.CONFIGURED, 900)
self.assertTrue(all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")

def test_20_getstate(self):
Expand Down Expand Up @@ -246,7 +250,7 @@ def test_22_removeresource(self):
self.assertTrue(success, msg="ERROR getting VM state:" + str(res))
self.assertEqual(vm_state, VirtualMachine.RUNNING, msg="ERROR unexpected state. Expected 'running' and obtained " + vm_state)

all_configured = self.wait_inf_state(VirtualMachine.CONFIGURED, 600)
all_configured = self.wait_inf_state(self.inf_id, VirtualMachine.CONFIGURED, 600)
self.assertTrue(all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")

def test_23_removeresource_noconfig(self):
Expand Down Expand Up @@ -274,7 +278,7 @@ def test_24_reconfigure(self):
(success, res) = self.server.Reconfigure(self.inf_id, "", self.auth_data)
self.assertTrue(success, msg="ERROR calling Reconfigure: " + str(res))

all_stopped = self.wait_inf_state(VirtualMachine.CONFIGURED, 600)
all_stopped = self.wait_inf_state(self.inf_id, VirtualMachine.CONFIGURED, 600)
self.assertTrue(all_stopped, msg="ERROR waiting the infrastructure to be configured (timeout).")

def test_25_reconfigure_vmlist(self):
Expand All @@ -284,7 +288,7 @@ def test_25_reconfigure_vmlist(self):
(success, res) = self.server.Reconfigure(self.inf_id, "", self.auth_data, [0])
self.assertTrue(success, msg="ERROR calling Reconfigure: " + str(res))

all_stopped = self.wait_inf_state(VirtualMachine.CONFIGURED, 600)
all_stopped = self.wait_inf_state(self.inf_id, VirtualMachine.CONFIGURED, 600)
self.assertTrue(all_stopped, msg="ERROR waiting the infrastructure to be configured (timeout).")

def test_26_reconfigure_radl(self):
Expand All @@ -295,7 +299,7 @@ def test_26_reconfigure_radl(self):
(success, res) = self.server.Reconfigure(self.inf_id, radl, self.auth_data)
self.assertTrue(success, msg="ERROR calling Reconfigure: " + str(res))

all_configured = self.wait_inf_state(VirtualMachine.CONFIGURED, 600)
all_configured = self.wait_inf_state(self.inf_id, VirtualMachine.CONFIGURED, 600)
self.assertTrue(all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")

(success, cont_out) = self.server.GetInfrastructureContMsg(self.inf_id, self.auth_data)
Expand All @@ -311,7 +315,7 @@ def test_30_stop(self):
self.assertTrue(success, msg="ERROR calling StopInfrastructure: " + str(res))
time.sleep(10)

all_stopped = self.wait_inf_state(VirtualMachine.STOPPED, 120, [VirtualMachine.RUNNING])
all_stopped = self.wait_inf_state(self.inf_id, VirtualMachine.STOPPED, 120, [VirtualMachine.RUNNING])
self.assertTrue(all_stopped, msg="ERROR waiting the infrastructure to be stopped (timeout).")

def test_31_start(self):
Expand All @@ -324,7 +328,7 @@ def test_31_start(self):
self.assertTrue(success, msg="ERROR calling StartInfrastructure: " + str(res))
time.sleep(10)

all_configured = self.wait_inf_state(VirtualMachine.CONFIGURED, 150, [VirtualMachine.RUNNING])
all_configured = self.wait_inf_state(self.inf_id, VirtualMachine.CONFIGURED, 150, [VirtualMachine.RUNNING])
self.assertTrue(all_configured, msg="ERROR waiting the infrastructure to be started (timeout).")

def test_32_stop_vm(self):
Expand All @@ -336,7 +340,7 @@ def test_32_stop_vm(self):
self.assertTrue(success, msg="ERROR calling StopVM: " + str(res))
time.sleep(10)

all_stopped = self.wait_inf_state(VirtualMachine.STOPPED, 120, [VirtualMachine.RUNNING], [0])
all_stopped = self.wait_inf_state(self.inf_id, VirtualMachine.STOPPED, 120, [VirtualMachine.RUNNING], [0])
self.assertTrue(all_stopped, msg="ERROR waiting the vm to be stopped (timeout).")

def test_33_start_vm(self):
Expand All @@ -349,7 +353,7 @@ def test_33_start_vm(self):
self.assertTrue(success, msg="ERROR calling StartVM: " + str(res))
time.sleep(10)

all_configured = self.wait_inf_state(VirtualMachine.CONFIGURED, 150, [VirtualMachine.RUNNING], [0])
all_configured = self.wait_inf_state(self.inf_id, VirtualMachine.CONFIGURED, 150, [VirtualMachine.RUNNING], [0])
self.assertTrue(all_configured, msg="ERROR waiting the vm to be started (timeout).")

def test_40_export_import(self):
Expand Down Expand Up @@ -393,7 +397,7 @@ def test_60_create_no_context(self):
self.assertTrue(success, msg="ERROR calling CreateInfrastructure: " + str(inf_id))
self.__class__.inf_id = inf_id

all_configured = self.wait_inf_state(VirtualMachine.CONFIGURED, 300)
all_configured = self.wait_inf_state(self.inf_id, VirtualMachine.CONFIGURED, 300)
self.assertTrue(all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")

def test_65_destroy(self):
Expand Down Expand Up @@ -438,7 +442,7 @@ def test_70_create_cloud_init(self):
self.assertTrue(success, msg="ERROR calling CreateInfrastructure: " + str(inf_id))
self.__class__.inf_id = inf_id

all_configured = self.wait_inf_state(VirtualMachine.CONFIGURED, 300)
all_configured = self.wait_inf_state(self.inf_id, VirtualMachine.CONFIGURED, 300)
self.assertTrue(all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")

def test_75_destroy(self):
Expand All @@ -448,5 +452,72 @@ def test_75_destroy(self):
(success, res) = self.server.DestroyInfrastructure(self.inf_id, self.auth_data)
self.assertTrue(success, msg="ERROR calling DestroyInfrastructure: " + str(res))

def test_80_create_ansible_host(self):
"""
Test the CreateInfrastructure IM function
"""
ansible_radl = """
network publicnet (outbound = 'yes')
network net ()
system node (
cpu.arch='x86_64' and
cpu.count>=1 and
memory.size>=512m and
net_interface.0.connection = 'publicnet' and
net_interface.1.connection = 'net' and
disk.0.os.flavour='ubuntu' and
disk.0.os.version>='12.04'
)
deploy node 1
"""

(success, inf_id) = self.server.CreateInfrastructure(ansible_radl, self.auth_data)
self.assertTrue(success, msg="ERROR calling CreateInfrastructure to create ansible master: " + str(inf_id))
self.__class__.inf_id = [inf_id]

all_configured = self.wait_inf_state(inf_id, VirtualMachine.CONFIGURED, 600)
self.assertTrue(all_configured, msg="ERROR waiting the ansible maste to be configured (timeout).")

(success, info) = self.server.GetVMInfo(inf_id, "0", self.auth_data)
self.assertTrue(success, msg="ERROR getting ansible master info: " + str(info))
master_radl = radl_parse.parse_radl(info)

host = master_radl.systems[0].getValue("net_interface.0.ip")
username = master_radl.systems[0].getValue("disk.0.os.credentials.username")
password = master_radl.systems[0].getValue("disk.0.os.credentials.password")

radl = """
ansible ansible_master (host = '%s' and credentials.username='%s' and credentials.password='%s')
network net ()
system node (
cpu.arch='x86_64' and
cpu.count>=1 and
memory.size>=512m and
net_interface.0.connection = 'net' and
disk.0.os.flavour='ubuntu' and
disk.0.os.version>='12.04'
)
deploy node 1
""" % (host, username, password)

(success, inf_id) = self.server.CreateInfrastructure(radl, self.auth_data)
self.assertTrue(success, msg="ERROR calling CreateInfrastructure: " + str(inf_id))
self.__class__.inf_id.append(inf_id)

all_configured = self.wait_inf_state(inf_id, VirtualMachine.CONFIGURED, 300)
self.assertTrue(all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")

def test_85_destroy(self):
"""
Test DestroyInfrastructure function
"""
for inf_id in self.inf_id:
(success, res) = self.server.DestroyInfrastructure(inf_id, self.auth_data)
self.assertTrue(success, msg="ERROR calling DestroyInfrastructure: " + str(res))

if __name__ == '__main__':
unittest.main()

0 comments on commit 745abb6

Please sign in to comment.