Class: Controlrepo::TestConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/controlrepo/testconfig.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ TestConfig

Returns a new instance of TestConfig.



19
20
21
22
23
24
25
26
27
28
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
# File 'lib/controlrepo/testconfig.rb', line 19

def initialize(file)
  begin
    config = YAML.load(File.read(file))
  rescue Errno::ENOENT
    raise "Could not find spec/controlrepo.yaml"
  rescue Psych::SyntaxError
    raise "Could not parse the YAML file, check that it is valid YAML and that the encoding is correct"
  end

  @classes = []
  @nodes = []
  @node_groups = []
  @class_groups = []
  @spec_tests = []
  @acceptance_tests = []

  # Add the 'all_classes' and 'all_nodes' default groups
  @node_groups << Controlrepo::Group.new('all_nodes',@nodes)
  @class_groups << Controlrepo::Group.new('all_classes',@classes)

  config['classes'].each { |clarse| @classes << Controlrepo::Class.new(clarse) } unless config['classes'] == nil
  config['nodes'].each { |node| @nodes << Controlrepo::Node.new(node) } unless config['nodes'] == nil
  config['node_groups'].each { |name, members| @node_groups << Controlrepo::Group.new(name, members) } unless config['node_groups'] == nil
  config['class_groups'].each { |name, members| @class_groups << Controlrepo::Group.new(name, members) } unless config['class_groups'] == nil

  config['test_matrix'].each do |test_hash|
    test_hash.each do |machines, settings|
      if settings['tests'] == 'spec'
        @spec_tests << Controlrepo::Test.new(machines,settings['classes'],settings['options'])
      elsif settings['tests'] == 'acceptance'
        @acceptance_tests << Controlrepo::Test.new(machines,settings['classes'],settings['options'])
      elsif settings['tests'] == 'all_tests'
        test = Controlrepo::Test.new(machines,settings['classes'],settings['options'])
        @spec_tests << test
        @acceptance_tests << test
      end
    end
  end
end

Instance Attribute Details

#acceptance_testsObject

Returns the value of attribute acceptance_tests.



16
17
18
# File 'lib/controlrepo/testconfig.rb', line 16

def acceptance_tests
  @acceptance_tests
end

#class_groupsObject

Returns the value of attribute class_groups.



14
15
16
# File 'lib/controlrepo/testconfig.rb', line 14

def class_groups
  @class_groups
end

#classesObject

Returns the value of attribute classes.



11
12
13
# File 'lib/controlrepo/testconfig.rb', line 11

def classes
  @classes
end

#environmentObject

Returns the value of attribute environment.



17
18
19
# File 'lib/controlrepo/testconfig.rb', line 17

def environment
  @environment
end

#node_groupsObject

Returns the value of attribute node_groups.



13
14
15
# File 'lib/controlrepo/testconfig.rb', line 13

def node_groups
  @node_groups
end

#nodesObject

Returns the value of attribute nodes.



12
13
14
# File 'lib/controlrepo/testconfig.rb', line 12

def nodes
  @nodes
end

#spec_testsObject

Returns the value of attribute spec_tests.



15
16
17
# File 'lib/controlrepo/testconfig.rb', line 15

def spec_tests
  @spec_tests
end

Class Method Details

.find_list(thing) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/controlrepo/testconfig.rb', line 59

def self.find_list(thing)
  # Takes a string and finds an object or list of objects to match, will
  # take nodes, classes or groups
  if Controlrepo::Group.find(thing)
    return Controlrepo::Group.find(thing).members
  elsif Controlrepo::Class.find(thing)
    return [Controlrepo::Class.find(thing)]
  elsif Controlrepo::Node.find(thing)
    return [Controlrepo::Node.find(thing)]
  else
    raise "Could not find #{thing} in list of classes, nodes or groups"
  end
end

Instance Method Details



178
179
180
181
182
183
184
185
186
187
# File 'lib/controlrepo/testconfig.rb', line 178

def create_fixtures_symlinks(repo)
  FileUtils.rm_rf("#{repo.tempdir}/spec/fixtures/modules")
  FileUtils.mkdir_p("#{repo.tempdir}/spec/fixtures/modules")
  repo.temp_modulepath.split(':').each do |path|
    Dir["#{path}/*"].each do |mod|
      modulename = File.basename(mod)
      FileUtils.ln_s(mod, "#{repo.tempdir}/spec/fixtures/modules/#{modulename}")
    end
  end
end

#pre_conditionObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/controlrepo/testconfig.rb', line 91

def pre_condition
  # Read all the pre_conditions and return the string
  spec_dir = Controlrepo.new.spec_dir
  puppetcode = []
  Dir["#{spec_dir}/pre_conditions/*.pp"].each do |condition_file|
    puppetcode << File.read(condition_file)
  end
  return nil if puppetcode.count == 0
  puppetcode.join("\n")
end

#r10k_deploy_local(repo = Controlrepo.new) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/controlrepo/testconfig.rb', line 102

