GitLab CI容器内无法访问本地Flask应用的问题咨询与解决
问题:GitLab CI容器内集成测试无法访问本地Flask应用
相关配置文件
.gitlab-ci.yml内容如下:
image: python:3.8 variables: PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip" cache: paths: - .cache/pip - venv/ before_script: - python -V # Print out python version for debugging - pip install --upgrade pip - pip install virtualenv - virtualenv venv - source venv/bin/activate - pip install -r requirements.txt - cp properties_ci properties - apt-get update - apt-get install net-tools stages: - build - test - security - leanix include: - ... test:unit: stage: ... test:integration: stage: test script: - echo "0" - python app.py & - curl 127.0.0.1:8126 - py.test tests/integration/test_integration.py services: - name: cassandra:3.11
应用启动日志
执行python app.py启动Flask应用时,日志显示Cassandra连接及初始化正常,服务启动在http://127.0.0.1:8126:
$ python app.py WARNING:cassandra.cluster:Cluster.__init__ called with contact_points specified, but no load_balancing_policy. In the next major version, this will raise an error; please specify a load-balancing policy. (contact_points = ['cassandra'], lbp = None) WARNING:cassandra.connection:An authentication challenge was not sent, this is suspicious because the driver expects authentication (configured authenticator = PlainTextAuthenticator) WARNING:cassandra.connection:An authentication challenge was not sent, this is suspicious because the driver expects authentication (configured authenticator = PlainTextAuthenticator) Creating keyspace if not exist... Creating tables if not exist... * Serving Flask app 'src' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:8126
集成测试报错信息
但集成测试执行时,访问http://127.0.0.1:8126出现连接拒绝错误:
Creating keyspace if not exist... =========================== short test summary info ============================ ERROR tests/integration/test_integration.py - requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8126): Max retries exceeded with url: /getCSRFToken (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f316d3df700>: Failed to establish a new connection: [Errno 111] Connection refused')) !!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!! =============================== 1 error in 3.05s ===============================
补充测试脚本代码(编辑1)
测试脚本test_integration.py中访问应用的相关代码:
... url = "http://localhost:8126/series/" import zipfile with zipfile.ZipFile("tests/integration/20230323.zip", mode="r") as archive: #Iterate through files in zip file for zipfilename in archive.filelist: txtdata = archive.read(zipfilename) if (zipfilename.filename == 'OP_lastchange_CRUDE_2023-03-23.json'): payload2 = json.dumps(json.loads(txtdata)) response = requests.request("POST", url, headers=headers, data=payload2) assert response.status_code == 204, "Santa Claus is not coming this year !" ...
解决方案(编辑2)
修改test:integration的script步骤,在后台启动应用后添加等待时间,确保应用完成初始化再执行测试:
test:integration: stage: test script: - echo "0" - python app.py & - sleep 5 # 等待应用完成Cassandra连接、表创建等初始化操作 - curl 127.0.0.1:8126 # 验证服务已就绪 - py.test tests/integration/test_integration.py services: - name: cassandra:3.11
原因:Flask应用启动后需要时间完成Cassandra连接、键空间/表创建等初始化工作,直接紧接着执行测试会因服务未完全就绪导致连接拒绝,添加等待时间让应用完成初始化后再执行测试即可解决问题。
内容的提问来源于stack exchange,提问作者R13mus
相关产品推荐
相关产品推荐

