Skip to content

Commit 9e97791

Browse files
committed
Switch context by test
1 parent c242836 commit 9e97791

16 files changed

+345
-34
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ precommit:
6262
switch:
6363
@ scripts/dev/switch_context.sh $(context) $(additional_override)
6464

65+
switcht:
66+
@ scripts/dev/switch_context_by_test.sh $(test)
67+
6568
# builds the Operator binary file and docker image and pushes it to the remote registry if using a remote registry. Deploys it to
6669
# k8s cluster
6770
operator: configure-operator build-and-push-operator-image

scripts/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Makes 'scripts' a Python package.

scripts/dev/contexts/root-context

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,8 @@ export MDB_SEARCH_COMMUNITY_VERSION
116116

117117
export MDB_SEARCH_COMMUNITY_NAME="mongodb-search-community"
118118
export MDB_SEARCH_COMMUNITY_REPO_URL="quay.io/mongodb"
119+
120+
121+
if [[ ${MDB_BASH_DEBUG:-0} == 1 ]]; then
122+
export PS4='+(${BASH_SOURCE}:${LINENO})[^$?]: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
123+
fi

scripts/dev/launch_e2e.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
set -Eeou pipefail
44

5+
test "${MDB_BASH_DEBUG:-0}" -eq 1 && set -x
56

67
# The script launches e2e test. Note, that the Operator and necessary resources are deployed
78
# inside the test
@@ -49,5 +50,3 @@ else
4950
fi
5051

5152
title "E2e test ${test} is finished"
52-
53-

scripts/dev/set_env_context.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/usr/bin/env bash
22

33
set -Eeou pipefail
4+
test "${MDB_BASH_DEBUG:-0}" -eq 1 && set -x
45

56
# shellcheck disable=1091
67
source scripts/funcs/errors
78

89
script_name=$(readlink -f "${BASH_SOURCE[0]}")
910
script_dir=$(dirname "${script_name}")
10-
context_file="${script_dir}/../../.generated/context.export.env"
11+
context_file="$(realpath "${script_dir}/../../.generated/context.export.env")"
1112

1213
if [[ ! -f ${context_file} ]]; then
1314
fatal "File ${context_file} not found! Make sure to follow this guide to get started: https://wiki.corp.mongodb.com/display/MMS/Setting+up+local+development+and+E2E+testing#SettinguplocaldevelopmentandE2Etesting-GettingStartedGuide(VariantSwitching)"