def r10k_deploy_local(repo = Controlrepo.new)
  require 'controlrepo'
  require 'pathname'
  if repo.tempdir == nil
    repo.tempdir = Dir.mktmpdir('r10k')
  else
    FileUtils.mkdir_p(repo.tempdir)
  end

  # We need to do the copy to a tempdir then move the tempdir to the
  # destination
  temp_controlrepo = Dir.mktmpdir('controlrepo')
  FileUtils.cp_r(Dir["#{repo.root}/*"], "#{temp_controlrepo}")
  FileUtils.mkdir_p("#{repo.tempdir}/#{repo.environmentpath}/production")
  FileUtils.mv(Dir["#{temp_controlrepo}/*"], "#{repo.tempdir}/#{repo.environmentpath}/production",:force => true)
  FileUtils.rm_rf(temp_controlrepo)

  # Pull the trigger! If it's not already been pulled
  if repo.tempdir
    if File.directory?(repo.tempdir)
      if Dir["#{repo.tempdir}/#{repo.environmentpath}/production/modules/*"].empty?
        Dir.chdir("#{repo.tempdir}/#{repo.environmentpath}/production") do
          system("r10k puppetfile install --verbose")
        end
      end
    else
      raise "#{repo.tempdir} is not a directory"
    end
  end

  # Return repo.tempdir for use
  repo.tempdir
end

#verify_acceptance_test(controlrepo, test) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/controlrepo/testconfig.rb', line 81

def verify_acceptance_test(controlrepo,test)
  require 'yaml'
  nodeset = YAML.load_file(controlrepo.nodeset_file)
  test.nodes.each do |node|
    unless nodeset['HOSTS'].has_key?(node.name)
      raise "Could not find nodeset for node: #{node.name}"
    end
  end
end

#verify_spec_test(controlrepo, test) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/controlrepo/testconfig.rb', line 73

def verify_spec_test(controlrepo,test)
  test.nodes.each do |node|
    unless controlrepo.facts_files.any? { |file| file =~ /\/#{node.name}\.json/ }
      raise "Could not find factset for node: #{node.name}"
    end
  end
end

#write_acceptance_tests(location, tests) ⇒ Object



144
145
146
147
148
# File 'lib/controlrepo/testconfig.rb', line 144

def write_acceptance_tests(location, tests)
  template_dir = File.expand_path('../../templates',File.dirname(__FILE__))
  acc_test_template = File.read(File.expand_path('./acceptance_test_spec.rb.erb',template_dir))
  File.write("#{location}/acceptance_spec.rb",ERB.new(acc_test_template, nil, '-').result(binding))
end

#write_rakefile(location, pattern) ⇒ Object



156
157
158
159
160
# File 'lib/controlrepo/testconfig.rb', line 156

def write_rakefile(location, pattern)
  template_dir = File.expand_path('../../templates',File.dirname(__FILE__))
  rakefile_template = File.read(File.expand_path('./Rakefile.erb',template_dir))
  File.write("#{location}/Rakefile",ERB.new(rakefile_template, nil, '-').result(binding))
end

#write_spec_helper(location, repo) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/controlrepo/testconfig.rb', line 162

def write_spec_helper(location, repo)
  environmentpath = "#{repo.tempdir}/#{repo.environmentpath}"
  modulepath = repo.config['modulepath']
  modulepath.delete("$basemodulepath")
  modulepath.map! do |path|
    "#{environmentpath}/production/#{path}"
  end
  modulepath = modulepath.join(":")
  repo.temp_modulepath = modulepath

  # Use an ERB template to write a spec test
  template_dir = File.expand_path('../../templates',File.dirname(__FILE__))
  spec_helper_template = File.read(File.expand_path('./spec_helper.rb.erb',template_dir))
  File.write("#{location}/spec_helper.rb",ERB.new(spec_helper_template, nil, '-').result(binding))
end

#write_spec_helper_acceptance(location, repo) ⇒ Object



150
151
152
153
154
# File 'lib/controlrepo/testconfig.rb', line 150

def write_spec_helper_acceptance(location, repo)
  template_dir = File.expand_path('../../templates',File.dirname(__FILE__))
  spec_heler_acc_template = File.read(File.expand_path('./spec_helper_acceptance.rb.erb',template_dir))
  File.write("#{location}/spec_helper_acceptance.rb",ERB.new(spec_heler_acc_template, nil, '-').result(binding))
end

#write_spec_test(location, test) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/controlrepo/testconfig.rb', line 136

def write_spec_test(location, test)
  # Use an ERB template to write a spec test
  template_dir = File.expand_path('../../templates',File.dirname(__FILE__))
  spec_template = File.read(File.expand_path('./test_spec.rb.erb',template_dir))
  randomness = (0...6).map { (65 + rand(26)).chr }.join
  File.write("#{location}/#{randomness}_#{test.to_s}_spec.rb",ERB.new(spec_template, nil, '-').result(binding))
end