Class: ChefSpec::Bootstrap
- Inherits:
-
Object
- Object
- ChefSpec::Bootstrap
- Defined in:
- lib/chefspec-bootstrap.rb
Instance Method Summary collapse
- #generate ⇒ Object
- #generate_test_cases(resources) ⇒ Object
- #get_chef_run(cookbook, recipe) ⇒ Object
- #get_resource_name(resource) ⇒ Object
- #get_resources(chef_run) ⇒ Object
-
#initialize(cookbooks_dir, spec_dir, template_file) ⇒ Bootstrap
constructor
A new instance of Bootstrap.
- #root ⇒ Object
- #setup ⇒ Object
Constructor Details
#initialize(cookbooks_dir, spec_dir, template_file) ⇒ Bootstrap
Returns a new instance of Bootstrap.
9 10 11 12 13 |
# File 'lib/chefspec-bootstrap.rb', line 9 def initialize(cookbooks_dir, spec_dir, template_file) @cookbooks_dir = cookbooks_dir @spec_dir = spec_dir @template_file = template_file end |
Instance Method Details
#generate ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/chefspec-bootstrap.rb', line 29 def generate setup() erb = ERB.new(File.read(@template_file)) # begin # require "#{@spec_dir}/spec_helper.rb" # rescue LoadError # end ::RSpec.configure { |config| config.cookbook_path = [@cookbooks_dir, 'cookbooks'] } Dir.glob("#{@cookbooks_dir}/*/recipes/*").each do |path| path, recipe_file = File.split(path) recipe = recipe_file.split('.')[0] cookbook = path.split(File::SEPARATOR)[1] filename = "#{@spec_dir}/#{cookbook}/#{recipe}_spec.rb" puts filename if File.exist?(filename) puts " spec already exists. Skipping." else FileUtils.mkdir_p "#{@spec_dir}/#{cookbook}" puts " executing recipe with ChefSpec..." chef_run = get_chef_run(cookbook, recipe) if chef_run puts " execution suceeded. Creating spec file." else puts " execution failed. Creating empty spec file." end resources = get_resources(chef_run) test_cases = generate_test_cases(resources) File.open(filename, "w") do |spec_file| spec_file.write(erb.result(binding)) end end end end |
#generate_test_cases(resources) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/chefspec-bootstrap.rb', line 99 def generate_test_cases(resources) test_cases = [] resources.each do |resource| verbs = resource.action if not verbs.respond_to?(:each) verbs = [verbs] end noun = resource.resource_name adjective = resource.name verbs.each do |verb| if not verb == :nothing test_cases.push({ :it => "#{verb}s the #{adjective} #{noun}", :action => "#{verb}_#{noun}", :name => adjective }) end end end return test_cases end |
#get_chef_run(cookbook, recipe) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/chefspec-bootstrap.rb', line 79 def get_chef_run(cookbook, recipe) begin return ChefSpec::Runner.new.converge("#{cookbook}::#{recipe}") rescue return nil end end |
#get_resource_name(resource) ⇒ Object
87 88 89 |
# File 'lib/chefspec-bootstrap.rb', line 87 def get_resource_name(resource) return resource.name || resource.identity end |
#get_resources(chef_run) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/chefspec-bootstrap.rb', line 91 def get_resources(chef_run) if chef_run return chef_run.resource_collection.all_resources else return [] end end |
#root ⇒ Object
75 76 77 |
# File 'lib/chefspec-bootstrap.rb', line 75 def root @root ||= Pathname.new(File.('../../', __FILE__)) end |
#setup ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/chefspec-bootstrap.rb', line 15 def setup if not Dir.exist?(@cookbooks_dir) abort "Unable to locate your cookbooks directory (#{@cookbooks_dir})" end if not @template_file @template_file = root.join('templates', 'default.erb') end if not File.exist?(@template_file) abort "Unable to locate template file (#{@template_file})" end end |