jimdo-rspec-puppet-helpers 0.0.3 → 0.0.4

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.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format documentation --color
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem "puppet", "~>2.7"
4
+
3
5
  # Specify your gem's dependencies in jimdo-rspec-puppet-helpers.gemspec
4
6
  gemspec
data/Gemfile.lock CHANGED
@@ -1,14 +1,28 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jimdo-rspec-puppet-helpers (0.0.3)
4
+ jimdo-rspec-puppet-helpers (0.0.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ diff-lcs (1.2.4)
10
+ facter (1.7.3)
11
+ puppet (2.7.23)
12
+ facter (~> 1.5)
13
+ rspec (2.14.1)
14
+ rspec-core (~> 2.14.0)
15
+ rspec-expectations (~> 2.14.0)
16
+ rspec-mocks (~> 2.14.0)
17
+ rspec-core (2.14.5)
18
+ rspec-expectations (2.14.2)
19
+ diff-lcs (>= 1.1.3, < 2.0)
20
+ rspec-mocks (2.14.3)
9
21
 
10
22
  PLATFORMS
11
23
  ruby
12
24
 
13
25
  DEPENDENCIES
14
26
  jimdo-rspec-puppet-helpers!
27
+ puppet (~> 2.7)
28
+ rspec
data/README.md CHANGED
@@ -1,6 +1,27 @@
1
1
  # jimdo-rspec-puppet-helpers
2
2
 
3
- TODO: Write a gem description
3
+ This gem adds a single rspec helper method to puppet-rspec in order to test the contents of a `file`
4
+ which has no `content` param, but a `source` param.
5
+
6
+ It looks up the path (e. g. `puppet:///modules/sample/test.ini`) like puppet would do and compares its content to
7
+ a given regular expression.
8
+
9
+ This is especially useful if you have a legacy codebase which excessively abuses `File->src`: E. g. you can do safe refactorings like this:
10
+
11
+ 1. Write tests for existing files with this helper
12
+
13
+ it 'should be able to upload 100 files in one HTTP POST' do
14
+ should puppet_file_contains('/etc/php5/php.ini', /^max_uploads = 100\n$/m)
15
+ end
16
+
17
+ 2. Refactor: Make thie `File` a template and replace `source` with `content`
18
+ 3. Adjust test:
19
+
20
+ should contain_file('/etc/php5/php.ini').with({
21
+ :content => /^max_uploads = 100\n$/m
22
+ })
23
+
24
+ 4. Use a module like inifile in order to get rid of the regular expression hell and in order to have real DESCRIPTIVE cfg mgmt.
4
25
 
5
26
  ## Installation
6
27
 
@@ -16,9 +37,10 @@ Or install it yourself as:
16
37
 
17
38
  $ gem install jimdo-rspec-puppet-helpers
18
39
 
19
- ## Usage
40
+ ## Testing
20
41
 
21
- TODO: Write usage instructions here
42
+ $ bundle install
43
+ $ bundle exec rake spec
22
44
 
23
45
  ## Contributing
24
46
 
data/Rakefile CHANGED
@@ -1,2 +1,5 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -1,15 +1,30 @@
1
1
  # -*- encoding: utf-8 -*-
2
- Gem::Specification.new do |gem|
3
- gem.authors = ["Daniel Bonkowski", "Ingo Oeser", "Soenke Ruempler"]
4
- gem.email = ["infrateam@jimdo.com" ]
5
- gem.description = %q{rspec-puppet helpers}
6
- gem.summary = %q{rspec-puppet helpers}
7
- gem.homepage = ""
8
-
9
- gem.files = `git ls-files`.split($\)
10
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
11
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
- gem.name = "jimdo-rspec-puppet-helpers"
13
- gem.require_paths = ["lib"]
14
- gem.version = '0.0.3'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{jimdo-rspec-puppet-helpers}
5
+ s.version = "0.0.4"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Daniel Bonkowski", "Ingo Oeser", "Soenke Ruempler", "Ilya Margolin"]
9
+ s.date = %q{2014-04-03}
10
+ s.description = %q{rspec-puppet helpers}
11
+ s.email = ["infrateam@jimdo.com"]
12
+ s.files = [".gitignore", ".rspec", "Gemfile", "Gemfile.lock", "LICENSE", "README.md", "Rakefile", "jimdo-rspec-puppet-helpers.gemspec", "lib/jimdo-rspec-puppet-helpers.rb", "spec/puppet_file_contains_spec.rb", "spec/spec_helper.rb"]
13
+ s.homepage = %q{}
14
+ s.require_paths = ["lib"]
15
+ s.rubygems_version = %q{1.6.2}
16
+ s.summary = %q{rspec-puppet helpers}
17
+ s.test_files = ["spec/puppet_file_contains_spec.rb", "spec/spec_helper.rb"]
18
+
19
+ if s.respond_to? :specification_version then
20
+ s.specification_version = 3
21
+
22
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
23
+ s.add_development_dependency(%q<rspec>, [">= 0"])
24
+ else
25
+ s.add_dependency(%q<rspec>, [">= 0"])
26
+ end
27
+ else
28
+ s.add_dependency(%q<rspec>, [">= 0"])
29
+ end
15
30
  end
@@ -1,50 +1,40 @@
1
1
  require 'rspec'
2
+ require 'puppet'
3
+ require 'puppet/file_serving'
4
+ require 'puppet/file_serving/metadata'
2
5
 
3
6
  RSpec::Matchers.define :puppet_file_contains do |file, expected|
4
7
  match do |actual|
5
- files = actual.resource('file', file).to_hash[:source].map do |catalog_file|
6
- catalog_file.gsub(/^puppet:\/\//, '')
8
+ @message = nil
9
+ resource = actual.resource('file', file)
10
+ if not resource then
11
+ @message = "expected 'File[#{file}]' in catalog, not found!"
12
+ next
13
+ end
14
+ source_attr = resource.to_hash[:source]
15
+ if not source_attr then
16
+ @message = "expected source attribute on #{resource}, found: #{resource.to_hash.keys.sort.join(",")}"
17
+ next
7
18
  end
8
- found_file = nil
9
- files.each do |catalog_file|
10
- sliced_path = catalog_file.split('/')
11
- module_path = sliced_path[1..2]
12
- relative_path_in_puppet_files = sliced_path.slice(3..sliced_path.length)
13
19
 
14
- # check whether we have rspec fixtures
15
- if File.directory?("./spec/fixtures") then
16
- path = File.expand_path(
17
- File.join(
18
- 'spec/fixtures',
19
- module_path,
20
- 'files',
21
- relative_path_in_puppet_files
22
- )
23
- );
24
- # else use files in puppet source tree
25
- else
26
- path = File.expand_path(
27
- File.join(
28
- module_path,
29
- 'files',
30
- relative_path_in_puppet_files
31
- )
32
- );
33
- end
34
- if File.file?(path) then
35
- found_file = path
36
- break
37
- end
20
+ file_data = nil
21
+ source_attr.each do |source|
22
+ file_data = Puppet::FileServing::Metadata.indirection.find(source)
23
+ break if file_data
38
24
  end
39
25
 
40
- if !found_file then
41
- false
26
+ if !file_data then
27
+ @message = "no files specified in 'source' exist"
28
+ next
42
29
  else
30
+ found_file = file_data.path
31
+
32
+ @message = "expected that #{file}(source=#{found_file}) matches #{expected}"
43
33
  expected.match(IO.read(found_file)) != nil
44
34
  end
45
35
  end
46
36
 
47
37
  failure_message_for_should do |actual|
48
- "expected that it matches #{expected}"
38
+ @message
49
39
  end
50
40
  end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'puppet_file_contains' do
4
+ context "resource not found" do
5
+ subject {
6
+ catalog = double()
7
+ resource = double()
8
+ catalog.stub(:resource) { nil }
9
+ catalog
10
+ }
11
+ it "will raise ExpectationNotMetError with 'expected File[/path/file] in catalog, not found!'" do
12
+ expect {
13
+ should puppet_file_contains('/path/file', %r'regex')
14
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError, %r(expected 'File\[/path/file\]' in catalog, not found!))
15
+ end
16
+ end
17
+
18
+ context "resource does not contain 'source' key" do
19
+ subject {
20
+ catalog = double()
21
+ resource = double()
22
+ catalog.stub(:resource) { resource }
23
+ resource.stub(:to_hash) { { :some_attribute => 1, :some_other_attribute => 2} }
24
+ catalog
25
+ }
26
+ it "will raise ExpectationNotMetError with 'expected source attribute'" do
27
+ expect {
28
+ should puppet_file_contains('/path/file', %r'regex')
29
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected source attribute on .*, found: some_attribute,some_other_attribute/)
30
+ end
31
+ end
32
+
33
+ context "resource is found, source is present" do
34
+ subject {
35
+ catalog = double()
36
+ resource = double()
37
+ catalog.stub(:resource) { resource }
38
+ resource.stub(:to_hash) { { :source => ['puppet:///modules/modulex/path1', 'puppet:///modules/modulex/path2'] } }
39
+ catalog
40
+ }
41
+
42
+ context "no files found" do
43
+ it "will raise ExpectationNotMetError with 'expected source attribute'" do
44
+ Puppet::FileServing::Metadata.indirection.stub(:find) {
45
+ nil
46
+ }
47
+ expect {
48
+ should puppet_file_contains('/path/file', %r'regex')
49
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /no files specified in 'source' exist/)
50
+ end
51
+ end
52
+
53
+ context "some file is found, but does not match regex" do
54
+ it "will raise ExpectationNotMetError with 'expected source attribute'" do
55
+ Puppet::FileServing::Metadata.indirection.stub(:find) {
56
+ double(:path => '/some/path/to/file')
57
+ }
58
+ IO.stub(:read).with('/some/path/to/file') { 'something else' }
59
+ expect {
60
+ should puppet_file_contains('/path/file', %r'regex')
61
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /^#{Regexp.quote("expected that /path/file(source=/some/path/to/file) matches")}/)
62
+ end
63
+ end
64
+
65
+ context "some file is found and matches regex" do
66
+ it "will raise ExpectationNotMetError with 'expected source attribute'" do
67
+ Puppet::FileServing::Metadata.indirection.stub(:find) {
68
+ double(:path => '/some/path/to/file')
69
+ }
70
+ IO.stub(:read).with('/some/path/to/file') { 'I do match a regex' }
71
+ should puppet_file_contains('/path/file', %r'regex')
72
+ end
73
+ end
74
+ end
75
+
76
+ end
@@ -0,0 +1 @@
1
+ require 'lib/jimdo-rspec-puppet-helpers.rb'
metadata CHANGED
@@ -1,26 +1,52 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: jimdo-rspec-puppet-helpers
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Daniel Bonkowski
9
14
  - Ingo Oeser
10
15
  - Soenke Ruempler
16
+ - Ilya Margolin
11
17
  autorequire:
12
18
  bindir: bin
13
19
  cert_chain: []
14
- date: 2013-05-29 00:00:00.000000000 Z
15
- dependencies: []
20
+
21
+ date: 2014-04-03 00:00:00 +02:00
22
+ default_executable:
23
+ dependencies:
24
+ - !ruby/object:Gem::Dependency
25
+ name: rspec
26
+ prerelease: false
27
+ requirement: &id001 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ hash: 3
33
+ segments:
34
+ - 0
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id001
16
38
  description: rspec-puppet helpers
17
- email:
39
+ email:
18
40
  - infrateam@jimdo.com
19
41
  executables: []
42
+
20
43
  extensions: []
44
+
21
45
  extra_rdoc_files: []
22
- files:
46
+
47
+ files:
23
48
  - .gitignore
49
+ - .rspec
24
50
  - Gemfile
25
51
  - Gemfile.lock
26
52
  - LICENSE
@@ -28,28 +54,42 @@ files:
28
54
  - Rakefile
29
55
  - jimdo-rspec-puppet-helpers.gemspec
30
56
  - lib/jimdo-rspec-puppet-helpers.rb
31
- homepage: ''
57
+ - spec/puppet_file_contains_spec.rb
58
+ - spec/spec_helper.rb
59
+ has_rdoc: true
60
+ homepage: ""
32
61
  licenses: []
62
+
33
63
  post_install_message:
34
64
  rdoc_options: []
35
- require_paths:
65
+
66
+ require_paths:
36
67
  - lib
37
- required_ruby_version: !ruby/object:Gem::Requirement
68
+ required_ruby_version: !ruby/object:Gem::Requirement
38
69
  none: false
39
- requirements:
40
- - - '>='
41
- - !ruby/object:Gem::Version
42
- version: '0'
43
- required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
78
  none: false
45
- requirements:
46
- - - '>='
47
- - !ruby/object:Gem::Version
48
- version: '0'
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
49
86
  requirements: []
87
+
50
88
  rubyforge_project:
51
- rubygems_version: 1.8.25
89
+ rubygems_version: 1.6.2
52
90
  signing_key:
53
91
  specification_version: 3
54
92
  summary: rspec-puppet helpers
55
- test_files: []
93
+ test_files:
94
+ - spec/puppet_file_contains_spec.rb
95
+ - spec/spec_helper.rb