I have tests running with `pants test` that reach ...
# general
p
I have tests running with
pants test
that reach outside the sandbox (specifically, integration tests that use a database in docker). This is fine locally, where I either have all relevant containers running or can easily determine what's missing, but less practical in CI where I could be starting up a bunch of unnecessary containers (and hence making CI run for longer, making people wait longer for PR checks to pass and running up a higher bill). I'll add a bit more specific of an example in the thread. Is there any way to encode these dependencies and have pants determine what needs to be running (and, ideally, start it too since we have a functional docker compose, but I'd happily settle for just listing what's needed)?
e.g. suppose I have tests under
foo/
that depend on a postgres container, and a compose file defining how to start said container:
Copy code
postgres:
    image: postgres:15.2
    ports:
      - 5432:5432
The idea being I can combine
--changed-since
(determining that I need to run tests under
foo/
) with this dependency detection to start the relevant containers ahead of actually running the tests.
c
I think this cannot be done without a custom plugin. Pants doesn't really model the idea of "services that must be running while other things run". A suggestion not at the Pants level: you could do it pretty easily with pytest fixtures that are scoped to "session" that will start the docker compose. (You can then either have the fixtures terminate the docker composes or keep them running for speed)
p
I ended up adding phony targets that I added to the dependencies of the files that incur those dependencies on services (e.g. the file that defines models stored in postgres has a dependency on the phony
//:service_postgres
target which is defined like:
Copy code
target(
    description="A placeholder target used only to detect what services need to be running for a given piece of code to run successfully",
    name="service_postgres",
    tags=["service"],
)
I then add a step in CI that filters dependencies for the tests it's about to run for targets with that tag (
pants --changed-since=${SHA} --changed-dependents=transitive --tag="service" list
), then start them ahead of the tests. Not very elegant or integrated, but works enough 🤷