【发布时间】:2019-09-23 21:59:21
【问题描述】:
有一个similar thread 有同样的问题,但无法为我工作。基本上,我正在尝试使用 testing.postgresql 对 myproject 进行单元测试,并且我在 docker 容器中运行它。
但是,当我运行我的命令时,我得到了 RuntimeError: command not found: initdb,
docker-compose exec myproject python3 -m unittest
这适用于 WSL,但不适用于 docker。
看起来它无法找到 initdb。是否与在 docker-compose.yml 中创建的数据库存在冲突问题,还是我在 docker 文件中缺少某些行?
我从docs 中概述了我的测试示例:
class TestPostgresqlInteraction(unittest.TestCase):
Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)
with testing.postgresql.Postgresql() as postgresql:
engine = create_engine(postgresql.url())
dbConn = psycopg2.connect(**postgresql.dsn())
cursor = dbConn.cursor()
def setUp(self):
self.postgresql = self.Postgresql()
def tearDown(self):
self.postgresql.stop()
def testMethod(self)
conn = psycopg2.connect(**self.postgresql.dsn())
cursor = conn.cursor()
# execute cursor and do tests...
def tearDownModule(self):
self.Postgresql.clear_cache()
docker-compose.yml
version: "3"
services:
myproject:
build:
context: .
dockerfile: ./myproject/Dockerfile
depends_on:
- database
volumes:
- ./myproject:/code
stdin_open: true
tty: true
database:
build:
context: .
dockerfile: ./database/Dockerfile
environment:
POSTGRES_USER_FILE: /secrets/dbUser.txt
POSTGRES_PASSWORD_FILE: /secrets/dbPassword.txt
ports:
- "8765:5432"
volumes:
- otherdata:/var/lib/postgresql/data
我的项目/Dockerfile
FROM python:3.7.3-stretch
RUN apt-get update && apt-get install -y \
poppler-utils
ARG moduleDir=myproject
WORKDIR /code
COPY secrets/ /secrets
# COPY $moduleDir/myproject/ ./
COPY $moduleDir/requirements.txt requirements.txt
RUN pip install -r requirements.txt
数据库/Dockerfile
FROM postgres:11.3
WORKDIR /secrets
COPY secrets/dbUser.txt dbUser.txt
COPY secrets/dbPassword.txt dbPassword.txt
RUN mkdir -p /docker-entrypoint-initdb.d
COPY database/schema.sql /docker-entrypoint-initdb.d
追溯:
ImportError: Failed to import test module: tests.testMyproject
File "/usr/local/lib/python3.7/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/local/lib/python3.7/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/code/tests/testmyProject.py", line 40, in <module>
class TestPostgresqlInteraction(unittest.TestCase):
File "/code/tests/testmyProject.py", line 46, in TestPostgresqlInteraction
Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)
File "/usr/local/lib/python3.7/site-packages/testing/common/database.py", line 52, in __init__
self.cache = self.target_class(**settings_noautostart)
File "/usr/local/lib/python3.7/site-packages/testing/common/database.py", line 92, in __init__
self.initialize()
File "/usr/local/lib/python3.7/site-packages/testing/postgresql.py", line 50, in initialize
self.initdb = find_program('initdb', ['bin'])
File "/usr/local/lib/python3.7/site-packages/testing/postgresql.py", line 144, in find_program
raise RuntimeError("command not found: %s" % name)
RuntimeError: command not found: initdb
【问题讨论】:
标签: python postgresql unit-testing docker