Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active July 27, 2018 07:47
Show Gist options
  • Save maxivak/2ad5acee05630f9ebdbbd068550fa761 to your computer and use it in GitHub Desktop.
Save maxivak/2ad5acee05630f9ebdbbd068550fa761 to your computer and use it in GitHub Desktop.
Testing Docker containers with Rspec and Serverspec

Testing Docker containers with Rspec and Serverspec

RSpec test

we have a container named 'mycontainer' and want to test its state.

  • spec/mycontainer/mycontainer_spec.rb
require 'spec_helper'

describe "mycontainer" do
  before(:all) do

    @container = Docker::Container.get('mycontainer')


    set :os, family: :debian
    set :backend, :docker
    set :docker_container, @container.id
  end


  # check Ubuntu version
  describe command('lsb_release -a') do
    it "ubuntu version" do
      expect(subject.stdout).to match(/ubuntu/)
      expect(subject.stdout).to match(/14.04/)
    end
  end

end



@manishroy171o
Copy link

Hello
my container is up and running, my container name is "apacheweb5" and i have my spec file also , i am getting error while running rspec command.

Please See the error below.

rspec apache5_spec.rb
No backend type is specified. Fall back to :exec type.
F

Failures:

  1. apacheweb5 Port "80" should be listening
    Failure/Error: it { should be_listening }
    NameError:
    uninitialized constant Specinfra::Command::Centos

    /usr/local/share/gems/gems/specinfra-2.72.0/lib/specinfra/command_factory.rb:31:in `const_get'

    /usr/local/share/gems/gems/specinfra-2.72.0/lib/specinfra/command_factory.rb:31:in `create_command_class'

    /usr/local/share/gems/gems/specinfra-2.72.0/lib/specinfra/command_factory.rb:16:in `get'

    /usr/local/share/gems/gems/specinfra-2.72.0/lib/specinfra/runner.rb:26:in `run'

    /usr/local/share/gems/gems/specinfra-2.72.0/lib/specinfra/runner.rb:13:in `method_missing'

    /usr/local/share/gems/gems/serverspec-2.41.1/lib/serverspec/type/port.rb:33:in `listening?'

    /usr/local/share/gems/gems/serverspec-2.41.1/lib/serverspec/matcher/be_listening.rb:3:in `block (2 levels) in <top (required)>'

    ./apache5_spec.rb:22:in `block (3 levels) in <top (required)>'

Finished in 0.04896 seconds (files took 1.37 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./apache5_spec.rb:22 # apacheweb5 Port "80" should be listening

docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
768e8861cd5f 54452a197cd4 "/bin/sh -c 'apachect" About an hour ago Up About an hour 80/tcp hungry_leakey
f4fcb4c81eaa centos7/apache:v3 "/bin/sh -c 'apachect" 26 hours ago Up 26 hours 0.0.0.0:32768->80/tcp apacheweb5

My spec file is

require "docker"
require "serverspec"

describe "apacheweb5" do
before(:all) do

@container = Docker::Container.get('apacheweb5')


set :os, family: :centos
set :backend, :docker
set :docker_container, @container.id

end

check Apache status

 describe package('httpd'), :if => os[:family] == 'Centos' do
     it {it should be_installed}
     end

    describe port(80) do
      it { should be_listening }
    end

end

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