1

DISCLAIMER: I am new to ruby,rspec etc. I am trying to find a test framework for my docker containers

I have a docker container that runs the aws cli. I have tested this manually and it work, as part of my tests I want to get the AWS version and check it.

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:

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
JaChNo
  • 1,493
  • 9
  • 28
  • 56

1 Answers1

0

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. 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. 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 so that it stays alive until your after blocks kills the container.

otupman
  • 1,238
  • 10
  • 20