1

我正在尝试编写 Ubuntu 16.04 docker 映像的基本测试。我的测试环境也是 Ubuntu 16.04,我已经安装了这个ruby-serverspec包。

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

目前这是一个故意失败的测试,因为它检查 Ubuntu 14,但它甚至没有达到失败。

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

我不知道如何Docker::Image正确地要求类模块。搜索路径对我来说仍然是一门魔法。我怎样才能让这个测试按预期失败?

4

1 回答 1

1

我不确定这是否是正确的方法,Serverspec 应该只验证您的环境,应该通过某种配置管理(Chef、Puppet 等)进行配置。

但是,看起来您包含了错误的 gem/file。您应该将第二行替换为require "docker-api". Docker::Imagedocker-apigem的一个类。必须在运行测试之前安装 Gem。

于 2016-12-28T12:01:13.997 回答