1

我在 Docker 容器中加载了一个数据库(Neo4J),但我认为我的单元测试在加载数据库之前就已经命中了容器。

def setUp(self):
    client = docker.from_env()
    self.container = client.containers.run("neo4j",
        detach=True,
        environment={'NEO4J_AUTH':'none'},
        ports={7474:7474, 7687:7687},
        volumes={'/Users/myuser/neo4j/data': {'bind': '/data', 'mode': 'rw'}}
    )
    self.container.start()

def test_database_loads(self):
    r = requests.get(self.database_url)
    self.assertEqual(r.status_code, 400)

我收到此错误:

======================================================================
ERROR: test_database_loads (backend.api.tests.test_base.BaseTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/aljabear/.pyenv/versions/visualist/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 386, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/Users/aljabear/.pyenv/versions/visualist/lib/python3.5/site-packages/requests/packages/urllib3/connectionpool.py", line 382, in _make_request
    httplib_response = conn.getresponse()
  File "/Users/aljabear/.pyenv/versions/3.5.3/lib/python3.5/http/client.py", line 1198, in getresponse
    response.begin()
  File "/Users/aljabear/.pyenv/versions/3.5.3/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/Users/aljabear/.pyenv/versions/3.5.3/lib/python3.5/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Users/aljabear/.pyenv/versions/3.5.3/lib/python3.5/socket.py", line 576, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

...ETC。基本上我的请求超时了。

我觉得我的环境还可以;如果我让容器保持运行,我可以稍后通过 curl 访问它。

我的预感是在 Neo4J 启动之前我正在 ping 容器。有什么方法可以强制 Dockerpy 等到容器托管的脚本启动并运行?

谢谢

4

0 回答 0