Fix false idempotence failure in Beaker tests
[integration/packaging/puppet-opendaylight.git] / spec / spec_helper_acceptance.rb
1 require 'beaker-rspec/spec_helper'
2 require 'beaker-rspec/helpers/serverspec'
3
4 # Install Puppet on all Beaker hosts
5 unless ENV['BEAKER_provision'] == 'no'
6   hosts.each do |host|
7     # Install Puppet
8     if host.is_pe?
9       install_pe
10     else
11       install_puppet
12     end
13   end
14 end
15
16 RSpec.configure do |c|
17   # Project root
18   proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
19
20   # Readable test descriptions
21   c.formatter = :documentation
22
23   # Configure all nodes in nodeset
24   c.before :suite do
25     # Install opendaylight module on any/all Beaker hosts
26     # TODO: Should this be done in host.each loop?
27     puppet_module_install(:source => proj_root, :module_name => 'opendaylight')
28     hosts.each do |host|
29       # Install stdlib, a dependency of the odl mod
30       on host, puppet('module', 'install', 'puppetlabs-stdlib'), { :acceptable_exit_codes => [0] }
31       # Install archive, a dependency of the odl mod use for tarball-type installs
32       on host, puppet('module', 'install', 'camptocamp-archive'), { :acceptable_exit_codes => [0] }
33       # Install Java Puppet mod, a dependency of the tarball install method
34       on host, puppet('module', 'install', 'puppetlabs-java'), { :acceptable_exit_codes => [0] }
35     end
36   end
37 end
38
39 #
40 # NB: These are a library of helper fns used by the Beaker tests
41 #
42
43 # NB: There are a large number of helper functions used in these tests.
44 # They make this code much more friendly, but may need to be referenced.
45 # The serverspec helpers (`should`, `be_running`...) are documented here:
46 #   http://serverspec.org/resource_types.html
47
48 def install_odl(options = {})
49   # NB: These param defaults should match the ones used by the opendaylight
50   #   class, which are defined in opendaylight::params
51   # TODO: Remove this possible source of bugs^^
52   # Extract params if given, defaulting to odl class defaults if not
53   # Default install method is passed via environment var, set in Rakefile
54   install_method = options.fetch(:install_method, ENV['INSTALL_METHOD'])
55   extra_features = options.fetch(:extra_features, [])
56   default_features = options.fetch(:default_features,
57     ['config', 'standard', 'region', 'package', 'kar', 'ssh', 'management'])
58   odl_rest_port = options.fetch(:odl_rest_port, 8080)
59   log_levels = options.fetch(:log_levels, {})
60   enable_l3 = options.fetch(:enable_l3, 'no')
61
62   # Build script for consumption by Puppet apply
63   it 'should work idempotently with no errors' do
64     pp = <<-EOS
65     class { 'opendaylight':
66       install_method => #{install_method},
67       default_features => #{default_features},
68       extra_features => #{extra_features},
69       odl_rest_port=> #{odl_rest_port},
70       enable_l3=> #{enable_l3},
71       log_levels=> #{log_levels},
72     }
73     EOS
74
75     # Apply our Puppet manifest on the Beaker host
76     apply_manifest(pp, :catch_failures => true)
77
78     # Not checking for idempotence because of false failures
79     # related to package manager cache updates outputting to
80     # stdout and different IDs for the puppet manifest apply.
81     # I think this is a limitation in how Beaker can check
82     # for changes, not a problem with the Puppet module.
83     end
84 end
85
86 # Shared function that handles generic validations
87 # These should be common for all odl class param combos
88 def generic_validations()
89   # Verify ODL's directory
90   describe file('/opt/opendaylight/') do
91     it { should be_directory }
92     it { should be_owned_by 'odl' }
93     it { should be_grouped_into 'odl' }
94   end
95
96   # Verify ODL's systemd service
97   describe service('opendaylight') do
98     it { should be_enabled }
99     it { should be_enabled.with_level(3) }
100     it { should be_running }
101   end
102
103   # Creation handled by RPM, or Puppet during tarball installs
104   describe user('odl') do
105     it { should exist }
106     it { should belong_to_group 'odl' }
107     # NB: This really shouldn't have a slash at the end!
108     #     The home dir set by the RPM is `/opt/opendaylight`.
109     #     Since we use the trailing slash elsewhere else, this
110     #     may look like a style issue. It isn't! It will make
111     #     Beaker tests fail if it ends with a `/`. A future
112     #     version of the ODL RPM may change this.
113     it { should have_home_directory '/opt/opendaylight' }
114   end
115
116   # Creation handled by RPM, or Puppet during tarball installs
117   describe group('odl') do
118     it { should exist }
119   end
120
121   # This should not be the odl user's home dir
122   describe file('/home/odl') do
123     # Home dir shouldn't be created for odl user
124     it { should_not be_directory }
125   end
126
127   # OpenDaylight will appear as a Java process
128   describe process('java') do
129     it { should be_running }
130   end
131
132   # Should contain Karaf features config file
133   describe file('/opt/opendaylight/etc/org.apache.karaf.features.cfg') do
134     it { should be_file }
135     it { should be_owned_by 'odl' }
136     it { should be_grouped_into 'odl' }
137   end
138
139   # Should contain ODL NB port config file
140   describe file('/opt/opendaylight/etc/jetty.xml') do
141     it { should be_file }
142     it { should be_owned_by 'odl' }
143     it { should be_grouped_into 'odl' }
144   end
145
146   # Should contain log level config file
147   describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
148     it { should be_file }
149     it { should be_owned_by 'odl' }
150     it { should be_grouped_into 'odl' }
151   end
152
153   # Should contain ODL OVSDB L3 enable/disable config file
154   describe file('/opt/opendaylight/etc/custom.properties') do
155     it { should be_file }
156     it { should be_owned_by 'odl' }
157     it { should be_grouped_into 'odl' }
158   end
159
160   if ['centos-7', 'centos-7-docker', 'fedora-22', 'fedora-23-docker'].include? ENV['RS_SET']
161     # Validations for modern Red Hat family OSs
162
163     # Verify ODL systemd .service file
164     describe file('/usr/lib/systemd/system/opendaylight.service') do
165       it { should be_file }
166       it { should be_owned_by 'root' }
167       it { should be_grouped_into 'root' }
168       it { should be_mode '644' }
169     end
170
171     # Java 8 should be installed
172     describe package('java-1.8.0-openjdk') do
173       it { should be_installed }
174     end
175   elsif ['ubuntu-1404', 'ubuntu-1404-docker'].include? ENV['RS_SET']
176     # Ubuntu-specific validations
177
178     # Verify ODL Upstart config file
179     describe file('/etc/init/opendaylight.conf') do
180       it { should be_file }
181       it { should be_owned_by 'root' }
182       it { should be_grouped_into 'root' }
183       it { should be_mode '644' }
184     end
185
186     # Java 7 should be installed
187     describe package('openjdk-7-jdk') do
188       it { should be_installed }
189     end
190   else
191     fail("Unexpected RS_SET (host OS): #{ENV['RS_SET']}")
192   end
193 end
194
195 # Shared function for validations related to the Karaf config file
196 def karaf_config_validations(options = {})
197   # NB: These param defaults should match the ones used by the opendaylight
198   #   class, which are defined in opendaylight::params
199   # TODO: Remove this possible source of bugs^^
200   extra_features = options.fetch(:extra_features, [])
201   default_features = options.fetch(:default_features, ['config', 'standard', 'region',
202                                   'package', 'kar', 'ssh', 'management'])
203
204   # Create one list of all of the features
205   features = default_features + extra_features
206
207   describe file('/opt/opendaylight/etc/org.apache.karaf.features.cfg') do
208     it { should be_file }
209     it { should be_owned_by 'odl' }
210     it { should be_grouped_into 'odl' }
211     its(:content) { should match /^featuresBoot=#{features.join(",")}/ }
212   end
213 end
214
215 # Shared function for validations related to the ODL REST port config file
216 def port_config_validations(options = {})
217   # NB: This param default should match the one used by the opendaylight
218   #   class, which is defined in opendaylight::params
219   # TODO: Remove this possible source of bugs^^
220   odl_rest_port = options.fetch(:odl_rest_port, 8080)
221
222   describe file('/opt/opendaylight/etc/jetty.xml') do
223     it { should be_file }
224     it { should be_owned_by 'odl' }
225     it { should be_grouped_into 'odl' }
226     its(:content) { should match /Property name="jetty.port" default="#{odl_rest_port}"/ }
227   end
228 end
229
230 # Shared function for validations related to custom logging verbosity
231 def log_level_validations(options = {})
232   # NB: This param default should match the one used by the opendaylight
233   #   class, which is defined in opendaylight::params
234   # TODO: Remove this possible source of bugs^^
235   log_levels = options.fetch(:log_levels, {})
236
237   if log_levels.empty?
238     # Should contain log level config file
239     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
240       it { should be_file }
241       it { should be_owned_by 'odl' }
242       it { should be_grouped_into 'odl' }
243     end
244     # Should not contain custom log level config
245     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
246       it { should be_file }
247       it { should be_owned_by 'odl' }
248       it { should be_grouped_into 'odl' }
249       its(:content) { should_not match /# Log level config added by puppet-opendaylight/ }
250     end
251   else
252     # Should contain log level config file
253     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
254       it { should be_file }
255       it { should be_owned_by 'odl' }
256       it { should be_grouped_into 'odl' }
257     end
258     # Should not contain custom log level config
259     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
260       it { should be_file }
261       it { should be_owned_by 'odl' }
262       it { should be_grouped_into 'odl' }
263       its(:content) { should match /# Log level config added by puppet-opendaylight/ }
264     end
265     # Verify each custom log level config entry
266     log_levels.each_pair do |logger, level|
267       describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
268         it { should be_file }
269         it { should be_owned_by 'odl' }
270         it { should be_grouped_into 'odl' }
271         its(:content) { should match /^log4j.logger.#{logger} = #{level}/ }
272       end
273     end
274   end
275 end
276
277 # Shared function for validations related to ODL OVSDB L3 config
278 def enable_l3_validations(options = {})
279   # NB: This param default should match the one used by the opendaylight
280   #   class, which is defined in opendaylight::params
281   # TODO: Remove this possible source of bugs^^
282   enable_l3 = options.fetch(:enable_l3, 'no')
283
284   if [true, 'yes'].include? enable_l3
285     # Confirm ODL OVSDB L3 is enabled
286     describe file('/opt/opendaylight/etc/custom.properties') do
287       it { should be_file }
288       it { should be_owned_by 'odl' }
289       it { should be_grouped_into 'odl' }
290       its(:content) { should match /^ovsdb.l3.fwd.enabled=yes/ }
291     end
292   elsif [false, 'no'].include? enable_l3
293     # Confirm ODL OVSDB L3 is disabled
294     describe file('/opt/opendaylight/etc/custom.properties') do
295       it { should be_file }
296       it { should be_owned_by 'odl' }
297       it { should be_grouped_into 'odl' }
298       its(:content) { should match /^ovsdb.l3.fwd.enabled=no/ }
299     end
300   end
301 end
302
303 # Shared function that handles validations specific to RPM-type installs
304 def rpm_validations()
305   describe yumrepo('opendaylight-41-release') do
306     it { should exist }
307     it { should be_enabled }
308   end
309
310   describe package('opendaylight') do
311     it { should be_installed }
312   end
313 end
314
315 # Shared function that handles validations specific to tarball-type installs
316 def tarball_validations()
317   describe package('opendaylight') do
318     it { should_not be_installed }
319   end
320
321   # Repo checks break (not fail) when yum doesn't make sense (Ubuntu)
322   if ['centos-7', 'fedora-22', 'fedora-23-docker'].include? ENV['RS_SET']
323     describe yumrepo('opendaylight-41-release') do
324       it { should_not exist }
325       it { should_not be_enabled }
326     end
327   end
328 end