Skip to content

Instantly share code, notes, and snippets.

@masonforest
Last active March 18, 2020 16:34
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save masonforest/194e0cb6bb16c88e21a0 to your computer and use it in GitHub Desktop.
Save masonforest/194e0cb6bb16c88e21a0 to your computer and use it in GitHub Desktop.
Test Drive Your Dockerfiles with RSpec and ServerSpec
FROM ubuntu:14.04
MAINTAINER Mason Fischer <mason@thoughtbot.com>
RUN apt-get update && apt-get install -y nodejs
require "serverspec"
require "docker"
describe "Dockerfile" do
image = Docker::Image.build_from_dir('.')
set :os, family: :debian
set :backend, :docker
set :docker_image, image.id
it "installs the right version of Ubuntu" do
expect(os_version).to include("Ubuntu 14")
end
it "installs required packages" do
expect(package("nodejs")).to be_installed
end
def os_version
command("lsb_release -a").stdout
end
end
@tfhartmann
Copy link

@masonforest great blog post! Any suggestions on how to go about removing the images after the tests get run? I've tried it this way, but this doesn't seem right (or to work!)

https://github.com/tfhartmann/docker-puppetserver/blob/73935b2a3aaedca27e3c0984823912b565f39546/spec/Dockerfile_spec.rb#L14-L16

@cjcjameson
Copy link

Just calling out: it's important to specify that you should use the docker-api gem. The docker gem is not the right one to use. But confusingly, the require statement is correct!
require "docker"

@bossjones
Copy link

@cjcjameson you just saved me a lot of time buddy, thx. and thx @masonforest !

@josefsalyer
Copy link

Oh my! The docker-api gem! what a difference that makes! @masonforest - do you think you can update the article to call this out? it would have saved me a bunch of time!

@josefsalyer
Copy link

@tfhartmann : I add this to my specs:

after(:all) do
    @image.remove(:force => true)
end

@jrglee
Copy link

jrglee commented Apr 8, 2016

In the example you build the image only. I assume ServerSpec is starting the container and stopping by the end of the test. How can you pass arguments to the container such as environment variables, commands, volumes, etc?

@irvifa
Copy link

irvifa commented May 30, 2016

Hello, this is my version of docker:

$ docker -v                                                                     
Docker version 1.11.1

I can't using rspec and always getting this message:

$ bundle exec rspec spec                                             
No examples found.


Finished in 0.00098 seconds (files took 1.23 seconds to load)
0 examples, 0 failures

this is my directory structure:

$ tree .                                                                          ‹ruby-2.3.0›
.
├── Dockerfile
├── run
└── spec
    └── Dockerfile_spec.rb

1 directory, 3 files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment