Example #1
0
 def test_set_none_works_with_missing_file(self):
     contents = factory.make_name("contents")
     env.set_maas_id(contents)
     atomic_delete(self.maas_id_path)
     env.set_maas_id(None)
     self.assertIsNone(env.get_maas_id())
     self.assertFalse(os.path.exists(self.maas_id_path))
Example #2
0
def set_maas_id(system_id):
    """Set the system_id for this rack/region permanently for MAAS."""
    global _maas_id
    system_id = _normalise_maas_id(system_id)
    with _maas_id_lock:
        maas_id_path = get_maas_data_path("maas_id")
        if system_id is None:
            try:
                atomic_delete(maas_id_path)
            except FileNotFoundError:
                _maas_id = None  # Job done already.
            else:
                _maas_id = None
        else:
            atomic_write(system_id.encode("ascii"), maas_id_path)
            _maas_id = system_id
Example #3
0
    def test_renames_file_before_deleting(self):
        # Intercept calls to os.remove.
        os_remove = self.patch(fs_module.os, "remove")

        contents = factory.make_name("contents").encode("ascii")
        filepath = self.make_file(contents=contents)
        filedir = os.path.dirname(filepath)

        atomic_delete(filepath)

        listing = os.listdir(filedir)
        self.assertThat(listing, HasLength(1))
        [deletedname] = listing
        self.assertThat(deletedname, MatchesRegex(r"^[.][^.]+[.]deleted$"))
        deletedpath = os.path.join(filedir, deletedname)
        self.assertThat(os_remove, MockCalledOnceWith(deletedpath))
        self.assertThat(deletedpath, FileContains(contents))
Example #4
0
 def test_atomic_delete_deletes_file(self):
     filename = self.make_file()
     atomic_delete(filename)
     self.assertThat(filename, Not(FileExists()))