5

I am trying to set up Codeception to do Acceptance and Functional testing for my web app. Below are my files:

docker-compose.yml

version: '3.7'

services:
  # nginx - web server
  nginx:
    build:
      context: ./docker-config/nginx
      dockerfile: ./Dockerfile
    env_file: &env
      - ./cms/.env
    init: true
    ports:
      - "8000:80"
    volumes:
      - cpresources:/var/www/project/cms/web/cpresources
      - ./cms/web:/var/www/project/cms/web:cached
    networks:
      mmc-network:
        aliases:
          - mmc.nginx
  # php - run php-fpm
  php:
    build: &php-build
      context: ./docker-config/php-prod-craft
      dockerfile: ./Dockerfile
    depends_on:
      - "mysql"
      - "redis"
    env_file:
      *env
    expose:
      - "9000"
    init: true
    volumes: &php-volumes
      - some volumes............
    networks:
      mmc-network:
        aliases:
          - mmc.php

  # mysql - database
  mysql:
    build:
      context: ./docker-config/mysql
      dockerfile: ./Dockerfile
    cap_add:
      - SYS_NICE  # CAP_SYS_NICE
    env_file:
      *env
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: project
      MYSQL_USER: project
      MYSQL_PASSWORD: project
    init: true
    ports:
      - "3306:3306"
    volumes:
      - db-data:/var/lib/mysql
      - ./db-seed/:/docker-entrypoint-initdb.d
    networks:
      - MMC-network

  # redis - key/value database for caching & php sessions
  redis:
    build:
      context: ./docker-config/redis
      dockerfile: ./Dockerfile
    expose:
      - "6379"
    init: true
    networks:
      - mmc-network

  # webpack - frontend build system
  webpack:
    build:
      context: ./docker-config/node-dev-webpack
      dockerfile: ./Dockerfile
    env_file:
      *env
    init: true
    ports:
      - "8080:8080"
    volumes:
      - some volumes..........
    networks:
      - mmc-network

  # selenium -  web driver for codeception testing
  selenium:
    container_name: mmc-selenium
    ports:
      - "4444:4444"
    volumes:
      - ./cms:/data
    build:
      context: ./docker-config/selenium
      dockerfile: ./Dockerfile
    networks:
      mmc-network:
        aliases:
          - mmc.selenium

volumes:
  db-data:
  cpresources:
  storage:

networks:
  mmc-network:

acceptance.suite.dist.yml:

actor: AcceptanceTester
extensions:
  enabled:
    - Codeception\Extension\RunFailed
    - Codeception\Extension\Recorder
modules:
  error_level: "E_ALL"
  enabled:
    - WebDriver:
        url: 'http://mmc.nginx' 
        host: mmc.selenium 
        port: '4444'
        window_size: 1920x1200 
        browser: 'chrome' 
        wait: 60 
        capabilities:
          os: Windows
          os_version: 10
          browserstack.local: true
    - \Helper\Acceptance

NavigationCept.php

<?php 
$I = new AcceptanceTester($scenario);

# define purpose of the test
$I->wantTo("check that navigation is functional.");

# check all menu items
$I->amOnPage('/');
$I->see('Search');

***Now, point to be noted, Codeception is already installed inside my PHP container and working perfectly.

When I try to run the test, I get the below error which indicates that the connection to my host (which is my Nginx server) has been refused. enter image description here

I tried with a different url, for example, https://google.com and it just connected fine and everything was successful. Am I doing something wrong in here? Is my url param incorrect? Please help me out if you can. Thanks in advance.

Sam Joshua
  • 310
  • 6
  • 17
  • Could you run tests with --debug flag and copy the output here – Ashok Jun 21 '21 at 18:56
  • Your nginx is listening on port 8000, not 80. How do you inform the client about this? (Sorry, know nothing about WebDriver params). Won't `url: http://mmc.nginx:8000` help? – Olesya Bolobova Jun 23 '21 at 00:40
  • hi, @OlesyaBolobova I think I tried that already but not sure. I will try it and let you know. :) – William Francis Gomes Jun 24 '21 at 10:47
  • I would debug that by entering the selenium container and see what you get from there `docker-compose exec -it selenium bash` `curl http://mmc.nginx/` - add the output to your question. – Jonas Eberle Jun 25 '21 at 10:07
  • @WilliamFrancisGomes were you able to fix the issue? – LMC Jun 29 '21 at 16:01

