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