Fix swift_dir setting in WSGI servers

Theoretically, the various WSGI servers should be able to operate on a
system without /etc/swift/swift.conf. However, this doesn't actually
work. WSGI servers call utils.validate_configuration() before looking
for a swift_dir option, and that validation reads swift.conf from its
default location. Even if you set swift_dir=/some/where/else, the WSGI
servers require /etc/swift/swift.conf to exist.

This commit makes the WSGI servers call utils.set_swift_dir before
calling utils.validate_configuration.

Motivation: I'm working on testing some client software against actual
Swift, but my CI environment doesn't have /etc/swift at all, so the
test suite can't start the Swift daemons.

Change-Id: Ie0efee33e684b1c5bad6ee2191c187bb680de5f1
Signed-off-by: Samuel Merritt <smerritt@nvidia.com>
This commit is contained in:
Samuel Merritt
2025-10-07 12:13:37 -07:00
parent 97e00e208f
commit 5568dd09b5
3 changed files with 31 additions and 2 deletions

View File

@@ -23,7 +23,7 @@ Chmouel Boudjnah <chmouel@enovance.com> <chmouel@chmouel.com>
Gaurav B. Gangalwar <gaurav@gluster.com> gaurav@gluster.com <>
Joe Arnold <joe@swiftstack.com> <joe@cloudscaling.com>
Kapil Thangavelu <kapil.foss@gmail.com> kapil.foss@gmail.com <>
Samuel Merritt <sam@swiftstack.com> <spam@andcheese.org>
Samuel Merritt <sam@swiftstack.com> <spam@andcheese.org> <smerritt@nvidia.com>
Morita Kazutaka <morita.kazutaka@gmail.com>
Zhongyue Luo <zhongyue.nah@intel.com> <lzyeval@gmail.com>
Russ Nelson <russ@crynwr.com> <nelson@nelson-laptop>

View File

@@ -41,7 +41,8 @@ from swift.common.swob import Request, wsgi_unquote
from swift.common.utils import capture_stdio, disable_fallocate, \
drop_privileges, get_logger, NullLogger, config_true_value, \
validate_configuration, get_hub, config_auto_int_value, \
reiterate, clean_up_daemon_hygiene, systemd_notify, NicerInterpolation
reiterate, clean_up_daemon_hygiene, systemd_notify, NicerInterpolation, \
set_swift_dir
SIGNUM_TO_NAME = {getattr(signal, n): n for n in dir(signal)
if n.startswith('SIG') and '_' not in n}
@@ -1229,6 +1230,9 @@ def _initrp(conf_path, app_section, *args, **kwargs):
raise ConfigFileError("Error trying to load config from %s: %s" %
(conf_path, e))
if conf.get('swift_dir'):
set_swift_dir(conf['swift_dir'])
validate_configuration()
# pre-configure logger

View File

