517c1739da44704778c2b5ecce9d5a4f4af4dece
[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   inherit_dscp_marking = options.fetch(:inherit_dscp_marking, false)
72   stats_polling_enabled = options.fetch(:stats_polling_enabled, false)
73
74   # Build script for consumption by Puppet apply
75   it 'should work idempotently with no errors' do
76     pp = <<-EOS
77     class { 'opendaylight':
78       rpm_repo => '#{rpm_repo}',
79       deb_repo => '#{deb_repo}',
80       default_features => #{default_features},
81       extra_features => #{extra_features},
82       odl_rest_port => #{odl_rest_port},
83       odl_bind_ip => '#{odl_bind_ip}',
84       enable_ha => #{enable_ha},
85       ha_node_ips => #{ha_node_ips},
86       ha_node_index => #{ha_node_index},
87       ha_db_modules => #{ha_db_modules},
88       log_levels => #{log_levels},
89       username => #{username},
90       password => #{password},
91       log_max_size => '#{log_max_size}',
92       log_max_rollover => #{log_max_rollover},
93       snat_mechanism => #{snat_mechanism},
94       enable_tls => #{enable_tls},
95       tls_keystore_password => #{tls_keystore_password},
96       log_mechanism => #{log_mechanism},
97       inherit_dscp_marking => #{inherit_dscp_marking},
98       stats_polling_enabled => #{stats_polling_enabled},
99     }
100     EOS
101
102     # Apply our Puppet manifest on the Beaker host
103     apply_manifest(pp, :catch_failures => true)
104
105     # Not checking for idempotence because of false failures
106     # related to package manager cache updates outputting to
107     # stdout and different IDs for the puppet manifest apply.
108     # I think this is a limitation in how Beaker can check
109     # for changes, not a problem with the Puppet module.
110     end
111 end
112
113 # Shared function that handles generic validations
114 # These should be common for all odl class param combos
115 def generic_validations(options = {})
116   java_opts = options.fetch(:java_opts, '-Djava.net.preferIPv4Stack=true')
117
118   # Verify ODL's directory
119   describe file('/opt/opendaylight/') do
120     it { should be_directory }
121     it { should be_owned_by 'odl' }
122     it { should be_grouped_into 'odl' }
123   end
124
125   # Verify ODL's systemd service
126   describe service('opendaylight') do
127     it { should be_enabled }
128     it { should be_enabled.with_level(3) }
129     it { should be_running.under('systemd') }
130   end
131
132   # Creation handled by RPM or Deb
133   describe user('odl') do
134     it { should exist }
135     it { should belong_to_group 'odl' }
136     # NB: This really shouldn't have a slash at the end!
137     #     The home dir set by the RPM is `/opt/opendaylight`.
138     #     Since we use the trailing slash elsewhere else, this
139     #     may look like a style issue. It isn't! It will make
140     #     Beaker tests fail if it ends with a `/`. A future
141     #     version of the ODL RPM may change this.
142     it { should have_home_directory '/opt/opendaylight' }
143   end
144
145   # Creation handled by RPM or Deb
146   describe group('odl') do
147     it { should exist }
148   end
149
150   # This should not be the odl user's home dir
151   describe file('/home/odl') do
152     # Home dir shouldn't be created for odl user
153     it { should_not be_directory }
154   end
155
156   # OpenDaylight will appear as a Java process
157   describe process('java') do
158     it { should be_running }
159   end
160
161   # Should contain Karaf features config file
162   describe file('/opt/opendaylight/etc/org.apache.karaf.features.cfg') do
163     it { should be_file }
164     it { should be_owned_by 'odl' }
165     it { should be_grouped_into 'odl' }
166   end
167
168   # Should contain karaf file with Java options set
169   describe file('/opt/opendaylight/bin/karaf') do
170     it { should be_file }
171     it { should be_owned_by 'odl' }
172     it { should be_grouped_into 'odl' }
173     its(:content) { should match /^EXTRA_JAVA_OPTS=#{java_opts}$/ }
174   end
175
176   # Should contain ODL NB port config file
177   describe file('/opt/opendaylight/etc/jetty.xml') do
178     it { should be_file }
179     it { should be_owned_by 'odl' }
180     it { should be_grouped_into 'odl' }
181   end
182
183   # Should contain log level config file
184   describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
185     it { should be_file }
186     it { should be_owned_by 'odl' }
187     it { should be_grouped_into 'odl' }
188   end
189
190   if ['centos-7', 'centos-7-docker'].include? ENV['RS_SET']
191     # Validations for modern Red Hat family OSs
192
193     # Verify ODL systemd .service file
194     describe file('/usr/lib/systemd/system/opendaylight.service') do
195       it { should be_file }
196       it { should be_owned_by 'root' }
197       it { should be_grouped_into 'root' }
198       it { should be_mode '644' }
199     end
200
201     # Java 8 should be installed
202     describe package('java-1.8.0-openjdk') do
203       it { should be_installed }
204     end
205
206   # Ubuntu 16.04 specific validation
207   elsif ['ubuntu-16', 'ubuntu-16-docker'].include? ENV['RS_SET']
208
209     # Verify ODL systemd .service file
210     describe file('/lib/systemd/system/opendaylight.service') do
211       it { should be_file }
212       it { should be_owned_by 'root' }
213       it { should be_grouped_into 'root' }
214       it { should be_mode '644' }
215     end
216
217     # Java 8 should be installed
218     describe package('openjdk-8-jre-headless') do
219       it { should be_installed }
220     end
221
222   else
223     fail("Unexpected RS_SET (host OS): #{ENV['RS_SET']}")
224   end
225 end
226
227 # Shared function for validations related to log file settings
228 def log_settings_validations(options = {})
229   # Should contain log level config file with correct file size and rollover values
230   log_max_size = options.fetch(:log_max_size, '10GB')
231   log_max_rollover = options.fetch(:log_max_rollover, 2)
232   log_mechanism = options.fetch(:log_mechanism, 'file')
233
234   if log_mechanism == 'console'
235     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
236       it { should be_file }
237       it { should be_owned_by 'odl' }
238       it { should be_grouped_into 'odl' }
239       its(:content) { should match /^karaf.log.console=INFO/ }
240       its(:content) { should match /^log4j2.appender.console.direct = true/ }
241     end
242   else
243     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
244       it { should be_file }
245       it { should be_owned_by 'odl' }
246       it { should be_grouped_into 'odl' }
247       its(:content) { should match /^log4j2.appender.rolling.policies.size.size = #{log_max_size}/ }
248       its(:content) { should match /^log4j2.appender.rolling.strategy.type = DefaultRolloverStrategy/ }
249       its(:content) { should match /^log4j2.appender.rolling.strategy.max = #{log_max_rollover}/ }
250     end
251   end
252 end
253
254 # Shared function for validations related to the Karaf config file
255 def karaf_config_validations(options = {})
256   # NB: These param defaults should match the ones used by the opendaylight
257   #   class, which are defined in opendaylight::params
258   # TODO: Remove this possible source of bugs^^
259   extra_features = options.fetch(:extra_features, [])
260   default_features = options.fetch(:default_features, ['standard', 'wrap', 'ssh'])
261
262   # Create one list of all of the features
263   features = default_features + extra_features
264
265   describe file('/opt/opendaylight/etc/org.apache.karaf.features.cfg') 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 /^featuresBoot=#{features.join(",")}/ }
270   end
271 end
272
273 # Shared function for validations related to the ODL REST port config file
274 def port_config_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   odl_rest_port = options.fetch(:odl_rest_port, 8181)
279
280   describe file('/opt/opendaylight/etc/jetty.xml') do
281     it { should be_file }
282     it { should be_owned_by 'odl' }
283     it { should be_grouped_into 'odl' }
284     its(:content) { should match /Property name="jetty.port" default="#{odl_rest_port}"/ }
285   end
286
287   describe file('/opt/opendaylight/etc/org.ops4j.pax.web.cfg') do
288     it { should be_file }
289     it { should be_owned_by 'odl' }
290     it { should be_grouped_into 'odl' }
291     its(:content) { should match /org.osgi.service.http.port = #{odl_rest_port}/ }
292   end
293 end
294
295 # Shared function for validations related to the ODL bind IP
296 def odl_bind_ip_validation(options = {})
297   # NB: This param default should match the one used by the opendaylight
298   #   class, which is defined in opendaylight::params
299   # TODO: Remove this possible source of bugs^^
300   odl_bind_ip = options.fetch(:odl_bind_ip, '0.0.0.0')
301
302   if odl_bind_ip != '0.0.0.0'
303     describe file('/opt/opendaylight/etc/org.apache.karaf.shell.cfg') do
304       it { should be_file }
305       it { should be_owned_by 'odl' }
306       it { should be_grouped_into 'odl' }
307       its(:content) { should match /sshHost = #{odl_bind_ip}/ }
308     end
309
310     describe command("loop_count=0; until [[ \$loop_count -ge 30 ]]; do netstat -punta | grep 8101 | grep #{odl_bind_ip} && break; loop_count=\$[\$loop_count+1]; sleep 1; done; echo \"Waited \$loop_count seconds to detect ODL karaf bound to IP\"") do
311       its(:exit_status) { should eq 0 }
312     end
313   end
314 end
315
316 # Shared function for validations related to custom logging verbosity
317 def log_level_validations(options = {})
318   # NB: This param default should match the one used by the opendaylight
319   #   class, which is defined in opendaylight::params
320   # TODO: Remove this possible source of bugs^^
321   log_levels = options.fetch(:log_levels, {})
322
323   if log_levels.empty?
324     # Should contain log level config file
325     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
326       it { should be_file }
327       it { should be_owned_by 'odl' }
328       it { should be_grouped_into 'odl' }
329     end
330   else
331     # Should contain log level config file
332     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
333       it { should be_file }
334       it { should be_owned_by 'odl' }
335       it { should be_grouped_into 'odl' }
336     end
337     # Verify each custom log level config entry
338     log_levels.each_pair do |logger, level|
339       underscored_version = "#{logger}".gsub('.', '_')
340       describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
341         it { should be_file }
342         it { should be_owned_by 'odl' }
343         it { should be_grouped_into 'odl' }
344         its(:content) { should match /^log4j2.logger.#{underscored_version}.level = #{level}/ }
345         its(:content) { should match /^log4j2.logger.#{underscored_version}.name = #{logger}/ }
346       end
347     end
348   end
349 end
350
351 # Shared function for validations related to ODL OVSDB HA config
352 def enable_ha_validations(options = {})
353   # NB: This param default should match the one used by the opendaylight
354   #   class, which is defined in opendaylight::params
355   # TODO: Remove this possible source of bugs^^
356   enable_ha = options.fetch(:enable_ha, false)
357   ha_node_ips = options.fetch(:ha_node_ips, [])
358   odl_bind_ip = options.fetch(:odl_bind_ip, '0.0.0.0')
359   ha_db_modules = options.fetch(:ha_db_modules, { 'default' => false })
360   # HA_NODE_IPS size
361   ha_node_count = ha_node_ips.size
362
363   if (enable_ha) && (ha_node_count < 2)
364     # Check for HA_NODE_COUNT < 2
365     fail("Number of HA nodes less than 2: #{ha_node_count} and HA Enabled")
366   end
367
368   if enable_ha
369     ha_node_index = ha_node_ips.index(odl_bind_ip)
370     describe file('/opt/opendaylight/configuration/initial/akka.conf') do
371       it { should be_file }
372       it { should be_owned_by 'odl' }
373       it { should be_grouped_into 'odl' }
374       its(:content) { should match /roles\s*=\s*\["member-#{ha_node_index}"\]/ }
375     end
376
377     ha_db_modules.each do |mod, urn|
378       describe file('/opt/opendaylight/configuration/initial/module-shards.conf') do
379         it { should be_file }
380         it { should be_owned_by 'odl' }
381         it { should be_grouped_into 'odl' }
382         its(:content) { should match /name = "#{mod}"/ }
383       end
384
385       if mod == 'default'
386         describe file('/opt/opendaylight/configuration/initial/modules.conf') do
387           it { should be_file }
388           it { should be_owned_by 'odl' }
389           it { should be_grouped_into 'odl' }
390         end
391       else
392         describe file('/opt/opendaylight/configuration/initial/modules.conf') do
393           it { should be_file }
394           it { should be_owned_by 'odl' }
395           it { should be_grouped_into 'odl' }
396           its(:content) { should match /name = "#{mod}"/ }
397           its(:content) { should match /namespace = "#{urn}"/ }
398         end
399       end
400     end
401   end
402 end
403
404 # Shared function that handles validations specific to RPM-type installs
405 def rpm_validations()
406   rpm_repo = ENV['RPM_REPO']
407
408   describe yumrepo('opendaylight') do
409     it { should exist }
410     it { should be_enabled }
411   end
412
413   describe package('opendaylight') do
414     it { should be_installed }
415   end
416 end
417
418 # Shared function that handles validations specific to Deb-type installs
419 def deb_validations()
420   deb_repo = ENV['DEB_REPO']
421   # Check ppa
422   # Docs: http://serverspec.org/resource_types.html#ppa
423   describe ppa(deb_repo) do
424     it { should exist }
425     it { should be_enabled }
426   end
427
428   describe package('opendaylight') do
429     it { should be_installed }
430   end
431 end
432
433 # Shared function for validations related to username/password
434 def username_password_validations(options = {})
435   # NB: This param default should match the one used by the opendaylight
436   #   class, which is defined in opendaylight::params
437   # TODO: Remove this possible source of bugs^^
438   odl_username = options.fetch(:username, 'admin')
439   odl_password = options.fetch(:password, 'admin')
440   odl_check_url = 'http://127.0.0.1:8181/restconf'
441
442   describe file('/opt/opendaylight/data/idmlight.db.mv.db') do
443     it { should be_file }
444   end
445
446   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
447     its(:exit_status) { should eq 0 }
448   end
449
450   describe command("curl -o /dev/null --fail --silent --head -u #{odl_username}:#{odl_password} #{odl_check_url}") do
451     its(:exit_status) { should eq 0 }
452   end
453 end
454
455 # Shared function for validations related to the SNAT config file
456 def snat_mechanism_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   snat_mechanism = options.fetch(:snat_mechanism, 'controller')
461
462   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/netvirt-natservice-config.xml') do
463     it { should be_file }
464     it { should be_owned_by 'odl' }
465     it { should be_grouped_into 'odl' }
466     its(:content) { should match /<nat-mode>#{snat_mechanism}<\/nat-mode>/ }
467   end
468 end
469
470 # Shared function for validations related to SFC
471 def sfc_validations(options = {})
472   # NB: This param default should match the one used by the opendaylight
473   #   class, which is defined in opendaylight::params
474   # TODO: Remove this possible source of bugs^^
475
476   extra_features = options.fetch(:extra_features, [])
477   if extra_features.include? 'odl-netvirt-sfc'
478     sfc_enabled = true
479   else
480     sfc_enabled = false
481   end
482
483   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/genius-itm-config.xml') do
484     it { should be_file }
485     it { should be_owned_by 'odl' }
486     it { should be_grouped_into 'odl' }
487     its(:content) { should match /<gpe-extension-enabled>#{sfc_enabled}<\/gpe-extension-enabled>/ }
488   end
489 end
490
491 # Shared function for validations related to tos value for DSCP marking
492 def dscp_validations(options = {})
493   # NB: This param default should match the one used by the opendaylight
494   #   class, which is defined in opendaylight::params
495   # TODO: Remove this possible source of bugs^^
496
497   inherit_dscp_marking = options.fetch(:inherit_dscp_marking, false)
498
499   if inherit_dscp_marking
500     describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/genius-itm-config.xml') do
501       it { should be_file }
502       it { should be_owned_by 'odl' }
503       it { should be_grouped_into 'odl' }
504       its(:content) { should match /<default-tunnel-tos>inherit<\/default-tunnel-tos>/ }
505     end
506   end
507 end
508
509 def websocket_address_validations(options = {})
510   # NB: This param default should match the one used by the opendaylight
511   #   class, which is defined in opendaylight::params
512   # TODO: Remove this possible source of bugs^^
513   odl_bind_ip = options.fetch(:odl_bind_ip, '0.0.0.0')
514
515   if not odl_bind_ip.eql? '0.0.0.0'
516     describe file('/opt/opendaylight/etc/org.opendaylight.restconf.cfg') do
517       it { should be_file }
518       it { should be_owned_by 'odl' }
519       it { should be_grouped_into 'odl' }
520       its(:content) { should match /^websocket-address=#{odl_bind_ip}/ }
521     end
522   else
523     describe file('/opt/opendaylight/etc/org.opendaylight.restconf.cfg') do
524       it { should be_file }
525     end
526   end
527 end
528
529 def tls_validations(options = {})
530   # NB: This param default should match the one used by the opendaylight
531   #   class, which is defined in opendaylight::params
532   # TODO: Remove this possible source of bugs^^
533   tls_keystore_password = options.fetch(:tls_keystore_password)
534   odl_rest_port = options.fetch(:odl_rest_port, 8181)
535
536   describe file('/opt/opendaylight/etc/org.ops4j.pax.web.cfg') do
537     it { should be_file }
538     it { should be_owned_by 'odl' }
539     it { should be_grouped_into 'odl' }
540     its(:content) { should match /org.osgi.service.http.port.secure = #{odl_rest_port}/ }
541     its(:content) { should match /org.ops4j.pax.web.ssl.keystore = configuration\/ssl\/ctl.jks/ }
542     its(:content) { should match /org.ops4j.pax.web.ssl.password = #{tls_keystore_password}/ }
543     its(:content) { should match /org.ops4j.pax.web.ssl.keypassword = #{tls_keystore_password}/ }
544     its(:content) { should match /org.osgi.service.http.secure.enabled = true/ }
545     its(:content) { should match /org.osgi.service.http.enabled = false/ }
546   end
547
548   describe file('/opt/opendaylight/etc/org.opendaylight.ovsdb.library.cfg') do
549     it { should be_file }
550     it { should be_owned_by 'odl' }
551     it { should be_grouped_into 'odl' }
552     its(:content) { should match /use-ssl = true/ }
553   end
554
555   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/default-openflow-connection-config.xml') do
556     it { should be_file }
557     it { should be_owned_by 'odl' }
558     it { should be_grouped_into 'odl' }
559     its(:content) { should match /<keystore-password>#{tls_keystore_password}<\/keystore-password>/ }
560     its(:content) { should match /<truststore-password>#{tls_keystore_password}<\/truststore-password>/ }
561     its(:content) { should match /<transport-protocol>TLS<\/transport-protocol>/ }
562   end
563
564   describe file('/opt/opendaylight/etc/opendaylight/datastore/initial/config/aaa-cert-config.xml') do
565     it { should be_file }
566     it { should be_owned_by 'odl' }
567     it { should be_grouped_into 'odl' }
568     its(:content) { should match /<store-password>#{tls_keystore_password}<\/store-password>/ }
569     its(:content) { should match /<use-mdsal>false<\/use-mdsal>/ }
570   end
571
572   describe file('/opt/opendaylight/etc/jetty.xml') do
573     it { should be_file }
574     it { should be_owned_by 'odl' }
575     it { should be_grouped_into 'odl' }
576     its(:content) { should match /<Property name="jetty.secure.port" default="#{odl_rest_port}" \/>/ }
577   end
578 end
579
580 # Shared function for validations related to OVS statistics polling
581 def stats_polling_validations(options = {})
582   # NB: This param default should match the one used by the opendaylight
583   #   class, which is defined in opendaylight::params
584   # TODO: Remove this possible source of bugs^^
585
586   stats_polling_enabled = options.fetch(:stats_polling_enabled, false)
587   describe file('/opt/opendaylight/etc/org.opendaylight.openflowplugin.cfg') do
588     it { should be_file }
589     it { should be_owned_by 'odl' }
590     it { should be_grouped_into 'odl' }
591     its(:content) { should match /is-statistics-polling-on=#{stats_polling_enabled}/ }
592   end
593 end