gpt4 book ai didi

ruby - serverspec 命令返回空白字符串

转载 作者:数据小太阳 更新时间:2023-10-29 08:36:58 24 4
gpt4 key购买 nike

我正在尝试使用 rspec、serverspec 和 docker-api gems 测试 Dockerfile。在大多数情况下,我的测试都通过了,但所有使用 command 方法的测试都失败了,无论我提供的是什么命令,都会返回空白字符串。

代码可以在 GitHub 上找到但最重要的文件如下:

我的 Dockerfile:

# Dockerfile for nginx with configurable persistent volumes

# Select nginx as the base image
FROM nginx

# Mount configurable persistent volumes
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"]

# Expose both HTTP and HTTPS ports
EXPOSE 80 443

# ENTRYPOINT
ENTRYPOINT ["service", "nginx", "start"]

gem 文件:

source 'https://rubygems.org'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

group :development, :test do
gem 'specinfra', '~> 2.12.7'
gem 'serverspec', '~> 2.8.2'
gem 'docker-api', '~> 1.21.4'
gem 'rspec', '~> 3.2.0'
end

规范:

require 'serverspec'
require 'docker'

Excon.defaults[:ssl_verify_peer] = false

describe 'Dockerfile' do
before(:all) do
image = Docker::Image.build_from_dir('.')

set :os, family: :debian
set :backend, :docker
set :docker_image, image.id
end

it 'installs the right version of Debian' do
expect(os_version).to include('Debian GNU/Linux 7')
end

describe package('nginx') do
it { should be_installed }
end

describe service('nginx') do
it { should be_enabled }
it { should be_running }
end

describe command('nginx -V') do
its(:stdout) { should include('http_ssl_module') }
its(:stdout) { should include('http_gzip_static_module') }
end

describe port(80) do
it { should be_listening }
end

describe port(443) do
it { should be_listening }
end

def os_version
command('lsb_release -a').stdout
end
end

我知道我使用的是旧版本的 gems,但我无法在最新版本中使用它。如果它与我在 OS X 上有任何相关性,因此使用 boot2docker。我从运行规范中得到的结果是:

Failures:

1) Dockerfile installs the right version of Debian
Failure/Error: expect(os_version).to include('Debian GNU/Linux 7')
expected "" to include "Debian GNU/Linux 7"

# ./spec/dockerfile_spec.rb:16:in `block (2 levels) in <top (required)>'

2) Dockerfile Command "nginx -V" stdout should include "http_ssl_module"
Failure/Error: its(:stdout) { should include('http_ssl_module') }
expected "" to include "http_ssl_module"

# ./spec/dockerfile_spec.rb:29:in `block (3 levels) in <top (required)>'

3) Dockerfile Command "nginx -V" stdout should include "http_gzip_static_module"
Failure/Error: its(:stdout) { should include('http_gzip_static_module') }
expected "" to include "http_gzip_static_module"

# ./spec/dockerfile_spec.rb:30:in `block (3 levels) in <top (required)>'

最佳答案

当上游 specinfra gems(Specinfra 2.37.02.37.1)随着 stderr 和 stdout 的处理而改变时,我遇到了类似的事情。尝试使用重定向手动运行命令以查看输出的来源:

nginx -V 2>/dev/null

如果你最终没有输出,那么它就会输出到 stderr 中。解决方法是在末尾使用 2>&1 将所有内容放入标准输出。

** 根据评论更新

~$ nginx -V
nginx version: nginx/1.4.4
built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
TLS SNI support enabled
<snip>
~$ nginx -V 2>/dev/null
~$

关于ruby - serverspec 命令返回空白字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32059421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com