简体   繁体   English

Rspec测试失败了Docker容器

[英]Rspec test failing against Docker container

DISCLAIMER: I am new to ruby,rspec etc. I am trying to find a test framework for my docker containers 免责声明:我是ruby,rspec等的新手。我正在尝试为我的docker容器找到一个测试框架

I have a docker container that runs the aws cli. 我有一个运行aws cli的docker容器。 I have tested this manually and it work, as part of my tests I want to get the AWS version and check it. 我已经手动测试了它并且它可以工作,作为我的测试的一部分,我想获得AWS版本并检查它。

I have created this test 我创建了这个测试

it "aws is the correct version" do
  expect(aws_version).to include("aws")
end

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

When I run it I get which shows the exepected as blank, it looks like it is not running and returning anything 当我运行它时,我得到的显示被视为空白,看起来它没有运行并返回任何东西

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

All my other tests run against the container correctly. 所有其他测试都正确地对容器运行。 I have included both my dockerfile and test_spec below 我在下面包含了我的dockerfile和test_spec

Dockerfile: 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: 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

Pretty simple (but took me a while to spot): for some reason the aws --version command pumps it's output onto stderr rather than stdout . 非常简单(但需要一段时间才能发现):由于某种原因, aws --version命令将其输出泵送到stderr而不是stdout So do this instead: 所以这样做:

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

Also, it's worth noting that all your container does by default is run the aws --version command and then exits. 此外,值得注意的是,默认情况下所有容器都运行aws --version命令然后退出。 So as long as your specs run fast enough you'll be okay, but if they don't then they're going to fail randomly. 因此,只要您的规格运行得足够快,您就可以了,但如果他们没有,那么他们将会随机失败。

In your specs I'd override your image's default entrypoint/cmd and put in a simple while true; do sleep 100; done 在你的规范中,我会覆盖你图像的默认入口点/ cmd,然后放入一个简单的while true; do sleep 100; done while true; do sleep 100; done while true; do sleep 100; done so that it stays alive until your after blocks kills the container. while true; do sleep 100; done这样while true; do sleep 100; done ,直到你的after块杀死容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM