Skip to content

test: fix test_schema when the schema validation takes longer#6822

Merged
holmanb merged 1 commit intocanonical:mainfrom
ani-sinha:fix-test
Apr 1, 2026
Merged

test: fix test_schema when the schema validation takes longer#6822
holmanb merged 1 commit intocanonical:mainfrom
ani-sinha:fix-test

Conversation

@ani-sinha
Copy link
Copy Markdown
Contributor

@ani-sinha ani-sinha commented Mar 31, 2026

When the schema validation takes more than 0.01 seconds, a log is emitted: Validating schema took 0.070 seconds

When this happens, the tuple length is 2 and not one and the test fails with the following:

def test_validateconfig_schema_non_strict_emits_warnings(self, caplog):
        """When strict is False validate_cloudconfig_schema emits warnings."""
        schema = {"properties": {"p1": {"type": "string"}}}
        validate_cloudconfig_schema({"p1": -1}, schema=schema, strict=False)
>       assert (
            caplog.record_tuples and len(caplog.record_tuples) == 1
        ), caplog.record_tuples
E       AssertionError: [('cloudinit.config.schema', 30, "cloud-config failed schema validation!
E         p1: -1 is not of type 'string'"), ('cloudinit.performance', 10, 'Validating schema took 0.070 seconds')]
E       assert ([('cloudinit.config.schema', 30, "cloud-config failed schema validation!\np1: -1 is not of type 'string'"), ('cloudinit.performance', 10, 'Validating schema took 0.070 seconds')] and 2 == 1)
E        +  where [('cloudinit.config.schema', 30, "cloud-config failed schema validation!\np1: -1 is not of type 'string'"), ('cloudinit.performance', 10, 'Validating schema took 0.070 seconds')] = <_pytest.logging.LogCaptureFixture object at 0xffffb27f9250>.record_tuples
E        +  and   2 = len([('cloudinit.config.schema', 30, "cloud-config failed schema validation!\np1: -1 is not of type 'string'"), ('cloudinit.performance', 10, 'Validating schema took 0.070 seconds')])
E        +    where [('cloudinit.config.schema', 30, "cloud-config failed schema validation!\np1: -1 is not of type 'string'"), ('cloudinit.performance', 10, 'Validating schema took 0.070 seconds')] = <_pytest.logging.LogCaptureFixture object at 0xffffb27f9250>.record_tuples

tests/unittests/config/test_schema.py:454: AssertionError

Fix it by checking if the length of the tuple is either one or two, not just one.

Proposed Commit Message

When the schema validation takes more than 0.01 seconds, a log is emitted:
"Validating schema took 0.070 seconds"

When this happens, the tuple length is 2 and not one and the test fails with
the following:

def test_validateconfig_schema_non_strict_emits_warnings(self, caplog):
        """When strict is False validate_cloudconfig_schema emits warnings."""
        schema = {"properties": {"p1": {"type": "string"}}}
        validate_cloudconfig_schema({"p1": -1}, schema=schema, strict=False)
>       assert (
            caplog.record_tuples and len(caplog.record_tuples) == 1
        ), caplog.record_tuples
E       AssertionError: [('cloudinit.config.schema', 30,
                           "cloud-config failed schema validation!
E         p1: -1 is not of type 'string'"), ('cloudinit.performance', 10,
                                  'Validating schema took 0.070 seconds')]

Fix it by checking if the length of the tuple is either one or two,
not just one.

Merge type

  • Squash merge using "Proposed Commit Message"
  • Rebase and merge unique commits. Requires commit messages per-commit each referencing the pull request number (#<PR_NUM>)

When the schema validation takes more than 0.01 seconds, a log is emitted:
"Validating schema took 0.070 seconds"

When this happens, the tuple length is 2 and not one and the test fails with
the following:

def test_validateconfig_schema_non_strict_emits_warnings(self, caplog):
        """When strict is False validate_cloudconfig_schema emits warnings."""
        schema = {"properties": {"p1": {"type": "string"}}}
        validate_cloudconfig_schema({"p1": -1}, schema=schema, strict=False)
>       assert (
            caplog.record_tuples and len(caplog.record_tuples) == 1
        ), caplog.record_tuples
E       AssertionError: [('cloudinit.config.schema', 30,
                           "cloud-config failed schema validation!
E         p1: -1 is not of type 'string'"), ('cloudinit.performance', 10,
                                  'Validating schema took 0.070 seconds')]

Fix it by checking if the length of the tuple is either one or two,
not just one.

Signed-off-by: Ani Sinha <anisinha@redhat.com>
Copy link
Copy Markdown
Member

@holmanb holmanb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should help in some circumstances, so this looks good to me. Thanks @ani-sinha.

Comment on lines +454 to +455
assert caplog.record_tuples and (
len(caplog.record_tuples) == 1 or len(caplog.record_tuples) == 2
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not personally a fan of logging tests for the brittleness which this problem demonstrates.

@holmanb holmanb merged commit ffd26f1 into canonical:main Apr 1, 2026
17 checks passed
blackboxsw pushed a commit to blackboxsw/cloud-init that referenced this pull request Apr 13, 2026
…cal#6822)

When the schema validation takes more than 0.01 seconds, a log is emitted:
"Validating schema took 0.070 seconds"

When this happens, the tuple length is 2 and not one and the test test_validateconfig_schema_non_strict_emits_warnings fails.

Fix it by checking if the length of the tuple is either one or two,
not just one.

Signed-off-by: Ani Sinha <anisinha@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants