vagrant_spec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +6 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +2 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +10 -0
  7. data/Gemfile +10 -0
  8. data/LICENSE +13 -0
  9. data/README.md +146 -0
  10. data/Rakefile +26 -0
  11. data/Vagrantfile +27 -0
  12. data/lib/vagrant_spec/ansible_inventory.rb +80 -0
  13. data/lib/vagrant_spec/command/base.rb +74 -0
  14. data/lib/vagrant_spec/command/init.rb +45 -0
  15. data/lib/vagrant_spec/command/test.rb +32 -0
  16. data/lib/vagrant_spec/command.rb +9 -0
  17. data/lib/vagrant_spec/config/base.rb +42 -0
  18. data/lib/vagrant_spec/config.rb +20 -0
  19. data/lib/vagrant_spec/machine_finder.rb +31 -0
  20. data/lib/vagrant_spec/spec_helper.rb +42 -0
  21. data/lib/vagrant_spec/templates/spec_helper.erb +19 -0
  22. data/lib/vagrant_spec/templates/vagrantspec_inventory.erb +12 -0
  23. data/lib/vagrant_spec/test_plan.rb +68 -0
  24. data/lib/vagrant_spec/utils.rb +23 -0
  25. data/lib/vagrant_spec/version.rb +6 -0
  26. data/lib/vagrant_spec.rb +27 -0
  27. data/scripts/poor_mans_smoke_test.sh +17 -0
  28. data/serverspec/fail_spec.rb +7 -0
  29. data/serverspec/ssh_spec.rb +10 -0
  30. data/spec/unit/spec_helper.rb +101 -0
  31. data/spec/unit/vagrant_spec_spec.rb +13 -0
  32. data/spec/unit/vagrant_spec_test/ansible_inventory_spec.rb +143 -0
  33. data/spec/unit/vagrant_spec_test/command_spec/base_spec.rb +105 -0
  34. data/spec/unit/vagrant_spec_test/command_spec/init_spec.rb +87 -0
  35. data/spec/unit/vagrant_spec_test/command_spec/test_spec.rb +45 -0
  36. data/spec/unit/vagrant_spec_test/config_spec/base_spec.rb +34 -0
  37. data/spec/unit/vagrant_spec_test/config_spec.rb +15 -0
  38. data/spec/unit/vagrant_spec_test/machine_finder_spec.rb +27 -0
  39. data/spec/unit/vagrant_spec_test/spec_helper_spec.rb +41 -0
  40. data/spec/unit/vagrant_spec_test/test_plan_spec.rb +104 -0
  41. data/spec/unit/vagrant_spec_test/utils_spec.rb +24 -0
  42. data/vagrant_spec.gemspec +30 -0
  43. metadata +184 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bbb7993f86c2fd21b7943bf6109f3fd0bb9fe5b6
