Skip to content

Tests: provide a way to generate a simple metadata set - #1898

Closed
MVrachev wants to merge 2 commits into
theupdateframework:developfrom
MVrachev:test-statics-data-generation
Closed

MVrachev wants to merge 2 commits into
theupdateframework:developfrom
MVrachev:test-statics-data-generation

Conversation

@MVrachev

@MVrachev MVrachev commented Mar 8, 2022

Copy link
Copy Markdown
Collaborator

Related to: #1806

Description of the changes being introduced by the pull request:

I created a new script called "generate_md.py" which can be used
to easily generate a repository. Additionally, I created a new
test file making sure that the locally stored metadata files and
the newly generated metadata roles are the same.
This will allow us to test that we are not changing the metadata
file structure when making changes.

Signed-off-by: Martin Vrachev [email protected]

Please verify and check that the pull request fulfills the following
requirements
:

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • Docs have been added for the bug fix or new feature

@MVrachev

MVrachev commented Mar 8, 2022

Copy link
Copy Markdown
Collaborator Author

@MVrachev
MVrachev requested a review from jku March 8, 2022 16:17
@coveralls

coveralls commented Mar 8, 2022 •

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 2023081596

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 98.312%

Totals Coverage Status
Change from base Build 2022257279: 0.0%
Covered Lines: 1179
Relevant Lines: 1195

💛 - Coveralls

@jku

jku commented Mar 9, 2022

Copy link
Copy Markdown
Member

I am not sure how to resolve the mypy issue: https://github.com/theupdateframework/python-tuf/runs/5467473728?check_suite_focus=true @jku any suggestions?

Directories do not become importable packages automatically. You need at least an empty __init__.py

@MVrachev
MVrachev force-pushed the test-statics-data-generation branch 2 times, most recently from 799bd3e to b87887e Compare March 9, 2022 15:23
@MVrachev

MVrachev commented Mar 9, 2022

Copy link
Copy Markdown
Collaborator Author

@jku I fixed the mypy issues.
Additionally, I had to add the second commit as on the previous CI run the test failed on all Windows venvs https://github.com/theupdateframework/python-tuf/runs/5467473050?check_suite_focus=true).
The second commit aims to achieve consistency on all operating systems.

The downside to this is that the metadata files won't be human-readable.

@jku

jku commented Mar 10, 2022

Copy link
Copy Markdown
Member

The second commit aims to achieve consistency on all operating systems.

The downside to this is that the metadata files won't be human-readable.

That sounds highly undesirable... not to mention that it sounds like a bug somewhere. What is the inconsistency?

@jku jku left a comment •

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.

Hmm, turns out I have opinions about this. I think we can both

  • reduce complexity and
  • make this more prepared for future additions

