infra-testing-helpers 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8b8db2803644966ac8a3466b8dc0dcc8dc115de
4
- data.tar.gz: bea9b38d4952f7e6a5de3ed5c85da5e453eb4c84
3
+ metadata.gz: 23f17c796b737aed580eb2254c6c81112a90a62d
4
+ data.tar.gz: ddba9dda5b5ad55f1ea34af756516ff5a7c85a1f
5
5
  SHA512:
6
- metadata.gz: e89716f7af5970d719766b6720caf51a60a1aada3361084519a58bfc5509c1a4826117769b70b46cc9b6d9d2f55b442a00ed27315e35907811cbf2aba00d29db
7
- data.tar.gz: 52404045732161e6f02e7cbee4b257a72390f3008b21580a3f3da5efeae4a771feb08906aafb02af4e26a5d646d91bca7f147fb8d2b4c63036facbcd8398154e
6
+ metadata.gz: 74107c7326119bfec7523e1788bcc8e0fe169fa68c85c417c09e233fe21f3b00d019c3dbf238083d1630d8b4e13adf977628a9bccc15a36ae039f82b84bf3d5a
7
+ data.tar.gz: cde6160c46ded14ad05265ab13ca1fc27168609e37787601ecf6ce45307036301d3cd273f8d167a9c742e08527b8dd02b2a482460e337c9b901544b67bb7b710
data/README.md CHANGED
@@ -12,12 +12,14 @@ Next, lets have a look at your `spec_helper.rb` (probably generated by Serverspe
12
12
  ```ruby
13
13
  ...
14
14
  require 'infra_testing_helpers'
15
+ InfraTestingHelpers.project_root = File.expand_path(File.dirname(__FILE__) + '/../')
15
16
  InfraTestingHelpers.module_path = 'modules/:another_module_path/'
16
17
  InfraTestingHelpers.site_pp = 'Exec { path => "/sbin:/usr/sbin:/bin:/usr/bin" }'
17
18
  InfraTestingHelpers.vagrant_shared_folder = '/etc/puppet'
18
19
  ...
19
20
  ```
20
21
  - `require 'infra_testing_helpers'` mixes in the functions we will use later in our specs as well as sets up a global `before` hook in RSpec.
22
+ - `InfraTestingHelpers.project_root` is the directory the Vagrantfile is located it. This is manditory
21
23
  - `InfraTestingHelpers.module_path` is the location of your modules. This defaults to just `modules/` if nothing is specified.
22
24
  - `InfraTestingHelpers.site_pp` is the contents of your own `site.pp` file. If yours is rather large, you might want to dynamically populate this (`File.read`). Again, this is optional.
23
25
  - `InfraTestingHelpers.vagrant_shared_folder` reflects where your shared folder is in your `Vagrantfile`. This defaults to `/vagrant`.
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "infra-testing-helpers"
7
- spec.version = '0.0.2'
7
+ spec.version = '0.0.3'
8
8
  spec.authors = ["Ryan Doyle"]
9
9
  spec.email = ["ryan@doylenet.net"]
10
10
  spec.summary = %q{Integrations with RSpec, Puppet and Vagrant to help infrastructure testing}
@@ -10,9 +10,11 @@ module InfraTestingHelpers
10
10
  attr_reader :name
11
11
 
12
12
  def apply(manifest)
13
- exit_code = run_command("sudo puppet apply --detailed-exitcode --modulepath #{@project_mount_point}/#{manifest.module_path}", :stdin => manifest.puppet_code)
14
- raise PuppetApplyFailed unless exit_code == 0 or exit_code == 2
15
- @applied = true
13
+ manifest.manifest_file do |file|
14
+ exit_code = run_command("sudo puppet apply --detailed-exitcode --modulepath #{@project_mount_point}/#{manifest.module_path} #{@project_mount_point}/#{file}")
15
+ raise PuppetApplyFailed unless exit_code == 0 or exit_code == 2
16
+ @applied = true
17
+ end
16
18
  end
17
19
 
18
20
  def applied?
@@ -6,7 +6,7 @@ module InfraTestingHelpers
6
6
 
7
7
  def apply_manifest(manifest_code)
8
8
  if box.applied?
9
- local_site = InfraTestingHelpers::Site.new(InfraTestingHelpers.site_pp, InfraTestingHelpers.module_path)
9
+ local_site = InfraTestingHelpers::Site.new(InfraTestingHelpers.site_pp, InfraTestingHelpers.module_path, InfraTestingHelpers.project_root)
10
10
  local_site.add_manifest(manifest_code)
11
11
  box.apply(local_site)
12
12
  else
@@ -22,7 +22,7 @@ module InfraTestingHelpers
22
22
  private
23
23
 
24
24
  def global_site
25
- @@global_site ||= InfraTestingHelpers::Site.new(InfraTestingHelpers.site_pp, InfraTestingHelpers.module_path)
25
+ @@global_site ||= InfraTestingHelpers::Site.new(InfraTestingHelpers.site_pp, InfraTestingHelpers.module_path, InfraTestingHelpers.project_root)
26
26
  end
27
27
 
28
28
  def box
@@ -1,9 +1,12 @@
1
+ require 'tempfile'
2
+
1
3
  module InfraTestingHelpers
2
4
  class Site
3
5
 
4
- def initialize(site_code, module_path)
6
+ def initialize(site_code, module_path, project_root)
5
7
  @site_code = site_code
6
8
  @module_path = module_path
9
+ @project_root = project_root
7
10
  @manifest_code = ""
8
11
  end
9
12
 
@@ -13,6 +16,18 @@ module InfraTestingHelpers
13
16
  @manifest_code << "#{manifest}\n"
14
17
  end
15
18
 
19
+ def manifest_file
20
+ file = Tempfile.new(['infra-testing-helpers', '.pp'], @project_root)
21
+ begin
22
+ file.puts puppet_code
23
+ file.fsync
24
+ yield(File.basename(file.path)) if block_given?
25
+ ensure
26
+ file.close
27
+ file.unlink
28
+ end
29
+ end
30
+
16
31
  def puppet_code
17
32
  "#{@site_code}\n#{@manifest_code}"
18
33
  end
@@ -5,7 +5,8 @@ module InfraTestingHelpers
5
5
  @settings = {
6
6
  :module_path => 'modules/',
7
7
  :site_pp => '',
8
- :vagrant_shared_folder => '/vagrant'
8
+ :vagrant_shared_folder => '/vagrant',
9
+ :project_root => nil,
9
10
  }
10
11
 
11
12
  def self.vagrant_shared_folder
@@ -32,6 +33,14 @@ module InfraTestingHelpers
32
33
  @settings[:module_path] = path
33
34
  end
34
35
 
36
+ def self.project_root
37
+ @settings[:project_root] or raise 'project_root has not been set'
38
+ end
39
+
40
+ def self.project_root=(path)
41
+ @settings[:project_root] = path
42
+ end
43
+
35
44
  end
36
45
 
37
46
  include InfraTestingHelpers::Helper
@@ -6,7 +6,10 @@ require 'tempfile'
6
6
  require 'infra_testing_helpers'
7
7
 
8
8
  InfraTestingHelpers.module_path = 'spec/functional/modules/'
9
- InfraTestingHelpers.site_pp = "notify {'This is my site.pp':}"
9
+ InfraTestingHelpers.site_pp = 'notify {"This is my site.pp":}
10
+ $extlookup_precedence = ["hosts/%{fqdn}", "domains/%{domain}", "common"]
11
+ '
12
+ InfraTestingHelpers.project_root = File.expand_path(File.dirname(__FILE__) + '/../')
10
13
 
11
14
  set :backend, :ssh
12
15
 
@@ -12,23 +12,24 @@ describe InfraTestingHelpers::Box do
12
12
  allow($?).to receive(:exitstatus).and_return(0)
13
13
  allow(manifest).to receive(:module_path).and_return('modules/')
14
14
  allow(manifest).to receive(:puppet_code).and_return('include some_manifest')
15
+ allow(manifest).to receive(:manifest_file).and_yield('tempfile_path.pp')
15
16
  end
16
17
 
17
18
  describe '#apply' do
18
19
 
19
20
  it 'successfully applies a manifest to the box' do
20
- expect(box).to receive(:run_command).with('sudo puppet apply --detailed-exitcode --modulepath /vagrant/modules/', {:stdin=>"include some_manifest"}).and_return(0)
21
+ expect(box).to receive(:run_command).with('sudo puppet apply --detailed-exitcode --modulepath /vagrant/modules/ /vagrant/tempfile_path.pp',).and_return(0)
21
22
  box.apply(manifest)
22
23
  end
23
24
 
24
25
  it 'successfully applies a manifest to the box and deals with weird exit codes' do
25
- expect(box).to receive(:run_command).with('sudo puppet apply --detailed-exitcode --modulepath /vagrant/modules/', {:stdin=>"include some_manifest"}).and_return(2)
26
+ expect(box).to receive(:run_command).with('sudo puppet apply --detailed-exitcode --modulepath /vagrant/modules/ /vagrant/tempfile_path.pp').and_return(2)
26
27
  box.apply(manifest)
27
28
  end
28
29
 
29
30
 
30
31
  it 'raises a InfraTestingHelpers::PuppetApplyFailed error if the puppet apply fails' do
31
- allow(box).to receive(:run_command).with('sudo puppet apply --detailed-exitcode --modulepath /vagrant/modules/', {:stdin=>"include some_manifest"}).and_return(1)
32
+ allow(box).to receive(:run_command).with('sudo puppet apply --detailed-exitcode --modulepath /vagrant/modules/ /vagrant/tempfile_path.pp').and_return(1)
32
33
 
33
34
  expect{box.apply(manifest)}.to raise_error(InfraTestingHelpers::PuppetApplyFailed)
34
35
  end
@@ -49,7 +50,7 @@ describe InfraTestingHelpers::Box do
49
50
  expect(box.applied?).to be true
50
51
  end
51
52
  it 'is false when a manifest is not applied successfully' do
52
- allow(box).to receive(:run_command).with('sudo puppet apply --detailed-exitcode --modulepath /vagrant/modules/', {:stdin=>"include some_manifest"}).and_return(1)
53
+ allow(box).to receive(:run_command).with('sudo puppet apply --detailed-exitcode --modulepath /vagrant/modules/ /vagrant/tempfile_path.pp').and_return(1)
53
54
  expect{box.apply(manifest)}.to raise_error(InfraTestingHelpers::PuppetApplyFailed)
54
55
 
55
56
  expect(box.applied?).to be false
@@ -20,6 +20,7 @@ describe InfraTestingHelpers::Helper do
20
20
  allow(box).to receive(:applied?).and_return false
21
21
  allow(InfraTestingHelpers).to receive(:module_path)
22
22
  allow(InfraTestingHelpers).to receive(:site_pp)
23
+ allow(InfraTestingHelpers).to receive(:project_root)
23
24
  end
24
25
 
25
26
  describe '#apply_manifest' do
@@ -4,7 +4,8 @@ require 'infra_testing_helpers/site'
4
4
 
5
5
  describe InfraTestingHelpers::Site do
6
6
 
7
- let(:site) { described_class.new('site.pp code', '/some/module/path') }
7
+ let(:site) { described_class.new('site.pp code', '/some/module/path', '/some/project/root') }
8
+ let(:tempfile) { double('Tempfile') }
8
9
 
9
10
  describe '#puppet_code' do
10
11
  it 'should contain the site.pp code' do
@@ -31,4 +32,39 @@ describe InfraTestingHelpers::Site do
31
32
  end
32
33
  end
33
34
 
35
+ describe '#manifest_file' do
36
+ before do
37
+ allow(Tempfile).to receive(:new).with(['infra-testing-helpers', '.pp'], '/some/project/root').and_return tempfile
38
+ allow(tempfile).to receive(:close)
39
+ allow(tempfile).to receive(:unlink)
40
+ allow(tempfile).to receive(:puts)
41
+ allow(tempfile).to receive(:fsync)
42
+ end
43
+ it 'writes the puppet code to a tempfile' do
44
+ expect(tempfile).to receive(:puts).with("site.pp code\n")
45
+
46
+ site.manifest_file
47
+ end
48
+ it 'syncs the file to the filesystem' do
49
+ expect(tempfile).to receive(:fsync)
50
+
51
+ site.manifest_file
52
+ end
53
+ it 'yeilds the base filename of the tempfile' do
54
+ allow(tempfile).to receive(:path).and_return('/tmp/tempfile_base_path.pp')
55
+
56
+ expect { |b| site.manifest_file(&b) }.to yield_with_args('tempfile_base_path.pp')
57
+ end
58
+ it 'closes the file after yielding' do
59
+ expect(tempfile).to receive(:close)
60
+
61
+ site.manifest_file
62
+ end
63
+ it 'closes the file after yielding' do
64
+ expect(tempfile).to receive(:unlink)
65
+
66
+ site.manifest_file
67
+ end
68
+ end
69
+
34
70
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infra-testing-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Doyle
@@ -113,3 +113,4 @@ test_files:
113
113
  - spec/unit/puppet_vagrant/box_spec.rb
114
114
  - spec/unit/puppet_vagrant/helper_spec.rb
115
115
  - spec/unit/puppet_vagrant/site_spec.rb
116
+ has_rdoc: