class TestIsDiskUsed(TestCase): def setUp(self): super(TestIsDiskUsed, self).setUp() self.patches = ExitStack() mod_name = "cloudinit.config.cc_disk_setup" self.enumerate_disk = self.patches.enter_context( mock.patch("{0}.enumerate_disk".format(mod_name))) self.check_fs = self.patches.enter_context( mock.patch("{0}.check_fs".format(mod_name))) def tearDown(self): super(TestIsDiskUsed, self).tearDown() self.patches.close() def test_multiple_child_nodes_returns_true(self): self.enumerate_disk.return_value = (mock.MagicMock() for _ in range(2)) self.check_fs.return_value = (mock.MagicMock(), None, mock.MagicMock()) self.assertTrue(cc_disk_setup.is_disk_used(mock.MagicMock())) def test_valid_filesystem_returns_true(self): self.enumerate_disk.return_value = (mock.MagicMock() for _ in range(1)) self.check_fs.return_value = ( mock.MagicMock(), "ext4", mock.MagicMock(), ) self.assertTrue(cc_disk_setup.is_disk_used(mock.MagicMock())) def test_one_child_nodes_and_no_fs_returns_false(self): self.enumerate_disk.return_value = (mock.MagicMock() for _ in range(1)) self.check_fs.return_value = (mock.MagicMock(), None, mock.MagicMock()) self.assertFalse(cc_disk_setup.is_disk_used(mock.MagicMock()))
def setUp(self): super().setUp() patches = ExitStack() self.addCleanup(patches.close) self.load_file = patches.enter_context( mock.patch.object(bsd_utils.util, 'load_file')) self.write_file = patches.enter_context( mock.patch.object(bsd_utils.util, 'write_file'))
def setUp(self): super(TestWalkerHandleHandler, self).setUp() tmpdir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, tmpdir) self.data = { "handlercount": 0, "frequency": "", "handlerdir": tmpdir, "handlers": helpers.ContentHandlers(), "data": None, } self.expected_module_name = "part-handler-%03d" % ( self.data["handlercount"], ) expected_file_name = "%s.py" % self.expected_module_name self.expected_file_fullname = os.path.join( self.data["handlerdir"], expected_file_name ) self.module_fake = FakeModule() self.ctype = None self.filename = None self.payload = "dummy payload" # Mock the write_file() function. We'll assert that it got called as # expected in each of the individual tests. resources = ExitStack() self.addCleanup(resources.close) self.write_file_mock = resources.enter_context( mock.patch("cloudinit.util.write_file") )
class TestGetMbrHddSize(TestCase): def setUp(self): super(TestGetMbrHddSize, self).setUp() self.patches = ExitStack() self.subp = self.patches.enter_context( mock.patch.object(cc_disk_setup.subp, "subp")) def tearDown(self): super(TestGetMbrHddSize, self).tearDown() self.patches.close() def _configure_subp_mock(self, hdd_size_in_bytes, sector_size_in_bytes): def _subp(cmd, *args, **kwargs): self.assertEqual(3, len(cmd)) if "--getsize64" in cmd: return hdd_size_in_bytes, None elif "--getss" in cmd: return sector_size_in_bytes, None raise Exception("Unexpected blockdev command called") self.subp.side_effect = _subp def _test_for_sector_size(self, sector_size): size_in_bytes = random.randint(10000, 10000000) * 512 size_in_sectors = size_in_bytes / sector_size self._configure_subp_mock(size_in_bytes, sector_size) self.assertEqual(size_in_sectors, cc_disk_setup.get_hdd_size("/dev/sda1")) def test_size_for_512_byte_sectors(self): self._test_for_sector_size(512) def test_size_for_1024_byte_sectors(self): self._test_for_sector_size(1024) def test_size_for_2048_byte_sectors(self): self._test_for_sector_size(2048) def test_size_for_4096_byte_sectors(self): self._test_for_sector_size(4096)
class TestNoCloudDataSource(CiTestCase): def setUp(self): super(TestNoCloudDataSource, self).setUp() self.tmp = self.tmp_dir() self.paths = helpers.Paths( {"cloud_dir": self.tmp, "run_dir": self.tmp} ) self.cmdline = "root=TESTCMDLINE" self.mocks = ExitStack() self.addCleanup(self.mocks.close) self.mocks.enter_context( mock.patch.object(util, "get_cmdline", return_value=self.cmdline) ) self.mocks.enter_context( mock.patch.object(dmi, "read_dmi_data", return_value=None) ) def _test_fs_config_is_read(self, fs_label, fs_label_to_search): vfat_device = "device-1" def m_mount_cb(device, callback, mtype): if device == vfat_device: return {"meta-data": yaml.dump({"instance-id": "IID"})} else: return {} def m_find_devs_with(query="", path=""): if "TYPE=vfat" == query: return [vfat_device] elif "LABEL={}".format(fs_label) == query: return [vfat_device] else: return [] self.mocks.enter_context( mock.patch.object( util, "find_devs_with", side_effect=m_find_devs_with ) ) self.mocks.enter_context( mock.patch.object(util, "mount_cb", side_effect=m_mount_cb) ) sys_cfg = {"datasource": {"NoCloud": {"fs_label": fs_label_to_search}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertEqual(dsrc.metadata.get("instance-id"), "IID") self.assertTrue(ret) def test_nocloud_seed_dir_on_lxd(self, m_is_lxd): md = {"instance-id": "IID", "dsmode": "local"} ud = b"USER_DATA_HERE" seed_dir = os.path.join(self.paths.seed_dir, "nocloud") populate_dir( seed_dir, {"user-data": ud, "meta-data": yaml.safe_dump(md)} ) sys_cfg = {"datasource": {"NoCloud": {"fs_label": None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertEqual(dsrc.userdata_raw, ud) self.assertEqual(dsrc.metadata, md) self.assertEqual(dsrc.platform_type, "lxd") self.assertEqual(dsrc.subplatform, "seed-dir (%s)" % seed_dir) self.assertTrue(ret) def test_nocloud_seed_dir_non_lxd_platform_is_nocloud(self, m_is_lxd): """Non-lxd environments will list nocloud as the platform.""" m_is_lxd.return_value = False md = {"instance-id": "IID", "dsmode": "local"} seed_dir = os.path.join(self.paths.seed_dir, "nocloud") populate_dir( seed_dir, {"user-data": "", "meta-data": yaml.safe_dump(md)} ) sys_cfg = {"datasource": {"NoCloud": {"fs_label": None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) self.assertTrue(dsrc.get_data()) self.assertEqual(dsrc.platform_type, "nocloud") self.assertEqual(dsrc.subplatform, "seed-dir (%s)" % seed_dir) def test_fs_label(self, m_is_lxd): # find_devs_with should not be called ff fs_label is None class PsuedoException(Exception): pass self.mocks.enter_context( mock.patch.object( util, "find_devs_with", side_effect=PsuedoException ) ) # by default, NoCloud should search for filesystems by label sys_cfg = {"datasource": {"NoCloud": {}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) self.assertRaises(PsuedoException, dsrc.get_data) # but disabling searching should just end up with None found sys_cfg = {"datasource": {"NoCloud": {"fs_label": None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertFalse(ret) def test_fs_config_lowercase_label(self, m_is_lxd): self._test_fs_config_is_read("cidata", "cidata") def test_fs_config_uppercase_label(self, m_is_lxd): self._test_fs_config_is_read("CIDATA", "cidata") def test_fs_config_lowercase_label_search_uppercase(self, m_is_lxd): self._test_fs_config_is_read("cidata", "CIDATA") def test_fs_config_uppercase_label_search_uppercase(self, m_is_lxd): self._test_fs_config_is_read("CIDATA", "CIDATA") def test_no_datasource_expected(self, m_is_lxd): # no source should be found if no cmdline, config, and fs_label=None sys_cfg = {"datasource": {"NoCloud": {"fs_label": None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) self.assertFalse(dsrc.get_data()) def test_seed_in_config(self, m_is_lxd): data = { "fs_label": None, "meta-data": yaml.safe_dump({"instance-id": "IID"}), "user-data": b"USER_DATA_RAW", } sys_cfg = {"datasource": {"NoCloud": data}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertEqual(dsrc.userdata_raw, b"USER_DATA_RAW") self.assertEqual(dsrc.metadata.get("instance-id"), "IID") self.assertTrue(ret) def test_nocloud_seed_with_vendordata(self, m_is_lxd): md = {"instance-id": "IID", "dsmode": "local"} ud = b"USER_DATA_HERE" vd = b"THIS IS MY VENDOR_DATA" populate_dir( os.path.join(self.paths.seed_dir, "nocloud"), { "user-data": ud, "meta-data": yaml.safe_dump(md), "vendor-data": vd, }, ) sys_cfg = {"datasource": {"NoCloud": {"fs_label": None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertEqual(dsrc.userdata_raw, ud) self.assertEqual(dsrc.metadata, md) self.assertEqual(dsrc.vendordata_raw, vd) self.assertTrue(ret) def test_nocloud_no_vendordata(self, m_is_lxd): populate_dir( os.path.join(self.paths.seed_dir, "nocloud"), {"user-data": b"ud", "meta-data": "instance-id: IID\n"}, ) sys_cfg = {"datasource": {"NoCloud": {"fs_label": None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertEqual(dsrc.userdata_raw, b"ud") self.assertFalse(dsrc.vendordata) self.assertTrue(ret) def test_metadata_network_interfaces(self, m_is_lxd): gateway = "103.225.10.1" md = { "instance-id": "i-abcd", "local-hostname": "hostname1", "network-interfaces": textwrap.dedent( """\ auto eth0 iface eth0 inet static hwaddr 00:16:3e:70:e1:04 address 103.225.10.12 netmask 255.255.255.0 gateway """ + gateway + """ dns-servers 8.8.8.8""" ), } populate_dir( os.path.join(self.paths.seed_dir, "nocloud"), {"user-data": b"ud", "meta-data": yaml.dump(md) + "\n"}, ) sys_cfg = {"datasource": {"NoCloud": {"fs_label": None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertTrue(ret) # very simple check just for the strings above self.assertIn(gateway, str(dsrc.network_config)) def test_metadata_network_config(self, m_is_lxd): # network-config needs to get into network_config netconf = { "version": 1, "config": [ { "type": "physical", "name": "interface0", "subnets": [{"type": "dhcp"}], } ], } populate_dir( os.path.join(self.paths.seed_dir, "nocloud"), { "user-data": b"ud", "meta-data": "instance-id: IID\n", "network-config": yaml.dump(netconf) + "\n", }, ) sys_cfg = {"datasource": {"NoCloud": {"fs_label": None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertTrue(ret) self.assertEqual(netconf, dsrc.network_config) def test_metadata_network_config_over_interfaces(self, m_is_lxd): # network-config should override meta-data/network-interfaces gateway = "103.225.10.1" md = { "instance-id": "i-abcd", "local-hostname": "hostname1", "network-interfaces": textwrap.dedent( """\ auto eth0 iface eth0 inet static hwaddr 00:16:3e:70:e1:04 address 103.225.10.12 netmask 255.255.255.0 gateway """ + gateway + """ dns-servers 8.8.8.8""" ), } netconf = { "version": 1, "config": [ { "type": "physical", "name": "interface0", "subnets": [{"type": "dhcp"}], } ], } populate_dir( os.path.join(self.paths.seed_dir, "nocloud"), { "user-data": b"ud", "meta-data": yaml.dump(md) + "\n", "network-config": yaml.dump(netconf) + "\n", }, ) sys_cfg = {"datasource": {"NoCloud": {"fs_label": None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertTrue(ret) self.assertEqual(netconf, dsrc.network_config) self.assertNotIn(gateway, str(dsrc.network_config)) @mock.patch("cloudinit.util.blkid") def test_nocloud_get_devices_freebsd(self, m_is_lxd, fake_blkid): populate_dir( os.path.join(self.paths.seed_dir, "nocloud"), {"user-data": b"ud", "meta-data": "instance-id: IID\n"}, ) sys_cfg = {"datasource": {"NoCloud": {"fs_label": None}}} self.mocks.enter_context( mock.patch.object(util, "is_FreeBSD", return_value=True) ) def _mfind_devs_with_freebsd( criteria=None, oformat="device", tag=None, no_cache=False, path=None, ): if not criteria: return ["/dev/msdosfs/foo", "/dev/iso9660/foo"] if criteria.startswith("LABEL="): return ["/dev/msdosfs/foo", "/dev/iso9660/foo"] elif criteria == "TYPE=vfat": return ["/dev/msdosfs/foo"] elif criteria == "TYPE=iso9660": return ["/dev/iso9660/foo"] return [] self.mocks.enter_context( mock.patch.object( util, "find_devs_with_freebsd", side_effect=_mfind_devs_with_freebsd, ) ) dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc._get_devices("foo") self.assertEqual(["/dev/msdosfs/foo", "/dev/iso9660/foo"], ret) fake_blkid.assert_not_called()
class TestNoCloudDataSource(CiTestCase): def setUp(self): super(TestNoCloudDataSource, self).setUp() self.tmp = self.tmp_dir() self.paths = helpers.Paths({ 'cloud_dir': self.tmp, 'run_dir': self.tmp }) self.cmdline = "root=TESTCMDLINE" self.mocks = ExitStack() self.addCleanup(self.mocks.close) self.mocks.enter_context( mock.patch.object(util, 'get_cmdline', return_value=self.cmdline)) self.mocks.enter_context( mock.patch.object(dmi, 'read_dmi_data', return_value=None)) def _test_fs_config_is_read(self, fs_label, fs_label_to_search): vfat_device = 'device-1' def m_mount_cb(device, callback, mtype): if (device == vfat_device): return {'meta-data': yaml.dump({'instance-id': 'IID'})} else: return {} def m_find_devs_with(query='', path=''): if 'TYPE=vfat' == query: return [vfat_device] elif 'LABEL={}'.format(fs_label) == query: return [vfat_device] else: return [] self.mocks.enter_context( mock.patch.object(util, 'find_devs_with', side_effect=m_find_devs_with)) self.mocks.enter_context( mock.patch.object(util, 'mount_cb', side_effect=m_mount_cb)) sys_cfg = {'datasource': {'NoCloud': {'fs_label': fs_label_to_search}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertEqual(dsrc.metadata.get('instance-id'), 'IID') self.assertTrue(ret) def test_nocloud_seed_dir_on_lxd(self, m_is_lxd): md = {'instance-id': 'IID', 'dsmode': 'local'} ud = b"USER_DATA_HERE" seed_dir = os.path.join(self.paths.seed_dir, "nocloud") populate_dir(seed_dir, { 'user-data': ud, 'meta-data': yaml.safe_dump(md) }) sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertEqual(dsrc.userdata_raw, ud) self.assertEqual(dsrc.metadata, md) self.assertEqual(dsrc.platform_type, 'lxd') self.assertEqual(dsrc.subplatform, 'seed-dir (%s)' % seed_dir) self.assertTrue(ret) def test_nocloud_seed_dir_non_lxd_platform_is_nocloud(self, m_is_lxd): """Non-lxd environments will list nocloud as the platform.""" m_is_lxd.return_value = False md = {'instance-id': 'IID', 'dsmode': 'local'} seed_dir = os.path.join(self.paths.seed_dir, "nocloud") populate_dir(seed_dir, { 'user-data': '', 'meta-data': yaml.safe_dump(md) }) sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) self.assertTrue(dsrc.get_data()) self.assertEqual(dsrc.platform_type, 'nocloud') self.assertEqual(dsrc.subplatform, 'seed-dir (%s)' % seed_dir) def test_fs_label(self, m_is_lxd): # find_devs_with should not be called ff fs_label is None class PsuedoException(Exception): pass self.mocks.enter_context( mock.patch.object(util, 'find_devs_with', side_effect=PsuedoException)) # by default, NoCloud should search for filesystems by label sys_cfg = {'datasource': {'NoCloud': {}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) self.assertRaises(PsuedoException, dsrc.get_data) # but disabling searching should just end up with None found sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertFalse(ret) def test_fs_config_lowercase_label(self, m_is_lxd): self._test_fs_config_is_read('cidata', 'cidata') def test_fs_config_uppercase_label(self, m_is_lxd): self._test_fs_config_is_read('CIDATA', 'cidata') def test_fs_config_lowercase_label_search_uppercase(self, m_is_lxd): self._test_fs_config_is_read('cidata', 'CIDATA') def test_fs_config_uppercase_label_search_uppercase(self, m_is_lxd): self._test_fs_config_is_read('CIDATA', 'CIDATA') def test_no_datasource_expected(self, m_is_lxd): # no source should be found if no cmdline, config, and fs_label=None sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) self.assertFalse(dsrc.get_data()) def test_seed_in_config(self, m_is_lxd): data = { 'fs_label': None, 'meta-data': yaml.safe_dump({'instance-id': 'IID'}), 'user-data': b"USER_DATA_RAW", } sys_cfg = {'datasource': {'NoCloud': data}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertEqual(dsrc.userdata_raw, b"USER_DATA_RAW") self.assertEqual(dsrc.metadata.get('instance-id'), 'IID') self.assertTrue(ret) def test_nocloud_seed_with_vendordata(self, m_is_lxd): md = {'instance-id': 'IID', 'dsmode': 'local'} ud = b"USER_DATA_HERE" vd = b"THIS IS MY VENDOR_DATA" populate_dir(os.path.join(self.paths.seed_dir, "nocloud"), { 'user-data': ud, 'meta-data': yaml.safe_dump(md), 'vendor-data': vd }) sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertEqual(dsrc.userdata_raw, ud) self.assertEqual(dsrc.metadata, md) self.assertEqual(dsrc.vendordata_raw, vd) self.assertTrue(ret) def test_nocloud_no_vendordata(self, m_is_lxd): populate_dir(os.path.join(self.paths.seed_dir, "nocloud"), { 'user-data': b"ud", 'meta-data': "instance-id: IID\n" }) sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertEqual(dsrc.userdata_raw, b"ud") self.assertFalse(dsrc.vendordata) self.assertTrue(ret) def test_metadata_network_interfaces(self, m_is_lxd): gateway = "103.225.10.1" md = { 'instance-id': 'i-abcd', 'local-hostname': 'hostname1', 'network-interfaces': textwrap.dedent("""\ auto eth0 iface eth0 inet static hwaddr 00:16:3e:70:e1:04 address 103.225.10.12 netmask 255.255.255.0 gateway """ + gateway + """ dns-servers 8.8.8.8""") } populate_dir(os.path.join(self.paths.seed_dir, "nocloud"), { 'user-data': b"ud", 'meta-data': yaml.dump(md) + "\n" }) sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertTrue(ret) # very simple check just for the strings above self.assertIn(gateway, str(dsrc.network_config)) def test_metadata_network_config(self, m_is_lxd): # network-config needs to get into network_config netconf = { 'version': 1, 'config': [{ 'type': 'physical', 'name': 'interface0', 'subnets': [{ 'type': 'dhcp' }] }] } populate_dir( os.path.join(self.paths.seed_dir, "nocloud"), { 'user-data': b"ud", 'meta-data': "instance-id: IID\n", 'network-config': yaml.dump(netconf) + "\n" }) sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertTrue(ret) self.assertEqual(netconf, dsrc.network_config) def test_metadata_network_config_with_toplevel_network(self, m_is_lxd): """network-config may have 'network' top level key.""" netconf = {'config': 'disabled'} populate_dir( os.path.join(self.paths.seed_dir, "nocloud"), { 'user-data': b"ud", 'meta-data': "instance-id: IID\n", 'network-config': yaml.dump({'network': netconf}) + "\n" }) sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertTrue(ret) self.assertEqual(netconf, dsrc.network_config) def test_metadata_network_config_over_interfaces(self, m_is_lxd): # network-config should override meta-data/network-interfaces gateway = "103.225.10.1" md = { 'instance-id': 'i-abcd', 'local-hostname': 'hostname1', 'network-interfaces': textwrap.dedent("""\ auto eth0 iface eth0 inet static hwaddr 00:16:3e:70:e1:04 address 103.225.10.12 netmask 255.255.255.0 gateway """ + gateway + """ dns-servers 8.8.8.8""") } netconf = { 'version': 1, 'config': [{ 'type': 'physical', 'name': 'interface0', 'subnets': [{ 'type': 'dhcp' }] }] } populate_dir( os.path.join(self.paths.seed_dir, "nocloud"), { 'user-data': b"ud", 'meta-data': yaml.dump(md) + "\n", 'network-config': yaml.dump(netconf) + "\n" }) sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc.get_data() self.assertTrue(ret) self.assertEqual(netconf, dsrc.network_config) self.assertNotIn(gateway, str(dsrc.network_config)) @mock.patch("cloudinit.util.blkid") def test_nocloud_get_devices_freebsd(self, m_is_lxd, fake_blkid): populate_dir(os.path.join(self.paths.seed_dir, "nocloud"), { 'user-data': b"ud", 'meta-data': "instance-id: IID\n" }) sys_cfg = {'datasource': {'NoCloud': {'fs_label': None}}} self.mocks.enter_context( mock.patch.object(util, 'is_FreeBSD', return_value=True)) def _mfind_devs_with_freebsd(criteria=None, oformat='device', tag=None, no_cache=False, path=None): if not criteria: return ["/dev/msdosfs/foo", "/dev/iso9660/foo"] if criteria.startswith("LABEL="): return ["/dev/msdosfs/foo", "/dev/iso9660/foo"] elif criteria == "TYPE=vfat": return ["/dev/msdosfs/foo"] elif criteria == "TYPE=iso9660": return ["/dev/iso9660/foo"] return [] self.mocks.enter_context( mock.patch.object(util, 'find_devs_with_freebsd', side_effect=_mfind_devs_with_freebsd)) dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc._get_devices('foo') self.assertEqual(['/dev/msdosfs/foo', '/dev/iso9660/foo'], ret) fake_blkid.assert_not_called()
class TestCloudStackPasswordFetching(CiTestCase): def setUp(self): super(TestCloudStackPasswordFetching, self).setUp() self.patches = ExitStack() self.addCleanup(self.patches.close) mod_name = MOD_PATH self.patches.enter_context(mock.patch('{0}.ec2'.format(mod_name))) self.patches.enter_context(mock.patch('{0}.uhelp'.format(mod_name))) default_gw = "192.201.20.0" get_latest_lease = mock.MagicMock(return_value=None) self.patches.enter_context( mock.patch(mod_name + '.get_latest_lease', get_latest_lease)) get_default_gw = mock.MagicMock(return_value=default_gw) self.patches.enter_context( mock.patch(mod_name + '.get_default_gateway', get_default_gw)) get_networkd_server_address = mock.MagicMock(return_value=None) self.patches.enter_context( mock.patch(mod_name + '.dhcp.networkd_get_option_from_leases', get_networkd_server_address)) self.tmp = self.tmp_dir() def _set_password_server_response(self, response_string): subp = mock.MagicMock(return_value=(response_string, '')) self.patches.enter_context( mock.patch('cloudinit.sources.DataSourceCloudStack.subp.subp', subp)) return subp def test_empty_password_doesnt_create_config(self): self._set_password_server_response('') ds = DataSourceCloudStack({}, None, helpers.Paths({'run_dir': self.tmp})) ds.get_data() self.assertEqual({}, ds.get_config_obj()) def test_saved_password_doesnt_create_config(self): self._set_password_server_response('saved_password') ds = DataSourceCloudStack({}, None, helpers.Paths({'run_dir': self.tmp})) ds.get_data() self.assertEqual({}, ds.get_config_obj()) @mock.patch(DS_PATH + '.wait_for_metadata_service') def test_password_sets_password(self, m_wait): m_wait.return_value = True password = '******' self._set_password_server_response(password) ds = DataSourceCloudStack({}, None, helpers.Paths({'run_dir': self.tmp})) ds.get_data() self.assertEqual(password, ds.get_config_obj()['password']) @mock.patch(DS_PATH + '.wait_for_metadata_service') def test_bad_request_doesnt_stop_ds_from_working(self, m_wait): m_wait.return_value = True self._set_password_server_response('bad_request') ds = DataSourceCloudStack({}, None, helpers.Paths({'run_dir': self.tmp})) self.assertTrue(ds.get_data()) def assertRequestTypesSent(self, subp, expected_request_types): request_types = [] for call in subp.call_args_list: args = call[0][0] for arg in args: if arg.startswith('DomU_Request'): request_types.append(arg.split()[1]) self.assertEqual(expected_request_types, request_types) @mock.patch(DS_PATH + '.wait_for_metadata_service') def test_valid_response_means_password_marked_as_saved(self, m_wait): m_wait.return_value = True password = '******' subp = self._set_password_server_response(password) ds = DataSourceCloudStack({}, None, helpers.Paths({'run_dir': self.tmp})) ds.get_data() self.assertRequestTypesSent(subp, ['send_my_password', 'saved_password']) def _check_password_not_saved_for(self, response_string): subp = self._set_password_server_response(response_string) ds = DataSourceCloudStack({}, None, helpers.Paths({'run_dir': self.tmp})) with mock.patch(DS_PATH + '.wait_for_metadata_service') as m_wait: m_wait.return_value = True ds.get_data() self.assertRequestTypesSent(subp, ['send_my_password']) def test_password_not_saved_if_empty(self): self._check_password_not_saved_for('') def test_password_not_saved_if_already_saved(self): self._check_password_not_saved_for('saved_password') def test_password_not_saved_if_bad_request(self): self._check_password_not_saved_for('bad_request')