【问题标题】:Testing multiple docker images with RSpec使用 RSpec 测试多个 docker 镜像
【发布时间】:2016-10-31 16:42:55
【问题描述】:

免责声明:我对 ruby​​ 和 rspec 非常陌生

我一直在尝试使用 docker 镜像作为我们不同项目的基础镜像构建一个私有仓库。我们还尝试将测试多个 docker 镜像作为测试套件的一部分。

虽然rspec 有奇怪的问题,但似乎测试在错误的 docker 机器上运行。

目前我们有两个 docker 镜像,每个镜像都在一个单独的文件夹中

.
├── Dockerfile
├── Gemfile
├── Gemfile.lock
├── Jenkinsfile
├── README.md
├── Rakefile
├── nodejs
│   ├── 7.0
│   │   ├── Dockerfile
│   │   ├── README.md
│   │   └── spec
│   │       └── image_spec.rb
│   └── README.md
├── python
│   ├── 2.7
│   │   ├── Dockerfile
│   │   ├── README.md
│   │   ├── docker-entrypoint.sh
│   │   ├── requirements.txt
│   │   └── spec
│   │       └── image_spec.rb
│   └── README.md
└── spec
    └── spec_helper.rb

这基本上是我们目前使用的结构,我们的spec/spec_helper.rb使用默认/简单

require 'serverspec'
require 'docker'

RSpec.configure do |config|
  # Use color in STDOUT
  config.color = true

  # Use color not only in STDOUT but also in pagers and files
  config.tty = true

  # Use the specified formatter
  config.formatter = :documentation # :progress, :html, :textmate
end

这是我们正在使用的Rakefile 内容

require 'rake'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:rspec) do |t|
  t.pattern = Dir.glob('*/*/spec/*_spec.rb')
  t.rspec_opts = '--format documentation --require spec_helper --color'
end

task :default => :spec

我们面临的问题是,当我们运行 bundle exec rake rspec 时,python/2.7/spec_image.rb 测试失败了,因为我们确信它们在 nodejs/7.0 上运行。当我们运行bundle exec rspec python/2.7/spec/image_spec.rb 时,它会成功运行。

这也是image_spec.rb 对于python/2.7 的样子

require 'serverspec'
require 'docker'

DOCKER_FOLDER = "python/2.7"
describe "Python/2.7 Specs" do

  before :all do
    add_simplejson_to_requirements

    image = Docker::Image.build_from_dir(DOCKER_FOLDER,  ARG: 'requirements.txt')

    set :path, '/usr/local/bin:$PATH'
    set :os, family: :alpine
    set :backend, :docker
    set :docker_image, image.id
  end

  #test for python version 2.7
  it 'has python 2.7 installed' do
    expect(command('python -c "import sys; print(sys.version_info[:])"').stdout).to include\
      '2, 7'
  end

  it 'installs requirements.txt given as --build-arg' do
    expect(command('pip install simplejson==3.6.3').stdout).to include\
     'Requirement already satisfied'
  end

end

有人能指出我们正确的方向吗?

谢谢

【问题讨论】:

    标签: docker rspec


    【解决方案1】:

    如果有人发现这个旧线程,我最近在测试多个图像/容器时遇到了同样的问题。我的解决方案是在后端添加一个调用clear 方法的after(:all) 块:

    after(:all) do
      # Reset the docker backend so other images/containers can be tested.
      Specinfra::Backend::Docker.clear
    end
    

    它有点臭,但它可以解决问题。更好的解决方案是创建自己的后端类并从一开始就实现多图像/容器支持。

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 2017-09-29
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      相关资源
      最近更新 更多