Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test loading of cached metadata in ngclient #1703

Merged

Conversation

ivanayov
Copy link
Collaborator

@ivanayov ivanayov commented Dec 2, 2021

After making a successful update of valid metadata which stores it
in cache and performing a second update with a new updater while
the metadata is already stored in cache, this test verifies that
timestamp, snaphot and targets are loaded from cache and not
downloaded

Addresses #1681

Signed-off-by: Ivana Atanasova iyovcheva@vmware.com

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • [n/a] Docs have been added for the bug fix or new feature

@coveralls
Copy link

coveralls commented Dec 2, 2021

Pull Request Test Coverage Report for Build 1667822716

  • 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 97.737%

Totals Coverage Status
Change from base Build 1667215048: 0.0%
Covered Lines: 4092
Relevant Lines: 4171

💛 - Coveralls

@kairoaraujo
Copy link
Collaborator

Hi @ivanayov, I think my PR #1704 will help you to remove the mock part. You will be able to test it directly against the Repository Simulator.

@ivanayov ivanayov force-pushed the test_loading_of_cached_metadata branch from 6d00db6 to bd17789 Compare December 2, 2021 14:24
@ivanayov
Copy link
Collaborator Author

ivanayov commented Dec 2, 2021

@jku I addressed your comment, if something is not very correct I'll edit it. (It doesn't cover if the loaded timestamp is used to verify the new timestamp, but AFAIR it's already covered and we wanted to check only what's loaded from disk.)

Thanks @kairoaraujo . With the latest change I don't check fetch anymore, but will keep in mind for future tests.

@ivanayov ivanayov mentioned this pull request Dec 3, 2021
2 tasks
@MVrachev
Copy link
Collaborator

Could you please rebase on top of develop?
In #1710 we started linting our tests and it will be good to lint the changes made in this pr.

Copy link
Member

@jku jku left a comment

Choose a reason for hiding this comment

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

Yes, this looks ok. Left some comments on get_targetinfo() calls: please fix those.

Since you are already modifying I'll leave another nitpick: can you double check that comments and commit messages do not claim to do something that isn't done: at least one comment says "Test that timestamp, snaphot and targets are ... not downloaded" which is not true. The test only checks that local files are opened in the expected way (and that's fine, this is useful and would have caught a past issue we had).

There is a potential follow-up for

  • adding a delegated target to repository (se the test can check that delegated target is also correctly loaded from local disk)
  • separately testing constructor, refresh() and get_targetinfo()

tests/test_updater_with_simulator.py Outdated Show resolved Hide resolved
tests/test_updater_with_simulator.py Outdated Show resolved Hide resolved
Copy link
Contributor

@sechkova sechkova left a comment

Choose a reason for hiding this comment

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

I added some comments to the code but then realized, this test does not prove much. Updater always tries to load metadata from cache. There will always be calls to open even if there are no cached files. If this is what we want to demonstrate, ok. Then we are testing that "Updater always tries to read the cache before downloading metadata" and a single call to "refresh" is enough. The whole test would be:

updater = self._run_refresh()
wrapped_open.assert_has_calls([
            call(os.path.join(self.metadata_dir, "root.json"), "rb"),
            call(os.path.join(self.metadata_dir, "timestamp.json"), "rb"),
            call(os.path.join(self.metadata_dir, "snapshot.json"), "rb"),
            call(os.path.join(self.metadata_dir, "targets.json"), "rb"),
        ])

Probably a way to show that it opened and used the cached metadata is to show that when cached metadata is loaded from the file system successfully, there are no calls to RepositorySimulator.fetch_metadata for snapshot and targets. This should be easier now, after the addition of RepositorySimulator.fetch_counters. "Usage" of local (intermediate) root is described in the spec and should have be covered in test_top_level_update.py. Timestamp is a special case that can be tested in another set of tests covering invalid metadata.

The test is not really consistent. It randomly calls _run_refresh and get_targetinfo.
In case it is not clear, the options are either:

  1. Test only top-level roles metadata
  • create a new updater, call updater.refresh to get the metadata locally
  • reset the mock
  • create a new updater, call updater.refresh a second time to get the metadata loaded from the local cache dir
  • check the calls to open, check the calls to RepositorySimulator.fetch_tracker.metadata

Then as Jussi suggests, add delegations as follow up.

  1. Test both top-level metadata and a delegated role
  • add a delegated role to simulator, there is an example in
    self.sim.add_delegation(
  • create a new updater, call updater.get_targetinfo("non_existent_target") to get the metadata locally, delegated role included
  • reset the mock
  • create a new updater, call updater.get_targetinfo("non_existent_target") a second time to get the metadata loaded from the local cache dir
  • check the the calls to open, check the calls to RepositorySimulator.fetch_tracker.metadata

tests/test_updater_with_simulator.py Outdated Show resolved Hide resolved
tests/test_updater_with_simulator.py Outdated Show resolved Hide resolved
tests/test_updater_with_simulator.py Outdated Show resolved Hide resolved
tests/test_updater_with_simulator.py Outdated Show resolved Hide resolved
@ivanayov ivanayov force-pushed the test_loading_of_cached_metadata branch 3 times, most recently from 46c5731 to a7e9c67 Compare December 13, 2021 21:32
Copy link
Contributor

@sechkova sechkova left a comment

Choose a reason for hiding this comment

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

The test makes sense to me now, thanks for updating it. Some minor comments still but it looks ok to me.

tests/test_updater_with_simulator.py Outdated Show resolved Hide resolved
@ivanayov ivanayov force-pushed the test_loading_of_cached_metadata branch from a7e9c67 to ab47dda Compare December 14, 2021 09:09
Copy link
Member

@jku jku left a comment

Choose a reason for hiding this comment

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

LGTM: @sechkova all yours to merge when you're happy

@ivanayov ivanayov force-pushed the test_loading_of_cached_metadata branch from ab47dda to e1b42f4 Compare December 14, 2021 09:36
@ivanayov
Copy link
Collaborator Author

Latest force-push is with addressing @sechkova 's comment, nothing else has changed.

@sechkova
Copy link
Contributor

sechkova commented Dec 17, 2021

@jku GH seems to believe you're still requesting changes and I cannot merge without your approval.

@ivanayov ivanayov force-pushed the test_loading_of_cached_metadata branch from e1b42f4 to c96b488 Compare January 4, 2022 13:13
@jku
Copy link
Member

jku commented Jan 7, 2022

Sorry Ivana, I just made your life a bit harder by merging Teodoras test move.

Could you rebase?

(test looks really nice btw)

After making a successful update of valid metadata which stores it
in cache and performing a second update with a new updater while
the metadata is already stored in cache, this test verifies that
timestamp, snaphot and targets are loaded from cache and not
downloaded

Fixes theupdateframework#1681

Signed-off-by: Ivana Atanasova <iyovcheva@vmware.com>
@ivanayov ivanayov force-pushed the test_loading_of_cached_metadata branch from c96b488 to d27c0fd Compare January 7, 2022 14:03
@ivanayov
Copy link
Collaborator Author

ivanayov commented Jan 7, 2022

@jku it's fine, I migrated it to tests/test_updater_top_level_update.py

@jku jku merged commit 45cf607 into theupdateframework:develop Jan 7, 2022
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.

None yet

6 participants