Dockerfile test require directive unable to find the correct module

152 views Asked by At

I am trying to write a basic test of an Ubuntu 16.04 docker image. My testing environment is Ubuntu 16.04 also and I have installed the ruby-serverspec package.

require "serverspec"
require "docker_image"

describe "Dockerfile" do
  before(:all) do
    image = Docker::Image.build_from_dir('..')

    set :os, family: :debian
    set :backend, :docker
    set :docker_image, image.id
  end

  it "installs the right version of Ubuntu" do
    expect(os_version).to include("Ubuntu 14")
  end

  def os_version
    command("lsb_release -a").stdout
  end
end

This is a deliberately failing test for now, as it checks for Ubuntu 14, but it is not even reaching the failure.

$ rspec /test/spec/localhost/my_spec.rb    
/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- docker_image (LoadError)
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /test/spec/localhost/my_spec.rb:4:in `<top (required)>'
    from /usr/lib/ruby/vendor_ruby/rspec/core/configuration.rb:1361:in `load'
    from /usr/lib/ruby/vendor_ruby/rspec/core/configuration.rb:1361:in `block in load_spec_files'
    from /usr/lib/ruby/vendor_ruby/rspec/core/configuration.rb:1359:in `each'
    from /usr/lib/ruby/vendor_ruby/rspec/core/configuration.rb:1359:in `load_spec_files'
    from /usr/lib/ruby/vendor_ruby/rspec/core/runner.rb:106:in `setup'
    from /usr/lib/ruby/vendor_ruby/rspec/core/runner.rb:92:in `run'
    from /usr/lib/ruby/vendor_ruby/rspec/core/runner.rb:78:in `run'
    from /usr/lib/ruby/vendor_ruby/rspec/core/runner.rb:45:in `invoke'
    from /usr/bin/rspec:4:in `<main>'

I am unaware of how to require the Docker::Image class module correctly. The search path is still a black art to me. How can I get this test to fail as expected?

1

There are 1 answers

0
Szymon On

I'm not sure if it's right approach, Serverspec should only verify your environment, which should be configured by some kind of configuration management (Chef, Puppet, etc..).

However, looks like you are including wrong gem/file. You should replace second line with require "docker-api". Docker::Image is a class from docker-api gem. Gem has to be installed before running tests.