Rspectesting对Docker容器失败

免责声明:我是新来ruby,RSpec等我试图find我的docker集装箱的testing框架

我有一个dockers容器,运行aws cli。 我已经testing了这个手动和它的工作,作为我的testing的一部分,我想获得AWS版本并检查它。

我已经创build了这个testing

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)>' 

我所有的其他testing正确地对容器运行。 我在下面包含了我的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 

很简单(但花了我一段时间才发现):出于某种原因, aws --version命令将其输出到stderr而不是stdout 。 所以做这个,而不是:

 def aws_version command("aws --version").stderr end 

另外,值得注意的是,默认情况下所有容器都运行aws --version命令,然后退出。 所以只要你的规格足够快,你会没事的,但如果他们不这样做,他们会随机失败。

在你的规格中,我会覆盖你的图像的默认入口点/ cmd,并把它放在一个简单的while true; do sleep 100; done while true; do sleep 100; done while true; do sleep 100; done做,直到你的after块杀死容器。