Beispiel #1
0
 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()):
             device = cfg_ds.device_name_to_device(name)
             self.assertEqual(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_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_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 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)
Beispiel #5
0
    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))
                device = cfg_ds.device_name_to_device(name)
                self.assertEqual(dev_name, device)

                find_mock.assert_called_once_with(mock.ANY)
                exists_mock.assert_called_once_with(mock.ANY)
    def test_find_candidates(self):
        devs_with_answers = {}

        def my_devs_with(*args, **kwargs):
            criteria = args[0] if len(args) else kwargs.pop('criteria', None)
            return devs_with_answers.get(criteria, [])

        def my_is_partition(dev):
            return dev[-1] in "0123456789" and not dev.startswith("sr")

        try:
            orig_find_devs_with = util.find_devs_with
            util.find_devs_with = my_devs_with

            orig_is_partition = util.is_partition
            util.is_partition = my_is_partition

            devs_with_answers = {"TYPE=vfat": [],
                                 "TYPE=iso9660": ["/dev/vdb"],
                                 "LABEL=config-2": ["/dev/vdb"]}
            self.assertEqual(["/dev/vdb"], ds.find_candidate_devs())

            # add a vfat item
            # zdd reverse sorts after vdb, but config-2 label is preferred
            devs_with_answers['TYPE=vfat'] = ["/dev/zdd"]
            self.assertEqual(["/dev/vdb", "/dev/zdd"],
                             ds.find_candidate_devs())

            # verify that partitions are considered, that have correct label.
            devs_with_answers = {"TYPE=vfat": ["/dev/sda1"],
                                 "TYPE=iso9660": [],
                                 "LABEL=config-2": ["/dev/vdb3"]}
            self.assertEqual(["/dev/vdb3"],
                             ds.find_candidate_devs())

            # Verify that uppercase labels are also found.
            devs_with_answers = {"TYPE=vfat": [],
                                 "TYPE=iso9660": ["/dev/vdb"],
                                 "LABEL=CONFIG-2": ["/dev/vdb"]}
            self.assertEqual(["/dev/vdb"], ds.find_candidate_devs())

        finally:
            util.find_devs_with = orig_find_devs_with
            util.is_partition = orig_is_partition
def populate_ds_from_read_config(cfg_ds, source, results):
    """Patch the DataSourceConfigDrive from the results of
    read_config_drive_dir hopefully in line with what it would have
    if cfg_ds.get_data had been successfully called"""
    cfg_ds.source = source
    cfg_ds.metadata = results.get('metadata')
    cfg_ds.ec2_metadata = results.get('ec2-metadata')
    cfg_ds.userdata_raw = results.get('userdata')
    cfg_ds.version = results.get('version')
    cfg_ds.network_json = results.get('networkdata')
    cfg_ds._network_config = ds.convert_network_data(cfg_ds.network_json)
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):
             device = cfg_ds.device_name_to_device(name)
             self.assertEqual(dev_name, device)
Beispiel #10
0
    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()))
                device = cfg_ds.device_name_to_device(name)
                self.assertEqual(dev_name, device)

                find_mock.assert_called_once_with(mock.ANY)
                self.assertEqual(exists_mock.call_count, 2)
Beispiel #11
0
 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))
Beispiel #12
0
    def test_find_candidates(self):
        devs_with_answers = {}

        def my_devs_with(*args, **kwargs):
            criteria = args[0] if len(args) else kwargs.pop('criteria', None)
            return devs_with_answers.get(criteria, [])

        def my_is_partition(dev):
            return dev[-1] in "0123456789" and not dev.startswith("sr")

        try:
            orig_find_devs_with = util.find_devs_with
            util.find_devs_with = my_devs_with

            orig_is_partition = util.is_partition
            util.is_partition = my_is_partition

            devs_with_answers = {"TYPE=vfat": [],
                "TYPE=iso9660": ["/dev/vdb"],
                "LABEL=config-2": ["/dev/vdb"],
            }
            self.assertEqual(["/dev/vdb"], ds.find_candidate_devs())

            # add a vfat item
            # zdd reverse sorts after vdb, but config-2 label is preferred
            devs_with_answers['TYPE=vfat'] = ["/dev/zdd"]
            self.assertEqual(["/dev/vdb", "/dev/zdd"],
                             ds.find_candidate_devs())

            # verify that partitions are considered, that have correct label.
            devs_with_answers = {"TYPE=vfat": ["/dev/sda1"],
                "TYPE=iso9660": [], "LABEL=config-2": ["/dev/vdb3"]}
            self.assertEqual(["/dev/vdb3"],
                              ds.find_candidate_devs())

        finally:
            util.find_devs_with = orig_find_devs_with
            util.is_partition = orig_is_partition