@@ -64,6 +64,15 @@ def _fake_rings(tmpdir):
policy.object_ring = None
def _fake_swift_conf(tmpdir):
swift_config = dedent("""
[swift-hash]
swift_hash_path_prefix = arbitrary-nonempty-value
""")
with open(os.path.join(tmpdir, 'swift.conf'), 'w') as f:
f.write(swift_config)
@patch_policies
class TestWSGI(unittest.TestCase):
"""Tests for swift.common.wsgi"""
@@ -87,6 +96,7 @@ class TestWSGI(unittest.TestCase):
with open(conf_file, 'w') as f:
f.write(contents.replace('TEMPDIR', t))
_fake_rings(t)
_fake_swift_conf(t)
app, conf, logger, log_name = wsgi.init_request_processor(
conf_file, 'proxy-server')
# verify pipeline is: catch_errors -> gatekeeper -> listing_formats ->
@@ -277,6 +287,7 @@ class TestWSGI(unittest.TestCase):
""",
}
_fake_rings(tempdir)
_fake_swift_conf(tempdir)
for filename, conf_body in conf_files.items():
path = os.path.join(tempdir, filename + '.conf')
with open(path, 'wt') as fd:
@@ -313,6 +324,7 @@ class TestWSGI(unittest.TestCase):
""",
}
_fake_rings(tempdir)
_fake_swift_conf(tempdir)
for filename, conf_body in conf_files.items():
path = os.path.join(tempdir, filename + '.conf')
with open(path, 'wt') as fd:
@@ -439,6 +451,7 @@ class TestWSGI(unittest.TestCase):
with open(os.path.join(conf_dir, 'swift.conf'), 'w') as f:
f.write('[DEFAULT]\nswift_dir = %s' % conf_root)
_fake_rings(conf_root)
_fake_swift_conf(conf_root)
app, conf, logger, log_name = wsgi.init_request_processor(
conf_dir, 'proxy-server')
# verify pipeline is catch_errors -> proxy-server
@@ -634,6 +647,7 @@ class TestWSGI(unittest.TestCase):
with open(conf_file, 'w') as f:
f.write(contents.replace('TEMPDIR', t))
_fake_rings(t)
_fake_swift_conf(t)
with mock.patch('swift.common.wsgi.wsgi') as _wsgi, \
mock.patch('swift.common.wsgi.eventlet') as _wsgi_evt:
conf = wsgi.appconfig(conf_file)
@@ -730,6 +744,7 @@ class TestWSGI(unittest.TestCase):
with open(conf_file, 'w') as f:
f.write(contents.replace('TEMPDIR', t))
_fake_rings(t)
_fake_swift_conf(t)
with mock.patch('swift.proxy.server.Application.'
'modify_wsgi_pipeline'), \
mock.patch('swift.common.wsgi.wsgi') as _wsgi, \
@@ -769,6 +784,7 @@ class TestWSGI(unittest.TestCase):
with open(os.path.join(conf_dir, 'swift.conf'), 'w') as f:
f.write('[DEFAULT]\nswift_dir = %s' % conf_root)
_fake_rings(conf_root)
_fake_swift_conf(conf_root)
with mock.patch('swift.proxy.server.Application.'
'modify_wsgi_pipeline'), \
mock.patch('swift.common.wsgi.wsgi') as _wsgi, \
@@ -820,6 +836,7 @@ class TestWSGI(unittest.TestCase):
with open(conf_file, 'w') as f:
f.write(contents.replace('TEMPDIR', t))
_fake_rings(t)
_fake_swift_conf(t)
with mock.patch('swift.proxy.server.Application.'
'modify_wsgi_pipeline'), \
mock.patch('swift.common.wsgi.wsgi') as _wsgi, \
@@ -1911,6 +1928,7 @@ class TestPipelineModification(unittest.TestCase):
with open(conf_file, 'w') as f:
f.write(contents.replace('TEMPDIR', t))
_fake_rings(t)
_fake_swift_conf(t)
with mock.patch(
'swift.proxy.server.Application.modify_wsgi_pipeline',
modify_func):
@@ -1975,6 +1993,7 @@ class TestPipelineModification(unittest.TestCase):
with open(conf_file, 'w') as f:
f.write(contents.replace('TEMPDIR', t))
_fake_rings(t)
_fake_swift_conf(t)
app = wsgi.loadapp(conf_file, global_conf={})
self.assertEqual(self.pipeline_modules(app),
@@ -2041,6 +2060,7 @@ class TestPipelineModification(unittest.TestCase):
with open(conf_file, 'w') as f:
f.write(contents.replace('TEMPDIR', t))
_fake_rings(t)
_fake_swift_conf(t)
app = wsgi.loadapp(conf_file, global_conf={})
self.assertEqual(self.pipeline_modules(app),
@@ -2074,6 +2094,7 @@ class TestPipelineModification(unittest.TestCase):
with open(conf_file, 'w') as f:
f.write(contents.replace('TEMPDIR', t))
_fake_rings(t)
_fake_swift_conf(t)
app = wsgi.loadapp(conf_file, global_conf={})
self.assertEqual(self.pipeline_modules(app),
@@ -2238,6 +2259,7 @@ class TestPipelineModification(unittest.TestCase):
with temptree(['proxy-server.conf']) as t:
_fake_rings(t)
_fake_swift_conf(t)
for version, pipeline, expected in to_test:
conf_file = os.path.join(t, 'proxy-server.conf')
with open(conf_file, 'w') as f:
@@ -2336,6 +2358,7 @@ class TestPipelineModification(unittest.TestCase):
with open(conf_file, 'w') as f:
f.write(contents.replace('TEMPDIR', t))
_fake_rings(t)
_fake_swift_conf(t)
with mock.patch.object(swift.proxy.server, 'required_filters',
new_req_filters):
app = wsgi.loadapp(conf_file, global_conf={})
@@ -2377,6 +2400,7 @@ class TestPipelineModification(unittest.TestCase):
with open(conf_file, 'w') as f:
f.write(contents.replace('TEMPDIR', t))
_fake_rings(t)
_fake_swift_conf(t)
app = wsgi.loadapp(conf_file, global_conf={})
return app
@@ -2447,6 +2471,7 @@ class TestPipelineModification(unittest.TestCase):
with open(conf_path, 'w') as f:
f.write(dedent(conf_body))
_fake_rings(tempdir)
_fake_swift_conf(tempdir)
account_ring_path = os.path.join(tempdir, 'account.ring.gz')
container_ring_path = os.path.join(tempdir, 'container.ring.gz')
object_ring_paths = {}