INTPAK-123 Move to log4j2
[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     install_puppet_agent_on(host, puppet_collection: "pc1")
9   end
10 end
11
12 RSpec.configure do |c|
13   # Project root
14   proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
15
16   # Readable test descriptions
17   c.formatter = :documentation
18
19   # Configure all nodes in nodeset
20   c.before :suite do
21     # Install opendaylight module on any/all Beaker hosts
22     # TODO: Should this be done in host.each loop?
23     puppet_module_install(:source => proj_root, :module_name => 'opendaylight')
24     hosts.each do |host|
25       # Install stdlib, a dependency of the odl mod
26       on host, puppet('module', 'install', 'puppetlabs-stdlib'), { :acceptable_exit_codes => [0] }
27       # Install apt, a dependency of the deb install method
28       on host, puppet('module', 'install', 'puppetlabs-apt'), { :acceptable_exit_codes => [0] }
29     end
30   end
31 end
32
33 #
34 # NB: These are a library of helper fns used by the Beaker tests
35 #
36
37 # NB: There are a large number of helper functions used in these tests.
38 # They make this code much more friendly, but may need to be referenced.
39 # The serverspec helpers (`should`, `be_running`...) are documented here:
40 #   http://serverspec.org/resource_types.html
41
42 def install_odl(options = {})
43   # Install params are passed via environment var, set in Rakefile
44   # Changing the installed version of ODL via `puppet apply` is not supported
45   # by puppet-odl, so it's not possible to vary these params in the same
46   # Beaker test run. Do a different run passing different env vars.
47   rpm_repo = ENV['RPM_REPO']
48   deb_repo = ENV['DEB_REPO']
49
50   # NB: These param defaults should match the ones used by the opendaylight
51   #   class, which are defined in opendaylight::params
52   # TODO: Remove this possible source of bugs^^
53   # Extract params if given, defaulting to odl class defaults if not
54   extra_features = options.fetch(:extra_features, ['odl-restconf'])
55   default_features = options.fetch(:default_features, ['standard', 'wrap', 'ssh'])
56   odl_rest_port = options.fetch(:odl_rest_port, 8181)
57   odl_bind_ip = options.fetch(:odl_bind_ip, '0.0.0.0')
58   log_levels = options.fetch(:log_levels, {})
59   enable_ha = options.fetch(:enable_ha, false)
60   ha_node_ips = options.fetch(:ha_node_ips, [])
61   ha_node_index = options.fetch(:ha_node_index, 0)
62   ha_db_modules = options.fetch(:ha_db_modules, { 'default' => false })
63   username = options.fetch(:username, 'admin')
64   password = options.fetch(:password, 'admin')
65   log_max_size = options.fetch(:log_max_size, '10GB')
66   log_max_rollover = options.fetch(:log_max_rollover, 2)
67   snat_mechanism = options.fetch(:snat_mechanism, 'controller')
68   enable_tls = options.fetch(:enable_tls, false)
69   tls_keystore_password = options.fetch(:tls_keystore_password, 'dummypass')
70   log_mechanism = options.fetch(:log_mechanism, 'file')
71
72   # Build script for consumption by Puppet apply
73   it 'should work idempotently with no errors' do
74     pp = <<-EOS
75     class { 'opendaylight':
76       rpm_repo => '#{rpm_repo}',
77       deb_repo => '#{deb_repo}',
78       default_features => #{default_features},
79       extra_features => #{extra_features},
80       odl_rest_port => #{odl_rest_port},
81       odl_bind_ip => '#{odl_bind_ip}',
82       enable_ha => #{enable_ha},
83       ha_node_ips => #{ha_node_ips},
84       ha_node_index => #{ha_node_index},
85       ha_db_modules => #{ha_db_modules},
86       log_levels => #{log_levels},
87       username => #{username},
88       password => #{password},
89       log_max_size => '#{log_max_size}',
90       log_max_rollover => #{log_max_rollover},
91       snat_mechanism => #{snat_mechanism},
92       enable_tls => #{enable_tls},
93       tls_keystore_password => #{tls_keystore_password},
94       log_mechanism => #{log_mechanism},
95     }
96     EOS
97
98     # Apply our Puppet manifest on the Beaker host
99     apply_manifest(pp, :catch_failures => true)
100
101     # Not checking for idempotence because of false failures
102     # related to package manager cache updates outputting to
103     # stdout and different IDs for the puppet manifest apply.
104     # I think this is a limitation in how Beaker can check
105     # for changes, not a problem with the Puppet module.
106     end
107 end
108
109 # Shared function that handles generic validations
110 # These should be common for all odl class param combos
111 def generic_validations()
112   # Verify ODL's directory
113   describe file('/opt/opendaylight/') do
114     it { should be_directory }
115     it { should be_owned_by 'odl' }
116     it { should be_grouped_into 'odl' }
117   end
118
119   # Verify ODL's systemd service
120   describe service('opendaylight') do
121     it { should be_enabled }
122     it { should be_enabled.with_level(3) }
123     it { should be_running.under('systemd') }
124   end
125
126   # Creation handled by RPM or Deb
127   describe user('odl') do
128     it { should exist }
129     it { should belong_to_group 'odl' }
130     # NB: This really shouldn't have a slash at the end!
131     #     The home dir set by the RPM is `/opt/opendaylight`.
132     #     Since we use the trailing slash elsewhere else, this
133     #     may look like a style issue. It isn't! It will make
134     #     Beaker tests fail if it ends with a `/`. A future
135     #     version of the ODL RPM may change this.
136     it { should have_home_directory '/opt/opendaylight' }
137   end
138
139   # Creation handled by RPM or Deb
140   describe group('odl') do
141     it { should exist }
142   end
143
144   # This should not be the odl user's home dir
145   describe file('/home/odl') do
146     # Home dir shouldn't be created for odl user
147     it { should_not be_directory }
148   end
149
150   # OpenDaylight will appear as a Java process
151   describe process('java') do
152     it { should be_running }
153   end
154
155   # Should contain Karaf features config file
156   describe file('/opt/opendaylight/etc/org.apache.karaf.features.cfg') do
157     it { should be_file }
158     it { should be_owned_by 'odl' }
159     it { should be_grouped_into 'odl' }
160   end
161
162   # Should contain ODL NB port config file
163   describe file('/opt/opendaylight/etc/jetty.xml') do
164     it { should be_file }
165     it { should be_owned_by 'odl' }
166     it { should be_grouped_into 'odl' }
167   end
168
169   # Should contain log level config file
170   describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
171     it { should be_file }
172     it { should be_owned_by 'odl' }
173     it { should be_grouped_into 'odl' }
174   end
175
176   if ['centos-7', 'centos-7-docker'].include? ENV['RS_SET']
177     # Validations for modern Red Hat family OSs
178
179     # Verify ODL systemd .service file
180     describe file('/usr/lib/systemd/system/opendaylight.service') do
181       it { should be_file }
182       it { should be_owned_by 'root' }
183       it { should be_grouped_into 'root' }
184       it { should be_mode '644' }
185     end
186
187     # Java 8 should be installed
188     describe package('java-1.8.0-openjdk') do
189       it { should be_installed }
190     end
191
192   # Ubuntu 16.04 specific validation
193   elsif ['ubuntu-16', 'ubuntu-16-docker'].include? ENV['RS_SET']
194
195     # Verify ODL systemd .service file
196     describe file('/lib/systemd/system/opendaylight.service') do
197       it { should be_file }
198       it { should be_owned_by 'root' }
199       it { should be_grouped_into 'root' }
200       it { should be_mode '644' }
201     end
202
203     # Java 8 should be installed
204     describe package('openjdk-8-jre-headless') do
205       it { should be_installed }
206     end
207
208   else
209     fail("Unexpected RS_SET (host OS): #{ENV['RS_SET']}")
210   end
211 end
212
213 # Shared function for validations related to log file settings
214 def log_settings_validations(options = {})
215   # Should contain log level config file with correct file size and rollover values
216   log_max_size = options.fetch(:log_max_size, '10GB')
217   log_max_rollover = options.fetch(:log_max_rollover, 2)
218   log_mechanism = options.fetch(:log_mechanism, 'file')
219
220   if log_mechanism == 'console'
221     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
222       it { should be_file }
223       it { should be_owned_by 'odl' }
224       it { should be_grouped_into 'odl' }
225       its(:content) { should match /^karaf.log.console=INFO/ }
226     end
227   else
228     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
229       it { should be_file }
230       it { should be_owned_by 'odl' }
231       it { should be_grouped_into 'odl' }
232       its(:content) { should match /^log4j2.appender.rolling.policies.size.size = #{log_max_size}/ }
233       its(:content) { should match /^log4j2.appender.rolling.strategy.type = DefaultRolloverStrategy/ }
234       its(:content) { should match /^log4j2.appender.rolling.strategy.max = #{log_max_rollover}/ }
235     end
236   end
237 end
238
239 # Shared function for validations related to the Karaf config file
240 def karaf_config_validations(options = {})
241   # NB: These param defaults should match the ones used by the opendaylight
242   #   class, which are defined in opendaylight::params
243   # TODO: Remove this possible source of bugs^^
244   extra_features = options.fetch(:extra_features, [])
245   default_features = options.fetch(:default_features, ['standard', 'wrap', 'ssh'])
246
247   # Create one list of all of the features
248   features = default_features + extra_features
249
250   describe file('/opt/opendaylight/etc/org.apache.karaf.features.cfg') do
251     it { should be_file }
252     it { should be_owned_by 'odl' }
253     it { should be_grouped_into 'odl' }
254     its(:content) { should match /^featuresBoot=#{features.join(",")}/ }
255   end
256 end
257
258 # Shared function for validations related to the ODL REST port config file
259 def port_config_validations(options = {})
260   # NB: This param default should match the one used by the opendaylight
261   #   class, which is defined in opendaylight::params
262   # TODO: Remove this possible source of bugs^^
263   odl_rest_port = options.fetch(:odl_rest_port, 8181)
264
265   describe file('/opt/opendaylight/etc/jetty.xml') do
266     it { should be_file }
267     it { should be_owned_by 'odl' }
268     it { should be_grouped_into 'odl' }
269     its(:content) { should match /Property name="jetty.port" default="#{odl_rest_port}"/ }
270   end
271 end
272
273 # Shared function for validations related to custom logging verbosity
274 def log_level_validations(options = {})
275   # NB: This param default should match the one used by the opendaylight
276   #   class, which is defined in opendaylight::params
277   # TODO: Remove this possible source of bugs^^
278   log_levels = options.fetch(:log_levels, {})
279
280   if log_levels.empty?
281     # Should contain log level config file
282     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
283       it { should be_file }
284       it { should be_owned_by 'odl' }
285       it { should be_grouped_into 'odl' }
286     end
287   else
288     # Should contain log level config file
289     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
290       it { should be_file }
291       it { should be_owned_by 'odl' }
292       it { should be_grouped_into 'odl' }
293     end
294     # Verify each custom log level config entry
295     log_levels.each_pair do |logger, level|
296       underscored_version = "#{logger}".gsub('.', '_')
297       describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
298         it { should be_file }
299         it { should be_owned_by 'odl' }
300         it { should be_grouped_into 'odl' }
301         its(:content) { should match /^log4j2.logger.#{underscored_version}.level = #{level}/ }
302         its(:content) { should match /^log4j2.logger.#{underscored_version}.name = #{logger}/ }
303       end
304     end
305   end
306 end
307
308 # Shared function for validations related to ODL OVSDB HA config
309 def enable_ha_validations(options = {})
310   # NB: This param default should match the one used by the opendaylight
311   #   class, which is defined in opendaylight::params
312   # TODO: Remove this possible source of bugs^^
313   enable_ha = options.fetch(:enable_ha, false)
314   ha_node_ips = options.fetch(:ha_node_ips, [])
315   odl_bind_ip = options.fetch(:odl_bind_ip, '0.0.0.0')
316   ha_db_modules = options.fetch(:ha_db_modules, { 'default' => false })
317   # HA_NODE_IPS size
318   ha_node_count = ha_node_ips.size
319
320   if (enable_ha) && (ha_node_count < 2)
321     # Check for HA_NODE_COUNT < 2
322     fail("Number of HA nodes less than 2: #{ha_node_count} and HA Enabled")
323   end
324
325   if enable_ha
326     ha_node_index = ha_node_ips.index(odl_bind_ip)
327     describe file('/opt/opendaylight/configuration/initial/akka.conf') do
328       it { should be_file }
329       it { should be_owned_by 'odl' }
330       it { should be_grouped_into 'odl' }
331       its(:content) { should match /roles\s*=\s*\["member-#{ha_node_index}"\]/ }
332     end
333
334     ha_db_modules.each do |mod, urn|
335       describe file('/opt/opendaylight/configuration/initial/module-shards.conf') do
336         it { should be_file }
337         it { should be_owned_by 'odl' }
338         it { should be_grouped_into 'odl' }
339         its(:content) { should match /name = "#{mod}"/ }
340       end
341
342       if mod == 'default'
343         describe file('/opt/opendaylight/configuration/initial/modules.conf') do
344           it { should be_file }
345           it { should be_owned_by 'odl' }
346           it { should be_grouped_into 'odl' }
347         end
348       else
349         describe file('/opt/opendaylight/configuration/initial/modules.conf') do
350           it { should be_file }
351           it { should be_owned_by 'odl' }
352           it { should be_grouped_into 'odl' }
353           its(:content) { should match /name = "#{mod}"/ }
354           its(:content) { should match /namespace = "#{urn}"/ }
355         end
356       end
357     end
358   end
359 end
360
361 # Shared function that handles validations specific to RPM-type installs
362 def rpm_validations()
363   rpm_repo = ENV['RPM_REPO']
364
365   describe yumrepo('opendaylight') do
366     it { should exist }
367     it { should be_enabled }
368   end
369
370   describe package('opendaylight') do
371     it { should be_installed }
372   end
373 end
374
375 # Shared function that handles validations specific to Deb-type installs
376 def deb_validations()
377   deb_repo = ENV['DEB_REPO']
378   # Check ppa
379   # Docs: http://serverspec.org/resource_types.html#ppa
380   describe ppa(deb_repo) do
381     it { should exist }
382     it { should be_enabled }
383   end
384
385   describe package('opendaylight') do
386     it { should be_installed }
387   end
388 end
389
390 # Shared function for validations related to username/password
391 def username_password_validations(options = {})
392   # NB: This param default should match the one used by the opendaylight
393   #   class, which is defined in opendaylight::params
394   # TODO: Remove this possible source of bugs^^
395   odl_username = options.fetch(:username, 'admin')
396   odl_password = options.fetch(:password, 'admin')
397   odl_check_url = 'http://127.0.0.1:8181/restconf'
398
399   describe file('/opt/opendaylight/data/idmlight.db.mv.db') do
400     it { should be_file }
401   end
402
403   describe command("loop_count=0; until [[ \$loop_count -ge 300 ]]; do curl -o /dev/null --fail --silent --head -u #{odl_username}:#{odl_password} #{odl_check_url} && break; loop_count=\$[\$loop_count+1]; sleep 1; done; echo \"Waited \$loop_count seconds for ODL to become active\"") do
404     its(:exit_status) { should eq 0 }
405   end
406
407   describe command("curl -o /dev/null --fail --silent --head -u #{odl_username}:#{odl_password} #{odl_check_url}") do
408     its(:exit_status) { should eq 0 }
409   end
410 end
411
412 # Shared function for validations related to the SNAT config file
413 def snat_mechanism_validations(options = {})
414   # NB: This param default should match the one used by the opendaylight
415   #   class, which is defined in opendaylight::params
416   # TODO: Remove this possible source of bugs^^
417   snat_mechanism = options.fetch(:snat_mechanism, 'controller')
418
419   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/netvirt-natservice-config.xml') do
420     it { should be_file }
421     it { should be_owned_by 'odl' }
422     it { should be_grouped_into 'odl' }
423     its(:content) { should match /<nat-mode>#{snat_mechanism}<\/nat-mode>/ }
424   end
425 end
426
427 # Shared function for validations related to SFC
428 def sfc_validations()
429   # NB: This param default should match the one used by the opendaylight
430   #   class, which is defined in opendaylight::params
431   # TODO: Remove this possible source of bugs^^
432
433   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/netvirt-elanmanager-config.xml') do
434     it { should be_file }
435     it { should be_owned_by 'odl' }
436     it { should be_grouped_into 'odl' }
437     its(:content) { should match /<use-of-tunnels>true<\/use-of-tunnels>/ }
438   end
439
440   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/genius-itm-config.xml') do
441     it { should be_file }
442     it { should be_owned_by 'odl' }
443     it { should be_grouped_into 'odl' }
444     its(:content) { should match /<gpe-extension-enabled>true<\/gpe-extension-enabled>/ }
445   end
446 end
447
448 def websocket_address_validations(options = {})
449   # NB: This param default should match the one used by the opendaylight
450   #   class, which is defined in opendaylight::params
451   # TODO: Remove this possible source of bugs^^
452   odl_bind_ip = options.fetch(:odl_bind_ip, '0.0.0.0')
453
454   if not odl_bind_ip.eql? '0.0.0.0'
455     describe file('/opt/opendaylight/etc/org.opendaylight.restconf.cfg') do
456       it { should be_file }
457       it { should be_owned_by 'odl' }
458       it { should be_grouped_into 'odl' }
459       its(:content) { should match /^websocket-address=#{odl_bind_ip}/ }
460     end
461   else
462     describe file('/opt/opendaylight/etc/org.opendaylight.restconf.cfg') do
463       it { should be_file }
464     end
465   end
466 end
467
468 def tls_validations(options = {})
469   # NB: This param default should match the one used by the opendaylight
470   #   class, which is defined in opendaylight::params
471   # TODO: Remove this possible source of bugs^^
472   tls_keystore_password = options.fetch(:tls_keystore_password)
473   odl_rest_port = options.fetch(:odl_rest_port, 8181)
474
475   describe file('/opt/opendaylight/etc/org.ops4j.pax.web.cfg') do
476     it { should be_file }
477     it { should be_owned_by 'odl' }
478     it { should be_grouped_into 'odl' }
479     its(:content) { should match /org.osgi.service.http.port.secure = #{odl_rest_port}/ }
480     its(:content) { should match /org.ops4j.pax.web.ssl.keystore = configuration\/ssl\/ctl.jks/ }
481     its(:content) { should match /org.ops4j.pax.web.ssl.password = #{tls_keystore_password}/ }
482     its(:content) { should match /org.ops4j.pax.web.ssl.keypassword = #{tls_keystore_password}/ }
483     its(:content) { should match /org.osgi.service.http.secure.enabled = true/ }
484   end
485
486   describe file('/opt/opendaylight/etc/org.opendaylight.ovsdb.library.cfg') do
487     it { should be_file }
488     it { should be_owned_by 'odl' }
489     it { should be_grouped_into 'odl' }
490     its(:content) { should match /use-ssl = true/ }
491   end
492
493   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/default-openflow-connection-config.xml') do
494     it { should be_file }
495     it { should be_owned_by 'odl' }
496     it { should be_grouped_into 'odl' }
497     its(:content) { should match /<keystore-password>#{tls_keystore_password}<\/keystore-password>/ }
498     its(:content) { should match /<truststore-password>#{tls_keystore_password}<\/truststore-password>/ }
499     its(:content) { should match /<transport-protocol>TLS<\/transport-protocol>/ }
500   end
501
502   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/aaa-cert-config.xml') do
503     it { should be_file }
504     it { should be_owned_by 'odl' }
505     it { should be_grouped_into 'odl' }
506     its(:content) { should match /<store-password>#{tls_keystore_password}<\/store-password>/ }
507     its(:content) { should match /<use-mdsal>false<\/use-mdsal>/ }
508   end
509
510   describe file('/opt/opendaylight/etc/jetty.xml') do
511     it { should be_file }
512     it { should be_owned_by 'odl' }
513     it { should be_grouped_into 'odl' }
514     its(:content) { should match /<Property name="jetty.secure.port" default="#{odl_rest_port}" \/>/ }
515   end
516 end