百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

dockerspec

> DevOps
开源

一个小型 Ruby Gem,可轻松地运行 RSpec 和 Serverspec、Infrataster 和 Capybara 测试,这些测试可针对 Dockerfile 或 Docker 镜像进行执行。

182 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

一个小型 Ruby Gem,可轻松地运行 RSpec 和 Serverspec、Infrataster 和 Capybara 测试,这些测试可针对 Dockerfile 或 Docker 镜像进行执行。

Dockerspec

A small Ruby Gem to run RSpec, Serverspec, Infrataster and Capybara tests against Dockerfiles or Docker images easily.

This gem is designed to work out of the box on Travis CI, CircleCI and other similar CI environments.

Table of Contents

  • Requirements
  • Installation
  • Documentation
    • Presentations
  • Usage Examples
    • Run Tests Against a Dockerfile in the Current Directory
    • Run Tests Against Docker Compose
    • Checking Container Logs
    • Retrying Tests That Fail Temporarily
    • Run HTTP Tests Using Infrataster
    • Run HTTP Tests Using Capybara
    • Run Database Tests Using infrataster-plugin-mysql Gem with Docker Compose
    • Run Different Tests on Each Platform
    • Real-world Examples
    • Prepare Your Ruby Environment
    • Travis CI Configuration Example
    • CircleCI Configuration Example
  • Testing
  • Contributing
  • TODO
  • License and Author

Requirements

  • Ruby 2.2 or higher.
  • Recommended Docker 1.7 or higher.

Installation

You can install the Ruby Gem with:

$ gem install dockerspec

Or you can add this line to the Gemfile of your application:

ruby
gem 'dockerspec', '~> 0.5.0'

And then execute:

$ bundle

Warning: As the gem is in its early development stages, the API is very likely to break between minor versions.

Documentation

  • Latest release documentation: http://www.rubydoc.info/gems/dockerspec
  • Master unreleased documentation: http://www.rubydoc.info/github/zuazo/dockerspec

Specific documentation sections for resources or functions that can be used to generate test cases:

  • docker_build
    • Docker Build helpers
  • docker_run
    • Docker Run helpers
    • Docker Run Serverspec resource types
    • Infrataster Resources
    • Capybara DSL
  • docker_compose
    • its_container

Presentations

Apart from the official documentation, Nan Liu presented a talk for Portland Docker user group regarding how to use Dockerspec in a container:

  • Trust, but verify | Testing with Docker Containers

Usage Examples

Run Tests Against a Dockerfile in the Current Directory

ruby
require 'dockerspec/serverspec'

describe 'My Dockerfile' do
  describe docker_build('.') do

    it { should have_maintainer /John Doe/ }
    it { should have_cmd ['/bin/dash'] }
    it { should have_expose '80' }
    it { should have_user 'nobody' }

    describe docker_run(described_image) do
      describe file('/etc/httpd.conf') do
        it { should be_file }
        it { should contain 'ServerName www.example.jp' }
      end

      describe service('httpd') do
        it { should be_enabled }
        it { should be_running }
      end
    end

  end
end

See the documentation above for more examples.

Run Tests Against Docker Compose

ruby
require 'dockerspec/serverspec'

describe docker_compose('.', wait: 30) do

  its_container(:myapp) do
    describe process('apache2') do
      it { should be_running }
      its(:args) { should match(/-DFOREGROUND/) }
    end
    # [...]
  end

  its_container(:db) do
    its(:stdout) { should include 'MySQL init process done.' }

    describe process('mysqld') do
      it { should be_running }
    end
    # [...]
  end

end

Important Warning: The docker_compose resource uses the docker-compose-api Ruby gem to emulate Docker Compose. So, some docker-compose.yml configuration options may not be supported yet or may not work exactly the same. Let us know if you find any bug or you need a missing feature. And thanks to Mauricio Klein for all his work by the way!

Checking Container Logs

To check the running container logs content, you can use the stdout and stderr helpers inside docker_run or its_container blocks.

For example:

ruby
require 'dockerspec/serverspec'

describe 'My Dockerfile' do
  describe docker_build('.') do
    describe docker_run(described_image) do
      its(:stdout) { should include 'Successfully Started.' }
      its(:stderr) { should eq '' }
    end
  end
end

Retrying Tests That Fail Temporarily

