chef-serverspec-handler 0.0.0

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "> 1.0.0"
12
+ gem "jeweler", "~> 1.8.4"
13
+ gem "rcov", ">= 0"
14
+ end
15
+
16
+ gem 'chef'
data/Gemfile.lock ADDED
@@ -0,0 +1,68 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ amq-protocol (1.6.0)
5
+ bunny (0.9.2)
6
+ amq-protocol (>= 1.6.0)
7
+ chef (0.8.10)
8
+ bunny (>= 0.6.0)
9
+ erubis
10
+ extlib
11
+ json
12
+ mixlib-authentication (>= 1.1.0)
13
+ mixlib-cli (>= 1.1.0)
14
+ mixlib-config (>= 1.1.0)
15
+ mixlib-log (>= 1.1.0)
16
+ moneta
17
+ ohai (>= 0.5.0)
18
+ diff-lcs (1.1.3)
19
+ erubis (2.7.0)
20
+ extlib (0.9.16)
21
+ git (1.2.5)
22
+ ipaddress (0.8.0)
23
+ jeweler (1.8.4)
24
+ bundler (~> 1.0)
25
+ git (>= 1.2.5)
26
+ rake
27
+ rdoc
28
+ json (1.8.0)
29
+ mixlib-authentication (1.3.0)
30
+ mixlib-log
31
+ mixlib-cli (1.3.0)
32
+ mixlib-config (1.1.2)
33
+ mixlib-log (1.6.0)
34
+ mixlib-shellout (1.2.0)
35
+ moneta (0.7.19)
36
+ ohai (6.18.0)
37
+ ipaddress
38
+ mixlib-cli
39
+ mixlib-config
40
+ mixlib-log
41
+ mixlib-shellout
42
+ systemu
43
+ yajl-ruby
44
+ rake (10.0.4)
45
+ rcov (0.9.11)
46
+ rdoc (3.12.2)
47
+ json (~> 1.4)
48
+ rspec (2.8.0)
49
+ rspec-core (~> 2.8.0)
50
+ rspec-expectations (~> 2.8.0)
51
+ rspec-mocks (~> 2.8.0)
52
+ rspec-core (2.8.0)
53
+ rspec-expectations (2.8.0)
54
+ diff-lcs (~> 1.1.2)
55
+ rspec-mocks (2.8.0)
56
+ systemu (2.5.2)
57
+ yajl-ruby (1.1.0)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ bundler (> 1.0.0)
64
+ chef
65
+ jeweler (~> 1.8.4)
66
+ rcov
67
+ rdoc (~> 3.12)
68
+ rspec (~> 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 tily
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Chef Serverspec Handler
2
+
3
+ ## ABOUT
4
+
5
+ Chef handler to convert recipe resources into serverspec examples.
6
+
7
+ ## INSTALL
8
+
9
+ gem install chef-serverspec-handler
10
+
11
+ ## USAGE
12
+
13
+ Edit your solo.rb config file to add lines like this:
14
+
15
+ require 'chef-serverspec-handler'
16
+
17
+ report_handlers << ChefServerspecHandler.new(:output_dir => '/path/to/dir')
18
+
19
+ Then run chef with --whyrun option (for not affecting your working system):
20
+
21
+ chef-solo -c solo.rb -j dna.json --whyrun
22
+
23
+ `chef-serverspec-handler` will be called at the end of the chef run, and generates serverspec examples to
24
+ `/path/to/dir`.
25
+
26
+ input (chef recipe):
27
+
28
+ template '/var/tmp/template.txt' do
29
+ source 'template.txt.erb'
30
+ mode 0777
31
+ owner 'root'
32
+ group 'root'
33
+ variables(:val1 => 'val1', :val2 => 'val2', :val3 => 'val3')
34
+ action :create
35
+ end
36
+
37
+ output (severspec example):
38
+
39
+ context file('/var/tmp/template.txt') do
40
+ it {
41
+ should be_file
42
+ should be_mode 777
43
+ should be_owned_by 'root'
44
+ should contain 'val1'
45
+ should contain 'val2'
46
+ should contain 'val3'
47
+ }
48
+ end
49
+
50
+ For more example, see below:
51
+
52
+ * [input chef recipe](https://github.com/tily/chef-serverspec-handler/blob/master/generated-spec/cookbooks/chef_serverspec_handler_test/recipes/default.rb)
53
+ * [output serverspec example](https://github.com/tily/chef-serverspec-handler/blob/master/generated-spec/spec/localhost/chef_serverspec_handler_test/default_spec.rb)
54
+
55
+ ## SUPPORTED RESOURCES
56
+
57
+ resource name | actions
58
+ -----------------|----------------------------------
59
+ file | create, create_if_missing, touch
60
+ cookbook_file | create, create_if_missing
61
+ remote_file | create, create_if_missing
62
+ directory | create
63
+ remote_directory | create, create_if_missing
64
+ template | create, create_if_missing
65
+ link | create
66
+ user | create
67
+ group | create
68
+ service | start, enable
69
+ cron | create
70
+
71
+ ## TODO
72
+
73
+ * mount
74
+ * apt_package
75
+ * dpkg_package
76
+ * yum_package
77
+ * rpm_package
78
+
79
+ ## SEE ALSO
80
+
81
+ * [serverspec](http://serverspec.org/)
82
+ * [About Exception and Report Handlers](http://docs.opscode.com/essentials_handlers.html)
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "chef-serverspec-handler"
18
+ gem.homepage = "http://github.com/tily/chef-serverspec-handler"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{convert chef recipe resources into serverspec examples}
21
+ gem.description = %Q{convert chef recipe resources into serverspec examples}
22
+ gem.email = "tidnlyam@gmail.com"
23
+ gem.authors = ["tily"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "chef-serverspec-handler #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "chef-serverspec-handler"
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["tily"]
12
+ s.date = "2013-07-21"
13
+ s.description = "convert chef recipe resources into serverspec examples"
14
+ s.email = "tidnlyam@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/chef-serverspec-handler.erb",
28
+ "lib/chef-serverspec-handler.rb",
29
+ "spec/chef-serverspec-handler_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = "http://github.com/tily/chef-serverspec-handler"
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = "1.8.13"
36
+ s.summary = "convert chef recipe resources into serverspec examples"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<chef>, [">= 0"])
43
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
44
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
45
+ s.add_development_dependency(%q<bundler>, ["> 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
47
+ s.add_development_dependency(%q<rcov>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<chef>, [">= 0"])
50
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
51
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
52
+ s.add_dependency(%q<bundler>, ["> 1.0.0"])
53
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
54
+ s.add_dependency(%q<rcov>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<chef>, [">= 0"])
58
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
59
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
60
+ s.add_dependency(%q<bundler>, ["> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,6 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.pattern = 'spec/*/*/*_spec.rb'
6
+ end
@@ -0,0 +1 @@
1
+ chef serverspec handler cookbook file test
@@ -0,0 +1,86 @@
1
+
2
+ file '/var/tmp/file.txt' do
3
+ content "chef serverspec handler file resource test\n"
4
+ mode 0777
5
+ owner 'root'
6
+ group 'root'
7
+ action :create
8
+ end
9
+
10
+ cookbook_file '/var/tmp/cookbook_file.txt' do
11
+ source 'cookbook_file.txt'
12
+ mode 0777
13
+ owner 'root'
14
+ group 'root'
15
+ action :create
16
+ end
17
+
18
+ remote_file '/var/tmp/remote_file.txt' do
19
+ source 'http://www.kanzaki.com/parts/me.gif'
20
+ mode 0777
21
+ owner 'root'
22
+ group 'root'
23
+ action :create
24
+ end
25
+
26
+ directory '/var/tmp/directory' do
27
+ mode 0777
28
+ owner 'root'
29
+ group 'root'
30
+ action :create
31
+ end
32
+
33
+ remote_directory '/var/tmp/remote_directory' do
34
+ source 'remote_directory'
35
+ mode 0777
36
+ owner 'root'
37
+ group 'root'
38
+ action :create
39
+ end
40
+
41
+ template '/var/tmp/template.txt' do
42
+ source 'template.txt.erb'
43
+ mode 0777
44
+ owner 'root'
45
+ group 'root'
46
+ variables(:val1 => 'val1', :val2 => 'val2', :val3 => 'val3')
47
+ action :create
48
+ end
49
+
50
+ package 'httpd' do
51
+ action :install
52
+ end
53
+
54
+ link '/var/tmp/link' do
55
+ owner 'root'
56
+ to '/var/tmp/file.txt'
57
+ end
58
+
59
+ user 'tily' do
60
+ uid 500
61
+ home '/home/tily'
62
+ shell '/bin/bash'
63
+ action :create
64
+ end
65
+
66
+ group 'tily' do
67
+ action :create
68
+ end
69
+
70
+ service 'httpd' do
71
+ action :start
72
+ end
73
+
74
+ service 'httpd' do
75
+ action :enable
76
+ end
77
+
78
+ cron 'chef_serverspec_handler_cron_test' do
79
+ minute "0"
80
+ hour "20"
81
+ day "*"
82
+ month "10"
83
+ weekday "1-5"
84
+ command 'echo hello > /var/tmp/cron.txt'
85
+ action :create
86
+ end
@@ -0,0 +1,4 @@
1
+ chef serverspec template test
2
+ val1: <%= @val1 %>
3
+ val2: <%= @val2 %>
4
+ val3: <%= @val3 %>
@@ -0,0 +1,5 @@
1
+ {
2
+ "run_list": [
3
+ "recipe[chef_serverspec_handler_test]"
4
+ ]
5
+ }
@@ -0,0 +1,14 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib/'
2
+ require 'chef-serverspec-handler'
3
+
4
+ here = File.absolute_path File.dirname(__FILE__)
5
+
6
+ file_cache_path here
7
+ cookbook_path File.join(here, 'cookbooks')
8
+
9
+ log_level :info
10
+
11
+ report_handlers << ChefServerspecHandler.new(
12
+ :output_dir => File.join(here, 'spec/localhost'),
13
+ :force => true
14
+ )
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'chef_serverspec_handler_test::default' do
4
+ context file('/var/tmp/file.txt') do
5
+ it {
6
+ should be_file
7
+ should contain <<-EOF
8
+ chef serverspec handler file resource test
9
+
10
+ EOF
11
+ should be_mode 777
12
+ should be_owned_by 'root'
13
+ }
14
+ end
15
+
16
+ context file('/var/tmp/cookbook_file.txt') do
17
+ it {
18
+ should be_file
19
+ should be_mode 777
20
+ should be_owned_by 'root'
21
+ }
22
+ end
23
+
24
+ context file('/var/tmp/remote_file.txt') do
25
+ it {
26
+ should be_file
27
+ should be_mode 777
28
+ should be_owned_by 'root'
29
+ }
30
+ end
31
+
32
+ context file('/var/tmp/directory') do
33
+ it {
34
+ should be_directory
35
+ should be_mode 777
36
+ should be_owned_by 'root'
37
+ }
38
+ end
39
+
40
+ context file('/var/tmp/remote_directory') do
41
+ it {
42
+ should be_directory
43
+ should be_mode 777
44
+ should be_owned_by 'root'
45
+ }
46
+ end
47
+
48
+ context file('/var/tmp/template.txt') do
49
+ it {
50
+ should be_file
51
+ should be_mode 777
52
+ should be_owned_by 'root'
53
+ should contain 'val1'
54
+ should contain 'val2'
55
+ should contain 'val3'
56
+ }
57
+ end
58
+
59
+ context file('/var/tmp/link') do
60
+ it {
61
+ should be_linked_to '/var/tmp/file.txt'
62
+ should be_owned_by 'root'
63
+ }
64
+ end
65
+
66
+ context user('tily') do
67
+ it {
68
+ should exist
69
+ should have_uid 500
70
+ should have_home_directory '/home/tily'
71
+ should have_login_shell '/bin/bash'
72
+ }
73
+ end
74
+
75
+ context group('tily') do
76
+ it { should exist }
77
+ it { should have_gid 500 }
78
+ end
79
+
80
+ context service('httpd') do
81
+ it { should be_running }
82
+ end
83
+
84
+ context service('httpd') do
85
+ it { should be_enabled }
86
+ end
87
+
88
+ context cron do
89
+ it { should have_entry('0 20 * 10 1-5 echo hello > /var/tmp/cron.txt').with_user('root') }
90
+ end
91
+
92
+ end
93
+
@@ -0,0 +1,17 @@
1
+ require 'serverspec'
2
+ require 'pathname'
3
+ ### include requirements ###
4
+
5
+ include Serverspec::Helper::Exec
6
+ include Serverspec::Helper::DetectOS
7
+
8
+ RSpec.configure do |c|
9
+ if ENV['ASK_SUDO_PASSWORD']
10
+ require 'highline/import'
11
+ c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
12
+ else
13
+ c.sudo_password = ENV['SUDO_PASSWORD']
14
+ end
15
+ c.before :all do
16
+ end
17
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe '<%= cookbook %>::<%= recipe %>' do
4
+ <%- resources.each do |resource| -%>
5
+ <%-
6
+ if [Chef::Resource::File, Chef::Resource::CookbookFile, Chef::Resource::RemoteFile, Chef::Resource::Template].include?(resource.class)
7
+ -%>
8
+ context file('<%= resource.path %>') do
9
+ it {
10
+ should be_file
11
+ <%- if resource.content -%>
12
+ should contain <<-EOF
13
+ <%= resource.content %>
14
+ EOF
15
+ <%- end -%>
16
+ <%- if resource.mode -%>
17
+ should be_mode <%= resource.mode.class == String ? resource.mode.to_i : resource.mode.to_i.to_s(8).to_i %>
18
+ <%- end -%>
19
+ <%- if resource.owner -%>
20
+ should be_owned_by '<%= resource.owner %>'
21
+ <%- end -%>
22
+ <%- if resource.respond_to?(:variables) && resource.variables && resource.variables.values.size > 0 -%>
23
+ <%- resource.variables.each_value do |value| -%>
24
+ should contain '<%= value %>'
25
+ <%- end -%>
26
+ <%- end -%>
27
+ }
28
+ end
29
+ <%- elsif [Chef::Resource::Directory, Chef::Resource::RemoteDirectory].include?(resource.class) -%>
30
+ context file('<%= resource.name %>') do
31
+ it {
32
+ should be_directory
33
+ <%- if resource.mode -%>
34
+ should be_mode <%= resource.mode.class == String ? resource.mode.to_i : resource.mode.to_i.to_s(8).to_i %>
35
+ <%- end -%>
36
+ <%- if resource.owner -%>
37
+ should be_owned_by '<%= resource.owner %>'
38
+ <%- end -%>
39
+ }
40
+ end
41
+ <%- elsif resource.class == Chef::Resource::Package -%>
42
+ context package('<%= resource.name %>') do
43
+ it { should be_installed }
44
+ end
45
+ <%- elsif resource.class == Chef::Resource::Mount -%>
46
+ context file('/') do
47
+ it {
48
+ should be_mounted_with(
49
+ )
50
+ }
51
+ end
52
+ <%- elsif resource.class == Chef::Resource::Link -%>
53
+ context file('<%= resource.target_file %>') do
54
+ it {
55
+ should be_linked_to '<%= resource.to %>'
56
+ <%- if resource.owner -%>
57
+ should be_owned_by '<%= resource.owner %>'
58
+ <%- end -%>
59
+ }
60
+ end
61
+ <%- elsif resource.class == Chef::Resource::User -%>
62
+ context user('<%= resource.username %>') do
63
+ it {
64
+ should exist
65
+ <%- if resource.uid -%>
66
+ should have_uid <%= resource.uid %>
67
+ <%- end -%>
68
+ <%- if resource.home -%>
69
+ should have_home_directory '<%= resource.home %>'
70
+ <%- end -%>
71
+ <%- if resource.shell -%>
72
+ should have_login_shell '<%= resource.shell %>'
73
+ <%- end -%>
74
+ }
75
+ end
76
+ <%- elsif resource.class == Chef::Resource::Group -%>
77
+ context group('<%= resource.group_name %>') do
78
+ it { should exist }
79
+ <%- if resource.gid -%>
80
+ it { should have_gid <%= resource.gid %> }
81
+ <%- end -%>
82
+ end
83
+ <%- elsif resource.class == Chef::Resource::Service -%>
84
+ context service('<%= resource.service_name %>') do
85
+ <%- if regular_actions(resource).include?(:enable) -%>
86
+ it { should be_enabled }
87
+ <%- elsif regular_actions(resource).include?(:start) -%>
88
+ it { should be_running }
89
+ <%- end -%>
90
+ end
91
+ <%- elsif resource.class == Chef::Resource::Cron -%>
92
+ context cron do
93
+ it { should have_entry('<%= resource.minute %> <%= resource.hour %> <%= resource.day %> <%= resource.month %> <%= resource.weekday %> <%= resource.command %>')<%- if resource.user -%>.with_user('<%= resource.user %>')<%- end -%> }
94
+ end
95
+ <%- end -%>
96
+
97
+ <%- end -%>
98
+ end
99
+
@@ -0,0 +1,80 @@
1
+ require 'fileutils'
2
+ require 'chef'
3
+
4
+ class ChefServerspecHandler < Chef::Handler
5
+
6
+ HERE = File.absolute_path File.dirname(__FILE__)
7
+
8
+ SUPPORTED_RESOURCES = {
9
+ Chef::Resource::File => [:create, :create_if_missing, :touch],
10
+ Chef::Resource::CookbookFile => [:create, :create_if_missing],
11
+ Chef::Resource::RemoteFile => [:create, :create_if_missing],
12
+ Chef::Resource::Directory => [:create],
13
+ Chef::Resource::RemoteDirectory => [:create, :create_if_missing],
14
+ Chef::Resource::Template => [:create, :create_if_missing],
15
+ Chef::Resource::Package => [:install],
16
+ Chef::Resource::Link => [:create],
17
+ Chef::Resource::User => [:create],
18
+ Chef::Resource::Group => [:create],
19
+ Chef::Resource::Service => [:start, :enable],
20
+ Chef::Resource::Cron => [:create]
21
+ }
22
+
23
+ def initialize(options={})
24
+ @options = options
25
+
26
+ if @options[:output_dir].nil?
27
+ raise ArgumentError, 'option :output_dir is required'
28
+ end
29
+
30
+ if !FileTest.directory?(@options[:output_dir])
31
+ raise ArgumentError, ":ouput_dir is not directory"
32
+ end
33
+
34
+ @options[:force] ||= false
35
+ end
36
+
37
+ def report
38
+ resources = extract_target_resources
39
+
40
+ resources.each do |cookbook, recipes|
41
+ cookbook_dir = File.join(@options[:output_dir], cookbook.to_s)
42
+ FileUtils.mkdir_p cookbook_dir
43
+
44
+ recipes.each do |recipe, resources|
45
+ spec_path = File.join(cookbook_dir, "#{recipe}_spec.rb")
46
+
47
+ if File.exists?(spec_path) && !@options[:force]
48
+ Chef::Log.warn "ChefServerspecHandler: #{cookbook}/#{recipe}.rb already exists. set :force option to true if you want to override it"
49
+ next
50
+ end
51
+
52
+ Chef::Log.info "ChefServerspecHandler: generating #{cookbook}/#{recipe}_spec.rb"
53
+
54
+ File.open(spec_path, 'w') do |file|
55
+ template = File.read File.join(HERE, 'chef-serverspec-handler.erb')
56
+ spec = ERB.new(template, nil, '-').result(binding)
57
+ file.write(spec)
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ def extract_target_resources
64
+ resources = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = []}}
65
+
66
+ all_resources.each do |resource|
67
+ if SUPPORTED_RESOURCES.keys.include?(resource.class)
68
+ if (SUPPORTED_RESOURCES[resource.class] & regular_actions(resource)).size > 0
69
+ resources[resource.cookbook_name][resource.recipe_name] << resource
70
+ end
71
+ end
72
+ end
73
+
74
+ resources
75
+ end
76
+
77
+ def regular_actions(resource)
78
+ [resource.action].flatten.map {|action| action.to_sym }
79
+ end
80
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ChefServerspecHandler" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'chef-serverspec-handler'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef-serverspec-handler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - tily
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-21 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: &133086480 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *133086480
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &133085480 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.8.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *133085480
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: &133084420 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.12'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *133084420
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &133083760 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>'
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *133083760
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &133083140 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.8.4
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *133083140
69
+ - !ruby/object:Gem::Dependency
70
+ name: rcov
71
+ requirement: &133064620 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *133064620
80
+ description: convert chef recipe resources into serverspec examples
81
+ email: tidnlyam@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files:
85
+ - LICENSE.txt
86
+ - README.md
87
+ files:
88
+ - .document
89
+ - .rspec
90
+ - Gemfile
91
+ - Gemfile.lock
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - VERSION
96
+ - chef-serverspec-handler.gemspec
97
+ - generated-spec/Rakefile
98
+ - generated-spec/cookbooks/chef_serverspec_handler_test/files/default/cookbook_file.txt
99
+ - generated-spec/cookbooks/chef_serverspec_handler_test/files/default/remote_directory/remote_directory_file.txt
100
+ - generated-spec/cookbooks/chef_serverspec_handler_test/recipes/default.rb
101
+ - generated-spec/cookbooks/chef_serverspec_handler_test/templates/default/template.txt.erb
102
+ - generated-spec/dna.json
103
+ - generated-spec/solo.rb
104
+ - generated-spec/spec/localhost/chef_serverspec_handler_test/default_spec.rb
105
+ - generated-spec/spec/spec_helper.rb
106
+ - lib/chef-serverspec-handler.erb
107
+ - lib/chef-serverspec-handler.rb
108
+ - spec/chef-serverspec-handler_spec.rb
109
+ - spec/spec_helper.rb
110
+ homepage: http://github.com/tily/chef-serverspec-handler
111
+ licenses:
112
+ - MIT
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ segments:
124
+ - 0
125
+ hash: 3112980122523123149
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.13
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: convert chef recipe resources into serverspec examples
138
+ test_files: []