diff options
| author | Scott Moser <smoser@ubuntu.com> | 2015-01-22 12:14:02 -0500 | 
|---|---|---|
| committer | Scott Moser <smoser@ubuntu.com> | 2015-01-22 12:14:02 -0500 | 
| commit | 78915c97c18d678db10e0fde0d9306823c5f4610 (patch) | |
| tree | fa98132617a50bcad3ab9a9e9e962ce1a2263bdd /tests/unittests/test_util.py | |
| parent | 8d453d2a4da4492857a4487b14fe7b11a014115b (diff) | |
| parent | de32623e6b34e3648958f1a08ef721ed9a03f2f8 (diff) | |
| download | vyos-cloud-init-78915c97c18d678db10e0fde0d9306823c5f4610.tar.gz vyos-cloud-init-78915c97c18d678db10e0fde0d9306823c5f4610.zip  | |
remove dependency on dmidecode on linux
on linux we can read dmi information from /sys rather than using 
dmidecode binary, and thus removing a dependency.
Diffstat (limited to 'tests/unittests/test_util.py')
| -rw-r--r-- | tests/unittests/test_util.py | 56 | 
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index 35e92445..3e079131 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -310,4 +310,60 @@ class TestMountinfoParsing(helpers.ResourceUsingTestCase):          expected = ('none', 'tmpfs', '/run/lock')          self.assertEqual(expected, util.parse_mount_info('/run/lock', lines)) + +class TestReadDMIData(helpers.FilesystemMockingTestCase): + +    def _patchIn(self, root): +        self.restore() +        self.patchOS(root) +        self.patchUtils(root) + +    def _write_key(self, key, content): +        """Mocks the sys path found on Linux systems.""" +        new_root = self.makeDir() +        self._patchIn(new_root) +        util.ensure_dir(os.path.join('sys', 'class', 'dmi', 'id')) + +        dmi_key = "/sys/class/dmi/id/{}".format(key) +        util.write_file(dmi_key, content) + +    def _no_syspath(self, key, content): +        """ +        In order to test a missing sys path and call outs to dmidecode, this +        function fakes the results of dmidecode to test the results. +        """ +        new_root = self.makeDir() +        self._patchIn(new_root) +        self.real_which = util.which +        self.real_subp = util.subp + +        def _which(key): +            return True +        util.which = _which + +        def _cdd(_key, error=None): +            return (content, error) +        util.subp = _cdd + +    def test_key(self): +        key_content = "TEST-KEY-DATA" +        self._write_key("key", key_content) +        self.assertEquals(key_content, util.read_dmi_data("key")) + +    def test_key_mismatch(self): +        self._write_key("test", "ABC") +        self.assertNotEqual("123", util.read_dmi_data("test")) + +    def test_no_key(self): +        self._no_syspath(None, None) +        self.assertFalse(util.read_dmi_data("key")) + +    def test_callout_dmidecode(self): +        """test to make sure that dmidecode is used when no syspath""" +        self._no_syspath("key", "stuff") +        self.assertEquals("stuff", util.read_dmi_data("key")) +        self._no_syspath("key", None) +        self.assertFalse(None, util.read_dmi_data("key")) + +  # vi: ts=4 expandtab  | 
