How can I test with serverspec that Jenkins is running in a jenkins docker container?

944 views Asked by At

I am trying to prove that Jenkins is running in a container built from the official docker hub jenkins image.

require "serverspec"
require "docker"

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

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

  describe process("java") do
    it { should be_running }
  end
end

I get the following result:

Failures:

  1) Dockerfile Process "java" should be running
     Failure/Error: it { should be_running }
       expected Process "java" to be running

     # ./spec_jenkins.rb:24:in `block (3 levels) in <top (required)>'

Am I misunderstanding that tests are run against a container instantiated from the built image ?

1

There are 1 answers

0
Godefroid Chapelle On

The official Jenkins image defines an ENTRYPOINT rather than a CMD.

However, specinfra looks for a CMD and uses /bin/sh if it does not find any.

IOW, specinfra does not run Jenkins. Reason why my test was failing.

By specifying proper docker_container_create_options, specinfra also starts Jenkins and the test passes.

Working code:

require "serverspec"
require "docker"

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

    set :os, family: :debian
    set :backend, :docker
    set :docker_image, image.id
    set :docker_container_create_options, {'Cmd' => ['/usr/local/bin/jenkins.sh']}
  end

  describe process("java") do
    it { should be_running }
  end
end