def test_db(self): """ Test DB data access """ inf = InfrastructureInfo() inf.id = "1" inf.auth = self.getAuth([0], [], [("Dummy", 0)]) cloud = CloudInfo() cloud.type = "Dummy" radl = RADL() radl.add( system( "s0", [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er")])) radl.add(deploy("s0", 1)) vm1 = VirtualMachine(inf, "1", cloud, radl, radl) vm2 = VirtualMachine(inf, "2", cloud, radl, radl) inf.vm_list = [vm1, vm2] inf.vm_master = vm1 # first create the DB table Config.DATA_DB = "sqlite:///tmp/ind.dat" InfrastructureList.load_data() success = InfrastructureList._save_data_to_db(Config.DATA_DB, {"1": inf}) self.assertTrue(success) res = InfrastructureList._get_data_from_db(Config.DATA_DB) self.assertEqual(len(res), 1) self.assertEqual(len(res['1'].vm_list), 2) self.assertEqual(res['1'].vm_list[0], res['1'].vm_master) self.assertEqual( res['1'].vm_master.info.systems[0].getValue("disk.0.image.url"), "mock0://linux.for.ev.er") self.assertTrue(res['1'].auth.compare(inf.auth, "InfrastructureManager"))
def test_inf_auth_with_token(self): im_auth = {"token": (self.gen_token())} im_auth['username'] = InfrastructureInfo.OPENID_USER_PREFIX + "micafer" im_auth['password'] = "******" # Check that a user/pass cred cannot access OpenID ones user_auth = Authentication([{ 'id': 'im', 'type': 'InfrastructureManager', 'username': im_auth['username'], 'password': im_auth['password'] }]) with self.assertRaises(Exception) as ex: IM.check_auth_data(user_auth) self.assertEqual( str(ex.exception), "Invalid username used for the InfrastructureManager.") inf = InfrastructureInfo() inf.id = "1" inf.auth = user_auth res = inf.is_authorized(user_auth) self.assertEqual(res, False) user_auth = Authentication([{ 'id': 'im', 'type': 'InfrastructureManager', 'username': im_auth['username'], 'password': im_auth['password'], 'token': im_auth['token'] }]) res = inf.is_authorized(user_auth) self.assertEqual(res, True)
def test_db(self, execute, select, table_exists, connect): table_exists.return_value = True select.return_value = [["1", "", read_file_as_string("../files/data.pkl")]] execute.return_value = True res = IM.get_data_from_db("mysql://*****:*****@server/db_name") self.assertEqual(len(res), 1) inf = InfrastructureInfo() inf.id = "1" success = IM.save_data_to_db("mysql://*****:*****@server/db_name", {"1": inf}) self.assertTrue(success)
def test_commands(self, bottle_request, check_auth_data, get_infrastructure, SSH): """Test REST StopInfrastructure.""" bottle_request.return_value = MagicMock() bottle_request.headers = { "AUTHORIZATION": ("type = InfrastructureManager; username = user; password = pass\n" "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; " "username = user; password = pass") } bottle_request.environ = {'HTTP_HOST': 'imserver.com'} inf = InfrastructureInfo() inf.id = "1" inf.auth = Authentication([{ 'type': 'InfrastructureManager', 'username': '******', 'password': '******' }]) get_infrastructure.return_value = inf bottle_request.params = {'step': '1'} res = RESTGetVMProperty("1", "1", "command") auth_str = "Authorization: type = InfrastructureManager; username = user; password = pass" url = "http://imserver.com/infrastructures/1/vms/1/command?step=2" expected_res = """ res="wait" while [ "$res" == "wait" ] do res=`curl -s -H "%s" -H "Accept: text/plain" %s` if [ "$res" != "wait" ] then eval "$res" else sleep 20 fi done""" % (auth_str, url) self.assertEqual(res, expected_res) radl_master = parse_radl(""" network publica (outbound = 'yes') network privada () system front ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.ip = '8.8.8.8' and net_interface.0.connection = 'publica' and net_interface.1.connection = 'privada' and disk.0.image.url = 'mock0://linux.for.ev.er' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' and disk.0.os.name = 'linux' ) """) radl_vm1 = parse_radl(""" network privada () system wn ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'privada' and disk.0.image.url = 'mock0://linux.for.ev.er' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' and disk.0.os.name = 'linux' ) """) radl_vm2 = parse_radl(""" network privada2 () system wn2 ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'privada2' and disk.0.image.url = 'mock0://linux.for.ev.er' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' and disk.0.os.name = 'linux' ) """) # in the Master VM bottle_request.params = {'step': '2'} inf.vm_master = VirtualMachine(inf, None, None, radl_master, radl_master) inf.vm_master.creation_im_id = 0 ssh = MagicMock() ssh.test_connectivity.return_value = True ssh.port = 22 ssh.private_key = None ssh.password = "******" ssh.username = "******" ssh.host = "8.8.8.8" SSH.return_value = ssh vm1 = VirtualMachine(inf, None, None, radl_vm1, radl_vm1) vm1.creation_im_id = 1 vm1.destroy = False vm2 = VirtualMachine(inf, None, None, radl_vm2, radl_vm2) vm2.creation_im_id = 2 vm2.destroy = False inf.vm_list = [inf.vm_master, vm1, vm2] res = RESTGetVMProperty("1", "0", "command") expected_res = "true" self.assertEqual(res, expected_res) bottle_request.params = {'step': '2'} res = RESTGetVMProperty("1", "1", "command") expected_res = "true" self.assertEqual(res, expected_res) # in VM not connected to the Master VM res = RESTGetVMProperty("1", "2", "command") expected_res = ( 'sshpass -pyoyoyo ssh -N -R 20002:localhost:22 -p 22 -o "UserKnownHostsFile=/dev/null"' ' -o "StrictHostKeyChecking=no" [email protected] &') self.assertEqual(res, expected_res)