orlangure/gnomock

Bug: gnomockd in github action 'services:' & 'container:' requires preallocated exposed port range

Closed this issue · 7 comments

Describe the bug
When using gnomockd as a GitHub Actions "service container" outside the container needs to connect to not only the gnomockd port, but also the ports of the services it starts.

This works when using services on the default runner container, however it fails when the services are run on a specified container.

In order for that to work, the service needs to expose 23042, which works, however the ports of the started services also need to be exposed, so they can be seen in sibling containers.

One way to remedy this is to expose large ranges of ports which gnomockd is likely to end up being given, like -p 24000-26000:24000-26000, however that fails because of moby/moby#11185

In order to get around that, gnomockd would need to force the service containers to be started with ports from a smaller preallocated range.

It might be possible for gnomockd to detect which ports were exposed when its container was set up (i.e. the docker -p 24000-24500:24000-24500). If not, the same range in the docker -p argument needs to be passed to gnomockd as a cli arg so that it knows which range are visible outside the container.

To Reproduce
In order to verify the created services can be accessed, I am using:

nc -vvv -z 127.0.0.1 $(curl 127.0.0.1:23042/start/mongo -d '{}' | jq .ports.default.port)

https://github.com/jayvdb/gnomock/blob/demo-github-action/.github/workflows/test.yaml contains four jobs.

https://github.com/jayvdb/gnomock/actions/runs/1836345457 is the output.

Ignore the first and fourth.

The second shows "services" working normally with image orlangure/gnomock (which is an old version of gnomockd). For some reason, only port 23042 needs to be exposed. I tried removing that, and it fails.

The third shows that using "container: .." for a job breaks gnomockd. It is able to connect to port 23042, but not to the port where the created service is hosted.

The fourth is a more manual but incomplete attempt at trying to get the github action to work with a sibling container. Even connecting to port 23042 fails.

Expected behavior
It should be possible to tell gnomockd to use a predetermined port range for services it starts, so that it is possible to expose them allowing the services started by gnomockd to be accessed from a sibling container.

System (please complete the following information):

  • OS: GitHub Actions runner ubuntu-latest (focal)
  • Version: 9770ce0 - v0.19.0+21
  • Docker version: GitHub Actions runner default

Hi @jayvdb and thank you for the report.

I don't fully understand the problem right away, so I'll need to spend some more time on this before I can get you a solution or any other response.

For now, can you explain in a few words why do you run gnomock server using a service container? In github actions, there is docker daemon in regular runners, so gnomock (and the server using its own github action) can start containers inside the same environment as the tests.

@orlangure , "can start containers inside the same environment as the tests." is the problem. This is only true if the tests are being run inside the GitHub Actions container. This is great for some workflows, especially rather simple ones.

There are lots of reasons why running the tests directly in GitHub Actions is suboptimal, or not possible.
e.g. if I want to run the tests in something more recent than Ubuntu Focal, or I have a complex build environment that takes a long time to build from scratch so instead I want to use a pre-built image, or I want a prebuilt test image because it is common to many repos/workflows/jobs, or re-using test existing github actions which utilise pre-built docker containers.

Hi @jayvdb,

I think I found a solution that will suit your use case: #449

With this, you will be able to provide custom port mappings when creating containers using JSON Payload according to the swagger spec (see the updated swagger.yaml in the pull request). See changes in test files (gnomockd package) for an example payload.

Please use orlangure/gnomock:v0.20.0-alpha1 image, and let me know if this helps.

Ya, that should work. I give it a whirl this w/e.

Hey @jayvdb, did you have a chance to try this out? I'd like to release this change, but it would be nice to get a confirmation from you first. Thanks.

Sorry for the delay.

Tried it out, unsuccessfully, and left some notes at #449 (comment)

@jayvdb, as I pointed out in #449, the explicit host port range is now supported in both direct and remote modes. Github specific usage is just one of the ways to use the feature, and I'll keep looking into adding an example CI job that demonstrates if (if it is even possible).