Skip to content

Commit

Permalink
✨ [#1] add option to disable selftests in the command
Browse files Browse the repository at this point in the history
  • Loading branch information
annashamray committed Feb 24, 2024
1 parent bda38bb commit a47235e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ def add_arguments(self, parser):
"of the env variables have been changed."
),
)
parser.add_argument(
"--no-selftest",
action="store_true",
dest="skip_selftest",
help=(
"Skip checking if configuration is successful. Use it if you "
"run this command in the init container before the web app is started"
),
)

@transaction.atomic
def handle(self, **options):
overwrite: bool = options["overwrite"]
skip_selftest: bool = options["skip_selftest"]

errors = ErrorDict()
steps: list[BaseConfigurationStep] = [
Expand Down Expand Up @@ -89,16 +99,19 @@ def handle(self, **options):
configured_steps.append(step)

# 3. Test configuration
for step in configured_steps:
# todo global env to turn off self tests?
try:
step.test_configuration()
except SelfTestFailed as exc:
errors[step] = exc
if skip_selftest:
self.stdout.write("Selftest is skipped.")

if errors:
raise CommandError(
f"Configuration test failed with errors: {errors.as_text()}"
)
else:
for step in configured_steps:
try:
step.test_configuration()
except SelfTestFailed as exc:
errors[step] = exc

if errors:
raise CommandError(
f"Configuration test failed with errors: {errors.as_text()}"
)

self.stdout.write(self.style.SUCCESS("Instance configuration completed."))
13 changes: 13 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,16 @@ def test_command_failed_selftest(mocker):
)
assert exc.value.args[0] == exc_description
assert User.objects.count() == 0


def test_command_skip_selftest(mocker):
"""
test that command skips selftest
"""
stdout = StringIO()
mocker.patch("testapp.configuration.authenticate", return_value=None)

call_command("setup_configuration", no_selftest=True, stdout=stdout)

output = stdout.getvalue()
assert "Selftest is skipped." in output

0 comments on commit a47235e

Please sign in to comment.