def test_20_launch(self, save_data, get_keystone_uri, requests): radl_data = """ network net1 (outbound = 'yes' and outports = '8080') network net2 () system test ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.1.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'http://server.com/666956cb-9d15-475e-9f19-a3732c82a327' and disk.0.os.credentials.username = '******' and disk.1.size=1GB and disk.1.device='hdb' and disk.1.mount_path='/mnt/path' and disk.2.image.url = 'http://server.com/storage/2' )""" radl = radl_parse.parse_radl(radl_data) radl.check() auth = Authentication([{ 'id': 'occi', 'type': 'OCCI', 'proxy': 'proxy', 'host': 'https://server.com:11443' }, { 'type': 'InfrastructureManager', 'username': '******', 'password': '******' }]) occi_cloud = self.get_occi_cloud() requests.side_effect = self.get_response get_keystone_uri.return_value = None, None inf = InfrastructureInfo() inf.auth = auth res = occi_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.") self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue()) self.return_error = True inf = InfrastructureInfo() inf.auth = auth res = occi_cloud.launch(inf, radl, radl, 1, auth) self.return_error = False success, msg = res[0] self.assertFalse(success) self.assertEqual(msg, "Error msg\n") self.assertEqual(self.call_count['DELETE']['/storage/1'], 1) self.assertNotIn('/storage/2', self.call_count['DELETE'])
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_20_launch(self, save_data, get_driver): radl_data = """ network net1 (outbound = 'yes' and outports = '8080,9000:9100' and sg_name= 'test') network net2 () system test ( cpu.arch='x86_64' and cpu.count=1 and memory.size=512m and instance_tags='key=value,key1=value2' and net_interface.0.connection = 'net1' and net_interface.1.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'cst://server.com/image-id' and disk.0.os.credentials.username = '******' )""" radl = radl_parse.parse_radl(radl_data) radl.check() auth = Authentication([{ 'id': 'ost', 'type': 'CloudStack', 'username': '******', 'password': '******', 'host': 'http://server.com' }, { 'type': 'InfrastructureManager', 'username': '******', 'password': '******' }]) osc_cloud = self.get_osc_cloud() driver = MagicMock() get_driver.return_value = driver node_size = MagicMock() node_size.ram = 512 node_size.price = 1 node_size.disk = 1 node_size.vcpus = 1 node_size.name = "small" driver.list_sizes.return_value = [node_size] driver.ex_create_security_group.return_value = { "name": "sgname", "id": "sgid" } driver.ex_list_security_groups.return_value = [] driver.ex_create_security_group_rule.return_value = True driver.create_node.side_effect = self.create_node inf = InfrastructureInfo() inf.auth = auth res = osc_cloud.launch_with_retry(inf, radl, radl, 1, auth, 2, 1) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.")
def test_20_launch(self, get_image_data, save_data, get_driver): radl_data = """ network net1 (outbound = 'yes' and provider_id = 'public' and outports = '8080,9000:9100' and sg_name= 'test') network net2 (dnsserver='1.1.1.1' and create = 'yes') system test ( cpu.arch='x86_64' and cpu.count=1 and memory.size=512m and instance_tags='key=value,key1=value2' and net_interface.1.connection = 'net1' and net_interface.0.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'ost://server.com/ami-id' and disk.0.os.credentials.username = '******' and disk.1.size=1GB and disk.1.device='hdb' and disk.2.image.url = 'ost://server.com/vol-id' and disk.2.device='hdc' )""" radl = radl_parse.parse_radl(radl_data) radl.check() auth = Authentication([{ 'id': 'ost', 'type': 'OpenStack', 'username': '******', 'password': '******', 'tenant': 'tenant', 'host': 'https://server.com:5000' }, { 'type': 'InfrastructureManager', 'username': '******', 'password': '******' }]) ost_cloud = self.get_ost_cloud() driver = MagicMock() get_driver.return_value = driver node_size = MagicMock() node_size.ram = 512 node_size.price = 1 node_size.disk = 1 node_size.vcpus = 1 node_size.name = "small" driver.list_sizes.return_value = [node_size] net1 = MagicMock() net1.name = "public" net1.id = "net1id" net1.extra = {'router:external': True} net1.cidr = None net2 = MagicMock() net2.name = "private" net2.id = "net2id" net2.cidr = "10.0.0.0/24" driver.ex_list_networks.return_value = [net2, net1] sg = MagicMock() sg.name = "sg" driver.ex_create_security_group.return_value = sg driver.ex_list_security_groups.return_value = [] driver.ex_create_security_group_rule.return_value = True driver.features = {'create_node': ['ssh_key']} driver.create_node.side_effect = self.create_node driver.ex_create_network.return_value = net2 subnet1 = MagicMock() driver.ex_create_subnet.return_value = subnet1 router = MagicMock() router.id = "id" router.name = "name" router.extra = {'external_gateway_info': {'network_id': net1.id}} driver.ex_list_routers.return_value = [router] driver.ex_add_router_subnet.return_value = True image = MagicMock() image.id = 'imageid' driver.get_image.return_value = image vol = MagicMock() vol.id = 'volid' driver.ex_get_volume.return_value = vol inf = InfrastructureInfo() inf.radl = radl inf.auth = auth res = ost_cloud.launch_with_retry(inf, radl, radl, 1, auth, 2, 1) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.") self.assertEqual(driver.create_node.call_args_list[0][1]['networks'], [net1]) mappings = [{ 'source_type': 'image', 'uuid': 'imageid', 'boot_index': 0, 'delete_on_termination': False, 'device_name': 'vda' }, { 'guest_format': 'ext3', 'boot_index': 1, 'volume_size': 1, 'device_name': 'vdb', 'source_type': 'blank', 'destination_type': 'volume', 'delete_on_termination': True }, { 'boot_index': 2, 'delete_on_termination': False, 'destination_type': 'volume', 'device_name': 'vdc', 'source_type': 'volume', 'uuid': 'volid' }] self.assertEqual( driver.create_node.call_args_list[0][1]['ex_blockdevicemappings'], mappings) self.assertEqual(driver.ex_create_subnet.call_args_list[0][0][2], "10.0.1.0/24") # test with proxy auth data auth = Authentication([{ 'id': 'ost', 'type': 'OpenStack', 'proxy': 'proxy', 'tenant': 'tenant', 'host': 'https://server.com:5000' }, { 'type': 'InfrastructureManager', 'username': '******', 'password': '******' }]) inf = InfrastructureInfo() inf.auth = auth inf.radl = radl res = ost_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.") get_image_data.return_value = "https://cloud.recas.ba.infn.it:5000", "image_id2", "" radl.systems[0].setValue( 'disk.0.image.url', 'appdb://CESNET-MetaCloud/egi.ubuntu.16.04?fedcloud.egi.eu') res = ost_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.") self.assertEqual(driver.get_image.call_args_list[3][0][0], "image_id2") radl_data = """ network net1 (outbound = 'yes') network net2 (create = 'yes') network net3 (create = 'yes') system test ( cpu.arch='x86_64' and cpu.count=1 and memory.size=512m and instance_tags='key=value,key1=value2' and net_interface.1.connection = 'net1' and net_interface.0.connection = 'net2' and net_interface.2.connection = 'net3' and disk.0.os.name = 'linux' and disk.0.image.url = 'ost://server.com/ami-id' and disk.0.os.credentials.username = '******' and disk.1.size=1GB and disk.1.device='hdb' and disk.2.image.url = 'ost://server.com/vol-id' and disk.2.device='hdc' ) """ radl = radl_parse.parse_radl(radl_data) radl.check() inf = InfrastructureInfo() inf.auth = auth inf.radl = radl res = ost_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.") self.assertEqual(driver.ex_create_subnet.call_args_list[5][0][2], "10.0.2.0/24")
def test_25_launch_spot(self, save_data, blockdevicemapping, VPCConnection, get_region): radl_data = """ network net1 (outbound = 'yes' and provider_id = 'vpc-id.subnet-id') network net2 () system test ( spot = 'yes' and cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.1.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'aws://us-east-one/ami-id' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.private_key = 'private' and disk.0.os.credentials.public_key = 'public' and disk.1.size=1GB and disk.1.device='hdb' and disk.1.mount_path='/mnt/path' )""" radl = radl_parse.parse_radl(radl_data) radl.check() auth = Authentication([{'id': 'ec2', 'type': 'EC2', 'username': '******', 'password': '******'}, {'type': 'InfrastructureManager', 'username': '******', 'password': '******'}]) ec2_cloud = self.get_ec2_cloud() region = MagicMock() get_region.return_value = region conn = MagicMock() VPCConnection.return_value = conn image = MagicMock() device = MagicMock() reservation = MagicMock() instance = MagicMock() device.snapshot_id = True device.volume_id = True image.block_device_mapping = {"device": device} instance.add_tag.return_value = True instance.id = "iid" reservation.instances = [instance] conn.run_instances.return_value = reservation conn.get_image.return_value = image sg = MagicMock() sg.id = "sgid" sg.name = "sgname" sg.authorize.return_value = True conn.create_security_group.return_value = sg conn.get_all_security_groups.return_value = [] blockdevicemapping.return_value = {'device': ''} zone = MagicMock() zone.name = 'us-east-1' conn.get_all_zones.return_value = [zone] history = MagicMock() history.price = 0.1 conn.get_spot_price_history.return_value = [history] request = MagicMock() request.id = "id" conn.request_spot_instances.return_value = [request] inf = InfrastructureInfo() inf.auth = auth res = ec2_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.") self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())
def test_20_launch(self, save_data, blockdevicemapping, VPCConnection, get_region): radl_data = """ network net1 (outbound = 'yes' and outports='8080,9000:9100' and sg_name = 'sgname') network net2 () system test ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and instance_tags = 'key=value,key1=value2' and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.1.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'aws://us-east-one/ami-id' and disk.0.os.credentials.username = '******' and disk.1.size=1GB and disk.1.device='hdb' and disk.1.mount_path='/mnt/path' )""" radl = radl_parse.parse_radl(radl_data) radl.check() auth = Authentication([{'id': 'ec2', 'type': 'EC2', 'username': '******', 'password': '******'}, {'type': 'InfrastructureManager', 'username': '******', 'password': '******'}]) ec2_cloud = self.get_ec2_cloud() region = MagicMock() get_region.return_value = region conn = MagicMock() VPCConnection.return_value = conn image = MagicMock() device = MagicMock() reservation = MagicMock() instance = MagicMock() device.snapshot_id = True device.volume_id = True image.block_device_mapping = {"device": device} instance.add_tag.return_value = True instance.id = "iid" reservation.instances = [instance] conn.run_instances.return_value = reservation conn.get_image.return_value = image subnet = MagicMock() subnet.id = "subnet-id" conn.get_all_subnets.return_value = [subnet] vpc = MagicMock() vpc.id = "vpc-id" conn.get_all_vpcs.return_value = [vpc] sg = MagicMock() sg.id = "sgid" sg.name = "sgname" sg.authorize.return_value = True conn.create_security_group.return_value = sg conn.get_all_security_groups.return_value = [] blockdevicemapping.return_value = {'device': ''} inf = InfrastructureInfo() inf.auth = auth inf.radl = radl res = ec2_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.") self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue()) self.assertEquals(len(conn.create_security_group.call_args_list), 3) self.assertEquals(conn.create_security_group.call_args_list[0][0][0], "im-%s" % inf.id) self.assertEquals(conn.create_security_group.call_args_list[1][0][0], "sgname") self.assertEquals(conn.create_security_group.call_args_list[2][0][0], "im-%s-net2" % inf.id) # Check the case that we do not use VPC radl_data = """ network net1 (outbound = 'yes' and outports='8080') network net2 (create='yes' and cidr='10.0.128.0/24') network net3 (create='yes' and cidr='10.0.*.0/24') network net4 (create='yes') system test ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=1g and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.1.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'aws://us-east-one/ami-id' and disk.0.os.credentials.username = '******' and #disk.0.os.credentials.private_key = 'private' and #disk.0.os.credentials.public_key = 'public' and disk.1.size=1GB and disk.1.device='hdb' and disk.1.mount_path='/mnt/path' )""" radl = radl_parse.parse_radl(radl_data) vpc = MagicMock() vpc.id = "vpc-id" conn.create_vpc.return_value = vpc conn.get_all_vpcs.side_effect = self._get_all_vpcs subnet = MagicMock() subnet.id = "subnet-id" subnet.cidr_block = "10.10.129.0/24" conn.create_subnet.return_value = subnet conn.get_all_subnets.side_effect = self.get_all_subnets inf = InfrastructureInfo() inf.auth = auth inf.radl = radl res = ec2_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.") # check the instance_type selected is correct self.assertEquals(conn.run_instances.call_args_list[1][1]["instance_type"], "t3a.micro") self.assertEquals(conn.create_vpc.call_args_list[0][0][0], "10.0.128.0/22") self.assertEquals(conn.create_subnet.call_args_list[0][0], ('vpc-id', '10.0.128.0/24')) self.assertEquals(conn.create_subnet.call_args_list[1][0], ('vpc-id', '10.0.129.0/24')) self.assertEquals(conn.create_subnet.call_args_list[2][0], ('vpc-id', '10.0.130.0/24')) self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())
def test_20_launch(self, save_data, get_driver): radl_data = """ network net1 (outbound = 'yes' and outports = '8080,9000:9100') network net2 () system test ( cpu.arch='x86_64' and cpu.count=1 and memory.size=512m and instance_tags='key=value,key1=value2' and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.1.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'gce://us-central1-a/centos-6' and disk.0.os.credentials.username = '******' and disk.1.size=1GB and disk.1.device='hdb' and disk.1.mount_path='/mnt/path' and disk.2.image.url='gce://us-central1-a/somedisk' and disk.2.device='hdc' and disk.2.mount_path='/mnt2/path' )""" radl = radl_parse.parse_radl(radl_data) radl.check() auth = Authentication([{ 'id': 'one', 'type': 'GCE', 'username': '******', 'password': '******', 'project': 'proj' }, { 'type': 'InfrastructureManager', 'username': '******', 'password': '******' }]) gce_cloud = self.get_gce_cloud() driver = MagicMock() get_driver.return_value = driver node_size = MagicMock() node_size.ram = 512 node_size.price = 1 node_size.disk = 1 node_size.vcpus = 1 node_size.name = "small" node_size.extra = {'guestCpus': 1} driver.list_sizes.return_value = [node_size] image = MagicMock() image.extra['selfLink'] = "image_selfLink" driver.ex_get_image.return_value = image driver.ex_create_address.return_value = "ip" net = MagicMock() net.name = "default" driver.ex_list_networks.return_value = [net] node = MagicMock() node.id = "gce1" node.name = "gce1name" driver.create_node.return_value = node node2 = MagicMock() node2.id = "gce2" node2.name = "gce2name" node3 = MagicMock() node3.id = "gce3" node3.name = "gce3name" driver.ex_create_multiple_nodes.return_value = [node, node2, node3] inf = InfrastructureInfo() inf.auth = auth inf.radl = radl res = gce_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a single VM.") self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue()) self.assertEqual(driver.create_node.call_args_list[0][1]['ex_network'], "default") self.assertEqual( driver.create_node.call_args_list[0][1]['external_ip'], "ephemeral") self.assertEqual( driver.create_node.call_args_list[0][1]['ex_disks_gce_struct'][1] ['deviceName'], "hdb") self.assertEqual( driver.create_node.call_args_list[0][1]['ex_disks_gce_struct'][1] ['autoDelete'], True) self.assertEqual( driver.create_node.call_args_list[0][1]['ex_disks_gce_struct'][2] ['deviceName'], "hdc") self.assertEqual( driver.create_node.call_args_list[0][1]['ex_disks_gce_struct'][2] ['autoDelete'], False) self.assertEqual(driver.ex_create_firewall.call_args_list[0][0][0], "im-%s-default-all" % inf.id) self.assertEqual(driver.ex_create_firewall.call_args_list[1][0][0], "im-%s-default" % inf.id) self.assertEqual(driver.ex_create_firewall.call_args_list[0][0][1], [{ 'IPProtocol': 'udp', 'ports': '1-65535' }, { 'IPProtocol': 'tcp', 'ports': '1-65535' }, { 'IPProtocol': 'icmp' }]) self.assertEqual(driver.ex_create_firewall.call_args_list[1][0][1], [{ 'IPProtocol': 'tcp', 'ports': ['22', '8080', '9000-9100'] }]) inf = InfrastructureInfo() inf.auth = auth inf.radl = radl res = gce_cloud.launch(inf, radl, radl, 3, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching 3 VMs.") self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue()) radl_data = """ network net1 (outbound = 'yes' and outports = '8080,9000:9100') network net2 (create='yes' and cidr='10.0.*.0/24') system test ( cpu.arch='x86_64' and cpu.count=1 and memory.size=512m and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.0.ip = '10.0.0.1' and net_interface.1.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'gce://us-central1-a/centos-6' and disk.0.os.credentials.username = '******' and disk.1.size=1GB and disk.1.device='hdb' and disk.1.mount_path='/mnt/path' )""" radl = radl_parse.parse_radl(radl_data) radl.check() driver.create_node.side_effect = Exception("Error msg") net = MagicMock() net.cidr = "10.0.1.0/24" driver.ex_list_networks.return_value = [net] driver.ex_get_network.return_value = None inf = InfrastructureInfo() inf.auth = auth inf.radl = radl res = gce_cloud.launch(inf, radl, radl, 1, auth) success, msg = res[0] self.assertFalse(success) self.assertEqual(msg, "ERROR: Error msg") self.assertEqual(driver.ex_destroy_address.call_count, 1) self.assertEqual(driver.ex_destroy_address.call_args_list, [call('ip')]) self.assertEqual(driver.ex_create_network.call_args_list[0][0][0], "im-%s-net2" % inf.id) self.assertEqual(driver.ex_create_network.call_args_list[0][0][1], "10.0.2.0/24")
def test_20_launch(self, save_data, credentials, network_client, compute_client, storage_client, resource_client): radl_data = """ network net1 (outbound = 'yes' and outports = '8080,9000:9100' and sg_name = 'nsgname') network net2 () system test ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and instance_tags = 'key=value,key1=value2' and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.1.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'azr://Canonical/UbuntuServer/16.04.0-LTS/latest' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' and disk.1.size=1GB and disk.1.device='hdb' and disk.1.mount_path='/mnt/path' and disk.2.image.url='RGname/DiskName' and disk.2.device='hdb' and disk.2.mount_path='/mnt/path2' )""" radl = radl_parse.parse_radl(radl_data) radl.check() auth = Authentication([{ 'id': 'azure', 'type': 'Azure', 'subscription_id': 'subscription_id', 'username': '******', 'password': '******' }, { 'type': 'InfrastructureManager', 'username': '******', 'password': '******' }]) azure_cloud = self.get_azure_cloud() cclient = MagicMock() compute_client.return_value = cclient nclient = MagicMock() network_client.return_value = nclient rclient = MagicMock() resource_client.return_value = rclient nclient.virtual_networks.get.side_effect = Exception() subnet_create = MagicMock() subnet_create_res = MagicMock() subnet_create_res.id = "subnet-0" subnet_create.result.return_value = subnet_create_res nclient.subnets.create_or_update.return_value = subnet_create public_ip_create = MagicMock() public_ip_create_res = MagicMock() public_ip_create_res.id = "ip-0" public_ip_create.result.return_value = public_ip_create_res nclient.public_ip_addresses.create_or_update.return_value = public_ip_create instace_type = MagicMock() instace_type.name = "instance_type1" instace_type.number_of_cores = 1 instace_type.memory_in_mb = 1024 instace_type.resource_disk_size_in_mb = 102400 instace_types = [instace_type] cclient.virtual_machine_sizes.list.return_value = instace_types cclient.virtual_machines.create_or_update.side_effect = self.create_vm disk = MagicMock() disk.name = "dname" disk.id = "did" cclient.disks.get.return_value = disk inf = InfrastructureInfo() inf.auth = auth inf.radl = radl res = azure_cloud.launch_with_retry(inf, radl, radl, 3, auth, 2, 0) self.assertEqual(len(res), 3) self.assertTrue(res[0][0]) self.assertTrue(res[1][0]) self.assertTrue(res[2][0]) self.assertEquals(rclient.resource_groups.delete.call_count, 2) self.assertIn("rg-userimage-", rclient.resource_groups.delete.call_args_list[0][0][0]) self.assertIn("rg-userimage-", rclient.resource_groups.delete.call_args_list[1][0][0]) json_vm_req = cclient.virtual_machines.create_or_update.call_args_list[ 0][0][2] self.assertEquals( json_vm_req['storage_profile']['data_disks'][0]['disk_size_gb'], 1) self.assertEquals( json_vm_req['storage_profile']['data_disks'][1]['managed_disk'] ['id'], "did") image_res = { 'sku': '16.04.0-LTS', 'publisher': 'Canonical', 'version': 'latest', 'offer': 'UbuntuServer' } self.assertEquals(json_vm_req['storage_profile']['image_reference'], image_res) self.assertEquals(json_vm_req['hardware_profile']['vm_size'], 'instance_type1') self.assertEquals(json_vm_req['os_profile']['admin_username'], 'user') self.assertEquals(json_vm_req['os_profile']['admin_password'], 'pass') self.assertEquals(json_vm_req['os_profile']['admin_password'], 'pass') self.assertEquals( nclient.subnets.create_or_update.call_args_list[0][0][3], {'address_prefix': '10.0.1.0/24'}) self.assertEquals( nclient.subnets.create_or_update.call_args_list[1][0][3], {'address_prefix': '10.0.2.0/24'}) radl_data = """ network net1 (outbound = 'yes') network net2 () system test ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and instance_tags = 'key=value,key1=value2' and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.1.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'azr://error/rgname/diskname' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' )""" radl = radl_parse.parse_radl(radl_data) radl.check() with self.assertRaises(Exception) as ex: azure_cloud.launch(inf, radl, radl, 1, auth) self.assertEquals(str(ex.exception), "Incorrect image url: it must be snapshot or disk.") radl_data = """ network net1 (outbound = 'yes') network net2 (cidr = '192.168.*.0/24') system test ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and instance_tags = 'key=value,key1=value2' and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.1.connection = 'net2' and disk.0.os.name = 'linux' and disk.0.image.url = 'azr://snapshot/rgname/diskname' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' )""" radl = radl_parse.parse_radl(radl_data) radl.check() res = azure_cloud.launch(inf, radl, radl, 1, auth) json_vm_req = cclient.virtual_machines.create_or_update.call_args_list[ 5][0][2] self.assertEquals(json_vm_req['storage_profile']['os_disk']['os_type'], 'linux') self.assertEquals( nclient.subnets.create_or_update.call_args_list[5][0][3], {'address_prefix': '192.168.1.0/24'})
def test_20_launch(self, save_data, getONEVersion, server_proxy): radl_data = """ network net1 (provider_id = 'publica' and outbound = 'yes' and outports = '8080,9000:9100' and sg_name= 'test') network net2 () system test ( cpu.arch='x86_64' and cpu.count=1 and memory.size=512m and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.1.connection = 'net2' and instance_tags = 'key=value,key1=value2' and disk.0.os.name = 'linux' and disk.0.image.url = 'one://server.com/1' and disk.0.os.credentials.username = '******' and disk.1.size=1GB and disk.1.device='hdb' and disk.1.mount_path='/mnt/path' )""" radl = radl_parse.parse_radl(radl_data) radl.check() auth = Authentication([{ 'id': 'one', 'type': 'OpenNebula', 'username': '******', 'password': '******', 'host': 'server.com:2633' }, { 'type': 'InfrastructureManager', 'username': '******', 'password': '******' }]) one_cloud = self.get_one_cloud() getONEVersion.return_value = "4.14.0" one_server = MagicMock() one_server.one.vm.allocate.return_value = (True, "1", 0) one_server.one.vnpool.info.return_value = ( True, self.read_file_as_string("files/nets.xml"), 0) one_server.one.secgrouppool.info.return_value = ( True, self.read_file_as_string("files/sgs.xml"), 0) one_server.one.secgroup.allocate.return_value = (True, 1, 0) server_proxy.return_value = one_server inf = InfrastructureInfo() inf.auth = auth res = one_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.") sg_template = ( 'NAME = test\nRULE = [ PROTOCOL = TCP, RULE_TYPE = inbound, RANGE = 22:22 ]\n' 'RULE = [ PROTOCOL = TCP, RULE_TYPE = inbound, RANGE = 8080:8080 ]\n' 'RULE = [ PROTOCOL = TCP, RULE_TYPE = inbound, RANGE = 9000:9100 ]\n' ) self.assertEqual(one_server.one.secgroup.allocate.call_args_list, [call('user:pass', sg_template)]) self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue()) # Now test an error in allocate one_server.one.vm.allocate.return_value = (False, "Error msg", 0) res = one_cloud.launch(inf, radl, radl, 1, auth) success, msg = res[0] self.assertFalse(success) self.assertEqual(msg, "ERROR: Error msg")
def test_20_launch(self, save_data, requests): radl_data = """ network net1 (outbound = 'yes') network net2 (outports = '8080,9000/udp') network net3 (federated = 'yes' and providers = 'p1,p2') network net4 (federated = 'yes' and providers = ['p1','p2']) system test ( gpu.count=1 and cpu.sgx.epc_size=8 and cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'net1' and net_interface.0.dns_name = 'test' and net_interface.1.connection = 'net2' and net_interface.2.connection = 'net3' and net_interface.3.connection = 'net4' and availability_zone = 'cloud@site' and disk.0.os.name = 'linux' and disk.0.image.url = 'fbw://server.com/fogbow-ubuntu' and disk.0.os.credentials.username = '******' )""" radl = radl_parse.parse_radl(radl_data) radl.check() auth = Authentication([{ 'id': 'fogbow', 'type': 'FogBow', 'token': 'user', 'host': 'server.com' }]) fogbow_cloud = self.get_fogbow_cloud() requests.side_effect = self.get_response inf = InfrastructureInfo() inf.auth = auth inf.radl = radl res = fogbow_cloud.launch(inf, radl, radl, 1, auth) success, _ = res[0] self.assertTrue(success, msg="ERROR: launching a VM.") self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue()) data = json.loads(requests.call_args_list[4][1]["data"]) self.assertEqual(data["allocationMode"], "dynamic") data = json.loads(requests.call_args_list[7][1]["data"]) self.assertEqual( data, { "direction": "IN", "protocol": "TCP", "etherType": "IPv4", "portTo": 8080, "portFrom": 8080, "cidr": "0.0.0.0/0" }) data = json.loads(requests.call_args_list[8][1]["data"]) self.assertEqual( data, { "direction": "IN", "protocol": "UDP", "etherType": "IPv4", "portTo": 9000, "portFrom": 9000, "cidr": "0.0.0.0/0" }) data = json.loads(requests.call_args_list[9][1]["data"]) self.assertEqual(data['cidr'], "10.0.2.0/24") data = json.loads(requests.call_args_list[13][1]["data"]) self.assertEqual(data["compute"]["cloudName"], "cloud") self.assertEqual(data["compute"]["provider"], "site") self.assertEqual(data["compute"]["vCPU"], 1) self.assertEqual(data["compute"]["memory"], 512) self.assertEqual(data["compute"]["imageId"], "fogbow-ubuntu") self.assertEqual(data["compute"]["requirements"], { 'sgx:epc_size': '8', 'gpu': 'true' }) self.assertEqual(data["federatedNetworkId"], "1")
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)