question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pytest hangs at "collecting..." when using GitLab CI/CD service

See original GitHub issue

Problem description

Yesterday, I ran into a problem when running a GitLab CI/CD pipeline using pytest to perform the integration tests step.

I can run my tests locally and I confirmed that I can reach my service on GitLab CI through Curl. However, when I try to reach my service through an http request inside my test stub, pytest gets stuck at “collecting…”, without any log to indicate what the error could be.

I have also tried to change the http request host to our production server or even giving it a wrong host and that works fine.

I do not believe the error is with GitLab, because I can reach the service just fine with Curl and I don’t think it is a DNS problem, because pytest is not throwing any error, it is simply stuck.

Either way, I believe there should at least be more logs on the pytest side to help me debug this problem, because right now I have no idea what can be causing this issue.

Example:

This is my .gitlab-ci.yml file:

test:
  image: python:latest
  stage: test
  tags:
    - docker
  variables:
    FF_NETWORK_PER_BUILD: 1 # Allows communication between services

    # Postgresql
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: xxx
    POSTGRES_DB: account_manager

  services:
    - name: postgres:latest
      alias: postgresql
      command: [ "postgres", "-c", "wal_level=logical" ]

    # SERVICE THAT GETS PYTEST STUCK
    - name: gitlab-docker-registry/account-manager-service:latest
      alias: account-manager-test

  before_script:
    - apt-get update

    # Test connection to account-manager-test service
    - |
      curl -H "X-Correlation-ID: 5c70300e-9f45-4447-bd09-72f4ba3c8209" http://account-manager-test:8080/api/account/user?user-ids=1234567892
    # Works just fine

    - pip install pytest

  script:
    - pytest device_manager_service -s -vvv --full-trace --color=yes --code-highlight=yes

My account-manager-test container has a flask-sqlalchemy API and I communicate with it using http with the python requests module. Below is the part of my test stub where I use the gitlab service in question:

import requests
import json

....
# The host is the service alias
new_user = requests.post(url="http://account-manager-test:8080/api/account"+ "/register", data=register_data, headers=headers)

new_user_id = json.loads(new_user.content)['user_id']

In my understanding, there should be no problems using the gitlab ci service here. However, this is the output I get on my shared runner:

image

I left it running for 1 hour and got no output from pytest.

And just to make sure, I made a normal python script that made an http request using the service alias (without pytest involved) and that worked! So I’m convinced the problem is indeed on the side of pytest.

Container environment:

pytest==6.2.*
pytest-cov==2.11.*
pytest-randomly==3.7.*
Flask-Testing==0.8.*
jsonschema==3.2.*
prance==0.20.*
importlib-metadata==4.11.*
connexion==2.7.*
python-dateutil==2.8.*
setuptools==56.2.*
Flask==1.1.*
waitress==2.0.*
Flask-SQLAlchemy==2.5.*
psycopg2==2.8.*
prometheus-flask-exporter==0.18.*
SQLAlchemy==1.4.*
opencensus==0.7.*
opencensus-ext-ocagent==0.7.*
opencensus-ext-flask==0.7.*
opencensus-ext-sqlalchemy==0.1.*
opencensus-ext-requests==0.7.*
opencensus-ext-logging==0.1.*
python-json-logger==2.0.*
kafka-python==2.0.*
marshmallow==3.13.*
urllib3==1.26.*
PyJWT==2.3.*
markupsafe==2.0.1

I know this is not an easily reproducible issue (you need a gitlab runner to reproduce), but I think that pytest should at least print some output to help the programmer understand where the error might be. If anyone has any sort of input, or has had a similar experience to mine, I would be very grateful for your help.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
Vasco27commented, May 12, 2022

Well, I had -s in every test, so I’m guessing the python on gitlab might be set to do block buffering instead of line buffering, but I’m not really sure. The fact is that I tried locally and it is printing ok during the collection phase, so it does seem like the problem is with the gitlab runner buffering.

Anyway, thank you for your help debugging. I know where the error is now and I can debug it properly. (and btw, your videos are great, you just gained a subscriber 😃 )

1reaction
RonnyPfannschmidtcommented, May 12, 2022

closing as resolved as ci interaction hiding ci related errors

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pytest in Gitlab-ci Docker Runner does not collect testcases
Hi I don't know exactly what is the root cause. However, I feel pytest will hint me first clue for further investigation. GitLab...
Read more >
Gitlab runner hangs infrequently on Python's `pip install` or ...
Hi, I use gitlab runner with docker in docker. Unfortunately, our runners sometimes ... GitLab CI/CD ... Pip hangs on "collecting numpy".
Read more >
Pytest stuck on 'collecting...' - python - Stack Overflow
In case someone is still facing this issue and none of their code can lead to infinite loop, try deleting pycache directories that...
Read more >
Continuous Integration With Python: An Introduction
In this Python tutorial, you'll learn the core concepts behind Continuous Integration (CI) and why they are essential for modern software engineering teams....
Read more >
Why Do My Tests Pass Locally but Fail on CircleCI?
This behavior has several possible explanations: Different language versions. Ensure you are using the same language version on your machine ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found