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

VCR testing setup and VCR testing for ebirdchecklistfeed #105

Merged
merged 4 commits into from
Mar 6, 2024

Conversation

slager
Copy link
Collaborator

@slager slager commented Jan 22, 2024

This implements CI test-coverage method number 2 from #91.

Old test coverage for ebirdchecklistfeed.R 0.00%
New test coverage for ebirdchecklistfeed.R 92.31%

See the HTTP Testing in R book and the vcr package.

With this method, no API secrets are used on CI or CRAN. CI or CRAN can use recordings of HTTP requests produced locally by the VCR package as YML files. The EBIRD_KEY api key is automatically redacted from the HTTP headers in the YML files based on the setup in helper-vcr.R. Local testing (or testing on CI using GitHub secrets) could periodically be done with real API keys to make sure the API output hasn't changed.

@sebpardo
Copy link
Contributor

It took me a while to wrap my head around vcr but I think I finally understand it well enough to see it as the way forward for maintaining tests with CI. I've added you as a maintainer so you should be able to merge PRs and the likes; not sure if it's better to also have you added to team_rebird instead, @maelle?

Please go ahead and implement this across the board. One thing I'm wondering if it's worth having a single test that accesses the API using GH Secrets? It would have to be skipped for CI but should work for all commits on the main branch. Just a thought...

@maelle
Copy link
Member

maelle commented Feb 27, 2024

I can add @slager to the GitHub team if you prefer, @sebpardo (the GitHub team has admin access to this repo)

@slager
Copy link
Collaborator Author

slager commented Feb 27, 2024

@sebpardo, sure, great, I'll start working on these

@slager slager changed the title Demo of VCR testing setup and VCR testing for ebirdchecklistfeed VCR testing setup and VCR testing for ebirdchecklistfeed Mar 3, 2024
@slager slager marked this pull request as ready for review March 3, 2024 23:31
@slager
Copy link
Collaborator Author

slager commented Mar 3, 2024

Getting started with VCR testing with this PR.

@slager slager requested review from sebpardo and removed request for sebpardo March 3, 2024 23:32
@slager slager requested a review from sebpardo March 3, 2024 23:42
@slager
Copy link
Collaborator Author

slager commented Mar 3, 2024

@sebpardo Merged in upstream changes, ready for review / merge

@sebpardo sebpardo merged commit c912003 into ropensci:master Mar 6, 2024
8 checks passed
@slager slager deleted the vcr_demo branch March 6, 2024 02:58
Copy link
Contributor

@sebpardo sebpardo left a comment

Choose a reason for hiding this comment

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

Looks great. Can't contribute much to code specifics on this as I am a vcr noob.

@slager
Copy link
Collaborator Author

slager commented Mar 6, 2024

More soon!

@sebpardo
Copy link
Contributor

sebpardo commented Mar 6, 2024

One thing I'm wondering more generally, if you think there's any need to keep a EBIRD_KEY in a GH Secret for any reason? In other words, do you see any reason for wanting/needing to test any key-related functionality remotely in committed tests?

@slager
Copy link
Collaborator Author

slager commented Mar 6, 2024

Some of the VCR documentation mentions it being useful to have a test run on CI with a key, but I don't think it's necessary at all, especially if it's a pain to set up. We could keep a "skip_on_ci" and "skin_on_cran" test in the test suite that uses the key, and just periodically run that test on our local machines.

@sebpardo
Copy link
Contributor

sebpardo commented Mar 6, 2024

It's pretty straightforward to set up for tests in merged commits, but GH Secrets are not loaded for PRs so they would fail during any CI tests in the PR process. The issue is that getting around this and loading these secrets in the PR process has potential security hazard as anyone could submit a PR that exploits this (e.g., sending a PR that changes R-CMD-CHECK.yaml so explicitly print the API_KEY), so it would have to be a test that is somehow only run during merges/commits. Not sure if this is even feasible...

Regardless, the key that would be used as a GH Secret is linked to a rebird package specific eBird account, so it wouldn't be an issue to withdraw/cancel if required.

@slager
Copy link
Collaborator Author

slager commented Mar 6, 2024

Not sure why the Windows CI is failing all of a sudden.

https://github.com/ropensci/rebird/actions/runs/8166208700/job/22325144642

I suspect it could be something up (temporarily?) with the runner system, since it worked fine before and this works on my machine.

withr::local_envvar(c(EBIRD_KEY = ''), {print(Sys.getenv('EBIRD_KEY')); devtools::test(filter = 'ebirdtaxonomy')})
[1] ""
ℹ Testing rebird
✔ | F W S OK | Context
✔ | 11 | ebirdtaxonomy [13.3s]

══ Results ═
Duration: 13.5 s

[ FAIL 0 | WARN 0 | SKIP 0 | PASS 11 ]

I probably won't worry about it for now.

@sebpardo
Copy link
Contributor

sebpardo commented Mar 6, 2024

See https://github.com/ropensci/rebird/actions/runs/8166208700/job/22325144642#step:6:119:

── Error ('test-ebirdtaxonomy.R:29:3'): ebirdtaxonomy works without an API key ──

I think it's because ebirdtaxonomy() should work without an API key and we're now creating a fake key for vcr so the test fails here: https://github.com/ropensci/rebird/blob/82771e63ff6466f10488c1d2a47962b65d8068eb/tests/testthat/test-ebirdtaxonomy.R#L28C1-L32C3

@slager
Copy link
Collaborator Author

slager commented Mar 6, 2024

I don't think that fully explains it, because the same stuff with VCR is working fine on the MacOS and Ubuntu runners.

Most of what I can find online suggests it's probably not really an error with rebird itself. Hopefully it goes away soon with an update to the Windows GH actions runner image.

rust-lang/cargo#12296

@maelle
Copy link
Member

maelle commented Mar 6, 2024

loading these secrets in the PR process has potential security hazard as anyone could submit a PR that exploits this (e.g., sending a PR that changes R-CMD-CHECK.yaml so explicitly print the API_KEY), so it would have to be a test that is somehow only run during merges/commits. Not sure if this is even feasible...

Secrets are not accessible during PRs. I can't remember from where I know this, I found https://securitylab.github.com/research/github-actions-preventing-pwn-requests/

Tests with real requests can be run on schedule, for instance once a week https://books.ropensci.org/http-testing/real-requests-chapter.html

@slager
Copy link
Collaborator Author

slager commented Mar 6, 2024

@sebpardo I have an idea that I think will fix some or all of the "no key variable set" errors. I'll test it in a PR tonight.

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