2 Answers2

0

Docker service are reachable from each other using the service name as hostname. So your configuration could be:

  enabled:
    - WebDriver:
        url: 'http://nginx' 
        host: selenium 
        port: '4444'

An alias to a service could be specified at another service as:

selenium:
    links:
      - "nginx:mmc.nginx"

Then the url could be url: 'http://mmc.nginx'

Workaround: use service IP address

  enabled:
    - WebDriver:
        url: 'http://172.18.0.3' 
        host: 172.18.0.2 
        port: '4444'

Test setup using busybox:
docker-compose.yaml that runs 2 dummy web servers with netcat utility for demonstration purposes.

version: '3.7'

services:
  nginx:
    container_name: mmc-nginx
    hostname: mmc-nginx
    image: busybox
    command: ["sh", "-c", "while true ; do (echo -e 'HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-type: application/xml\r\n\r\n'; echo 'response from '`hostname`':80') | nc -l -p 80 ;done" ]
    init: true
    ports:
      - "8000:80"
    networks:
      - mmc-network

  # selenium -  web driver for codeception testing
  selenium:
    container_name: mmc-selenium
    hostname: mmc-selenium
    command: ["nc", "-l", "-p", "4444"]
    ports:
      - "4444:4444"
    image: busybox
    networks:
      - mmc-network

networks:
  mmc-network:
    name: mmc-network

test command:

docker exec -it mmc-selenium wget -q "http://mmc-nginx" -O -
docker exec -it mmc-selenium wget -q "http://172.18.0.3" -O -

Response:

response from mmc-nginx:80

Access from outside docker network:

wget -q "http://localhost:8000" -O -

response from mmc-nginx:80

Ping to mmc-nginx

docker exec -it mmc-selenium ping -c2 mmc-nginx

PING mmc-nginx (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.071 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.111 ms

--- mmc-nginx ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.071/0.091/0.111 ms

Find service IP address:

Using docker. Change svc value accordingly.:

svc='friendly_solomon' docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$svc"

Using jq. Change svc value accordingly. jq is a great json processing tool to have btw.

svc='mmc-nginx'; docker network inspect bridge | jq -r --arg svc "$svc" '.[] | .Containers | .[] | select(.Name == $svc) | .IPv4Address'

Using grep

svc='mmc-nginx' docker network inspect bridge | grep -A5 "$svc" | grep 'IPv4Address
LMC
  • 10,453
  • 2
  • 27
  • 52
  • I tried both but didn't help i am afraid. :( – William Francis Gomes Jun 25 '21 at 09:56
  • Did you tried opening a bash session on selenium and pinging the nginx box? will add some about that to my answer. – LMC Jun 25 '21 at 15:15
  • i see that you updated the code. not so clear with the two commands that you wrote in your yml file, can you please explain what is the command for the Nginx container is doing? thanks. :) – William Francis Gomes Jun 25 '21 at 21:18
  • Both commands open a dummy webserver for demonstration purposes since I don't have your exact setup. The idea is to show that service names are hostnames in docker. In your case the problem might be that port 80 is not opened by `nginx` service if you run the setup as non root user. `nc` is `netcat`utility. – LMC Jun 25 '21 at 21:25
  • Port 80 actually works on the docker internal network. This is important to set the hostname to what you want: `container_name: mmc-nginx`. `docker ps` should show the actual container name. – LMC Jun 25 '21 at 21:53
0

I have seen such exception earlier with Codeception + Selenium. I do not have that project config file to share with you though.

My strong guess here is this error could be due to conflicts around Codeception-ChromeDriver-Selenium versions

  • Have you tried to execute this on a lower ChromeDriver version say 80 or 75?
  • Or change the Selenium version?

You could review the following notes which may help you debug this more

Will update this answer if I find other resources to resolve this.

Xtraterrestrial
  • 651
  • 1
  • 6
  • 12