写了一个dockerfile。在其中,我使用模块 testinfra 运行测试。镜像模块,用于dockerile FROM alpine:3.11
有这样的错误
Step 24/27 : RUN py.test /etc/nginx/test/test.py
---> Running in 595d6978e9a4
============================= test session starts ==============================
platform linux -- Python 3.8.2, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: /etc/nginx/test
plugins: testinfra-6.2.0
collected 3 items
../../../etc/nginx/test/test.py ..F [100%]
=================================== FAILURES ===================================
____________________ test_nginx_running_and_enabled[local] _____________________
host = <testinfra.host.Host local>
def test_nginx_running_and_enabled(host):
nginx = host.service('nginx')
> assert nginx.is_running
/etc/nginx/test/test.py:12:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/lib/python3.8/site-packages/testinfra/modules/service.py:107: in is_running
[0, 1, 3, 8], "%s %s status", self._service_command, self.name
/usr/lib/python3.8/site-packages/testinfra/utils/__init__.py:29: in __get__
value = obj.__dict__[self.func.__name__] = self.func(obj)
/usr/lib/python3.8/site-packages/testinfra/modules/service.py:95: in _service_command
return self.find_command("service")
/usr/lib/python3.8/site-packages/testinfra/modules/base.py:53: in find_command
return cls._host.find_command(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <testinfra.host.Host local>, command = 'service'
extrapaths = ('/sbin', '/usr/sbin')
def find_command(self, command, extrapaths=("/sbin", "/usr/sbin")):
"""Return path of given command
raise ValueError if command cannot be found
"""
out = self.run_expect([0, 1, 127], "command -v %s", command)
if out.rc == 0:
return out.stdout.rstrip("\r\n")
for basedir in extrapaths:
path = os.path.join(basedir, command)
if self.exists(path):
return path
> raise ValueError('cannot find "{}" command'.format(command))
E ValueError: cannot find "service" command
/usr/lib/python3.8/site-packages/testinfra/host.py:46: ValueError
由于某种原因,它不想检查nginx是否正在运行以及是否启用,据我了解,它没有找到服务功能本身
测试代码看起来像这样
import testinfra
def test_os_release(host):
assert host.file("/etc/os-release").contains("Alpine Linux")
def test_nginx_is_installed(host):
nginx = host.package('nginx')
assert nginx.is_installed
assert nginx.version.startswith('1.16.1')
def test_nginx_running_and_enabled(host):
nginx = host.service('nginx')
assert nginx.is_running
assert nginx.is_enabled
dockerfile
FROM alpine:3.11
ADD https://dl.bintray.com/php-alpine/key/php-alpine.rsa.pub /etc/apk/keys/php-alpine.rsa.pub
# make sure you can use HTTPS
RUN apk --update add ca-certificates
RUN echo "https://dl.bintray.com/php-alpine/v3.11/php-7.4" >> /etc/apk/repositories
# Install packages
RUN apk --no-cache add php php-fpm php-opcache php-openssl php-curl \
nginx supervisor curl
# Install python/pip/setuptools/testinfra
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools
RUN pip install pytest-testinfra
#create folder for tests
RUN mkdir -p /etc/nginx/test
COPY test.py /etc/nginx/test
# https://github.com/codecasts/php-alpine/issues/21
RUN ln -s /usr/bin/php7 /usr/bin/php
# Configure nginx
COPY config/nginx.conf /etc/nginx/nginx.conf
# Remove default server definition
RUN rm /etc/nginx/conf.d/default.conf
# Configure PHP-FPM
COPY config/fpm-pool.conf /etc/php7/php-fpm.d/www.conf
COPY config/php.ini /etc/php7/conf.d/custom.ini
# Configure supervisord
COPY config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Setup document root
RUN mkdir -p /var/www/html
# Make sure files/folders needed by the processes are accessable when they run under the nobody user
RUN chown -R nobody.nobody /var/www/html && \
chown -R nobody.nobody /run && \
chown -R nobody.nobody /var/lib/nginx && \
chown -R nobody.nobody /var/log/nginx
# Switch to use a non-root user from here on
USER nobody
# Add application
WORKDIR /var/www/html
COPY --chown=nobody src/ /var/www/html/
#start test
RUN py.test /etc/nginx/test/test.py
# Expose the port nginx is reachable on
EXPOSE 8080
# Let supervisord start nginx & php-fpm
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# Configure a healthcheck to validate that everything is up&running
HEALTHCHECK --timeout=10s CMD curl --silent --fail http://127.0.0.1:8080/fpm-ping