Testing multiple docker images with RSpec

Disclaimer: I am very new to ruby โ€‹โ€‹and rspec

I am trying to create a private repo with docker images as base images for our various projects. We are also trying to enable testing of several docker images as part of a test suite.

We have a strange problem with rspec, it seems that the tests are running on the wrong docker machine.

We currently have two docker images in a separate folder

.
โ”œโ”€โ”€ 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

This is basically the structure we are currently using, ours spec/spec_helper.rbusing default / simple

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

And this is the Rakefilecontent we use

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

- ?

+4

Source: https://habr.com/ru/post/1659429/


All Articles