From a47235e76633d65533a34f9058a5cfb27873b4ec Mon Sep 17 00:00:00 2001 From: Anna Shamray Date: Sat, 24 Feb 2024 18:14:02 +0100 Subject: [PATCH] :sparkles: [#1] add option to disable selftests in the command --- .../commands/setup_configuration.py | 33 +++++++++++++------ tests/test_command.py | 13 ++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/django_setup_configuration/management/commands/setup_configuration.py b/django_setup_configuration/management/commands/setup_configuration.py index 55269fe..e09abb0 100644 --- a/django_setup_configuration/management/commands/setup_configuration.py +++ b/django_setup_configuration/management/commands/setup_configuration.py @@ -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] = [ @@ -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.")) diff --git a/tests/test_command.py b/tests/test_command.py index 5b2a001..8bfd3b8 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -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