def test_non_existant_instance(self, monkeypatch): ref_name = 'test-123' ref_image = image.Image('file:///someimage.qcow2') stub_listdir = mock.Mock() stub_listdir.return_value = [] monkeypatch.setattr(os, 'listdir', stub_listdir) test_instance = instance.find_instance(ref_name, ref_image) assert test_instance is None
def test_find_exist_instance(self, monkeypatch): ref_name = 'test-123' ref_image = image.Image('file:///someimage.qcow2') ref_path = os.path.join(self.conf.DATA_DIR, 'instances/{}'.format(ref_name)) stub_listdir = mock.Mock() stub_listdir.return_value = [ref_name] monkeypatch.setattr(os, 'listdir', stub_listdir) test_instance = instance.find_instance(ref_name, ref_image) assert test_instance.path == ref_path
def prepare(self, distro, release, flavor, arch): '''Prepare a virtual machine for running tasks. :param str distro: Distro to use in image discovery :param str release: Distro's release to use in image discovery :param str flavor: base-image flavor to use in image discovery :param str arch: arch to use in image discovery :raises CheckbRemoteError: if there are any errors while preparing the virtual machine. ''' tc_image = self._prepare_image(distro, release, flavor, arch) self._check_existing_instance(should_exist=False) self._prepare_instance(tc_image) tc_instance = instance.find_instance(self.instancename) self.ipaddr = tc_instance.get_ip()
def _check_existing_instance(self, should_exist=False): '''Check whether an instance with the same name already exists, raise errors if the result was not as expected. :param should_exist: sets expectation on whether the instance should exist already or not :raises CheckbRemoteError: if the preset expectation is not met ''' existing_instance = instance.find_instance(self.instancename) if existing_instance is None: if should_exist: raise exc.CheckbRemoteError( "Was expecting to find instance {} but it does not" " already exist".format(self.instancename)) else: if not should_exist: raise exc.CheckbRemoteError( "Expected to NOT find instance {} but it is " "already defined".format(self.instancename)) return existing_instance