scripts/dev/switch_context_by_test.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bash
2+
3+
set -Eeou pipefail
4+
test "${MDB_BASH_DEBUG:-0}" -eq 1 && set -x
5+
6+
usage() {
7+
echo "Switch context by passing the test (evergreen task name or full evergreen task URL)."
8+
echo "If there is more than one variant running given test, then fzf picker is used."
9+
echo "Usage: $0 <test>"
10+
echo " <test> is a task name from .evergreen.yml (e.g. 'e2e_search_community_basic') or a full Evergreen task URL."
11+
}
12+
13+
source scripts/funcs/errors
14+
15+
list_pytest_marks() {
16+
rg -g '*.py' -o --no-line-number --no-heading --replace '$1' -m 1 \
17+
'@(?:pytest\.)?mark\.(e2e_[a-zA-Z0-9_]+)' \
18+
docker/mongodb-kubernetes-tests
19+
}
20+
21+
pick_test_by_file_mark_or_task_url() {
22+
if ! test_list="$(list_pytest_marks | sort -u)"; then
23+
echo "Couldn't list pytest marks."
24+
echo "${test_list}"
25+
return 1
26+
fi
27+
28+
test=$(fzf --print-query --header-first --with-nth '{2}: {1}' -d ':' --accept-nth 2 \
29+
--header "Select file or task to find contexts where its used. You can paste full task's evergreen url here" <<< "${test_list}") || true
30+
if [[ -z ${test} ]]; then
31+
echo "Aborted selecting test"
32+
return 1
33+
fi
34+
35+
# test may contain one or two lines (file:mark or just mark/url)
36+
number_of_selected_lines=$(wc -l <<< "${test}")
37+
if [[ ${number_of_selected_lines} -eq 2 ]]; then
38+
test="$(tail -n 1 <<< "${test}")"
39+
elif [[ ${number_of_selected_lines} -gt 2 ]]; then
40+
echo "Too many lines selected: ${test}"
41+
return 1
42+
fi
43+
44+
echo "${test}"
45+
}
46+
47+
main() {
48+
test="${1:-}"
49+
50+
if [[ -z ${test} ]]; then
51+
test=$(pick_test_by_file_mark_or_task_url)
52+
echo "Selected test: ${test}"
53+
fi
54+
55+
if [[ "${test}" = *spruce.mongodb.com* ]]; then
56+
find_variant_arg="--task-url"
57+
else
58+
find_variant_arg="--task-name"
59+
fi
60+
61+
if ! contexts=$(scripts/evergreen/run_python.sh scripts/python/find_test_variants.py "${find_variant_arg}" "${test}"); then
62+
echo "Couldn't find any test contexts running test: ${test}"
63+
echo "${contexts}"
64+
exit 1
65+
fi
66+
67+
echo "Found contexts that are running test: ${test}"
68+
echo "${contexts}"
69+
70+
selected_context="${contexts}"
71+
if [[ $(wc -l <<< "${contexts}") -gt 1 ]]; then
72+
if ! selected_context=$(fzf --header "${test} runs in multiple variants/contexts. Select one to switch context into." --header-first --layout=reverse <<< "${contexts}"); then
73+
echo "Aborted selecting context"
74+
exit 1
75+
fi
76+
fi
77+
78+
scripts/dev/switch_context.sh "${selected_context}"
79+
}
80+
81+
main "$@"

scripts/evergreen/deployments/test-app/templates/mongodb-enterprise-tests.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,19 @@ spec:
192192
image: {{ .Values.repo }}/mongodb-kubernetes-tests:{{ .Values.tag }}
193193
# Options to pytest command should go in the pytest.ini file.
194194
command: ["pytest"]
195-
{{ if .Values.otel_endpoint }}
196-
args: ["-vv", "-m", "{{ .Values.taskName }}", "--trace-parent", "00-{{ .Values.otel_trace_id }}-{{ .Values.otel_parent_id }}-01", "--export-traces"]
197-
{{ else }}
198-
args: ["-vv", "-m", "{{ .Values.taskName }}"]
199-
{{ end }}
195+
args:
196+
- "-vv"
197+
{{- if .Values.testFile }}
198+
- "{{ .Values.testFile }}"
199+
{{- else }}
200+
- "-m"
201+
- "{{ .Values.taskName }}"
202+
{{- end }}
203+
{{- if .Values.otel_endpoint }}
204+
- "--trace-parent"
205+
- "00-{{ .Values.otel_trace_id }}-{{ .Values.otel_parent_id }}-01"
206+
- "--export-traces"
207+
{{- end }}
200208
imagePullPolicy: Always
201209
volumeMounts:
202210
- name: results

scripts/evergreen/deployments/test-app/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ apiKey: omApiKey
88
orgId: ""
99
projectId: omProjectId
1010
tag:
11+
12+
# if testFile is specified, then test is executed as pytest <testFile>
13+
testFile:
14+
# if testFile is empty, executes the test by fixture mark: pytest -m <taskName>
1115
taskName: ${TASK_NAME}
1216

1317
pytest:

scripts/evergreen/e2e/single_e2e.sh

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ source scripts/funcs/operator_deployment
1515

1616
check_env_var "TEST_NAME" "The 'TEST_NAME' must be specified to run the Operator single e2e test"
1717

