Skip to content

Add test to ensure docs examples are valid cloud-init configs#355

Merged
OddBloke merged 5 commits into
canonical:masterfrom
TheRealFalcon:test-examples-schema
May 8, 2020
Merged

Add test to ensure docs examples are valid cloud-init configs#355
OddBloke merged 5 commits into
canonical:masterfrom
TheRealFalcon:test-examples-schema

Conversation

@TheRealFalcon
Copy link
Copy Markdown
Contributor

@OddBloke OddBloke self-assigned this May 6, 2020
Copy link
Copy Markdown
Collaborator

@OddBloke OddBloke left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, James! I think this test is a good candidate for parameterization, and I'll follow up with you on IRC about that. Other than that, I have some inline comments.

]

examples_dir = Path(
__file__).parent.parent.parent.parent / 'doc' / 'examples'
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This file (or test) could move around, which would break this determination. Adding an import cloudinit and using cloudinit.__file__ (which is cloudinit/__init__.py, to save you looking it up too) would (a) be more robust, and (b) require less .parenting.

'cloud-config-archive.txt',
]

examples_dir = Path(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice use of pathlib! It's so nice to not have to support Python 2 any longer.


all_text_files = [f for f in examples_dir.glob('cloud-config*.txt')
if f.name not in ignored_files]
for text_file_path in all_text_files:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When I see a for loop like this, containing assertions or code under test that could fail, it suggests to me that we have a good candidate for a parametrized test. Could we convert this?

validate_cloudconfig_file(str(text_file_path), get_schema())
except: # noqa
print('Failed on {}'.format(text_file_path))
raise
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This will only show us the first failure that occurs, without performing tests across all the files. (Parametrizing this test will address this issue.)

assert examples_dir.is_dir()

all_text_files = [f for f in examples_dir.glob('cloud-config*.txt')
if f.name not in ignored_files]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

cloud-config-archive is a prefix for these files we want to exclude, in the same way that cloud-config is a prefix for the files that we do want to include. Can we drop the static list above if we make this something along the lines of not f.name.startswith('cloud-config-archive') instead?

@TheRealFalcon TheRealFalcon force-pushed the test-examples-schema branch from 035d8b4 to 3eeb668 Compare May 7, 2020 00:34
@TheRealFalcon TheRealFalcon requested a review from OddBloke May 7, 2020 14:09
Copy link
Copy Markdown
Collaborator

@OddBloke OddBloke left a comment

Choose a reason for hiding this comment

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

Thanks, this looks good! I have a couple of minor comments inline.

Comment thread tests/unittests/test_handler/test_schema.py


class TestSchemaDocExamples:
schema = get_schema()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a nit (so feel free to not address it), but TestCloudConfigExamples has SCHEMA in all-caps and referred to as TestCloudConfigExamples.SCHEMA in its test: it would be nice to be consistent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Would it be ok if I updated the other class? Class variables usually don't use all caps unless they're constants, and self is a less verbose way of accessing a class variable than typing out the whole class every time.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would it be ok if I updated the other class?

In general, I expect us all to have ownership of all parts of the codebase, so the answer is yes. In this specific case, the code in question was added by @lucasmoura this week, so lets give him an opportunity to weigh in on what his thinking was.

Class variables usually don't use all caps unless they're constants

This is a constant, in the sense that we only expect to read from it, but I don't feel strongly about this either way.

self is a less verbose way of accessing a class variable than typing out the whole class every time.

Yep, I'd also be fine with this. I can see using the class name makes sense if we're really treating classes purely as namespaces (in which use self shouldn't have any special meaning), but I agree that it is an unusual spelling (and also makes renaming this class more work, for example).

Copy link
Copy Markdown
Contributor

@lucasmoura lucasmoura May 8, 2020

Choose a reason for hiding this comment

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

Sorry for the long delay, but I don't have any problem with the proposed change. Originally I thought about the schema as a constant, but I do agree that using self for that particular use case is less verbose

@TheRealFalcon TheRealFalcon force-pushed the test-examples-schema branch from 6ef6f7f to a3d0167 Compare May 8, 2020 21:12
@TheRealFalcon
Copy link
Copy Markdown
Contributor Author

Addressed most recent comments and pushed a rebase

@TheRealFalcon TheRealFalcon requested a review from OddBloke May 8, 2020 21:13
Copy link
Copy Markdown
Collaborator

@OddBloke OddBloke left a comment

Choose a reason for hiding this comment

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

Thanks!

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.

3 participants