예제 #1
0
 def test_modified_database_uses_safe_permissions_if_file_missing(self):
     # ConfigurationFile.open() uses a sensible u=rw,g=r file mode when
     # saving if the database file has been inexplicably removed. This is
     # the same mode as used when opening a new database.
     config_file = os.path.join(self.make_dir(), "config")
     open(config_file, "wb").close()  # touch.
     os.chmod(config_file, 0o644)  # u=rw,go=r
     with ConfigurationFile.open_for_update(config_file) as config:
         config["foobar"] = "I am a modification"
         os.unlink(config_file)
     perms = FilePath(config_file).getPermissions()
     self.assertEqual("rw-r-----", perms.shorthand())
예제 #2
0
 def test_opened_configuration_file_saves_on_exit(self):
     # ConfigurationFile.open() returns a context manager that will save an
     # updated configuration on a clean exit.
     config_file = os.path.join(self.make_dir(), "config")
     config_key = factory.make_name("key")
     config_value = factory.make_name("value")
     with ConfigurationFile.open_for_update(config_file) as config:
         config[config_key] = config_value
         self.assertEqual({config_key: config_value}, config.config)
         self.assertTrue(config.dirty)
     with ConfigurationFile.open(config_file) as config:
         self.assertEqual(config_value, config[config_key])
예제 #3
0
 def test_modified_database_retains_permissions(self):
     # ConfigurationFile.open() leaves the file permissions of existing
     # configuration databases if they're modified.
     config_file = os.path.join(self.make_dir(), "config")
     open(config_file, "wb").close()  # touch.
     os.chmod(config_file, 0o644)  # u=rw,go=r
     with ConfigurationFile.open_for_update(config_file) as config:
         perms = FilePath(config_file).getPermissions()
         self.assertEqual("rw-r--r--", perms.shorthand())
         config["foobar"] = "I am a modification"
     perms = FilePath(config_file).getPermissions()
     self.assertEqual("rw-r--r--", perms.shorthand())