Refactored the environment-based test filtering.

Review: https://reviews.apache.org/r/25569