def test_open_permissions_new_database(self): # ConfigurationDatabase.open() applies restrictive file permissions to # newly created configuration databases. config_file = os.path.join(self.make_dir(), "config") with ConfigurationDatabase.open(config_file): perms = FilePath(config_file).getPermissions() self.assertEqual("rw-r-----", perms.shorthand())
def test_open_yields_immutable_backend(self): config_file = os.path.join(self.make_dir(), "config") config_key = factory.make_name("key") with ConfigurationDatabase.open(config_file) as config: with ExpectedException(ConfigurationImmutable): config[config_key] = factory.make_name("value") with ExpectedException(ConfigurationImmutable): del config[config_key]
def test_opened_database_commits_on_exit(self): config_file = os.path.join(self.make_dir(), "config") config_key = factory.make_name("key") config_value = factory.make_name("value") with ConfigurationDatabase.open_for_update(config_file) as config: config[config_key] = config_value with ConfigurationDatabase.open(config_file) as config: self.assertEqual(config_value, config[config_key])
def test_open_permissions_existing_database(self): # ConfigurationDatabase.open() leaves the file permissions of existing # configuration databases. 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 ConfigurationDatabase.open(config_file): perms = FilePath(config_file).getPermissions() self.assertEqual("rw-r--r--", perms.shorthand())
def test_opened_database_rolls_back_on_unclean_exit(self): config_file = os.path.join(self.make_dir(), "config") config_key = factory.make_name("key") config_value = factory.make_name("value") exception_type = factory.make_exception_type() # Set a configuration option, then crash. with ExpectedException(exception_type): with ConfigurationDatabase.open_for_update(config_file) as config: config[config_key] = config_value raise exception_type() # No value has been saved for `config_key`. with ConfigurationDatabase.open(config_file) as config: self.assertRaises(KeyError, lambda: config[config_key])