我有以下.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
启动python应用程序时,我可以看到以下输出:
$ 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
因此,连接到Cassandra并创建/插入数据运行良好,但集成测试无法访问localhost上的python应用程序。
在我的集成测试中,我调用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 ===============================
有什么想法可以在docker容器中调用localhost,这样我就可以访问我的应用程序了吗?我也试过127.0.0.1,但它不起作用。
编辑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
解决方案由@KamilCuk提供——将python应用程序设置为在后台before_script部分运行,如python app.py &
,并睡眠几秒钟,使我的python应用程序可以像http://localhost:8126
一样被调用。
请考虑固定装置。在你的conftest.py中放入以下内容:
@pytest.fixture(scope="session")
def app(request):
pp = subprocess.popen("python app.py")
time.sleep(5) # TODO: replace with waiting for open port
request.addfinalizer(lambda: (pp.terminate(), pp.wait()))
return pp
然后在测试中使用:
def test_something(app):
pass
在我的案例中,KamilCuk提出的一个可行的解决方案如下:
image: python:3.8
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
cache:
paths:
- .cache/pip
- venv/
before_script:
- ...
- python app.py &
- sleep 20 # wait for application to start-up
stages:
- build
- run
- test
- security
- leanix
include:
- project: ...
services:
- name: cassandra:3.11
alias: cassandra
test:unit:
stage: ...
test:integration:
stage: test
script:
- echo "0"
- py.test tests/integration/test_series.py
解决方案由@KamilCuk提供-将python应用程序设置为在before_script部分的后台运行,如python app.py &
,并睡眠几秒钟sleep 20
使我的python应用程序可以被调用,如http://localhost:8126.
我需要调查KamilCuk使用夹具提出的另一种解决方案。