def test_wait_for(self): def get_fake_checker_delayed(**delay): deadline = datetime.datetime.now() + datetime.timedelta(**delay) return lambda obj: datetime.datetime.now() > deadline def fake_checker_false(obj): return False def fake_updater(obj): return obj resource = object() fake_checker_delayed = get_fake_checker_delayed(seconds=0.3) loaded_resource = utils.wait_for(resource, fake_checker_delayed, fake_updater, 1, 0.2) self.assertEqual(loaded_resource, resource) loaded_resource = utils.wait_for(resource, fake_checker_delayed, None, 1, 0.2) self.assertEqual(loaded_resource, resource) self.assertRaises(exceptions.TimeoutException, utils.wait_for, object(), fake_checker_false, fake_updater, 0.3, 0.1)
def _delete_image(cls, image): """Deletes the given image. Returns when the image is actually deleted. :param image: Image object """ image.delete() utils.wait_for(image, is_ready=bench_utils.resource_is("DELETED"), update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=3)
def _delete_server(cls, server): """Deletes the given server. Returns when the server is actually deleted. :param server: Server object """ server.delete() utils.wait_for(server, is_ready=bench_utils.is_none, update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=3)
def _delete_volume(cls, volume): """Delete the given volume. Returns when the volume is actually deleted. :param volume: volume object """ volume.delete() utils.wait_for(volume, is_ready=bench_utils.is_none, update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=2)
def _stop_server(cls, server): """Stop the given server. Issues a stop on the given server and waits for the server to become SHUTOFF. :param server: The server to stop. """ server.stop() utils.wait_for(server, is_ready=bench_utils.resource_is("SHUTOFF"), update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=2)
def _wait_for_list_statuses(mgr, statuses, list_query=None, timeout=10, check_interval=1): list_query = list_query or {} def _list_statuses(mgr): for resource in mgr.list(**list_query): if resource.status not in statuses: return False return True utils.wait_for(mgr, is_ready=_list_statuses, update_resource=None, timeout=timeout, check_interval=check_interval)
def _unrescue_server(cls, server): """Unrescue the given server. Returns when the server is unrescue and waits to become ACTIVE :param server: Server object """ server.unrescue() time.sleep(2) utils.wait_for(server, is_ready=bench_utils.resource_is("ACTIVE"), update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=3)
def _start_server(cls, server): """Starts the given server. A start will be issued for the given server upon which time this method will wait for it to become ACTIVE. :param server: The server to start and wait to become ACTIVE. """ server.start() utils.wait_for(server, is_ready=bench_utils.resource_is("ACTIVE"), update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=2)
def _suspend_server(cls, server): """Suspends the given server. Returns when the server is actually suspended and is in the "Suspended" state. :param server: Server object """ server.suspend() time.sleep(2) utils.wait_for(server, is_ready=bench_utils.resource_is("SUSPENDED"), update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=3)
def _rescue_server(cls, server): """Rescue the given server. Returns when the server is actually rescue and is in the "Rescue" state. :param server: Server object """ server.rescue() time.sleep(2) utils.wait_for(server, is_ready=bench_utils.resource_is("RESCUE"), update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=3)
def _reboot_server(cls, server, soft=True): """Reboots the given server using hard or soft reboot. A reboot will be issued on the given server upon which time this method will wait for the server to become active. :param server: The server to reboot. :param soft: False if hard reboot should be used, otherwise soft reboot is done (default). """ server.reboot(reboot_type=("SOFT" if soft else "HARD")) time.sleep(5) utils.wait_for(server, is_ready=bench_utils.resource_is("ACTIVE"), update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=3)
def _boot_servers(cls, name_prefix, image_id, flavor_id, requests, instances_per_request=1, **kwargs): """Boots multiple servers. Returns when all the servers are actually booted and are in the "Active" state. :param name_prefix: The prefix to use while naming the created servers. The rest of the server names will be '_No.' :param image_id: ID of the image to be used for server creation :param flavor_id: ID of the flavor to be used for server creation :param requests: Number of booting requests to perform :param instances_per_request: Number of instances to boot per each request :returns: List of created server objects """ for i in range(requests): cls.clients("nova").servers.create('%s_%d' % (name_prefix, i), image_id, flavor_id, min_count=instances_per_request, max_count=instances_per_request, **kwargs) # NOTE(msdubov): Nova python client returns only one server even when # min_count > 1, so we have to rediscover all the # created servers manyally. servers = filter(lambda server: server.name.startswith(name_prefix), cls.clients("nova").servers.list()) time.sleep(5) servers = [utils.wait_for(server, is_ready=_resource_is("ACTIVE"), update_resource=_get_from_manager, timeout=600, check_interval=3) for server in servers] return servers
def _boot_server(cls, server_name, image_id, flavor_id, **kwargs): """Boots one server. Returns when the server is actually booted and is in the "Active" state. :param server_name: String used to name the server :param image_id: ID of the image to be used for server creation :param flavor_id: ID of the flavor to be used for server creation :param **kwargs: Other optional parameters to initialize the server :returns: Created server object """ if 'security_groups' not in kwargs: kwargs['security_groups'] = ['rally_open'] else: if 'rally_open' not in kwargs['security_groups']: kwargs['security_groups'].append('rally_open') server = cls.clients("nova").servers.create( server_name, image_id, flavor_id, **kwargs) # NOTE(msdubov): It is reasonable to wait 5 secs before starting to # check whether the server is ready => less API calls. time.sleep(5) server = utils.wait_for(server, is_ready=bench_utils.resource_is("ACTIVE"), update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=3) return server
def _delete_server(cls, server): """Deletes the given server. Returns when the server is actually deleted. :param server: Server object """ server.delete() # NOTE(msdubov): When the server gets deleted, the # clients("nova").servers.get() method raises # a NotFound exception. try: utils.wait_for(server, is_ready=_false, update_resource=_get_from_manager, timeout=600, check_interval=3) except exceptions.NotFound: pass
def create_servers(self): """Create VMs with chosen image.""" image_uuid = self.get_image_uuid() userdata = self.get_userdata() flavor = self.config['flavor_id'] public_key_path = self.config.get( 'ssh_public_key_file', os.path.expanduser('~/.ssh/id_rsa.pub')) public_key = open(public_key_path, 'r').read().strip() key_name = self.config['deployment_name'] + '-key' keypair = self.nova.keypairs.create(key_name, public_key) self.resources.create({'id': keypair.id}, type=KEYPAIR_TYPE) os_servers = [] for i in range(self.config.get('amount', 1)): name = "%s-%d" % (self.config['deployment_name'], i) server = self.nova.servers.create(name, image_uuid, flavor, key_name=keypair.name, userdata=userdata) os_servers.append(server) self.resources.create({'id': server.id}, type=SERVER_TYPE) kwargs = { 'is_ready': benchmark_utils.resource_is("ACTIVE"), 'update_resource': benchmark_utils.get_from_manager(), 'timeout': 120, 'check_interval': 5 } for os_server in os_servers: utils.wait_for(os_server, **kwargs) servers = [provider.Server(ip=s.addresses.values()[0][0]['addr'], user='******', key=public_key_path) for s in os_servers] for s in servers: s.ssh.wait(timeout=120, interval=5) # NOTE(eyerediskin): usually ssh is ready much earlier then cloud-init time.sleep(8) return servers
def _create_image(cls, server): """Creates an image of the given server Uses the server name to name the created image. Returns when the image is actually created and is in the "Active" state. :param server: Server object for which the image will be created :returns: Created image object """ image_uuid = cls.clients("nova").servers.create_image(server, server.name) image = cls.clients("nova").images.get(image_uuid) image = utils.wait_for(image, is_ready=_resource_is("ACTIVE"), update_resource=_get_from_manager, timeout=600, check_interval=3) return image
def _create_volume(cls, size, **kwargs): """create one volume. Returns when the volume is actually created and is in the "Available" state. :param size: int be size of volume in GB :param **kwargs: Other optional parameters to initialize the volume :returns: Created volume object """ volumename = kwargs.get('display_name', generate_volume_name(10)) kwargs['display_name'] = volumename volume = cls.clients("cinder").volumes.create(size, **kwargs) # NOTE(msdubov): It is reasonable to wait 5 secs before starting to # check whether the volume is ready => less API calls. time.sleep(3) volume = utils.wait_for(volume, is_ready=bench_utils.resource_is("available"), update_resource=bench_utils.get_from_manager(), timeout=600, check_interval=3) return volume
def _wait_for_list_size(mgr, sizes=[0], timeout=10, check_interval=1): utils.wait_for(mgr, is_ready=manager_list_size(sizes), update_resource=None, timeout=timeout, check_interval=check_interval)