chefspec-ohai 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d64ccf311fb089b7f97897e42e8fdbe9ef0d7e78
4
+ data.tar.gz: 2e0dc97593f992fe2306639d4dc92ce972a146d4
5
+ SHA512:
6
+ metadata.gz: be73a60edb56cc7349b9b1269a2f9ff69373e82a06b59bb5f6faa39a28462341eedfa1533fb98732561877aafddd288c44dc76d41fb404db3cc515b1822d3696
7
+ data.tar.gz: 0ea98d27be390b8b9f443d5cc20db71a365ef31f42eba2d0e2f11725b4e9d5b53182cf62c9a0b1ea1684d80a778471c86301e765844f1bacf80e2d0858577646
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Franklin Webber
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,130 @@
1
+ # Chefspec::Ohai
2
+
3
+ Traditionally if you have developed Ohai plugins for Chef you have had to test
4
+ them on the node after they have been deployed. If they have failed during the
5
+ deployment you will not see an error and instead see no data within the node
6
+ object. This is tiring, confusing, and makes it difficult to practice
7
+ test-driven development within regard to Ohai plugins.
8
+
9
+ The ability to test your Ohai plugins locally with RSpec has always been possible
10
+ if you understand how Ohai finds and loads plugins. This gem simplifies this
11
+ process and provides a number of helpers and methods to make it express your
12
+ expectations about your Ohai plugin.
13
+
14
+ > This gem is called ChefSpec::Ohai but it does not need ChefSpec to work. It
15
+ > relies solely on RSpec. However, when you are working with testing your
16
+ > cookbooks you are often using ChefSpec so it seemed like naming as an extension
17
+ > to ChefSpec was the right idea.
18
+
19
+ ## Installation
20
+
21
+ Installation on your local workstation will require that you have this gem
22
+ installed. To do that you can create a Gemfile for your cookbook and then perform
23
+ the following:
24
+
25
+ Create a Gemfile and then add this line to your cookbook's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'chefspec-ohai'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ chef exec bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ chef exec gem install chefspec-ohai
38
+
39
+ ## Usage
40
+
41
+ First you will want to load the helpers within `spec/spec_helper.rb` file:
42
+
43
+ ```ruby
44
+ require 'chefspec' # Auto-generated with cookbook
45
+ require 'chefspec/berkshelf' # Auto-generated with cookbook
46
+ require 'chefspec/ohai' # Added by YOU if you want to use this gem
47
+ ```
48
+
49
+ Now you will want to write a specification that will test your Ohai plugin. There
50
+ is currently no convention and no requirements for where you store your ohai
51
+ plugin tests within the cookbook. I have chosen to create the directory
52
+ `spec/unit/plugins`.
53
+
54
+ I often choose to name the specification after the name of the file that stores
55
+ the Ohai plugin. I were testing an Ohai plugin stored in `files/default/httpd_modules.rb`
56
+ I would create a specifiation named `spec/unit/plugins/httpd_modules_spec.rb`.
57
+ The most important thing is that the file ends with `_spec.rb` so that RSpec
58
+ will automatically find this file and load it appropriately.
59
+
60
+ Within the specification file that you need to require the content found in the
61
+ `spec/spec_helper.rb`. This will ensure that this gem is loaded prior to this
62
+ file being evaluated.
63
+
64
+ This gem provides an alias of RSpec's `describe` named `describe_ohai_plugin`
65
+ which loads some additional helper methods to assist you with expressing your
66
+ examples and the expectations within your examples.
67
+
68
+ You must specify a let helper, named `:plugin_file`, which is a relative path
69
+ within the cookbook to the file that contains the Ohai plugin.
70
+
71
+ When creating this gem there are two examples that I thought that you would
72
+ want to assert when testing your Ohai plugin:
73
+
74
+ * I expect the Ohai plugin to provide particular attributes
75
+ * I expect the Ohai plugin, when run, to set those attributes
76
+
77
+ Here is a sample specification file that asserts that an Ohai plugin provides
78
+ particular attributes and those attributes, when run (and stubbing the environment),
79
+ are set properly.
80
+
81
+ ```ruby
82
+ require 'spec_helper'
83
+
84
+ describe_ohai_plugin :Apache do
85
+ let(:plugin_file) { "files/default/httpd_modules.rb" }
86
+
87
+ context "default collect data" do
88
+ it "provides 'apache/modules'" do
89
+ expect(plugin).to provides_attribute("apache/modules")
90
+ end
91
+
92
+ it "correctly captures output" do
93
+ stub_plugin_shell_out('apachectl -t -D DUMP_MODULES','OUTPUT')
94
+ expect(plugin_attribute('apache/modules')).to eq("OUTPUT")
95
+ end
96
+ end
97
+ end
98
+ ```
99
+
100
+ Here is the plugin that is being tested with that above specification:
101
+
102
+ ```ruby
103
+ Ohai.plugin(:Apache) do
104
+ provides 'apache/modules'
105
+
106
+ collect_data(:default) do
107
+ apache Mash.new
108
+ modules_cmd = shell_out('apachectl -t -D DUMP_MODULES')
109
+ apache[:modules] = modules_cmd.stdout
110
+ end
111
+ end
112
+ ```
113
+
114
+ ## Development
115
+
116
+ This gem only provides a subset of the features that are possible to specify when
117
+ defining an Ohai plugin. I encourage you to open issues and pull-requests to
118
+ enhance this project.
119
+
120
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
121
+
122
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
123
+
124
+ ## Contributing
125
+
126
+ 1. Fork it ( https://github.com/[my-github-username]/chefspec-ohai/fork )
127
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
128
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
129
+ 4. Push to the branch (`git push origin my-new-feature`)
130
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "chefspec/ohai"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chefspec/ohai/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chefspec-ohai"
8
+ spec.version = Chefspec::Ohai::VERSION
9
+ spec.authors = ["Franklin Webber"]
10
+ spec.email = ["franklin@chef.io"]
11
+
12
+ spec.summary = %q{Provides additional RSpec helpers to test Ohai plugins}
13
+ spec.description = %q{Ohai plugins often go untested within the cookbooks that we create.
14
+ When an Ohai plugin fails it is often a trial-and-error process that requires us to redeploy it.
15
+ This gem provides additional helpers to RSpec to be used in conjunction with ChefSpec to test your plugins.}
16
+ spec.homepage = "https://github.com/burtlo/chefspec-ohai"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.9"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
@@ -0,0 +1,102 @@
1
+ require "chefspec/ohai/version"
2
+
3
+ RSpec.configure do |config|
4
+ #
5
+ # Add a convienent name for the example group to the RSpec lexicon. This
6
+ # allows a user to write:
7
+ # 'desribe_ohai_plugin :Apache'.
8
+ #
9
+ # As opposed to appending a type to the declaration of the spec:
10
+ # 'describe :Apache, type: :ohai_plugin'
11
+ #
12
+ config.alias_example_group_to :describe_ohai_plugin, type: :ohai_plugin
13
+ end
14
+
15
+ # This defines the shared context with all the helper methods
16
+ shared_context 'Ohai Plugins', type: :ohai_plugin do
17
+
18
+ # The plugin helper will take the symbol name provided as the subject to the
19
+ # describe and create the Ohai plugin class and then instantiate that class
20
+ # for use within the specifications.
21
+ let(:plugin) do
22
+ ohai_plugin_class = Ohai.plugin(subject) {}
23
+ ohai_plugin_class.new(plugin_data)
24
+ end
25
+
26
+ # When an Ohai plugin is created there is a Hash of data that must be provided.
27
+ # It is likely not important to provide data but allowing it to be overriden
28
+ # here if necessary.
29
+ let(:plugin_data) { Hash.new }
30
+
31
+ # Loads the plugin source from the specified plugin_file helper
32
+ let(:plugin_source) { File.read(plugin_file) }
33
+ # Determine the plugin_path from the specified plugin_file
34
+ let(:plugin_path) { File.dirname(plugin_file) }
35
+ # This helper defines the path to the plugin file. This must be specified
36
+ # by the author of the specification. Without specifying it, overriding this
37
+ # helper, an error is raised informing them that it must be specifed and
38
+ # provides an example to get them started.
39
+ let(:plugin_file) do
40
+ raise %(
41
+ Please specify the path to the Ohai plugin file within this cookbook.
42
+
43
+ Example:
44
+ let(:plugin_file) { "files/default/httpd_modules.rb" }
45
+ )
46
+ end
47
+
48
+ # A Loader requires a controller. This controller may need to be overriden so
49
+ # it is provided as a helper.
50
+ let(:plugin_controller) { double('plugin_controller') }
51
+ # The plugin_loader will evaluate the source of the plugin with the controller
52
+ let(:plugin_loader) { Ohai::Loader.new(plugin_controller) }
53
+
54
+ #
55
+ # Before each example we want to load the plugin from source. This asks the
56
+ # plugin_loader to load the plugin from the specifed source and path and
57
+ # evalute the contents as a version 7 ohai plugin.
58
+ #
59
+ before :each do
60
+ ps = plugin_source
61
+ pp = plugin_path
62
+ plugin_loader.instance_eval { load_v7_plugin_class(ps,pp) }
63
+ end
64
+
65
+ # This provides a new matcher when wanting to make assertions that the plugin
66
+ # does provide the correct body of attributes. The Ohai plugin class returns
67
+ # an array of attributes that it provides through `#provides_attrs` which
68
+ # is evaluated to ensure that the expected value is within that list.
69
+ RSpec::Matchers.define :provides_attribute do |expected|
70
+ match do |plugin|
71
+ expect(plugin.class.provides_attrs).to include(expected)
72
+ end
73
+ end
74
+
75
+ # Provides a simplier way to stub out the shell_out that is probably going on
76
+ # within the Ohai plugin.
77
+ def stub_plugin_shell_out(command,result)
78
+ allow(plugin).to receive(:shell_out).with(command) { double(stdout: result) }
79
+ end
80
+
81
+ # To make the process of verifying the attributes a little more streamlined
82
+ # you can use this helper to request the attributes from the plugin itself.
83
+ #
84
+ # The attributes are not loaded into the plugin until the plugin is run.
85
+ # Then the plugin will provide a top-level method that matches the first
86
+ # element in the ohai 'provides' String.
87
+ #
88
+ # The remaining elements within the 'provides' are elements within the Mash
89
+ # that must be traversed to get to the value desired. That is done here and
90
+ # then returned.
91
+ def plugin_attribute(attribute)
92
+ plugin.run
93
+
94
+ components = attribute.split('/')
95
+ top_level_mash_name = components.first
96
+ attributes = components[1..-1]
97
+
98
+ top_level_mash = plugin.send(top_level_mash_name)
99
+ attributes.inject(top_level_mash) { |mash,child| mash[child] }
100
+ end
101
+
102
+ end
@@ -0,0 +1,5 @@
1
+ module Chefspec
2
+ module Ohai
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chefspec-ohai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Franklin Webber
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: |-
42
+ Ohai plugins often go untested within the cookbooks that we create.
43
+ When an Ohai plugin fails it is often a trial-and-error process that requires us to redeploy it.
44
+ This gem provides additional helpers to RSpec to be used in conjunction with ChefSpec to test your plugins.
45
+ email:
46
+ - franklin@chef.io
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".gitignore"
52
+ - CODE_OF_CONDUCT.md
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - bin/console
58
+ - bin/setup
59
+ - chefspec-ohai.gemspec
60
+ - lib/chefspec/ohai.rb
61
+ - lib/chefspec/ohai/version.rb
62
+ homepage: https://github.com/burtlo/chefspec-ohai
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Provides additional RSpec helpers to test Ohai plugins
86
+ test_files: []