Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running localhost server for Unit Tests executed through GitHub Actions

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?

like image 325
Drejc Avatar asked Feb 05 '20 15:02

Drejc


Video Answer


1 Answers

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.

like image 105
Peter Devenyi Avatar answered Sep 20 '22 15:09

Peter Devenyi