aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Burgess IV <gbiv@google.com>2024-04-24 16:55:58 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2024-04-29 15:24:46 +0000
commitf222450312e996b10351c60700668b6736a95924 (patch)
treed35fb5a97f333a55f52530bf2deb51b0d432672a
parentafb1b188082588becea87c4edf1c62d5a181efc1 (diff)
downloadtoolchain-utils-f222450312e996b10351c60700668b6736a95924.tar.gz
afdo_tools: fix e2e tests
These depend on $(pwd). Make them not do that. BUG=b:336823685 TEST=pytest Change-Id: Ie48e784d877c042eccc80875db4104fc5fcbc6c4 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5486676 Tested-by: George Burgess <gbiv@chromium.org> Reviewed-by: Ryan Beltran <ryanbeltran@chromium.org> Commit-Queue: George Burgess <gbiv@chromium.org>
-rwxr-xr-xafdo_tools/bisection/afdo_prof_analysis_e2e_test.py46
-rwxr-xr-xafdo_tools/bisection/state_assumption_external.sh3
-rwxr-xr-xafdo_tools/bisection/state_assumption_interrupt.sh5
3 files changed, 33 insertions, 21 deletions
diff --git a/afdo_tools/bisection/afdo_prof_analysis_e2e_test.py b/afdo_tools/bisection/afdo_prof_analysis_e2e_test.py
index 5bc6ea93..a6e5135e 100755
--- a/afdo_tools/bisection/afdo_prof_analysis_e2e_test.py
+++ b/afdo_tools/bisection/afdo_prof_analysis_e2e_test.py
@@ -8,6 +8,7 @@
from datetime import date
import json
+import logging
import os
import shutil
import tempfile
@@ -141,7 +142,7 @@ class AfdoProfAnalysisE2ETest(unittest.TestCase):
def test_state_assumption(self):
def compare_runs(tmp_dir, first_ctr, second_ctr):
- """Compares given prof versions between first and second run in test."""
+ """Compares given prof versions between 1st and 2nd run in test."""
first_prof = "%s/.first_run_%d" % (tmp_dir, first_ctr)
second_prof = "%s/.second_run_%d" % (tmp_dir, second_ctr)
with open(first_prof) as f:
@@ -163,18 +164,21 @@ class AfdoProfAnalysisE2ETest(unittest.TestCase):
"bad_only_functions": False,
}
- # using a static temp dir rather than a dynamic one because these files are
- # shared between the bash scripts and this Python test, and the arguments
- # to the bash scripts are fixed by afdo_prof_analysis.py so it would be
- # difficult to communicate dynamically generated directory to bash scripts
- scripts_tmp_dir = "%s/afdo_test_tmp" % os.getcwd()
+ my_dir = os.path.dirname(os.path.abspath(__file__))
+ # using a static temp dir rather than a dynamic one because these files
+ # are shared between the bash scripts and this Python test, and the
+ # arguments to the bash scripts are fixed by afdo_prof_analysis.py so
+ # it would be difficult to communicate dynamically generated directory
+ # to bash scripts
+ scripts_tmp_dir = os.path.join(my_dir, "afdo_test_tmp")
os.mkdir(scripts_tmp_dir)
self.addCleanup(shutil.rmtree, scripts_tmp_dir, ignore_errors=True)
# files used in the bash scripts used as external deciders below
- # - count_file tracks the current number of calls to the script in total
+ # - count_file tracks the current number of calls to the script in
+ # total
# - local_count_file tracks the number of calls to the script without
- # interruption
+ # interruption
count_file = "%s/.count" % scripts_tmp_dir
local_count_file = "%s/.local_count" % scripts_tmp_dir
@@ -183,17 +187,21 @@ class AfdoProfAnalysisE2ETest(unittest.TestCase):
good_prof,
bad_prof,
expected,
- extern_decider="state_assumption_external.sh",
+ extern_decider=os.path.join(my_dir, "state_assumption_external.sh"),
)
with open(count_file) as f:
num_calls = int(f.read())
os.remove(count_file) # reset counts for second run
- finished_state_file = "afdo_analysis_state.json.completed.%s" % str(
- date.today()
+ finished_state_file = os.path.join(
+ my_dir,
+ f"afdo_analysis_state.json.completed.{date.today()}",
)
self.addCleanup(os.remove, finished_state_file)
# runs the same analysis but interrupted each iteration
+ interrupt_decider = os.path.join(
+ my_dir, "state_assumption_interrupt.sh"
+ )
for i in range(2 * num_calls + 1):
no_resume_run = i == 0
seed = initial_seed if no_resume_run else None
@@ -203,7 +211,7 @@ class AfdoProfAnalysisE2ETest(unittest.TestCase):
bad_prof,
expected,
no_resume=no_resume_run,
- extern_decider="state_assumption_interrupt.sh",
+ extern_decider=interrupt_decider,
seed=seed,
)
break
@@ -218,7 +226,8 @@ class AfdoProfAnalysisE2ETest(unittest.TestCase):
start = 3
for ctr in range(start, num_calls):
- # second run counter incremented by 4 for each one first run is because
+ # second run counter incremented by 4 for each one first run is
+ # because
# +2 for performing initial checks on good and bad profs each time
# +1 for PROBLEM_STATUS run which causes error and restart
compare_runs(scripts_tmp_dir, ctr, 6 + (ctr - start) * 4)
@@ -237,8 +246,8 @@ class AfdoProfAnalysisE2ETest(unittest.TestCase):
temp_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, temp_dir, ignore_errors=True)
- good_prof_file = "%s/%s" % (temp_dir, "good_prof.txt")
- bad_prof_file = "%s/%s" % (temp_dir, "bad_prof.txt")
+ good_prof_file = os.path.join(temp_dir, "good_prof.txt")
+ bad_prof_file = os.path.join(temp_dir, "bad_prof.txt")
good_prof_text = analysis.json_to_text(good_prof)
bad_prof_text = analysis.json_to_text(bad_prof)
with open(good_prof_file, "w") as f:
@@ -249,14 +258,15 @@ class AfdoProfAnalysisE2ETest(unittest.TestCase):
dir_path = os.path.dirname(
os.path.realpath(__file__)
) # dir of this file
- external_script = "%s/%s" % (
+ external_script = os.path.join(
dir_path,
extern_decider or "e2e_external.sh",
)
- # FIXME: This test ideally shouldn't be writing to $PWD
+ # FIXME: This test ideally shouldn't be writing to the directory of
+ # this file.
if state_file is None:
- state_file = "%s/afdo_analysis_state.json" % os.getcwd()
+ state_file = os.path.join(dir_path, "afdo_analysis_state.json")
def rm_state():
try:
diff --git a/afdo_tools/bisection/state_assumption_external.sh b/afdo_tools/bisection/state_assumption_external.sh
index a2076b0d..ecc48af0 100755
--- a/afdo_tools/bisection/state_assumption_external.sh
+++ b/afdo_tools/bisection/state_assumption_external.sh
@@ -11,7 +11,8 @@ BAD_STATUS=1
SKIP_STATUS=125
PROBLEM_STATUS=127
-tmp_dir=$(pwd)/afdo_test_tmp
+my_dir="$(dirname "$(readlink -m "$0")")"
+tmp_dir="${my_dir}/afdo_test_tmp"
count_file=${tmp_dir}/.count
# keep count for purpose of filenames
diff --git a/afdo_tools/bisection/state_assumption_interrupt.sh b/afdo_tools/bisection/state_assumption_interrupt.sh
index d1599d0b..6eb5c2cb 100755
--- a/afdo_tools/bisection/state_assumption_interrupt.sh
+++ b/afdo_tools/bisection/state_assumption_interrupt.sh
@@ -8,7 +8,8 @@
PROBLEM_STATUS=127
-tmp_dir=$(pwd)/afdo_test_tmp
+my_dir="$(dirname "$(readlink -m "$0")")"
+tmp_dir="${my_dir}/afdo_test_tmp"
count_file="${tmp_dir}/.count"
if [[ -f "${count_file}" ]]; then
@@ -34,5 +35,5 @@ if [[ ${local_count} -ge 2 ]] && [[ $(( ${num_call}%2 )) -ne 0 ]]; then
fi
# script just needs any second argument to write profs to .second_run_*
-$(pwd)/state_assumption_external.sh "$1" 'second_run'
+"${my_dir}/state_assumption_external.sh" "$1" 'second_run'
exit $?