Автоматизируйте e2e-тестирование с помощью selenium приложения Django в gitlab cicd -Error: selenium.common.exceptions.WebDriverException: neterror?e=dnsNotFound

Вот вывод моего cicd pipline, который не работает

base/tests/e2e_tests/test_register.py F                                  [100%]
=================================== FAILURES ===================================
_____________ TestRegistrationPage.test_register_valid_credentials _____________
self = <test_register.TestRegistrationPage testMethod=test_register_valid_credentials>
    def test_register_valid_credentials(self):
        """
        Test whether the registration process works flawlessly.
        This method asserts that after sucessful redirect url equals home.
        """
>       self.driver.get("http://secprog:8080/")

FAILED base/tests/e2e_tests/test_register.py::TestRegistrationPage::test_register_valid_credentials - selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=dnsNotFound&u=http%3A//secprog%3A8080/&c=UTF-8&d=We%20can%E2%80%99t%20connect%20to%20the%20server%20at%20secprog.
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:193:5
UnknownError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:832:5
checkReadyState@chrome://remote/content/marionette/navigate.sys.mjs:58:24
onNavigation@chrome://remote/content/marionette/navigate.sys.mjs:330:39
emit@resource://gre/modules/EventEmitter.sys.mjs:148:20
receiveMessage@chrome://remote/content/marionette/actors/MarionetteEventsParent.sys.mjs:33:25

Вот мой Dockerfile:


# Stage 1: Build stage
FROM python:3.12.0b2-alpine3.17

RUN apk update


WORKDIR /app
COPY . .

EXPOSE 8080

CMD ["python", "manage.py", "runserver", "0.0.0:8080"]

Вот мой .gitlab-ci.yml. на этапе сборки все работает нормально и передается. Проблема возникает в моем run_e2e_test. Я не знаю, где у меня ошибка. Я предполагаю, что проблема в том, как я определяю псевдонимы для служб, но я не знаю, как эти службы могут взаимодействовать друг с другом:


stages:
    - unit_tests
    - build
    - integration_tests
    - static_code_analysis
    - start_server
    - sec_vuln_assessment
    - e2e_tests


run_build:
    stage: build
    image: docker:20.10.16
    services: 
        - docker:20.10.16-dind
    variables:
        DOCKER_TLS_CERTDIR: "/certs"
    before_script:
        # login working, but -p is unsecure. try --password-stdin
        - docker login registry.mygit.th-deg.de -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD
    script:
        - docker build -t registry.mygit.th-deg.de/pk27532/secprog .
        - docker push registry.mygit.th-deg.de/pk27532/secprog

run_e2e_tests:
    stage: e2e_tests
    image: docker:20.10.16
    services:
        - name: selenium/standalone-firefox:latest
          alias: selenium
        - name: registry.mygit.th-deg.de/pk27532/secprog
          alias: secprog
    variables:
        FF_NETWORK_PER_BUILD: 1
    before_script:
        - apk update
        - apk add python3 
        - python3 -m ensurepip
        - pip3 install -r requirements.txt
    script:
        - python3 -m pytest base/tests/e2e_tests/test_register.py

Вот мой тестовый файл: Как только мой код выполняется в gitlab cicd pipeline, тесты e2e не работают:


import os
import time
import subprocess
import socket
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from django.test import LiveServerTestCase

load_dotenv()
valid_registration_username = os.getenv("VALID_REGISTRATION_USERNAME")
valid_registration_email = os.getenv("VALID_REGISTRATION_EMAIL")
valid_registration_email_pwd = os.getenv("VALID_REGISTRATION_EMAIL_PWD")
valid_registration_test_pwd = os.getenv("VALID_REGISTRATION_TEST_PWD")


class TestRegistrationPage(LiveServerTestCase):
    """
    Class for testing the registration process.
    This class inherits from Django's LiveServerTestCase to run tests against a live server.
    """

    def setUp(self):
        """
        This method is called before each test case to set up the Selenium WebDriver.
        """
        super().setUp()
        if "CI" in os.environ.keys():
            options = webdriver.FirefoxOptions()
            self.driver = webdriver.Remote(
                command_executor="http://selenium:4444", options=options
            )
        else:
            subprocess.Popen(["python", "manage.py", "runserver"])
            time.sleep(5)
            self.driver = webdriver.Firefox()

    def tearDown(self):
        """
        This method is called after each test case to quit the Selenium WebDriver.
        """
        self.driver.quit()
        super().tearDown()

    def test_register_valid_credentials(self):
        """
        Test whether the registration process works flawlessly.
        This method asserts that after sucessful redirect url equals home.
        """
        self.driver.get("http://secprog:8080/")
        self.assertIn("secProg", self.driver.title)

Я пробовал несколько разных способов, но так и не смог заставить их работать. Локально тесты selenium работают отлично, но я не могу автоматизировать их в своем конвейере.

Вернуться на верх