Some suggestions:

  • keys that are meant for use in multiple places should not be named for their role (this is the reason I don't want to use the legacy test keys). they should be named for the type of key they are (like "ed25519_1") so that individual tests, or in this case generating functions, can decide what they use the key for.
  • The CLI complexity is unused: let's not have arguments unless we really need them. Currently neither argument is needed
  • The generated data (files/roles) are now hard coded in four different functions: if I next want to add a single file, I would have to modify all four functions. This cannot be the optimal design. I think the object does not need attributes at all: generate_repository() could just do everything it needs to (either write the files it wants to write or validate the same files, depending on the argument): once the function is finished, no-one else needs to know which files it touched.
  • generate_repository() calls the data it generates a repository and I get the aim of providing examples of python-tuf repositories... but I think it may be premature:
    • Most importantly we don't actually check that it's a valid repository. It looks like one but I can only tell because it's the simplest possible, anything more complex is going to be hard to estimate. The filenames are at least incorrect for a consistent_snapshot repository
    • what if I want to add e.g. a file that tests all keytypes we support? Do I have to create a complete repository to add that one file? Looking at the metadata types, timestamp probably only needs one test case but targets might need multiple different files...
    • Would a better approach be to have one or more methods to generate just a bunch of independent metadata files that are not called a repository, and later we add repository support when we can reasonably test it?

@MVrachev
MVrachev force-pushed the test-statics-data-generation branch 2 times, most recently from adea8f5 to 45f20be Compare March 15, 2022 13:50
@MVrachev
MVrachev requested a review from jku March 15, 2022 13:53
@MVrachev

Copy link
Copy Markdown
Collaborator Author

@jku I think I have simplified the test script a lot.
The test doesn't require context and it should be a lot better.

Comment thread tests/generated_data/generate_md.py Outdated
md_targets: Metadata[Targets] = Metadata(targets, {})

for md in [md_root, md_timestamp, md_snapshot, md_targets]:
assert isinstance(md, Metadata)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This check is required from mypy otherwise there is a warning:
tests/generated_data/generate_md.py:142: error: Argument 1 to "verify_generation" of "MetadataGenerator" has incompatible type "object"; expected "Metadata[Any]" [arg-type]

@lukpueh lukpueh left a comment

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 commented a couple of observations/questions, mostly about further simplifying the script.
But maybe I'm missing the big picture. In what direction should the script scale?

I'd probably make it just a collection of generate_*_metadata functions, plus helper functions to be re-used by those generation function, like your verification function.

Comment thread tests/generated_data/generate_md.py Resolved
Comment thread tests/generated_data/generate_md.py Resolved
Comment thread tests/generated_data/generate_md.py Resolved
Comment thread tests/generated_data/generate_md.py Resolved
Comment thread tests/generated_data/generate_md.py Resolved
Comment thread tests/generated_data/generate_md.py Outdated
# Dump metadata files in a human readable format.
self.serializer = JSONSerializer()

def verify_generation(self, md: Metadata, path: str) -> None:

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 like this function. It's a good example for factoring out functionality that could be in generate_all_files, so that it can also be used re-used by other metadata generating functions as well.

@MVrachev

Copy link
Copy Markdown
Collaborator Author

I commented a couple of observations/questions, mostly about further simplifying the script. But maybe I'm missing the big picture. In what direction should the script scale?

I'd probably make it just a collection of generate_*_metadata functions, plus helper functions to be re-used by those generation function, like your verification function.

I did it that way before and @jku mentioned that there is no sense in having separate functions now when we generate only one variation per role.
If we enhance this script to generate more variations (as we plan to) it will make sense to have separate generate_*_metadata functions.
@jku has mentioned something like this in his last points here #1806 (comment).

@MVrachev
MVrachev force-pushed the test-statics-data-generation branch from 45f20be to 14d3f1a Compare March 18, 2022 18:07
Comment thread tests/generated_data/generate_md.py Resolved
@lukpueh

lukpueh commented Mar 21, 2022

Copy link
Copy Markdown
Member

I'd probably make it just a collection of generate_*_metadata functions, plus helper functions to be re-used by those generation function, like your verification function.

I did it that way before and @jku mentioned that there is no sense in having separate functions now when we generate only one variation per role.

Oh, I probably wasn't clear. I didn't mean that you should split generate_all_files now. I was talking about the future when you have multiple functions that each generate a set of metadata files for a specific use case. So I think we are on the same page here.

@MVrachev
MVrachev force-pushed the test-statics-data-generation branch 2 times, most recently from 62d15d3 to e763b47 Compare March 21, 2022 11:45
@MVrachev

Copy link
Copy Markdown
Collaborator Author

@lukpueh I updated the pr with removing the class and a couple of small simplifications.
@jku and @lukpueh it's ready for another review.

@MVrachev
MVrachev requested a review from lukpueh March 21, 2022 11:53
@MVrachev MVrachev changed the title Tests: provide a way to generate a simple repository Tests: provide a way to generate a simple metadata set Mar 21, 2022

@lukpueh lukpueh left a comment

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.

Cool! Thanks for the updates, Martin! I think it's easier to review and use now, without making it much harder to scale later. Here are two more comments about signing...

Comment thread tests/generated_data/generate_md.py Outdated
os.mkdir(OUT_DIR)

SERIALIZER = JSONSerializer()
key_info = copy.deepcopy(keys["ed25519_0"].to_securesystemslib_key())

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.

Why deepcopy?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Because at keys["ed25519_0"] we have the dictionary information of a dict containing a dictionary of a dictionary.
More precisely the keyval of a key is again a dictionary and a simple copy.copy() doesn't work here.

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.

What do you mean by "doesn't work here"? And why do you need to copy it at all?

Comment thread tests/generated_data/generate_md.py Outdated

for md in [md_root, md_timestamp, md_snapshot, md_targets]:
assert isinstance(md, Metadata)
md.sign(SIGNER)

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.

Why do you sign all metadata files with the root key? Why not sign timestamp with timestamp, snapshot with snapshot and targets with targets keys?

@MVrachev MVrachev Mar 22, 2022 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Changed that.
Initially (while there was a class), it was to reuse the signer.

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 am not sure I understand. So initially you wanted to sign with one key on purpose?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yea, because the files are not supposed to be read as a stand-alone repository, but as separate independent files, and as such we can reuse the same key as many times as we want.
The only reason I defined 4 keys is because of the root roles.

Martin Vrachev added 2 commits March 22, 2022 16:42
A really specific bug occurred on CI runs on all Windows machines
https://github.com/theupdateframework/python-tuf/runs/5467473050?check_suite_focus=true
where we weren't able to verify that what was generated is the same
as the stored on Git.

After research with Jussi, we found out that the problem comes not
from the content of the file that was generated, but because on Windows
Git proactively replaced all line endings for text files with CRLF symbol
("\r") this made the locally stored JSON files different from the one
generated.

We want to make sure such bugs doesn't occur again and that's why we
disable this behavior for all JSON files.

Signed-off-by: Martin Vrachev <[email protected]>
I created a new script called "generate_md.py" which can be used
to easily generate a repository. Additionally, I created a new
test file making sure that the locally stored metadata files and
the newly generated metadata roles are the same.
This will allow us to test that we are not changing the metadata
file structure when making changes.

Signed-off-by: Martin Vrachev <[email protected]>
@MVrachev
MVrachev force-pushed the test-statics-data-generation branch from e763b47 to 545ff37 Compare March 22, 2022 14:46
@MVrachev
MVrachev requested a review from lukpueh March 22, 2022 14:47
targets = Targets(1, SPEC_VERSION, EXPIRY, {})
md_targets: Metadata[Targets] = Metadata(targets, {})

i = 0

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.

hint: enumerate() gives you a running index.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I remembered it just when I sent it, thanks.

for md in [md_root, md_timestamp, md_snapshot, md_targets]:
assert isinstance(md, Metadata)
key_info = copy.deepcopy((keys[f"ed25519_{i}"].to_securesystemslib_key()))
key_info["keyval"]["private"] = private_values[0]

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.

Suggested change
key_info["keyval"]["private"] = private_values[0]
key_info["keyval"]["private"] = private_values[i]

?

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 think you could avoid deepcopy, to_securesystemslib_key and the assignment of private_values[i] if you use the private key dictionary defined above (before passing it to from_securesystemslib_key).

@MVrachev

Copy link
Copy Markdown
Collaborator Author

Not sure why, but GitHub doesn't see my update which exists on the same branch on my fork...
Will close and reopen the pr.

@MVrachev

MVrachev commented Mar 22, 2022 •

Copy link
Copy Markdown
Collaborator Author

@lukpueh because of a GitHub bug the new changes inside test-static-data-generation branch of my fork
weren't noticed. I closed it, but there was no option to reopen it.
That's why I created #1915.

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.

4 participants