From 78355a1167e0ded4704832046126541256d69fa5 Mon Sep 17 00:00:00 2001 From: Jonathan Sokolowski Date: Fri, 17 Jun 2016 20:28:48 +1000 Subject: [PATCH 1/6] Setup rspec testing infrastructure --- .rspec | 2 ++ Gemfile | 8 +++++++- Gemfile.lock | 24 +++++++++++++++++++++++- Rakefile | 10 ++++++++-- spec/spec_helper.rb | 10 ++++++++++ 5 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 .rspec create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..83e16f8 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/Gemfile b/Gemfile index 2b245b5..6d07894 100644 --- a/Gemfile +++ b/Gemfile @@ -2,4 +2,10 @@ source "http://rubygems.org" gem "puppet", ENV['PUPPET_VERSION'] || '~> 3.8' gem "vault", '~> 0.4' -gem "rake" +gem "rake", '~> 11.1' + +group :development, :test do + gem 'rspec', "~> 3.3", :require => false + gem "rspec-legacy_formatters", "~> 1.0", :require => false + gem 'mocha', "~> 0.10.5", :require => false +end diff --git a/Gemfile.lock b/Gemfile.lock index aa651a5..b48646d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,22 +2,44 @@ GEM remote: http://rubygems.org/ specs: CFPropertyList (2.2.8) + diff-lcs (1.2.5) facter (2.4.6) CFPropertyList (~> 2.2.6) hiera (1.3.4) json_pure json_pure (1.8.3) + metaclass (0.0.4) + mocha (0.10.5) + metaclass (~> 0.0.1) puppet (3.8.6) facter (> 1.6, < 3) hiera (~> 1.0) json_pure rake (11.1.2) + rspec (3.4.0) + rspec-core (~> 3.4.0) + rspec-expectations (~> 3.4.0) + rspec-mocks (~> 3.4.0) + rspec-core (3.4.4) + rspec-support (~> 3.4.0) + rspec-expectations (3.4.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.4.0) + rspec-legacy_formatters (1.0.1) + rspec (~> 3.0) + rspec-mocks (3.4.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.4.0) + rspec-support (3.4.1) vault (0.4.0) PLATFORMS ruby DEPENDENCIES + mocha (~> 0.10.5) puppet (~> 3.8) - rake + rake (~> 11.1) + rspec (~> 3.3) + rspec-legacy_formatters (~> 1.0) vault (~> 0.4) diff --git a/Rakefile b/Rakefile index 18d83fe..24faa41 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,9 @@ -task :default do - puts "TBD: https://github.com/jsok/hiera-vault/issues/17" +require 'rake' +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) do |t| + t.pattern = Dir.glob('spec/**/*_spec.rb') + t.rspec_opts = '--format documentation' end + +task :default => :spec diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..23e7465 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,10 @@ +$:.insert(0, File.join([File.dirname(__FILE__), "..", "lib"])) + +require 'rubygems' +require 'rspec' +require 'mocha' +require 'hiera' + +RSpec.configure do |config| + config.mock_with :mocha +end From b555aee72f6c8a6e59dd5a415597cd531f84acc8 Mon Sep 17 00:00:00 2001 From: Jonathan Sokolowski Date: Fri, 17 Jun 2016 20:29:29 +1000 Subject: [PATCH 2/6] spec: Add basic initialize spec --- spec/vault_backend_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 spec/vault_backend_spec.rb diff --git a/spec/vault_backend_spec.rb b/spec/vault_backend_spec.rb new file mode 100644 index 0000000..0cf14c6 --- /dev/null +++ b/spec/vault_backend_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' +require 'hiera/backend/vault_backend' + +class Hiera + module Backend + describe Vault_backend do + + before do + Hiera.stubs(:debug) + Hiera.stubs(:warn) + Hiera::Backend.stubs(:empty_answer).returns(nil) + end + + describe '#initialize' do + it "should work with empty config" do + Config.load({:vault => {}}) + Vault_backend.new + end + + it "should validate :default_field_parse" do + Config.load({:vault => {:default_field_parse => 'invalid'}}) + expect { Vault_backend.new }.to raise_error /invalid value for :default_field_parse/ + end + + it "should validate :default_field_behavior" do + Config.load({:vault => {:default_field_behavior => 'invalid'}}) + expect { Vault_backend.new }.to raise_error /invalid value for :default_field_behavior/ + end + + end + + end + end +end From a2f9459fbc1076c2802bf7cb15e1d5a5fb2cbbf0 Mon Sep 17 00:00:00 2001 From: Jonathan Sokolowski Date: Fri, 17 Jun 2016 21:02:08 +1000 Subject: [PATCH 3/6] spec: Mock vault client create failure --- spec/spec_helper.rb | 1 + spec/vault_backend_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 23e7465..df7aadd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,7 @@ require 'rspec' require 'mocha' require 'hiera' +require 'vault' RSpec.configure do |config| config.mock_with :mocha diff --git a/spec/vault_backend_spec.rb b/spec/vault_backend_spec.rb index 0cf14c6..071b2a5 100644 --- a/spec/vault_backend_spec.rb +++ b/spec/vault_backend_spec.rb @@ -27,6 +27,13 @@ module Backend expect { Vault_backend.new }.to raise_error /invalid value for :default_field_behavior/ end + it "should skip backend on configuration error" do + Vault::Client.stubs(:new).raises(Exception) + Hiera.expects(:warn).with(regexp_matches /Skipping backend/).once + + Config.load({:vault => {}}) + Vault_backend.new + end end end From d6d5f9506cb8e457661adbe7d539f408b989ae05 Mon Sep 17 00:00:00 2001 From: Jonathan Sokolowski Date: Mon, 20 Jun 2016 21:37:17 +1000 Subject: [PATCH 4/6] Copy vault server spec support code from vault-ruby --- spec/{ => integration}/vault_backend_spec.rb | 0 spec/spec_helper.rb | 2 + spec/support/vault_server.rb | 79 ++++++++++++++++++++ 3 files changed, 81 insertions(+) rename spec/{ => integration}/vault_backend_spec.rb (100%) create mode 100644 spec/support/vault_server.rb diff --git a/spec/vault_backend_spec.rb b/spec/integration/vault_backend_spec.rb similarity index 100% rename from spec/vault_backend_spec.rb rename to spec/integration/vault_backend_spec.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index df7aadd..7adc051 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,6 +6,8 @@ require 'hiera' require 'vault' +require_relative "support/vault_server" + RSpec.configure do |config| config.mock_with :mocha end diff --git a/spec/support/vault_server.rb b/spec/support/vault_server.rb new file mode 100644 index 0000000..9338d88 --- /dev/null +++ b/spec/support/vault_server.rb @@ -0,0 +1,79 @@ +### +# Graciously copied from https://github.com/hashicorp/vault-ruby/blob/master/spec/support/vault_server.rb +#### + +require "open-uri" +require "singleton" +require "timeout" +require "tempfile" + +module RSpec + class VaultServer + include Singleton + + TOKEN_PATH = File.expand_path("~/.vault-token").freeze + TOKEN_PATH_BKUP = "#{TOKEN_PATH}.bak".freeze + + def self.method_missing(m, *args, &block) + self.instance.public_send(m, *args, &block) + end + + attr_reader :token + attr_reader :unseal_token + + def initialize + # If there is already a vault-token, we need to move it so we do not + # clobber! + if File.exist?(TOKEN_PATH) + FileUtils.mv(TOKEN_PATH, TOKEN_PATH_BKUP) + at_exit do + FileUtils.mv(TOKEN_PATH_BKUP, TOKEN_PATH) + end + end + + io = Tempfile.new("vault-server") + pid = Process.spawn({}, "vault server -dev", out: io.to_i, err: io.to_i) + + at_exit do + Process.kill("INT", pid) + Process.waitpid2(pid) + + io.close + io.unlink + end + + wait_for_ready do + @token = File.read(TOKEN_PATH) + + output = "" + while + io.rewind + output = io.read + break if !output.empty? + end + + if output.match(/Unseal Key: (.+)/) + @unseal_token = $1.strip + else + raise "Vault did not return an unseal token!" + end + end + end + + def address + "http://127.0.0.1:8200" + end + + def wait_for_ready(&block) + Timeout.timeout(5) do + while !File.exist?(TOKEN_PATH) + sleep(0.25) + end + end + + yield + rescue Timeout::Error + raise "Vault did not start in 5 seconds!" + end + end +end From 82c876b39a3223d8cd0fc504adec48cca398768a Mon Sep 17 00:00:00 2001 From: Jonathan Sokolowski Date: Mon, 20 Jun 2016 21:37:50 +1000 Subject: [PATCH 5/6] spec: Use vault server in integration tests --- spec/integration/vault_backend_spec.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spec/integration/vault_backend_spec.rb b/spec/integration/vault_backend_spec.rb index 071b2a5..0bb0c7b 100644 --- a/spec/integration/vault_backend_spec.rb +++ b/spec/integration/vault_backend_spec.rb @@ -9,11 +9,14 @@ module Backend Hiera.stubs(:debug) Hiera.stubs(:warn) Hiera::Backend.stubs(:empty_answer).returns(nil) + RSpec::VaultServer.token end describe '#initialize' do - it "should work with empty config" do - Config.load({:vault => {}}) + it "should work with valid config" do + Config.load({:vault => { + :addr => RSpec::VaultServer.address + }}) Vault_backend.new end From 4f3021b0927f116bbee44cab383ebbf5a55a043b Mon Sep 17 00:00:00 2001 From: Jonathan Sokolowski Date: Mon, 20 Jun 2016 21:42:25 +1000 Subject: [PATCH 6/6] CI: Install vault 0.6.0 binary --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index 952e71e..d56e144 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,11 @@ rvm: - 2.2.0 cache: bundler +before_install: |- + wget -O vault.zip -q https://releases.hashicorp.com/vault/0.6.0/vault_0.6.0_linux_amd64.zip + unzip vault.zip -d $HOME/bin + export PATH="$HOME/bin:$PATH" + deploy: provider: rubygems on: