在Travis上阻止测试/过程的网络访问?

问题描述 投票:6回答:2

基于this question及其答案:什么是在Travis CI上阻止测试或过程的网络访问的适用方法(如果有)?


背景:我想测试离线功能,即缓存,从而可以使用没有Internet /网络访问权限的库。该过程中涉及的依赖项(第三方软件)尝试连接到Internet /网络。目前,我正在设法禁止其Internet /网络访问(通过“拔出插头”在本地确认),尽管我想找到一种方法来实施适当的CI测试以进行长期维护。大多数测试基础结构是使用python并基于pytest编写的。

python pytest travis-ci
2个回答
5
投票

您的Travis作业在功能齐全的Linux环境中运行,该环境包括使用iptables命令创建防火墙规则的能力。考虑这个非常简单的.travis.yml文件:

---
script:
  - curl http://icanhazip.com

将其粘贴在存储库中并运行它,它将正常工作:

$ curl http://icanhazip.com
104.196.57.92
The command "curl http://icanhazip.com" exited with 0.

为了模拟离线行为,我们只添加了防火墙规则来阻止端口80上的出站访问:

---
script:
  - sudo iptables -A OUTPUT -p tcp --dport 80 -j REJECT
  - curl http://icanhazip.com

这将失败:

$ curl http://icanhazip.com
curl: (7) Failed to connect to icanhazip.com port 80: Connection refused
The command "curl http://icanhazip.com" exited with 7.

2
投票

我用travis-ci完成此操作的一种方法是利用docker--net=none功能

来自old example of mine

我构建了一个docker镜像,并通过.travis.yaml调用了它>

这里是Makefile组件:

.PHONY: test-docker_%
test-docker_%:
    docker build -t tox-virtualenv-no-download_$* --build-arg PYTHON=$* .
    docker run --rm --net=none tox-virtualenv-no-download_$*

有问题的Dockerfile(根据python版本进行了参数设置)

FROM ubuntu:bionic

ARG PYTHON=python2.7
RUN : \
    && apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        dumb-init $PYTHON virtualenv \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

ENV PATH=/venv/bin:$PATH
ADD . /code/
RUN virtualenv /venv -p $PYTHON && pip install /code

WORKDIR /example
ADD example /example
CMD ["dumb-init", "tox"]

以及关联的.travis.yml,它使用matrix功能来测试它们:

language: python
services: docker
env:
    - PYTHON=python2.7
    - PYTHON=python3.6
    - PYTHON=pypy
# ...
script: make test-docker_$PYTHON
# ...
© www.soinside.com 2019 - 2024. All rights reserved.