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