Clean producer and consumer env var between tests
We have different types of integration tests:
1. Some talk to a system tracing service started by the test itself on a
custom socket. E.g. src/tracing/test/api_integrationtest.cc
2. Some want to talk to the real system tracing service on android. E.g.
test/android_integrationtest.cc
Type 1 tests change PERFETTO_*_SOCK_NAME environment variables, in order
to redirect consumer and producer to the "testing" service. They don't
restore the previous values of the environment variables though.
Today (by chance?) we execute type 2 tests before type 1 in
perfetto_integrationtests. r.android.com/2203568 added a type 1 test
that was executed before the type 2 tests. The type 2 tests started
failing because suddenly they were redirected to the "testing" service
instead of the real one, because the type 1 tests were leaking the
environment variables.
This commit fixes the problem by restoring the previous values for the
environment variables on each test TearDown().
In one case (src/tracing/test/api_test_support.cc), this is not as
straightforward, because the code caches the "testing" server among
different test executions.
Tested by running `atest perfetto_integrationtests` with or without
r.android.com/2203568
Change-Id: I935d6a8fe964c039ad7f804ec1498466ba701a03
diff --git a/src/base/utils.cc b/src/base/utils.cc
index cb24d25..198daa9 100644
--- a/src/base/utils.cc
+++ b/src/base/utils.cc
@@ -187,6 +187,14 @@
#endif
}
+void UnsetEnv(const std::string& key) {
+#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
+ PERFETTO_CHECK(::_putenv_s(key.c_str(), "") == 0);
+#else
+ PERFETTO_CHECK(::unsetenv(key.c_str()) == 0);
+#endif
+}
+
void Daemonize(std::function<int()> parent_cb) {
#if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \