Add option to configure karaf logging
[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, 8080)
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 /log4j.rootLogger=INFO, stdout, osgi:*/ }
226       its(:content) { should match /log4j.appender.stdout.direct=true/ }
227     end
228   else
229     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
230       it { should be_file }
231       it { should be_owned_by 'odl' }
232       it { should be_grouped_into 'odl' }
233       its(:content) { should match /^log4j.appender.out.maxFileSize=#{log_max_size}/ }
234       its(:content) { should match /^log4j.appender.out.maxBackupIndex=#{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, 8080)
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       describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
297         it { should be_file }
298         it { should be_owned_by 'odl' }
299         it { should be_grouped_into 'odl' }
300         its(:content) { should match /^log4j.logger.#{logger}=#{level}/ }
301       end
302     end
303   end
304 end
305
306 # Shared function for validations related to ODL OVSDB HA config
307 def enable_ha_validations(options = {})
308   # NB: This param default should match the one used by the opendaylight
309   #   class, which is defined in opendaylight::params
310   # TODO: Remove this possible source of bugs^^
311   enable_ha = options.fetch(:enable_ha, false)
312   ha_node_ips = options.fetch(:ha_node_ips, [])
313   odl_bind_ip = options.fetch(:odl_bind_ip, '0.0.0.0')
314   ha_db_modules = options.fetch(:ha_db_modules, { 'default' => false })
315   # HA_NODE_IPS size
316   ha_node_count = ha_node_ips.size
317
318   if (enable_ha) && (ha_node_count < 2)
319     # Check for HA_NODE_COUNT < 2
320     fail("Number of HA nodes less than 2: #{ha_node_count} and HA Enabled")
321   end
322
323   if enable_ha
324     ha_node_index = ha_node_ips.index(odl_bind_ip)
325     describe file('/opt/opendaylight/configuration/initial/akka.conf') do
326       it { should be_file }
327       it { should be_owned_by 'odl' }
328       it { should be_grouped_into 'odl' }
329       its(:content) { should match /roles\s*=\s*\["member-#{ha_node_index}"\]/ }
330     end
331
332     ha_db_modules.each do |mod, urn|
333       describe file('/opt/opendaylight/configuration/initial/module-shards.conf') do
334         it { should be_file }
335         it { should be_owned_by 'odl' }
336         it { should be_grouped_into 'odl' }
337         its(:content) { should match /name = "#{mod}"/ }
338       end
339
340       if mod == 'default'
341         describe file('/opt/opendaylight/configuration/initial/modules.conf') do
342           it { should be_file }
343           it { should be_owned_by 'odl' }
344           it { should be_grouped_into 'odl' }
345         end
346       else
347         describe file('/opt/opendaylight/configuration/initial/modules.conf') do
348           it { should be_file }
349           it { should be_owned_by 'odl' }
350           it { should be_grouped_into 'odl' }
351           its(:content) { should match /name = "#{mod}"/ }
352           its(:content) { should match /namespace = "#{urn}"/ }
353         end
354       end
355     end
356   end
357 end
358
359 # Shared function that handles validations specific to RPM-type installs
360 def rpm_validations()
361   rpm_repo = ENV['RPM_REPO']
362
363   describe yumrepo('opendaylight') do
364     it { should exist }
365     it { should be_enabled }
366   end
367
368   describe package('opendaylight') do
369     it { should be_installed }
370   end
371 end
372
373 # Shared function that handles validations specific to Deb-type installs
374 def deb_validations()
375   deb_repo = ENV['DEB_REPO']
376   # Check ppa
377   # Docs: http://serverspec.org/resource_types.html#ppa
378   describe ppa(deb_repo) do
379     it { should exist }
380     it { should be_enabled }
381   end
382
383   describe package('opendaylight') do
384     it { should be_installed }
385   end
386 end
387
388 # Shared function for validations related to username/password
389 def username_password_validations(options = {})
390   # NB: This param default should match the one used by the opendaylight
391   #   class, which is defined in opendaylight::params
392   # TODO: Remove this possible source of bugs^^
393   odl_username = options.fetch(:username, 'admin')
394   odl_password = options.fetch(:password, 'admin')
395   odl_check_url = 'http://127.0.0.1:8080/restconf'
396
397   describe file('/opt/opendaylight/data/idmlight.db.mv.db') do
398     it { should be_file }
399   end
400
401   describe command("sleep 180 && curl -o /dev/null --fail --silent --head -u #{odl_username}:#{odl_password} #{odl_check_url}") do
402     its(:exit_status) { should eq 0 }
403   end
404 end
405
406 # Shared function for validations related to the SNAT config file
407 def snat_mechanism_validations(options = {})
408   # NB: This param default should match the one used by the opendaylight
409   #   class, which is defined in opendaylight::params
410   # TODO: Remove this possible source of bugs^^
411   snat_mechanism = options.fetch(:snat_mechanism, 'controller')
412
413   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/netvirt-natservice-config.xml') do
414     it { should be_file }
415     it { should be_owned_by 'odl' }
416     it { should be_grouped_into 'odl' }
417     its(:content) { should match /<nat-mode>#{snat_mechanism}<\/nat-mode>/ }
418   end
419 end
420
421 # Shared function for validations related to SFC
422 def sfc_validations()
423   # NB: This param default should match the one used by the opendaylight
424   #   class, which is defined in opendaylight::params
425   # TODO: Remove this possible source of bugs^^
426
427   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/netvirt-elanmanager-config.xml') do
428     it { should be_file }
429     it { should be_owned_by 'odl' }
430     it { should be_grouped_into 'odl' }
431     its(:content) { should match /<use-of-tunnels>true<\/use-of-tunnels>/ }
432   end
433
434   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/genius-itm-config.xml') do
435     it { should be_file }
436     it { should be_owned_by 'odl' }
437     it { should be_grouped_into 'odl' }
438     its(:content) { should match /<gpe-extension-enabled>true<\/gpe-extension-enabled>/ }
439   end
440 end
441
442 def websocket_address_validations(options = {})
443   # NB: This param default should match the one used by the opendaylight
444   #   class, which is defined in opendaylight::params
445   # TODO: Remove this possible source of bugs^^
446   odl_bind_ip = options.fetch(:odl_bind_ip, '0.0.0.0')
447
448   describe file('/opt/opendaylight/etc/opendaylight/karaf/10-rest-connector.xml') do
449     it { should be_file }
450     it { should be_owned_by 'odl' }
451     it { should be_grouped_into 'odl' }
452     its(:content) { should match /<websocket-address>#{odl_bind_ip}<\/websocket-address>/ }
453   end
454 end
455
456 def tls_validations(options = {})
457   # NB: This param default should match the one used by the opendaylight
458   #   class, which is defined in opendaylight::params
459   # TODO: Remove this possible source of bugs^^
460   tls_keystore_password = options.fetch(:tls_keystore_password)
461   odl_rest_port = options.fetch(:odl_rest_port, 8080)
462
463   describe file('/opt/opendaylight/etc/org.ops4j.pax.web.cfg') do
464     it { should be_file }
465     it { should be_owned_by 'odl' }
466     it { should be_grouped_into 'odl' }
467     its(:content) { should match /org.osgi.service.http.port.secure = #{odl_rest_port}/ }
468     its(:content) { should match /org.ops4j.pax.web.ssl.keystore = configuration\/ssl\/ctl.jks/ }
469     its(:content) { should match /org.ops4j.pax.web.ssl.password = #{tls_keystore_password}/ }
470     its(:content) { should match /org.ops4j.pax.web.ssl.keypassword = #{tls_keystore_password}/ }
471     its(:content) { should match /org.osgi.service.http.secure.enabled = true/ }
472   end
473
474   describe file('/opt/opendaylight/etc/org.opendaylight.ovsdb.library.cfg') do
475     it { should be_file }
476     it { should be_owned_by 'odl' }
477     it { should be_grouped_into 'odl' }
478     its(:content) { should match /use-ssl = true/ }
479   end
480
481   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/default-openflow-connection-config.xml') do
482     it { should be_file }
483     it { should be_owned_by 'odl' }
484     it { should be_grouped_into 'odl' }
485     its(:content) { should match /<keystore-password>#{tls_keystore_password}<\/keystore-password>/ }
486     its(:content) { should match /<truststore-password>#{tls_keystore_password}<\/truststore-password>/ }
487     its(:content) { should match /<transport-protocol>TLS<\/transport-protocol>/ }
488   end
489
490   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/aaa-cert-config.xml') do
491     it { should be_file }
492     it { should be_owned_by 'odl' }
493     it { should be_grouped_into 'odl' }
494     its(:content) { should match /<store-password>#{tls_keystore_password}<\/store-password>/ }
495     its(:content) { should match /<use-mdsal>false<\/use-mdsal>/ }
496   end
497
498   describe file('/opt/opendaylight/etc/jetty.xml') do
499     it { should be_file }
500     it { should be_owned_by 'odl' }
501     it { should be_grouped_into 'odl' }
502     its(:content) { should match /<Property name="jetty.secure.port" default="#{odl_rest_port}" \/>/ }
503   end
504 end