diff --git a/README.md b/README.md index e0fe453..5a46dd1 100644 --- a/README.md +++ b/README.md @@ -246,4 +246,25 @@ describe 'my example' do end ``` -For this `examples/my_example.pp' must exist and contain valid Puppet code. It then uses the idempotent resource shared example. +For this `examples/my_example.pp` must exist and contain valid Puppet code. It then uses the idempotent resource shared example. + +## Serverspec extensions + +Some [Serverspec](https://serverspec.org/) extensions are shipped and enabled by default. These make it easier to write tests but were not accepted by Serverspec upstream. + +### `curl_command` + +Often you want to test some service that exposes things over HTTP. +Instead of using [`command("curl …")`](https://serverspec.org/resource_types.html#command) you can use `curl_command(…)` which behaves like a Serverspec `command` but adds matchers for the HTTP response code and the response body. + +```ruby +describe curl_command("http://localhost:8080/api/ping") do + its(:response_code) { is_expected.to eq(200) } + its(:exit_status) { is_expected.to eq 0 } +end + +describe curl_command('http://localhost:8080/api/status', headers: { 'Accept' => 'application/json' }) do + its(:response_code) { is_expected.to eq(200) } + its(:body_as_json) { is_expected.to eq({'status': 'ok'}) } +end +``` diff --git a/examples/defaults_serverspec/Gemfile b/examples/defaults_serverspec/Gemfile new file mode 100644 index 0000000..02fbf59 --- /dev/null +++ b/examples/defaults_serverspec/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'voxpupuli-acceptance', path: '../..' diff --git a/examples/defaults_serverspec/Rakefile b/examples/defaults_serverspec/Rakefile new file mode 100644 index 0000000..a4abcaa --- /dev/null +++ b/examples/defaults_serverspec/Rakefile @@ -0,0 +1 @@ +require 'voxpupuli/acceptance/rake' diff --git a/examples/defaults_serverspec/manifests/init.pp b/examples/defaults_serverspec/manifests/init.pp new file mode 100644 index 0000000..5e27a04 --- /dev/null +++ b/examples/defaults_serverspec/manifests/init.pp @@ -0,0 +1,8 @@ +class defaults_serverspec { + package {'nginx': + ensure => present + } + service {'nginx': + ensure => running + } +} diff --git a/examples/defaults_serverspec/metadata.json b/examples/defaults_serverspec/metadata.json new file mode 100644 index 0000000..98d5d89 --- /dev/null +++ b/examples/defaults_serverspec/metadata.json @@ -0,0 +1,14 @@ +{ + "name": "voxpupuliacceptancetests-defaults_serverspec", + "version": "0.0.1", + "author": "Vox Pupuli", + "license": "Apache-2.0", + "summary": "The voxpupuli-acceptance test suite defaults_serverspec", + "description": "test the serverspec extensions shipped in voxpupuli-acceptance", + "source": "https://github.com/voxpupuli/voxpupuli-acceptance", + "project_page": "https://github.com/voxpupuli/voxpupuli-acceptance", + "issues_url": "https://github.com/voxpupuli/voxpupuli-acceptance/issues", + "dependencies": [], + "requirements": [], + "operatingsystem_support": [] +} diff --git a/examples/defaults_serverspec/spec/acceptance/basic_spec.rb b/examples/defaults_serverspec/spec/acceptance/basic_spec.rb new file mode 100644 index 0000000..9014a55 --- /dev/null +++ b/examples/defaults_serverspec/spec/acceptance/basic_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper_acceptance' + +describe 'Basic integration test' do + it_behaves_like 'an idempotent resource' do + let(:manifest) { 'include defaults_serverspec' } + end + + describe curl_command('http://localhost') do + its(:response_code) { is_expected.to eq(200) } + its(:exit_status) { is_expected.to eq 0 } + end +end diff --git a/examples/defaults_serverspec/spec/spec_helper_acceptance.rb b/examples/defaults_serverspec/spec/spec_helper_acceptance.rb new file mode 100644 index 0000000..ffbdaf4 --- /dev/null +++ b/examples/defaults_serverspec/spec/spec_helper_acceptance.rb @@ -0,0 +1,3 @@ +require 'voxpupuli/acceptance/spec_helper_acceptance' + +configure_beaker diff --git a/examples/new_example b/examples/new_example index d41c732..8935c7e 100755 --- a/examples/new_example +++ b/examples/new_example @@ -25,7 +25,7 @@ RAKEFILE cat > $EXAMPLE/metadata.json <\d+)}.match(stderr) + return 0 unless m + + m[:code].to_i + end + + def body + command_result.stdout + end + + def body_as_json + MultiJson.load(body) + end + + private + + def curl_command + # curl supports %{stderr} to --write-out since 7.63.0 + # so the following doesn't work on EL8, which has curl 7.61.1 + command = "curl --silent --write-out '%{stderr}Response-Code: %{response_code}\\n' '#{@name}'" + + @options.each do |option, value| + case option + when :cacert, :cert, :key + command += " --#{option} '#{value}'" + when :headers + value.each do |header, header_value| + command += if header_value + " --header '#{header}: #{header_value}'" + else + " --header '#{header};'" + end + end + else + raise "Unknown option #{option} (value: #{value})" + end + end + + command + end + + def command_result + @command_result ||= @runner.run_command(curl_command) + end + end + end + end +end diff --git a/lib/voxpupuli/acceptance/spec_helper_acceptance.rb b/lib/voxpupuli/acceptance/spec_helper_acceptance.rb index f7b327f..e1cb3aa 100644 --- a/lib/voxpupuli/acceptance/spec_helper_acceptance.rb +++ b/lib/voxpupuli/acceptance/spec_helper_acceptance.rb @@ -1,4 +1,5 @@ require_relative 'examples' +require_relative 'serverspec_extensions' def configure_beaker(modules: :metadata, &block) collection = ENV['BEAKER_PUPPET_COLLECTION'] || 'puppet'