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