Beispiel #13
0
    def test_dir_valid(self):
        """Verify a dir is read as such."""

        populate_dir(self.tmp, CFG_DRIVE_FILES_V2)

        found = ds.read_config_drive_dir(self.tmp)

        expected_md = copy(OSTACK_META)
        expected_md['instance-id'] = expected_md['uuid']

        self.assertEqual(USER_DATA, found['userdata'])
        self.assertEqual(expected_md, found['metadata'])
        self.assertEqual(found['files']['/etc/foo.cfg'], CONTENT_0)
        self.assertEqual(found['files']['/etc/bar/bar.cfg'], CONTENT_1)
Beispiel #14
0
    def test_seed_dir_valid_extra(self):
        """Verify extra files do not affect datasource validity."""

        data = copy(CFG_DRIVE_FILES_V2)
        data["myfoofile.txt"] = "myfoocontent"
        data["openstack/latest/random-file.txt"] = "random-content"

        populate_dir(self.tmp, data)

        found = ds.read_config_drive_dir(self.tmp)

        expected_md = copy(OSTACK_META)
        expected_md['instance-id'] = expected_md['uuid']

        self.assertEqual(expected_md, found['metadata'])
    def test_dir_valid(self):
        """Verify a dir is read as such."""

        populate_dir(self.tmp, CFG_DRIVE_FILES_V2)

        found = ds.read_config_drive(self.tmp)

        expected_md = copy(OSTACK_META)
        expected_md["instance-id"] = expected_md["uuid"]
        expected_md["local-hostname"] = expected_md["hostname"]

        self.assertEqual(USER_DATA, found["userdata"])
        self.assertEqual(expected_md, found["metadata"])
        self.assertEqual(NETWORK_DATA, found["networkdata"])
        self.assertEqual(found["files"]["/etc/foo.cfg"], CONTENT_0)
        self.assertEqual(found["files"]["/etc/bar/bar.cfg"], CONTENT_1)
 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_ec2_metadata(self):
     populate_dir(self.tmp, CFG_DRIVE_FILES_V2)
     found = ds.read_config_drive(self.tmp)
     self.assertTrue('ec2-metadata' in found)
     ec2_md = found['ec2-metadata']
     self.assertEqual(EC2_META, ec2_md)
 def test_ec2_metadata(self):
     populate_dir(self.tmp, CFG_DRIVE_FILES_V2)
     found = ds.read_config_drive_dir(self.tmp)
     self.assertTrue('ec2-metadata' in found)
     ec2_md = found['ec2-metadata']
     self.assertEqual(EC2_META, ec2_md)
Beispiel #19
0
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
Beispiel #20
0
 def test_network_config_is_converted(self):
     """Verify that network_data is converted and present on ds object."""
     populate_dir(self.tmp, CFG_DRIVE_FILES_V2)
     myds = cfg_ds_from_dir(self.tmp)
     network_config = ds.convert_network_data(NETWORK_DATA)
     self.assertEqual(myds.network_config, network_config)
Beispiel #21
0
 def test_network_config_is_converted(self):
     """Verify that network_data is converted and present on ds object."""
     populate_dir(self.tmp, CFG_DRIVE_FILES_V2)
     myds = cfg_ds_from_dir(self.tmp)
     network_config = ds.convert_network_data(NETWORK_DATA)
     self.assertEqual(myds.network_config, network_config)