18+
find_test_file_by_fixture_mark() {
19+
fixture_mark="$1"
20+
21+
cd docker/mongodb-kubernetes-tests
22+
if ! test_files="$(grep -l -R "mark.${fixture_mark}" --include '*.py')"; then
23+
>&2 echo "Cannot find any test file containing a pytest fixture mark: ${fixture_mark}"
24+
return 1
25+
fi
26+
number_of_files_matched=$(wc -l <<< "${test_files}")
27+
if [[ ${number_of_files_matched} -gt 1 ]]; then
28+
>&2 echo "Found more than one file with the same pytest fixture mark ${fixture_mark}:"
29+
grep --color=auto --line-number --recursive -C2 "mark.${fixture_mark}" --include '*.py' .
30+
return 1
31+
fi
32+
33+
echo -n "${test_files}"
34+
}
1835

1936
deploy_test_app() {
2037
printenv
@@ -33,12 +50,17 @@ deploy_test_app() {
3350
BUILD_ID="${BUILD_ID:-default_build_id}"
3451
BUILD_VARIANT="${BUILD_VARIANT:-default_build_variant}"
3552

53+
if ! test_file=$(find_test_file_by_fixture_mark "${TASK_NAME}"); then
54+
return 1
55+
fi
56+
3657
# note, that the 4 last parameters are used only for Mongodb resource testing - not for Ops Manager
3758
helm_params=(
3859
"--set" "taskId=${task_id:-'not-specified'}"
3960
"--set" "repo=${BASE_REPO_URL:=268558157000.dkr.ecr.us-east-1.amazonaws.com/dev}"
4061
"--set" "namespace=${NAMESPACE}"
4162
"--set" "taskName=${task_name}"
63+
"--set" "testFile=${test_file}"
4264
"--set" "tag=${tag}"
4365
"--set" "aws.accessKey=${AWS_ACCESS_KEY_ID}"
4466
"--set" "aws.secretAccessKey=${AWS_SECRET_ACCESS_KEY}"
@@ -128,7 +150,9 @@ deploy_test_app() {
128150

129151
helm_params+=("--set" "opsManagerVersion=${ops_manager_version}")
130152

131-
helm template "scripts/evergreen/deployments/test-app" "${helm_params[@]}" > "${helm_template_file}" || exit 1
153+
echo "Executing helm template:"
154+
echo "helm template scripts/evergreen/deployments/test-app ${helm_params[*]}"
155+
helm template "scripts/evergreen/deployments/test-app" "${helm_params[@]}" > "${helm_template_file}" || return 1
132156

133157
cat "${helm_template_file}"
134158

@@ -189,7 +213,9 @@ run_tests() {
189213

190214
prepare_operator_config_map "${operator_context}"
191215

192-
deploy_test_app "${test_pod_context}"
216+
if ! deploy_test_app "${test_pod_context}"; then
217+
return 1
218+
fi
193219

194220
wait_until_pod_is_running_or_failed_or_succeeded "${test_pod_context}"
195221

scripts/evergreen/flakiness-report.py renamed to scripts/evergreen/flakiness_report.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import json
2-
import os
31
import sys
42

53
import requests
64

5+
from scripts.python.evergreen_api import get_evergreen_auth_headers
6+
77
EVERGREEN_API = "https://evergreen.mongodb.com/api"
88

99

@@ -18,16 +18,13 @@ def print_usage():
1818

1919

2020
def get_variants_with_retried_tasks() -> dict[str, list[dict]]:
21-
evg_user = os.environ.get("EVERGREEN_USER", "")
22-
api_key = os.environ.get("API_KEY", "")
23-
24-
if len(sys.argv) != 2 or evg_user == "" or api_key == "":
21+
if len(sys.argv) != 2:
2522
print_usage()
26-
exit(1)
23+
raise RuntimeError("Exactly one argument (patch version number) must be provided")
2724

2825
version = sys.argv[1]
26+
headers = get_evergreen_auth_headers()
2927

30-
headers = {"Api-User": evg_user, "Api-Key": api_key}
3128
print("Fetching build variants...", file=sys.stderr)
3229
build_ids = requests.get(url=f"{EVERGREEN_API}/rest/v2/versions/{version}", headers=headers).json()
3330
build_statuses = [build_status for build_status in build_ids["build_variants_status"]]

0 commit comments

Comments
 (0)