This gem includes the rspec-retry gem. So, you can add :retry to the RSpec metadata in order to retry some tests multiple times.

For example:

ruby
describe docker_run('mariadb') do
  its(:stdout, retry: 30) { should include 'MySQL init process done.' }
end

By default, it will do a sleep of 1 second between each retry. You can adjust it with :retry_wait. See rspec-retry documentation for more details.

You can also make all tests within a block retry:

ruby
describe docker_run('mariadb'), retry: 30 do
  its(:stdout) { should include 'MySQL init process done.' }
  its(:stderr) { should include 'MySQL init process done.' }

  describe command('mysqld -V'), retry: 1 do # disable retries here
    its(:stdout) { should match(/^mysqld .*MariaDB/i) }
  end
end

The same applies to its_container blocks.

Run HTTP Tests Using Infrataster

ruby
# require 'dockerspec/serverspec' # Only if you want to run both types of tests
require 'dockerspec/infrataster'

describe docker_run('nginx') do
  describe server(described_container) do # Infrataster

    describe http('/') do
      it 'responds content including "Welcome to nginx!"' do
        expect(response.body).to include 'Welcome to nginx!'
      end

      it 'responds as "nginx" server' do
        expect(response.headers['server']).to match(/nginx/i)
      end
    end

  end
end

See the Infrataster Resources documentation for more information.

Run HTTP Tests Using Capybara

In the following example we set the admin password and log in in a hypothetical web application:

…

See the Capybara DSL documentation for more information.

Run Database Tests Using infrataster-plugin-mysql Gem with Docker Compose

You need to include the infrataster-plugin-mysql gem in your Gemfile:

ruby
# Gemfile

# gem [...]
gem 'infrataster-plugin-mysql', '~> 0.2.0'

A docker-compose.yml file example with a database:

yaml
myapp:
  image: myapp
  links:
  - db:mysql
  ports:
  - 8080:80

db:
  image: mariadb
  environment:
  - MYSQL_ROOT_PASSWORD=example

The file with the tests:

…

Run Different Tests on Each Platform

Sometimes, you may want to be able to run different tests depending on the platform. You can use Serverspec's os helper method for that:

ruby
require 'dockerspec/serverspec'

describe docker_build('.', tag: 'mywebapp') do
  describe docker_run('mywebapp') do
    case os[:family]
    when 'debian'

      describe file('/etc/debian_version') do
        it { should exist }
      end

      # [...]

    when 'alpine'

      describe file('/etc/alpine-release') do
        it { should exist }
      end

      # [...]

    end
  end
end

For more details, see Serverspec documenation on how to get OS information.

Real-world Examples

  • alpine-tor image (spec/, Gemfile, .travis.yml).
  • chef-local image (spec/, Gemfile, .travis.yml): Runs the same tests against multiple tags.
  • keywhiz image (spec/, Gemfile, .travis.yml).
  • irssi-tor image (spec/, Gemfile, .travis.yml).
  • dradis image (spec/, Gemfile, .travis.yml): Runs some Capybara HTTP tests.

Prepare Your Ruby Environment

If you are new to Ruby, you can follow these steps:

1. Create a Gemfile:

ruby
# Gemfile

source 'https://rubygems.org'

gem 'dockerspec', '~> 0.5.0'

2. Create the spec/ directory:

$ mkdir spec

3. Add your tests to a file with the spec/myapp_spec.rb format:

With this gem, you can use both Serverspec and Infrataster tests.

If you just want to use Serverspec tests:

ruby
require 'dockerspec/serverspec'

If you just want to use Infrataster tests:

ruby
require 'dockerspec/infrataster'

But, of course, you can use both types of tests if you want:

ruby
require 'dockerspec/serverspec'
require 'dockerspec/infrataster'

For example, you can create a file in spec/myapp_spec.rb with the following content:

ruby
# spec/myapp_spec.rb

require 'dockerspec/serverspec'

describe 'My Dockerfile' do
  describe docker_build('.') do
    # [...]
    describe docker_run(described_image) do
      # [...]
    end
  end
end

4. Install the gems:

$ bundle

5. Run the tests:

$ bundle exec rspec

Travis CI Configuration Example

.travis.yml file example:

yaml
language: ruby

sudo: required

services: docker

script: travis_retry bundle exec rspec

CircleCI Configuration Ex

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubycapybaracirclecicompliancedevops

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理