Docker容器的上下文管理器。

dockerctx的Python项目详细描述


dockerctx是用于管理Docker容器生存期的上下文管理器。

主要的用例是为运行测试设置脚手架,在需要的地方 比unit tests稍宽一点,但集成度不如, 比如,你可以用Robot framework来写什么。

安装

$ pip install dockerctx

对于dev,您必须使用flit

$ pip install flit
$ flit install

将自动安装特定于开发的要求。

演示

这是从其中一个测试中获取的:

importtimeimportredisimportpytestfromdockerctximportnew_container# First make a pytest fixture@pytest.fixture(scope='function')deff_redis():# This is the new thing! It's pretty clear.  The `ready_test` provides# a way to customize what "ready" means for each container. Here,# we simply pause for a bit.withnew_container(image_name='redis:latest',ports={'6379/tcp':56379},ready_test=lambda:time.sleep(0.5)orTrue)ascontainer:yieldcontainer# Here is the test.  Since the fixture is at the "function" level, a fully# new Redis container will be created for each test that uses this fixture.# After the test completes, the container will be removed.deftest_redis_a(f_redis):# The container object comes from the `docker` python package. Here we# access only the "name" attribute, but there are many others.print('Container %s'%f_redis.name)r=redis.StrictRedis(host='localhost',port=56379,db=0)r.set('foo','bar')assertr.get('foo')==b'bar'

注意,这里创建了一个全新的redis容器,用于 上下文管理器的上下文(包装在pytestfixture中 在这里),然后在上下文管理器之后销毁容器 出口。

在src中,还有另一个更详细的测试,它

  1. 运行postgres容器;
  2. 等待Postgres开始接受连接;
  3. 创建数据库;
  4. 创建表(使用SQLAlchemyorm);
  5. 执行数据库操作;
  6. 拆下容器,然后取出。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaJSonarray不会从SeekBar读取double   使用另一个类从Java中的2D数组打印用户输入   java ClassNotFoundException的原因   spring调用两个方法以返回Java中的不同页面   httpurlconnection Java禁止的代码错误,但浏览器错误(2)   java画布矩阵转换   java:在另一个java映射中使用“Map”作为值   java“未找到用于解密的证书”(Apache CXF,WSSecurity)   java如何查看JTable中选择的行   java在没有xmlwrappers的情况下重复xml元素序列集   java将垂直直方图打印到控制台   java Spring JDBCTemplate:构造不带特殊字符的JSON   java PayPal RestApi获取用户信息