Пример #1
0
    def test_update_no_change(self):
        # process all changes that have happen
        # changes will always take place the first time during testing
        ret = roots.update()
        self.assertTrue(ret["changed"])

        # check if no changes took place
        ret = roots.update()
        self.assertFalse(ret["changed"])
        self.assertEqual(ret["files"]["changed"], [])
        self.assertEqual(ret["files"]["removed"], [])
        self.assertEqual(ret["files"]["added"], [])
Пример #2
0
    def test_update_mtime_map(self):
        """
        Test that files with colons in the filename are properly handled in the
        mtime_map, and that they are properly identified as having changed.
        """
        mtime_map_path = os.path.join(self.opts["cachedir"], "roots",
                                      "mtime_map")
        mtime_map_mock = mock_open(
            read_data={
                mtime_map_path:
                textwrap.dedent("""\
                    /srv/salt/kleine_Datei.txt:1594263154.0469685
                    /srv/salt/große:Datei.txt:1594263160.9336357
                    """),
            })
        new_mtime_map = {
            "/srv/salt/kleine_Datei.txt": 1594263154.0469685,
            "/srv/salt/große:Datei.txt": 1594263261.0616212,
        }

        with patch("salt.fileserver.reap_fileserver_cache_dir",
                   MagicMock(return_value=True)), patch(
                       "salt.fileserver.generate_mtime_map",
                       MagicMock(return_value=new_mtime_map)), patch.dict(
                           roots.__opts__,
                           {"fileserver_events": False}), patch(
                               "salt.utils.files.fopen", mtime_map_mock):
            ret = roots.update()

        # Confirm the expected return from the function
        assert ret == {
            "changed": True,
            "files": {
                "changed": ["/srv/salt/große:Datei.txt"],
                "removed": [],
                "added": [],
            },
            "backend": "roots",
        }, ret

        # Confirm that the new values were written to the mtime_map. Sort both
        # lists of lines to account for variances in dictionary iteration order
        # between Python releases.
        lines_written = sorted(mtime_map_mock.write_calls())
        expected = sorted([
            salt.utils.stringutils.to_bytes("{key}:{val}\n".format(key=key,
                                                                   val=val))
            for key, val in new_mtime_map.items()
        ])
        assert lines_written == expected, lines_written
Пример #3
0
    def test_update_mtime_map_unicode_error(self):
        """
        Test that a malformed mtime_map (which causes an UnicodeDecodeError
        exception) is handled properly.
        """
        new_mtime_map = {
            "/srv/salt/große:Datei.txt": 1594263261.0616212,
        }
        with tempfile.TemporaryDirectory() as tmpdirname:
            mtime_map_path = os.path.join(tmpdirname, "roots", "mtime_map")
            os.makedirs(os.path.dirname(mtime_map_path))
            with salt.utils.files.fopen(mtime_map_path, "wb") as fp:
                fp.write(b"\x9c")

            with patch(
                    "salt.fileserver.reap_fileserver_cache_dir",
                    MagicMock(return_value=True),
            ), patch(
                    "salt.fileserver.generate_mtime_map",
                    MagicMock(return_value=new_mtime_map),
            ), patch.dict(
                    roots.__opts__,
                {
                    "fileserver_events": False,
                    "cachedir": tmpdirname
                },
            ):
                ret = roots.update()

        assert ret == {
            "changed": True,
            "files": {
                "changed": [],
                "removed": [],
                "added": ["/srv/salt/große:Datei.txt"],
            },
            "backend": "roots",
        }, ret