4
+ data.tar.gz: 0bc50057dc57d35882ae4fb5d31eb7b15023cbc5
5
+ SHA512:
6
+ metadata.gz: 2715c4b9b32916f0b9818200e5048b71585ef59bd2bcff8b3023bd94ff63bf6c269bd33ad211e1d02b0362647a9f2d3569179f4bd937c47d59a1f8603c5c71b7
7
+ data.tar.gz: 56e0e9e7f840fc370b35319696a8f8a87ef51ae62e80b886fb455a1b698ef731cf32540123163959e7b53211cac50273c8a88e7d52f8cd17978695b5f7a222b5
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .vagrant
2
+ Gemfile.lock
3
+ vagrantspec_inventory
4
+ serverspec/spec_helper.rb
5
+ coverage
6
+ pkg
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ Metrics/AbcSize:
2
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.3
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ ---
2
+ language: ruby
3
+
4
+ #addons:
5
+ # apt:
6
+ # packages:
7
+ # - virtualbox
8
+ # - vagrant
9
+
10
+ script: bundle exec rake travis
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development do
4
+ gemspec
5
+ gem 'vagrant', git: 'https://github.com/mitchellh/vagrant.git'
6
+ end
7
+
8
+ group :plugins do
9
+ gem 'vagrant_spec', path: '.'
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright [2016] [Demitri Swan]
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,146 @@
1
+ # vagrant_spec
2
+
3
+ [![Build Status](https://travis-ci.org/miroswan/vagrant_spec.svg?branch=master)](https://travis-ci.org/miroswan/vagrant_spec)
4
+ [![Coverage Status](https://coveralls.io/repos/github/miroswan/vagrant_spec/badge.svg?branch=master)](https://coveralls.io/github/miroswan/vagrant_spec?branch=master)
5
+
6
+ Vagrant Spec is a Vagrant plugin that makes integration testing for deployments
7
+ to clustered systems a breeze. It also separates the build and deployment steps
8
+ to clearly delineate pipeline tasks.
9
+
10
+ ## Why not use TestKitchen or vagrant-serverspec?
11
+
12
+ * Test Kitchen is an excellent integration testing system developed by Chef.
13
+ However, it is designed to provision, test, and destroy each system one at a
14
+ time. The directory structure it expects makes sharing tests across nodes
15
+ difficult to manager. This is undesireable for testing clustered or
16
+ distributed systems. vagrant-serverspec has similar pitfalls.
17
+
18
+ * vagrant_spec allows you to leverage your deployment tools just like you would
19
+ in staging and production. It generates an ansible inventory file after all
20
+ nodes are brought up. This allows you to run the same ansible_playbook commands
21
+ against the local node set as you would elsewhere.
22
+
23
+ * routing tests to nodes is flexible and simple.
24
+
25
+ * vagrant_spec allows you to provision your nodes with configuration management
26
+ and leverage ansible for orchestration and deployment. TestKitchen currently
27
+ does not support this scenario without much trouble.
28
+
29
+ Here is a sample Vagrantfile:
30
+
31
+ ```
32
+ Vagrant.configure(2) do |config|
33
+ config.vm.define 'test_ansible' do |b|
34
+ b.vm.box = 'ubuntu/trusty64'
35
+ end
36
+
37
+ config.vm.define 'test_pansible' do |b|
38
+ b.vm.box = 'ubuntu/trusty64'
39
+ end
40
+
41
+ # key: Ansible Group Name
42
+ # value: Regexp matching your node names or an array of nodes
43
+ config.spec.ansible_inventory = { 'all' => /test/ }
44
+
45
+ # nodes: Regexp matching the desired nodes or array of nodes
46
+ # flags: Command line flags you would pass to rspec
47
+ config.spec.test_plan = [
48
+ {
49
+ 'nodes' => /test_ansi/,
50
+ 'flags' => '--format documentation --color --pattern serverspec/ssh*'
51
+ },
52
+ {
53
+ 'nodes' => /test_pansi/,
54
+ 'flags' => '--format documentation --color --pattern serverspec/fail*'
55
+ }
56
+ ]
57
+ end
58
+ ```
59
+
60
+ ## Configuration
61
+
62
+ * config.spec.directory: relative path to your serverspec test files. This
63
+ defaults to serverspec.
64
+
65
+ * config.spec.ansible_inventory: a hash that maps ansible groups to your nodes.
66
+ You can specify as many groups as you need. You can match nodes by regular
67
+ expression or explicitly provide an array of node names. This will generate
68
+ an vagrantspec_inventory based on your active nodes. You use this file for
69
+ running ansible playbooks against your Vagrant instances.
70
+
71
+ * config.spec.test_plan: an array of hashes. nodes can either be a regular
72
+ expression object that matches your desired nodes or an explicit array of
73
+ nodes. flags is a string that matches the desired flags you would pass to
74
+ rspec. The last argument is typically a pattern that maps to the tests you'd
75
+ like to run. This allows you to split out what tests you'd like to run across
76
+ your cluster of nodes.
77
+
78
+ ## Sub Commands
79
+
80
+ * init: This will generate your spec_helper.rb for serverspec testing and your
81
+ ansible inventory file. You'll typically want to run this after a vagrant up,
82
+ and before your deployment and testing tasks.
83
+
84
+ * test: This will run the tests specified in your Vagrantfile. Tests are
85
+ executed against each node in your fleet and the exit code is stored if an error
86
+ occurs. All tests will run. The exit code of the last failed run will return
87
+ to the shell. Otherwise, it will return zero.
88
+
89
+ ## Sample Output
90
+
91
+ ```
92
+ > bundle exec vagrant spec init
93
+
94
+ > bundle exec vagrant spec test
95
+
96
+ *******************************************************
97
+ ***************** ServerSpec Test Run *****************
98
+ *******************************************************
99
+
100
+ [test_ansible]
101
+
102
+ ssh
103
+ ssh should be running
104
+
105
+ Finished in 0.46065 seconds (files took 1.68 seconds to load)
106
+ 1 example, 0 failures
107
+
108
+ [test_pansible]
109
+
110
+ Thing that fails
111
+ dumb_service totally fails (FAILED - 1)
112
+
113
+ Failures:
114
+
115
+ 1) Thing that fails dumb_service totally fails
116
+ On host `127.0.0.1'
117
+ Failure/Error: expect(service('dumb_service')).to be_running
118
+ expected Service "dumb_service" to be running
119
+ sudo -p 'Password: ' /bin/sh -c ps\ aux\ \|\ grep\ -w\ --\ dumb_service\ \|\ grep\ -qv\ grep
120
+
121
+ # ./serverspec/fail_spec.rb:5:in `block (2 levels) in <top (required)>'
122
+ # ./lib/vagrant_spec/test_plan.rb:62:in `execute_plan_tests'
123
+ # ./lib/vagrant_spec/test_plan.rb:31:in `block (2 levels) in run'
124
+ # ./lib/vagrant_spec/test_plan.rb:31:in `each'
125
+ # ./lib/vagrant_spec/test_plan.rb:31:in `block in run'
126
+ # ./lib/vagrant_spec/test_plan.rb:30:in `each'
127
+ # ./lib/vagrant_spec/test_plan.rb:30:in `run'
128
+ # ./lib/vagrant_spec/command/test.rb:18:in `execute'
129
+ # ./lib/vagrant_spec/command/base.rb:61:in `parse_subcommand'
130
+ # ./lib/vagrant_spec/command/base.rb:25:in `execute'
131
+
132
+ Finished in 0.05726 seconds (files took 0.58135 seconds to load)
133
+ 1 example, 1 failure
134
+
135
+ Failed examples:
136
+
137
+ rspec ./serverspec/fail_spec.rb:4 # Thing that fails dumb_service totally fails
138
+
139
+ > echo $?
140
+ 1
141
+ ```
142
+
143
+ ## Development
144
+
145
+ * Fork the development branch
146
+ * ```bundle exec rake test```
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rspec/core/rake_task'
4
+ require 'rubocop/rake_task'
5
+
6
+ task default: %i(unit)
7
+
8
+ desc 'Run unit tests'
9
+ RSpec::Core::RakeTask.new(:unit)
10
+
11
+ desc 'Run rubocop'
12
+ RuboCop::RakeTask.new(:cop)
13
+
14
+ desc 'Run smoke test'
15
+ task :smoke_test do
16
+ system('scripts/poor_mans_smoke_test.sh')
17
+ end
18
+
19
+ desc 'Run rubocop and unit tests'
20
+ task travis: %i(cop unit)
21
+
22
+ desc 'Run all tests'
23
+ task test: %i(cop unit smoke_test)
24
+
25
+ Dir.chdir(File.expand_path('../', __FILE__))
26
+ Bundler::GemHelper.install_tasks
data/Vagrantfile ADDED
@@ -0,0 +1,27 @@
1
+
2
+ Vagrant.configure(2) do |config|
3
+ config.vm.define 'test_ansible' do |b|
4
+ b.vm.box = 'ubuntu/trusty64'
5
+ end
6
+
7
+ config.vm.define 'test_pansible' do |b|
8
+ b.vm.box = 'ubuntu/trusty64'
9
+ end
10
+
11
+ # key: Ansible Group Name
12
+ # value: Regexp matching your node names or an array of nodes
13
+ config.spec.ansible_inventory = { 'all' => /test/ }
14
+
15
+ # nodes: Regexp matching the desired nodes or array of nodes
16
+ # flags: Command line flags you would pass to rspec
17
+ config.spec.test_plan = [
18
+ {
19
+ 'nodes' => /test_ansi/,
20
+ 'flags' => '--format documentation --color --pattern serverspec/ssh*'
21
+ },
22
+ {
23
+ 'nodes' => /test_pansi/,
24
+ 'flags' => '--format documentation --color --pattern serverspec/fail*'
25
+ }
26
+ ]
27
+ end
@@ -0,0 +1,80 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'erb'
4
+
5
+ require 'vagrant_spec/utils'
6
+ require 'vagrant_spec/machine_finder'
7
+
8
+ module VagrantSpec
9
+ # Generate ansible ansible_inventory file
10
+ #
11
+ # env [Vagrant::Environment]
12
+ class AnsibleInventory
13
+ include VagrantSpec::Utils
14
+ attr_accessor :env, :config, :ansible_inventory, :inventory, :m_finder
15
+ def initialize(env)
16
+ @env = env
17
+ @config = env.vagrantfile.config
18
+ @ansible_inventory = @config.spec.ansible_inventory
19
+ @inventory = {}
20
+ @m_finder = VagrantSpec::MachineFinder.new(env)
21
+ end
22
+
23
+ def generate
24
+ load_ansible_inventory
25
+ template_path = File.join(template_dir, 'vagrantspec_inventory.erb')
26
+ template = IO.read(template_path)
27
+ f = ERB.new(template, 0, '<>').result(binding)
28
+ IO.write('vagrantspec_inventory', f)
29
+ end
30
+
31
+ def load_ansible_inventory
32
+ @ansible_inventory.each do |group, nodes|
33
+ if nodes.is_a?(Array)
34
+ handle_array(group, nodes)
35
+ elsif nodes.is_a?(Regexp)
36
+ handle_regexp(group, nodes)
37
+ end
38
+ end
39
+ end
40
+
41
+ # group [String]
42
+ # nodes [Array<String>]
43
+ def handle_array(group, nodes)
44
+ @inventory[group] = nodes.collect do |name|
45
+ generate_machine_config(name)
46
+ end
47
+ end
48
+
49
+ # group [String]
50
+ # nodes [Array<String>]
51
+ def handle_regexp(group, nodes)
52
+ machines = @m_finder.match_nodes(nodes)
53
+ if machines
54
+ @inventory[group] = machines.collect do |name|
55
+ generate_machine_config(name.name)
56
+ end
57
+ end
58
+ end
59
+
60
+ # name [String]
61
+ def generate_machine_config(name)
62
+ node = @m_finder.machine(name.to_sym)
63
+ return nil unless node
64
+ ssh_config = machine_ssh_config(node)
65
+ { name => ssh_config }
66
+ end
67
+
68
+ # machine [Vagrant::Machine]
69
+ def machine_ssh_config(machine)
70
+ ssh_config = machine.ssh_info
71
+ key = ssh_config[:private_key_path][0]
72
+ config = {}
73
+ config['ansible_host'] = ssh_config[:host]
74
+ config['ansible_port'] = ssh_config[:port]
75
+ config['ansible_user'] = ssh_config[:username]
76
+ config['ansible_ssh_private_key_file'] = key
77
+ config
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,74 @@
1
+ # encoding: UTF-8
2
+
3
+ module VagrantSpec
4
+ module Command
5
+ # This command plugin routes to the available subcommands
6
+ class Base < Vagrant.plugin(2, :command)
7
+ def self.synopsis
8
+ 'test deployments to clustered systems'
9
+ end
10
+
11
+ attr_accessor :valid_commands, :subcommands, :env, :opts
12
+ def initialize(argv, env)
13
+ super
14
+ @main_args,
15
+ @sub_command,
16
+ @sub_args = split_main_and_subcommand(argv)
17
+ @valid_commands = Dir.glob(File.join(File.dirname(__FILE__), '*'))
18
+ .collect { |f| File.basename(f).gsub(/\.rb$/, '') }
19
+ .select { |f| f != 'base' }
20
+ @subcommands = Vagrant::Registry.new
21
+ @env = env
22
+ @opts = nil
23
+ end
24
+
25
+ def execute
26
+ register_subcommands
27
+ return unless parse_main_args
28
+ return unless parse_subcommand
29
+ end
30
+
31
+ def register_subcommands
32
+ @valid_commands.each do |cmd|
33
+ @subcommands.register cmd.to_sym do
34
+ cmd_str = cmd.to_s
35
+ require "vagrant_spec/command/#{cmd_str}"
36
+ Object.const_get "VagrantSpec::Command::#{cmd_str.capitalize}"
37
+ end
38
+ end
39
+ end
40
+
41
+ def help
42
+ opts = OptionParser.new do |o|
43
+ o.banner = "\nUsage: vagrant spec <command> [<args>]"
44
+ o.separator ''
45
+ o.separator 'Available subcommands:'
46
+ @valid_commands.sort.each { |cmd| o.separator " #{cmd}" }
47
+ o.separator ''
48
+ o.separator 'For help on any individual command run `vagrant spec ' \
49
+ '<command> -h`'
50
+ end
51
+ parse_options(opts) && @opts = opts
52
+ end
53
+
54
+ def print_help
55
+ help
56
+ @env.ui.info @opts
57
+ end
58
+
59
+ def parse_main_args
60
+ if @main_args.include?('-h') || @main_args.include?('--help')
61
+ return help
62
+ end
63
+ true
64
+ end
65
+
66
+ def parse_subcommand
67
+ klass = @subcommands.get(@sub_command.to_sym) if @sub_command
68
+ return print_help if @sub_command.nil? || klass.nil?
69
+ klass.new(@sub_args, @env).execute
70
+ true
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'vagrant_spec/ansible_inventory'
4
+ require 'vagrant_spec/spec_helper'
5
+ require 'vagrant_spec/config'
6
+
7
+ module VagrantSpec
8
+ module Command
9
+ # This command instantiates serveral files for deployment and testing
10
+ #
11
+ # argv
12
+ # env [Vagrant::Environment]
13
+ class Init < Vagrant.plugin(2, :command)
14
+ include VagrantSpec::Utils
15
+
16
+ DEFAULTS = VagrantSpec::Config::DEFAULTS
17
+
18
+ attr_accessor :config, :directory, :ansible_inventory
19
+ def initialize(argv, env)
20
+ super
21
+ @config = VagrantSpec::Config.load env
22
+ @directory = @config.spec.directory
23
+ @ansible_inventory = @config.spec.ansible_inventory
24
+ end
25
+
26
+ def execute
27
+ return unless parse_opts
28
+ VagrantSpec::SpecHelper.new(@env).generate
29
+ unless @ansible_inventory == DEFAULTS['ansible_inventory']
30
+ VagrantSpec::AnsibleInventory.new(@env).generate
31
+ end
32
+ end
33
+
34
+ def parse_opts
35
+ opts = OptionParser.new do |o|
36
+ o.banner = "\nCreates the spec/spec_helper.rb file for testing"
37
+ o.separator ''
38
+ o.separator 'Usage: vagrant spec init'
39
+ o.separator ''
40
+ end
41
+ parse_options(opts)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'vagrant_spec/test_plan'
4
+
5
+ module VagrantSpec
6
+ module Command
7
+ # This command instantiates serveral files for deployment and testing
8
+ #
9
+ # argv
10
+ # env [Vagrant::Environment]
11
+ class Test < Vagrant.plugin(2, :command)
12
+ def initialize(argv, env)
13
+ super
14
+ end
15
+
16
+ def execute
17
+ return unless parse_opts
18
+ VagrantSpec::TestPlan.new(@env).run
19
+ end
20
+
21
+ def parse_opts
22
+ opts = OptionParser.new do |o|
23
+ o.banner = "\nRun the tests configured in the Vagrantfile"
24
+ o.separator ''
25
+ o.separator 'Usage: vagrant spec test'
26
+ o.separator ''
27
+ end
28
+ parse_options(opts)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+
3
+ module VagrantSpec
4
+ # Autoload
5
+ module Command
6
+ autoload :Base, 'vagrant_spec/command/base'
7
+ autoload :Init, 'vagrant_spec/command/init'
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'vagrant_spec/config'
4
+
5
+ module VagrantSpec
6
+ module Config
7
+ # This class handles the configuration options in the Vagrantfile
8
+ class Base < Vagrant.plugin(2, :config)
9
+ DEFAULTS = VagrantSpec::Config::DEFAULTS
10
+
11
+ attr_accessor :directory
12
+ attr_accessor :ansible_inventory
13
+ attr_accessor :test_plan
14
+
15
+ def initialize
16
+ @directory = UNSET_VALUE
17
+ @ansible_inventory = UNSET_VALUE
18
+ @test_plan = UNSET_VALUE
19
+ end
20
+
21
+ def final_directory
22
+ @directory = DEFAULTS['directory'] if @directory == UNSET_VALUE
23
+ end
24
+
25
+ def final_ansible_inventory
26
+ if @ansible_inventory == UNSET_VALUE
27
+ @ansible_inventory = DEFAULTS['ansible_inventory']
28
+ end
29
+ end
30
+
31
+ def final_test_plan
32
+ @test_plan = DEFAULTS['test_plan'] if @test_plan == UNSET_VALUE
33
+ end
34
+
35
+ def finalize!
36
+ final_directory
37
+ final_ansible_inventory
38
+ final_test_plan
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: UTF-8
2
+
3
+ module VagrantSpec
4
+ # Autoload
5
+ module Config
6
+ autoload :Base, 'vagrant_spec/config/base'
7
+
8
+ DEFAULTS = {
9
+ 'directory' => 'serverspec',
10
+ 'ansible_inventory' => {},
11
+ 'test_plan' => []
12
+ }.freeze
13
+
14
+ class << self
15
+ def load(env)
16
+ env.vagrantfile.config
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+
3
+ module VagrantSpec
4
+ # Convenience methods for discovering machines
5
+ #
6
+ # env [Vagrant::Environment]
7
+ class MachineFinder
8
+ def initialize(env)
9
+ @env = env
10
+ end
11
+
12
+ # name [Symbol]
13
+ #
14
+ # return [Vagrant::Machine]
15
+ def machine(name)
16
+ @env.active_machines.each do |m|
17
+ node = @env.machine(*m)
18
+ return node if node.name == name
19
+ end
20
+ nil
21
+ end
22
+
23
+ # reg [Regexp]
24
+ #
25
+ # return [Array<Vagrant::Machine>]
26
+ def match_nodes(reg)
27
+ @env.active_machines.collect { |m| machine(m[0]) }
28
+ .select { |m| m if reg.match(m.name.to_s) }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'erb'
4
+
5
+ require 'vagrant_spec/config'
6
+ require 'vagrant_spec/utils'
7
+
8
+ module VagrantSpec
9
+ # Generates a spec_helper.rb for integration testing
10
+ #
11
+ # env [Vagrant::Environment]
12
+ class SpecHelper
13
+ include VagrantSpec::Utils
14
+ def initialize(env)
15
+ @env = env
16
+ @directory = VagrantSpec::Config.load(env).spec.directory
17
+ end
18
+
19
+ # Generate the spec_helper.rb
20
+ def generate
21
+ create_directory
22
+ return if File.exist? spec_helper_path
23
+ sh = ERB.new(IO.read(spec_helper_template), 0, '<>').result binding
24
+ IO.write(spec_helper_path, sh)
25
+ end
26
+
27
+ # return [String]
28
+ def create_directory
29
+ Dir.mkdir @directory unless Dir.exist? @directory
30
+ end
31
+
32
+ # return [String]
33
+ def spec_helper_path
34
+ File.join(@directory, 'spec_helper.rb')
35
+ end
36
+
37
+ # return [String]
38
+ def spec_helper_template
39
+ File.join(template_dir, 'spec_helper.erb')
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'serverspec'
4
+ require 'net/ssh'
5
+ require 'rspec'
6
+
7
+ ###################
8
+ # Serverspec Config
9
+
10
+ set :backend, :ssh
11
+
12
+ host = ENV['VAGRANT_HOST']
13
+ options = Net::SSH::Config.for(host)
14
+ options[:user] = ENV['VAGRANT_USER']
15
+ options[:keys] = ENV['VAGRANT_KEY']
16
+ options[:port] = ENV['VAGRANT_PORT']
17
+
18
+ set :host, host
19
+ set :ssh_options, options
@@ -0,0 +1,12 @@
1
+
2
+ <% @inventory.each do |group, nodes| %>
3
+ [<%= group %>]
4
+ <% nodes.each do |node| %>
5
+ <% node.each do |node_name, data| %>
6
+ <%= node_name %> <%= data.collect { |k, v| "#{k}=#{v}" }.join(' ') %>
7
+
8
+ <% end %>
9
+ <% end %>
10
+
11
+
12
+ <% end %>