免责声明:我是ruby,rspec等的新手。我正在为我的docker容器找到一个测试框架
我有一个运行aws cli的docker容器。我已经手动测试了它并且它可以工作,作为我的测试的一部分,我想获得AWS版本并检查它。
我已经创建了这个测试
it "aws is the correct version" do
expect(aws_version).to include("aws")
end
def aws_version
command("aws --version").stdout
end
当我运行它时,我得到的显示被视为空白,看起来它没有运行并返回任何内容
1) Dockerfile aws is the coorect version
Failure/Error: expect(aws_version).to include("aws")
expected "" to include "aws"
# ./tests_spec.rb:37:in `block (2 levels) in <top (required)>'
所有其他测试都正确地对容器运行。我在下面包含了我的dockerfile和test_spec
Dockerfile:
FROM ubuntu:16.04
ENV LAST_UPDATE=09-04-2017
#####################################################################################
# Current version is aws-cli/1.11.83 Python/2.7.12 Linux/4.4.0-75-generic botocore/1.5.46
#####################################################################################
RUN apt-get update && apt-get -y upgrade
RUN apt-get install python-pip -y
RUN pip install --upgrade pip
RUN pip install --upgrade awscli s3cmd python-magic
RUN export PATH=~/.local/bin:$PATH
RUN mkdir /root/.aws
COPY config /root/.aws
#COPY credentials /root/.aws
WORKDIR /root
ENTRYPOINT ["aws"]
CMD ["--version"]
test_spec.rb:
require "docker"
require "serverspec"
describe "Dockerfile" do
before(:all) do
@image = Docker::Image.build_from_dir('.')
set :os, family: :ubuntu
set :backend, :docker
set :docker_image, @image.id
@container = Docker::Container.create(
'Image' => @image.id,
)
@container.start
end
it "installs the right version Name of Ubuntu" do
expect(os_version).to include("xenial")
end
it "installs the right version of Ubuntu" do
expect(os_version).to include("Ubuntu 16.04.2")
end
it "installs required packages" do
expect(package("python-pip")).to be_installed
end
it "aws is the coorect version" do
expect(aws_version).to include("aws")
end
def aws_version
command("aws --version").stdout
end
def os_version
command("cat /etc/lsb-release").stdout
end
after(:all) do
@container.kill
@container.delete(:force => true)
end
end
答案 0 :(得分:0)
非常简单(但我花了一些时间才发现):由于某种原因,aws --version
命令将其输出泵送到stderr
而不是stdout
。所以这样做:
def aws_version
command("aws --version").stderr
end
另外,值得注意的是默认情况下所有容器都运行aws --version
命令,然后退出。因此,只要您的规格运行得足够快,您就可以了,但如果他们没有,那么他们将会随机失败。
在你的规范中,我会覆盖你图片的默认入口点/ cmd并放入一个简单的while true; do sleep 100; done
,这样它就会保持活着,直到你的after
块杀死容器。