def test_dev_os_map(self): populate_dir(self.tmp, CFG_DRIVE_FILES_V2) cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) found = ds.read_config_drive(self.tmp) os_md = found['metadata'] cfg_ds.metadata = os_md name_tests = { 'ami': '/dev/vda1', 'root': '/dev/vda1', 'ephemeral0': '/dev/vda2', 'swap': '/dev/vda3', } for name, dev_name in name_tests.items(): with ExitStack() as mocks: find_mock = mocks.enter_context( mock.patch.object(util, 'find_devs_with', return_value=[dev_name])) exists_mock = mocks.enter_context( mock.patch.object(os.path, 'exists', return_value=True)) self.assertEqual(dev_name, cfg_ds.device_name_to_device(name)) find_mock.assert_called_once_with(mock.ANY) exists_mock.assert_called_once_with(mock.ANY)
def test_dev_ec2_remap(self): populate_dir(self.tmp, CFG_DRIVE_FILES_V2) cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) found = ds.read_config_drive(self.tmp) ec2_md = found['ec2-metadata'] os_md = found['metadata'] cfg_ds.ec2_metadata = ec2_md cfg_ds.metadata = os_md name_tests = { 'ami': '/dev/vda1', 'root': '/dev/vda1', 'ephemeral0': '/dev/vda2', 'swap': '/dev/vda3', None: None, 'bob': None, 'root2k': None, } for name, dev_name in name_tests.items(): # We want os.path.exists() to return False on its first call, # and True on its second call. We use a handy generator as # the mock side effect for this. The mocked function returns # what the side effect returns. def exists_side_effect(): yield False yield True with mock.patch.object(os.path, 'exists', side_effect=exists_side_effect()): self.assertEqual(dev_name, cfg_ds.device_name_to_device(name))
def test_dev_os_remap(self): populate_dir(self.tmp, CFG_DRIVE_FILES_V2) cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) found = ds.read_config_drive_dir(self.tmp) cfg_ds.metadata = found['metadata'] name_tests = { 'ami': '/dev/vda1', 'root': '/dev/vda1', 'ephemeral0': '/dev/vda2', 'swap': '/dev/vda3', } for name, dev_name in name_tests.items(): with unit_helpers.mocker() as my_mock: find_mock = my_mock.replace(util.find_devs_with, spec=False, passthrough=False) provided_name = dev_name[len('/dev/'):] provided_name = "s" + provided_name[1:] find_mock(mocker.ARGS) my_mock.result([provided_name]) exists_mock = my_mock.replace(os.path.exists, spec=False, passthrough=False) exists_mock(mocker.ARGS) my_mock.result(False) exists_mock(mocker.ARGS) my_mock.result(True) my_mock.replay() device = cfg_ds.device_name_to_device(name) self.assertEquals(dev_name, device)
def test_dev_os_remap(self): populate_dir(self.tmp, CFG_DRIVE_FILES_V2) cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) found = ds.read_config_drive(self.tmp) cfg_ds.metadata = found['metadata'] name_tests = { 'ami': '/dev/vda1', 'root': '/dev/vda1', 'ephemeral0': '/dev/vda2', 'swap': '/dev/vda3', } for name, dev_name in name_tests.items(): with ExitStack() as mocks: provided_name = dev_name[len('/dev/'):] provided_name = "s" + provided_name[1:] find_mock = mocks.enter_context( mock.patch.object(util, 'find_devs_with', return_value=[provided_name])) # We want os.path.exists() to return False on its first call, # and True on its second call. We use a handy generator as # the mock side effect for this. The mocked function returns # what the side effect returns. def exists_side_effect(): yield False yield True exists_mock = mocks.enter_context( mock.patch.object(os.path, 'exists', side_effect=exists_side_effect())) self.assertEqual(dev_name, cfg_ds.device_name_to_device(name)) find_mock.assert_called_once_with(mock.ANY) self.assertEqual(exists_mock.call_count, 2)
def test_dev_ec2_map(self): populate_dir(self.tmp, CFG_DRIVE_FILES_V2) cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) found = ds.read_config_drive_dir(self.tmp) exists_mock = self.mocker.replace(os.path.exists, spec=False, passthrough=False) exists_mock(mocker.ARGS) self.mocker.count(0, None) self.mocker.result(True) self.mocker.replay() ec2_md = found['ec2-metadata'] os_md = found['metadata'] cfg_ds.ec2_metadata = ec2_md cfg_ds.metadata = os_md name_tests = { 'ami': '/dev/sda1', 'root': '/dev/sda1', 'ephemeral0': '/dev/sda2', 'swap': '/dev/sda3', None: None, 'bob': None, 'root2k': None, } for name, dev_name in name_tests.items(): device = cfg_ds.device_name_to_device(name) self.assertEquals(dev_name, device)
def cfg_ds_from_dir(seed_d): cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) cfg_ds.seed_dir = seed_d cfg_ds.known_macs = KNOWN_MACS.copy() if not cfg_ds.get_data(): raise RuntimeError("Data source did not extract itself from" " seed directory %s" % seed_d) return cfg_ds
def test_subplatform_config_drive_when_starts_with_dev(self): """subplatform reports config-drive when source starts with /dev/.""" cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) with mock.patch(M_PATH + "find_candidate_devs") as m_find_devs: with mock.patch(M_PATH + "util.mount_cb"): with mock.patch(M_PATH + "on_first_boot"): m_find_devs.return_value = ["/dev/anything"] self.assertEqual(True, cfg_ds.get_data()) self.assertEqual("config-disk (/dev/anything)", cfg_ds.subplatform)
def test_subplatform_config_drive_when_starts_with_dev(self): """subplatform reports config-drive when source starts with /dev/.""" cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) with mock.patch(M_PATH + 'find_candidate_devs') as m_find_devs: with mock.patch(M_PATH + 'util.is_FreeBSD', return_value=False): with mock.patch(M_PATH + 'util.mount_cb'): with mock.patch(M_PATH + 'on_first_boot'): m_find_devs.return_value = ['/dev/anything'] self.assertEqual(True, cfg_ds.get_data()) self.assertEqual('config-disk (/dev/anything)', cfg_ds.subplatform)
def cfg_ds_from_dir(base_d, files=None): run = os.path.join(base_d, "run") os.mkdir(run) cfg_ds = ds.DataSourceConfigDrive( settings.CFG_BUILTIN, None, helpers.Paths({'run_dir': run})) cfg_ds.seed_dir = os.path.join(base_d, "seed") if files: populate_dir(cfg_ds.seed_dir, files) cfg_ds.known_macs = KNOWN_MACS.copy() if not cfg_ds.get_data(): raise RuntimeError("Data source did not extract itself from" " seed directory %s" % cfg_ds.seed_dir) return cfg_ds
def test_dev_ec2_map(self): populate_dir(self.tmp, CFG_DRIVE_FILES_V2) cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) found = ds.read_config_drive(self.tmp) ec2_md = found['ec2-metadata'] os_md = found['metadata'] cfg_ds.ec2_metadata = ec2_md cfg_ds.metadata = os_md name_tests = { 'ami': '/dev/sda1', 'root': '/dev/sda1', 'ephemeral0': '/dev/sda2', 'swap': '/dev/sda3', None: None, 'bob': None, 'root2k': None, } for name, dev_name in name_tests.items(): with mock.patch.object(os.path, 'exists', return_value=True): self.assertEqual(dev_name, cfg_ds.device_name_to_device(name))
def test_dev_ec2_map(self): populate_dir(self.tmp, CFG_DRIVE_FILES_V2) cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) found = ds.read_config_drive(self.tmp) ec2_md = found["ec2-metadata"] os_md = found["metadata"] cfg_ds.ec2_metadata = ec2_md cfg_ds.metadata = os_md name_tests = { "ami": "/dev/sda1", "root": "/dev/sda1", "ephemeral0": "/dev/sda2", "swap": "/dev/sda3", None: None, "bob": None, "root2k": None, } for name, dev_name in name_tests.items(): with mock.patch.object(os.path, "exists", return_value=True): self.assertEqual(dev_name, cfg_ds.device_name_to_device(name))
def cfg_ds_from_dir(seed_d): found = ds.read_config_drive_dir(seed_d) cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None, helpers.Paths({})) populate_ds_from_read_config(cfg_ds, seed_d, found) return cfg_ds