26
from contextlib import ExitStack
27
from pathlib import Path
26
28
from systemimage.config import Configuration
29
from systemimage.helpers import temporary_directory
27
30
from systemimage.settings import Settings
28
from systemimage.testing.helpers import configuration
31
from systemimage.testing.helpers import chmod, configuration
32
from unittest.mock import patch
31
35
class TestSettings(unittest.TestCase):
99
103
keyval = list(settings)
101
105
self.assertEqual(keyval, [('a', 'ant'), ('b', 'bee'), ('c', 'cat')])
108
def test_settings_db_permission_denied(self, ini_file):
109
# LP: #1349478 - some tests are run as non-root, meaning they don't
110
# have write permission to /var/lib/system-image. This is where
111
# settings.db gets created, but if the process can't create files
112
# there, we get a sqlite3 exception.
113
config = Configuration(ini_file)
114
db_file = Path(config.system.settings_db)
115
self.assertFalse(db_file.exists())
116
with ExitStack() as resources:
117
resources.enter_context(chmod(str(db_file.parent), 0o555))
118
# With no fallback, this will fail.
119
with patch('systemimage.settings.Settings._check_fallback',
120
side_effect=RuntimeError):
121
self.assertRaises(RuntimeError, Settings)
122
# Now, set the XDG cache directory to a temporary directory, allow
123
# the fallback to work and try again.
124
tmpdir = resources.enter_context(temporary_directory())
125
resources.enter_context(
126
patch('systemimage.settings.xdg_cache_home', tmpdir))
127
settings = Settings()
128
settings.set('bar', 'baz')
129
self.assertEqual(Settings().get('bar'), 'baz')
130
# The settings.db file still doesn't exist because it got
131
# created in a different place.
132
self.assertFalse(db_file.exists())