首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过模块testinfra修复文本?

如何通过模块testinfra修复文本?
EN

Stack Overflow用户
提问于 2021-04-15 20:16:05
回答 1查看 121关注 0票数 0

写了一个dockerfile。在其中,我使用模块testinfra运行测试。图片模块,用于来自阿尔卑斯山的dockerile :3.11

有这样一个错误

代码语言:javascript
复制
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是否正在运行以及它是否被启用,据我所知,它没有找到服务功能本身

测试代码如下所示

代码语言:javascript
复制
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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-05-21 18:23:27

我不确定,但我认为你应该尝试在你的Dockerfile openrc包中添加。在基本的高山图像中,没有像'service‘这样的命令。

代码语言:javascript
复制
RUN apk --update add openrc --no-cache

编辑:

正如您所看到的,testinfra在测试主机上查找bin,这就是引发错误的原因。

out = self.run_expect([0, 1, 127], "command -v %s", command)

在这种情况下,我建议使用https://command-not-found.com/在许多Linux发行版上查找软件包和安装命令。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67108280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档