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

Add auto-disabling/enabling to the custom test runner #161

Closed
wants to merge 10 commits into from

Conversation

baioc
Copy link
Contributor

@baioc baioc commented Aug 24, 2021

This PR closes #145 and adds optional state tracking to the custom test runner added in #147. It uses a MySQL-compatible DB to track test history and will automatically skip unit tests marked as auto-disabled in the DB provided through the --db CLI option.

The default behavior is to only read from the test DB. Writes require a --commit option with a hash used as a unique identifier for this test run. This is meant to be used with git revision hashes when running this on the CI tool. When writing is enabled, all test results are logged and the auto-disabler/enabler runs.

Auto-enabling is quite simple: any passing test which was previously auto-disabled will be re-enabled. Of course, if the test was disabled it wouldn't normally be executed, so a --run-disabled command line switch was added to force those tests to run.

Auto-disabling takes place after the current run. It will auto-disable a test iff it failed the last 3 times it ran. This criterion is currently hard-coded but should be easy to change in the future.

This PR also includes an SQL script to initialize an empty DB with the table schemas assumed by the implementation.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 24, 2021
@baioc
Copy link
Contributor Author

baioc commented Aug 24, 2021

@vmagro Could you take a look at this?

Although I tested it using cargo to get the new postgres dependency, it does not currently build in antlir since that's missing.
I think Postgres pulls in a bunch of other stuff (from tokio, for instance), so if that's too heavy we could switch back to SQLite.

When running this with a live Postgres server, we would need to pass in credentials in the CLI as part of the connection string. I don't have much experience dealing with this so let me know if this isn't the safest way to do it.
One possible issue is running this with the correct credentials on the CI and the DB server goes down for some reason. The program will crash, but the error message might just leak the credentials which were used.

tools/testinfra/runner/src/main.rs Outdated Show resolved Hide resolved
conn: Option<String>,

/// Commit SHA-1 used to update the test DB on stateful runs
#[structopt(long = "commit", requires("conn"))]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use --revision for the cli arg?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As it is currently, the CLI help message suggests using this option as --commit <revision>, which I kind of prefer over --revision <revision>. Also, I think "commit" makes it pretty clear we're writing to the DB, whereas "revision" not so much.

tools/testinfra/runner/src/main.rs Outdated Show resolved Hide resolved
tools/testinfra/runner/src/main.rs Outdated Show resolved Hide resolved
tools/testinfra/runner/src/main.rs Outdated Show resolved Hide resolved
tools/testinfra/runner/src/main.rs Outdated Show resolved Hide resolved
tools/testinfra/runner/src/main.rs Outdated Show resolved Hide resolved
tools/testinfra/runner/src/main.rs Outdated Show resolved Hide resolved
list: bool,

/// Path to generated test report in JUnit XML format
#[structopt(long = "xml")]
report: Option<PathBuf>,

/// Connection string of the DB used in stateful test runs
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't entirely true though? Ideally we could have a default readonly connection so that non-recording runs (eg local users) can also get the list of disabled tests. Or are you planning to solve that separately?

If the disabled test feature only work on CI, that is fine for now, but it would be nice for non-stateful runs to still get the readonly info from the db

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was using the term "stateful" to refer to any execution of the test runner that uses the DB.
The default behavior is read-only and writes are only enabled when the --commit option is provided.

@baioc
Copy link
Contributor Author

baioc commented Sep 7, 2021

After rebasing onto #162, this now uses MySQL instead of Postgres and I believe most of your previous comments were addressed @vmagro. As far as I'm aware, all functionalities described in this PR are working.

That said, when moving it as a local project (using Cargo) to the buck-based build inside the project's source tree, I'm getting linking errors against the indirect dependencies openssl and flate2.
For a working cargo build using the same third-party dependency versions we have in Antlir, use this Cargo.toml.

How to test this once it is built:

  1. Add an external_runner option in the [test] section of .buckconfig, set it to the path to the test runner binary.
  2. To check if the config is working, try appending -- --help to the buck test command line.
  3. Run some Python and/or Rust and/or Shell tests. Other types are not yet supported. JUnit test reports are created with the optional --xml <path> CLI switch.
  4. To set up the test initial state, add these schemas to a new database (the previous steps should work without any DB).
  5. Append -- --db "mysql://<user>:<password>@<host>/<database>" to the buck test command line, replacing <user>, <password>, <host> and <database> with the appropriate information.
  6. Find one or more failing tests. For testing purposes I've been forcing some tests to fail.
  7. Add --commit <some_hash> to the command line. This enables writing results to the test DB.
  8. After 3 runs with different commit revisions, those failing tests will be disabled. A fourth run should have 0 fails.
  9. Add the --run-disabled CLI option to see those tests running again. If they pass and writes were enabled, they will be automatically re-enabled.

Copy link
Contributor

@vmagro vmagro left a comment

Choose a reason for hiding this comment

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

Nice!
I'll import this as-is (and just make the changes I suggested here for you).
I'll make the ci action build the runner with Cargo first, so that we can land this before I fix the linker issues you ran into

tools/testinfra/runner/src/main.rs Show resolved Hide resolved
tools/testinfra/runner/src/main.rs Show resolved Hide resolved
@@ -226,6 +295,129 @@ fn report<P: AsRef<Path>>(tests: Vec<TestResult>, path: P) -> Result<()> {
return Ok(());
}

fn xml_escape_text(unescaped: String) -> String {
fn xml_escape_text(unescaped: &String) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

&str would be more conventional, and String/&String can be dereferenced as a &str

@facebook-github-bot
Copy link
Contributor

@vmagro has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@vmagro
Copy link
Contributor

vmagro commented May 8, 2024

Sadly all of this is incompatible with buck2 :( But I think some of the details might still be useful to incorporate later

@vmagro vmagro closed this May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement a custom test runner for buck test
3 participants