Class: ChefSpec::Bootstrap

Inherits:
Object
  • Object
show all
Defined in:
lib/chefspec-bootstrap.rb

Instance Method Summary collapse

Constructor Details

#initialize(cookbooks_dir, cookbooks_path, spec_dir, template_file, recursive) ⇒ Bootstrap

Returns a new instance of Bootstrap.



10
11
12
13
14
15
16
# File 'lib/chefspec-bootstrap.rb', line 10

def initialize(cookbooks_dir, cookbooks_path, spec_dir, template_file, recursive)
  @cookbooks_dir = cookbooks_dir
  @cookbooks_path = cookbooks_path
  @spec_dir = spec_dir
  @template_file = template_file
  @recursive = recursive
end

Instance Method Details

#escape_string(string) ⇒ Object



173
174
175
# File 'lib/chefspec-bootstrap.rb', line 173

def escape_string(string)
  return string.gsub("\\","\\\\").gsub("\"", "\\\"")
end

#generateObject



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
74
75
76
77
78
79
80
# File 'lib/chefspec-bootstrap.rb', line 44

def generate
  setup()

  erb = ERB.new(File.read(@template_file))

  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, cookbook, recipe)
      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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chefspec-bootstrap.rb', line 117

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 => get_it_block(noun, verb, adjective),
          :expect => get_expect_block(noun, verb),
          :name => adjective
        })
      end
    end
  end
  return test_cases
end

#get_all_resources(chef_run) ⇒ Object



98
99
100
# File 'lib/chefspec-bootstrap.rb', line 98

def get_all_resources(chef_run)
  return chef_run.resource_collection.all_resources
end

#get_chef_run(cookbook, recipe) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/chefspec-bootstrap.rb', line 86

def get_chef_run(cookbook, recipe)
  begin
    return ChefSpec::Runner.new.converge("#{cookbook}::#{recipe}")
  rescue Exception => e
    return nil
  end
end

#get_expect_block(noun, verb) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/chefspec-bootstrap.rb', line 158

def get_expect_block(noun, verb)
  expect = "%{verb}_%{noun}"
  string_variables = {:noun => noun, :verb => verb}

  if @api_map[noun] and @api_map[noun][:expect]
    if @api_map[noun][:expect][verb]
      expect = @api_map[noun][:expect][verb]
    elsif @api_map[noun][:expect][:default]
      expect = @api_map[noun][:expect][:default]
    end
  end

  return escape_string(expect % string_variables)
end

#get_it_block(noun, verb, adjective) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/chefspec-bootstrap.rb', line 141

def get_it_block(noun, verb, adjective)
  it = "%{verb}s the %{adjective} %{noun}"
  noun_readable = noun.to_s.gsub("_", " ")
  verb_readable = verb.to_s.gsub("_", " ")
  string_variables = {:noun => noun_readable, :verb => verb_readable, :adjective => adjective}

  if @api_map[noun] and @api_map[noun][:it]
    if @api_map[noun][:it][verb]
      it = @api_map[noun][:it][verb]
    elsif @api_map[noun][:it][:default]
      it = @api_map[noun][:it][:default]
    end
  end

  return escape_string(it  % string_variables)
end

#get_resource_name(resource) ⇒ Object



94
95
96
# File 'lib/chefspec-bootstrap.rb', line 94

def get_resource_name(resource)
  return resource.name || resource.identity
end

#get_resources(chef_run, cookbook, recipe) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chefspec-bootstrap.rb', line 102

def get_resources(chef_run, cookbook, recipe)
  if chef_run
    resources = get_all_resources(chef_run)
    if @recursive
      return resources
    else
      return resources.select do |resource|
        resource.cookbook_name == cookbook.to_sym and resource.recipe_name == recipe
      end
    end
  else
    return []
  end
end

#rootObject



82
83
84
# File 'lib/chefspec-bootstrap.rb', line 82

def root
  @root ||= Pathname.new(File.expand_path('../../', __FILE__))
end

#setupObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chefspec-bootstrap.rb', line 18

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

  @api_map = ChefSpec::APIMap.new.map

  begin
    require File.expand_path("#{@spec_dir}/spec_helper.rb")
    @spec_helper = true
  rescue LoadError
    @spec_helper = false
    ::RSpec.configure do |config|
      config.cookbook_path = @cookbooks_path || [@cookbooks_dir, 'cookbooks']
    end
  end
end