def test__get_image_download_in_progress(self): def _create_instance_path(*args): open(master_path, 'w').close() return True temp_dir = tempfile.mkdtemp() instance_path = os.path.join(temp_dir, 'instance_path') fileutils.ensure_tree(temp_dir) master_uuid = 'instance_uuid' master_path = os.path.join(temp_dir, master_uuid) lock_file = os.path.join(temp_dir, 'instance_uuid.lock') self.mox.StubOutWithMock(pxe, '_download_in_progress') pxe._download_in_progress(lock_file).\ WithSideEffects(_create_instance_path).\ AndReturn(True) self.mox.ReplayAll() pxe._get_image(None, instance_path, master_uuid, temp_dir) self.mox.VerifyAll() self.assertTrue(os.path.exists(instance_path))
def test__get_image_download_in_progress(self): def _create_instance_path(*args): open(master_path, 'w').close() return True temp_dir = tempfile.mkdtemp() instance_path = os.path.join(temp_dir, 'instance_path') fileutils.ensure_tree(temp_dir) master_path = os.path.join(temp_dir, self.node.uuid) lock_file = os.path.join(temp_dir, self.node.uuid + '.lock') with mock.patch.object(pxe, '_download_in_progress') \ as download_in_progress_mock: download_in_progress_mock.side_effect = _create_instance_path pxe._get_image(None, instance_path, self.node.uuid, temp_dir) download_in_progress_mock.assert_called_once_with(lock_file) self.assertTrue(os.path.exists(instance_path))
def test__download_in_progress_wait(self): try: self.config(auth_strategy='keystone') except Exception: opts = [ cfg.StrOpt('auth_strategy', default='keystone'), ] CONF.register_opts(opts) ctx = context.RequestContext(auth_token=True) uuid = 'node_uuid' temp_dir = tempfile.mkdtemp() master_path = os.path.join(temp_dir, 'master_path') instance_path = os.path.join(temp_dir, 'instance_path') os.mkdir(master_path) os.mkdir(instance_path) lock_file = os.path.join(master_path, 'node_uuid.lock') open(lock_file, 'w').close() class handler_deploying(threading.Thread): def __init__(self, lock_file): threading.Thread.__init__(self) self.lock_file = lock_file def run(self): time.sleep(0.2) open(os.path.join(master_path, 'node_uuid'), 'w').close() pxe._remove_download_in_progress_lock(self.lock_file) handler = handler_deploying(lock_file) handler.start() pxe._get_image(ctx, os.path.join(instance_path, 'node_uuid'), uuid, master_path) self.assertFalse(os.path.exists(lock_file)) self.assertTrue( os.path.exists(os.path.join(instance_path, 'node_uuid'))) self.assertEqual( 2, os.stat(os.path.join(master_path, 'node_uuid')).st_nlink)
def test__download_in_progress_wait(self): try: CONF.set_default('auth_strategy', 'keystone') except Exception: opts = [ cfg.StrOpt('auth_strategy', default='keystone'), ] CONF.register_opts(opts) ctx = context.RequestContext(auth_token=True) uuid = 'instance_uuid' temp_dir = tempfile.mkdtemp() master_path = os.path.join(temp_dir, 'master_path') instance_path = os.path.join(temp_dir, 'instance_path') os.mkdir(master_path) os.mkdir(instance_path) lock_file = os.path.join(master_path, 'instance_uuid.lock') open(lock_file, 'w').close() class handler_deploying(threading.Thread): def __init__(self, lock_file): threading.Thread.__init__(self) self.lock_file = lock_file def run(self): time.sleep(2) open(os.path.join(master_path, 'instance_uuid'), 'w').close() pxe._remove_download_in_progress_lock(self.lock_file) handler = handler_deploying(lock_file) handler.start() pxe._get_image(ctx, os.path.join(instance_path, 'instance_uuid'), uuid, master_path) self.assertFalse(os.path.exists(lock_file)) self.assertTrue(os.path.exists(os.path.join(instance_path, 'instance_uuid'))) self.assertEqual(os.stat(os.path.join(master_path, 'instance_uuid')). st_nlink, 2)