changelog.com icon indicating copy to clipboard operation
changelog.com copied to clipboard

Docker: Make it easier to run mix tests

Open lawik opened this issue 4 years ago • 8 comments

I currently run this: DB_URL="ecto://postgres@db:5432/changelog_test" mix test

There was some additional setup to get there as well if I recall. There are some things that could be cleaned up there.

lawik avatar Jul 25 '20 09:07 lawik

I haven't run the test suite for changelog in a while but I know on other Elixir projects you need to explicitly set the MIX_ENV to be "test" when running your tests.

What happens if you do this:

  1. docker-compose up
  2. docker-compose exec -e MIX_ENV=test app mix test

nickjj avatar Jul 25 '20 09:07 nickjj

Then it connects to the dev database. mix test handles the MIX_ENV=test for me, as it usually does. So there is some adjustments around config or environment variables that need to be made to run that cleanly. I mostly left the issue here so this isn't a mystery to myself or the next person. I intend to take a look at it when I have some time to spare. Probably as part of project "make Jerod use the Docker setup" ;)

lawik avatar Jul 25 '20 11:07 lawik

Ah, right I copied a lot of things from this repo into my own project but one thing I did change was explicitly appending _test to the database name in my config/test.exs but I still did need to add the MIX_ENV env variable when running mix test like above.

nickjj avatar Jul 25 '20 11:07 nickjj

The simplest way to run tests for changelog.com is via make test which translates into:

docker-compose run --rm -e MIX_ENV=test -e DB_NAME=changelog_test app mix test

The thing with docker-compose run is that it runs a one-off command against a running service: https://docs.docker.com/compose/reference/run/. In our case, the service is app which is already running in development mode. To run tests, we need to override MIX_ENV and DB_NAME. We previously had DB_URL, but this commit changes it: https://github.com/thechangelog/changelog.com/commit/1b573a0da1367e0d9382d2805225f85a30b50d4b

I'm wondering whether the following would address the issue that you raise @lawik

docker-compose run test

Having said that, even if we make it easier to run tests in Docker, I am wondering whether we should take a step back and re-think the contributing to changelog.com via Docker approach.

On a Mac, running tests in Docker is 2x-3x slower than running them locally - it's the primary reason why @jerodsanto doesn't develop via Docker. When it comes to loading pages, the slow-down is even more significant: https://github.com/thechangelog/changelog.com#why-is-docker-for-mac-slow

On Linux, there are all the issues that you have raised in #324 & #325. While we could solve them, Docker for Linux will still behave differently from Docker for Mac, and there is nothing that we can do about it.

Rather than focusing on Docker, maybe it would make more sense to capture @jerodsanto'd Mac dev workflow & your Linux (Ubuntu) dev workflow in a single command. This command would handle all Mac vs Linux differences and configure all deps required to develop changelog.com locally. While that was the original intent of make contrib, we have sufficient evidence to suggest that a Docker-based implementation doesn't work that well in practice.

If you were to start from a clean slate, what does the ideal path to contributing code to changelog.com look like to you @lawik? Help me see your starting point, and how you would expect things to work. Focus on what you care about, and less on how you solved things.

gerhard avatar Jul 31 '20 15:07 gerhard

Have you tried the edge release of Docker Desktop with Mutagen?

I don't run MacOS but some folks are reporting that volume mount performance is at native MacOS speed with it. The Mutagen feature is something you can enable in Docker Desktop.

Here's some benchmarks and how to use it: https://github.com/docker/for-mac/issues/1592#issuecomment-634960996

On Windows 10 with Docker Desktop, the volume performance is very good out of the box btw. No issues with anything really.

Also #325 isn't a bug with Docker. I'm using Live Reload here on both Linux and Windows in a Phoenix app. It just required adding inotify-tools in the Docker image.

nickjj avatar Aug 01 '20 14:08 nickjj

Hmm.. my starting point. I mean I would probably just run a local postgres and want to run:

mix deps.get mix ecto.create mix ecto.migrate mix phx.server

And I'd be up and running with whatever the requirements are.

And to run tests I'd like to use:

mix test

I think the docker-compose experience is fine apart from the slowdowns and I think the issues beyond the slowness are minor fixes. I think getting the docker effect to not have to support multiple types of environments would be good overall. But normally Elixir projects are fairly cross-platform so what are the things the Jerod setup does today that is currently enshrined in the Docker Compose?

I don't have a strong opinion on how this should work. I'll roll with whatever :)

Did you listen to the Jessica Kerr episode on Changelog? She insisted on Docker for dev. Thought it was interesting at least.

lawik avatar Aug 01 '20 18:08 lawik

Have had a moment to try some of the stuff. make test works fine. But a very common use-case for me is running a part of the test-suite. Such as mix test test/changelog and make test doesn't pass those args on.

It did work with docker-compose run --rm -e MIX_ENV=test -e DB_NAME=changelog_test app mix test test/changelog which is a bit of a screenful as far as character-count. But I'm glad it works.

lawik avatar Aug 11 '20 07:08 lawik

Idem https://github.com/thechangelog/changelog.com/issues/324#issuecomment-1224976158

gerhard avatar Aug 23 '22 23:08 gerhard