Skip to content

Switch context by test #204

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

Merged
merged 2 commits into from
Jun 26, 2025
Merged

Switch context by test #204

merged 2 commits into from
Jun 26, 2025

Conversation

lsierant
Copy link
Contributor

@lsierant lsierant commented Jun 19, 2025

Summary

This PR helps in switching to a proper context/variant by specifying a test mark or full task's evergreen url.

It solves a source of friction in our day-to-day workflows, which is finding which context to switch to in order to run a particular test. And by being able to switch context by specifying evergreen task's url we're a step closer to having automation that reruns a failed script locally.

Current workflow

To find a context to run the test locally we often start from two places:

  1. Starting from Evergreen UI (easier): look at the evergreen variant in the evergreen UI, copy it and run make switch context=<copied variant> or use interactive fzf-powered make switch (without args), paste or type the variant and press enter.
  2. Starting from an e2e test file (harder): find a @pytest.mark in the file, copy it, go to evergreen.yml, find a task group containing that file, copy its name, then find a variant where that task group is used, copy variant name, run make switch context=<copied variant>...

Proposed workflow

make switcht is a new tool which is a supercharged make switch. It's named similarly (typing make switch is kind of a muscle memory now) and works in two modes:

  1. With explicit test mark/url on test= argument:
  • make switcht test=<test>, where <test> is a pytest mark (e.g. e2e_replica_set) - it will list all the variants that this test is running in to be picked with fzf
  • make switcht test=<task url>, where <task url> is a full url to the evergreen test - it will switch directly to the variant from that test
  1. Without specifying test argument, which works in a similar interactive way like make switch, but lists first all test files with their marks to fuzzy-find the test, which then is used to list pick its variants: make switcht

Requirements

It requires:

  • fzf - for the interactive fuzzy finder (we already use it for make switch)
  • ripgrep - to quickly list all the pytest marks in all e2e test files
  • Having evg credentials in env vars, e.g. in your scripts/dev/contexts/private-context
EVERGREEN_USER=$(cat ~/.evergreen.yml | yq .user)
EVERGREEN_API_KEY=$(cat ~/.evergreen.yml | yq .api_key)

Changes

Majority of the implementation is in two places:

  • scripts/dev/switch_context_by_test.sh - it's mostly for fzf, finding pytest marks using rg. It calls scripts/python/find_test_variants.py
  • scripts/python/find_test_variants.py - a new python script that lists all the variants of a pytest's mark (--task-name) or a variant from a full evg task url (--task-url)
  • introduced scripts/python directory, to limit proliferation of python scripts in random directories. Having them in one place makes it easier to import/run them as modules
  • refactored some evergreen API helpers into scripts/python/evergreen_api.py
  • some minor, random changes improving debuggability of bash scripts

Proof of Work

Here are some examples for the most common use cases:

With explicit test= parameter

  • Switch to one of test's context by pytest mark: make switcht test=e2e_replica_set
    switcht with mark
  • Switch to context by test's url: make switcht test='https://spruce.mongodb.com/task/[...]/logs?execution=0'
    switcht with url

Without any parameters (fully interactive)

  • Switch to context by fuzzy-searching a test first: make switcht
    switcht by fuzzy search
  • Switch to context by pasting full file path into test picker: make switcht
    switcht by pasting full filepath
  • Switch to context by pasting full url into fzf picker: make switcht
    switcht by pasting url

Afterwards, if the context needs to be regenerated again just run make switch (without t) + enter - it will remember last switched context and be selected first.

Checklist

  • Have you linked a jira ticket and/or is the ticket in the title?
  • Have you checked whether your jira ticket required DOCSP changes?
  • Have you checked for release_note changes?

Reminder (Please remove this when merging)

  • Please try to Approve or Reject Changes the PR, keep PRs in review as short as possible
  • Our Short Guide for PRs: Link
  • Remember the following Communication Standards - use comment prefixes for clarity:
    • blocking: Must be addressed before approval.
    • follow-up: Can be addressed in a later PR or ticket.
    • q: Clarifying question.
    • nit: Non-blocking suggestions.
    • note: Side-note, non-actionable. Example: Praise
    • --> no prefix is considered a question

@lsierant lsierant marked this pull request as ready for review June 19, 2025 16:56
@lsierant lsierant requested a review from a team as a code owner June 19, 2025 16:56
description: The MongoDB Controllers for Kubernetes enable easy deploys of
MongoDB into Kubernetes clusters, using our management, monitoring and
backup platforms, Ops Manager and Cloud Manager.
description: The MongoDB Controllers for Kubernetes enable easy deploys of MongoDB
Copy link
Contributor Author

Choose a reason for hiding this comment

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

for some reason it started to format it differently despite being up-to-date on master

Copy link
Contributor

Choose a reason for hiding this comment

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

try updating your python packages

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, that did the trick, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

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

this fixes it: #210


# shellcheck disable=1091
source scripts/funcs/errors

script_name=$(readlink -f "${BASH_SOURCE[0]}")
script_dir=$(dirname "${script_name}")
context_file="${script_dir}/../../.generated/context.export.env"
context_file="$(realpath "${script_dir}/../../.generated/context.export.env")"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this resolves to the physical path and avoids huge paths with multiple /../../ when debugging with -x

@lsierant lsierant force-pushed the lsierant/switcht branch 2 times, most recently from fb08643 to 9a76e72 Compare June 19, 2025 17:10
Copy link
Collaborator

@Julien-Ben Julien-Ben left a comment

Choose a reason for hiding this comment

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

Awesome PR ! I like that it doesn't break anything to existing workflows

Nice demo gif as well
Thanks for this improvement 🙏

Copy link
Collaborator

@nammn nammn left a comment

Choose a reason for hiding this comment

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

Awesome work -
lets merge mine first #210

@@ -116,3 +116,8 @@ export MDB_SEARCH_COMMUNITY_VERSION

export MDB_SEARCH_COMMUNITY_NAME="mongodb-search-community"
export MDB_SEARCH_COMMUNITY_REPO_URL="quay.io/mongodb"


if [[ ${MDB_BASH_DEBUG:-0} == 1 ]]; then
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you add a small comment? I actually have no clue what this does :D

@@ -49,5 +50,3 @@ else
fi

title "E2e test ${test} is finished"

Copy link
Collaborator

Choose a reason for hiding this comment

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

eof new line?

@@ -2,6 +2,7 @@

set -Eeou pipefail

test "${MDB_BASH_DEBUG:-0}" -eq 1 && set -x
Copy link
Collaborator

Choose a reason for hiding this comment

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

a small comment for the non bash users?

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 don't want to put comment to each that line, because that line should be present in all of our bash scripts.
It's allowing us to debug deeply nested shell scripts, e.g.
MDB_BASH_DEBUG=1 make e2e ... will just propagate set -x in all scripts that declare this

@lsierant lsierant merged commit fe81867 into master Jun 26, 2025
35 checks passed
@lsierant lsierant deleted the lsierant/switcht branch June 26, 2025 11:51
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