Running localhost server for Unit Tests executed through GitHub Actions
Asked Answered
H

1

12

I have the following GitHub workflow for building my project

name: build

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    - name: Build with Maven
      run: mvn clean compile test

The build works just fine. However the project JUnit test require that a localhost server is listening on port 4444 ... and I get the following error:

Connection refused: localhost/127.0.0.1:4444

The server is spun up before each JUnit test and is part of the tests suite.

How do I tell the docker container that network connections are allowed on this port? Or are there any open ports by default?

Hildick answered 5/2, 2020 at 15:43 Comment(7)
what is your test docker container config?Repartee
don't know if you can influence that ... this is a docker provided by github as isHildick
Is the server running in the same container as the the JUnit test? You can explicit expose ports on a docker container. Make sure to reference the correct container if the server is running in a different containerTelephonist
Did you make any progress to this?Titograd
@Titograd no I gave up, and disabled the build, have opened an issue with GitHub, but no constructive response from them. I have already wasted too many hours on this.Hildick
I'm also getting the same problem. Did you get the solution?Rebuke
@Rebuke I was not able to fix this issue (after hours spent). But I think this is not an GitHub Actions problem but a Docker related issue. I have created a separate Docker image and run the same scenario with tests locally. Tests kept failing randomly. Also tried increasing the port number on each test, but same result. You can take a look at my open source project, check out: github.com/zandero/rest.vertx/blob/master/docker.txt and github.com/zandero/rest.vertx/blob/master/Dockerfile, it might be a timing issue with ports not closing fast enough?Hildick
H
3

I share my solution. Hopefully it will help.

  1. The Dockerfile for the test server listening on port (in my case 8080). As the comments mention above, you need to expose your port
FROM golang:1.15.2-alpine

# Setup you server

# This container exposes port 8080 to the outside world
EXPOSE 8080

# Run the executable
CMD ["./main"]
  1. The docker-compose file (this is not a must, but in my case I had to start multiple depending containers). Again make sure the port is defined
  main-server:
    build: ./
    container_name: main-server
    image: artofimagination/main-server
    ports:
      - 8080:8080
  1. The github action. Just run the docker-compose command for your server. Then the application that needs to connect to the port. In this case pytest tries to send requests to main-server through port 8080 It is worth to mention that in my example pytest accessing 127.0.0.1:8080
      - name: Check out code into the Go module directory
        uses: actions/checkout@v2

      - name: Start test server
        run: docker-compose up -d main-server

      - name: Run functional test
        run: pip3 install -r test/requirements.txt && pytest -v test

Good luck and I hope this helps.

Healion answered 2/12, 2020 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.