Merge "Revert "Revert "Adds ability to configure ODL.."""
[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     if host.is_pe?
9       install_pe
10     elsif host.name == "ubuntu-16-docker" || host.name == "ubuntu-16"
11       install_puppet_agent_on(host, puppet_collection: "pc1")
12     else
13       install_puppet
14     end
15   end
16 end
17
18 RSpec.configure do |c|
19   # Project root
20   proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
21
22   # Readable test descriptions
23   c.formatter = :documentation
24
25   # Configure all nodes in nodeset
26   c.before :suite do
27     # Install opendaylight module on any/all Beaker hosts
28     # TODO: Should this be done in host.each loop?
29     puppet_module_install(:source => proj_root, :module_name => 'opendaylight')
30     hosts.each do |host|
31       # Install stdlib, a dependency of the odl mod
32       on host, puppet('module', 'install', 'puppetlabs-stdlib'), { :acceptable_exit_codes => [0] }
33       # Install apt, a dependency of the deb install method
34       on host, puppet('module', 'install', 'puppetlabs-apt'), { :acceptable_exit_codes => [0] }
35     end
36   end
37 end
38
39 #
40 # NB: These are a library of helper fns used by the Beaker tests
41 #
42
43 # NB: There are a large number of helper functions used in these tests.
44 # They make this code much more friendly, but may need to be referenced.
45 # The serverspec helpers (`should`, `be_running`...) are documented here:
46 #   http://serverspec.org/resource_types.html
47
48 def install_odl(options = {})
49   # Install params are passed via environment var, set in Rakefile
50   # Changing the installed version of ODL via `puppet apply` is not supported
51   # by puppet-odl, so it's not possible to vary these params in the same
52   # Beaker test run. Do a different run passing different env vars.
53   rpm_repo = ENV['RPM_REPO']
54   deb_repo = ENV['DEB_REPO']
55
56   if rpm_repo == ''
57     rpm_repo = 'none'
58   elsif deb_repo == ''
59     deb_repo = 'none'
60   end
61
62   # NB: These param defaults should match the ones used by the opendaylight
63   #   class, which are defined in opendaylight::params
64   # TODO: Remove this possible source of bugs^^
65   # Extract params if given, defaulting to odl class defaults if not
66   extra_features = options.fetch(:extra_features, [])
67   default_features = options.fetch(:default_features,
68     ['config', 'standard', 'region', 'package', 'kar', 'ssh', 'management'])
69   odl_rest_port = options.fetch(:odl_rest_port, 8080)
70   log_levels = options.fetch(:log_levels, {})
71   enable_ha = options.fetch(:enable_ha, false)
72   ha_node_ips = options.fetch(:ha_node_ips, [])
73   ha_node_index = options.fetch(:ha_node_index, 0)
74   username = options.fetch(:username, 'admin')
75   password = options.fetch(:password, 'admin')
76
77   # Build script for consumption by Puppet apply
78   it 'should work idempotently with no errors' do
79     pp = <<-EOS
80     class { 'opendaylight':
81       rpm_repo => '#{rpm_repo}',
82       deb_repo => '#{deb_repo}',
83       default_features => #{default_features},
84       extra_features => #{extra_features},
85       odl_rest_port=> #{odl_rest_port},
86       enable_ha=> #{enable_ha},
87       ha_node_ips=> #{ha_node_ips},
88       ha_node_index=> #{ha_node_index},
89       log_levels=> #{log_levels},
90       username=> #{username},
91       password=> #{password},
92     }
93     EOS
94
95     # Apply our Puppet manifest on the Beaker host
96     apply_manifest(pp, :catch_failures => true)
97
98     # Not checking for idempotence because of false failures
99     # related to package manager cache updates outputting to
100     # stdout and different IDs for the puppet manifest apply.
101     # I think this is a limitation in how Beaker can check
102     # for changes, not a problem with the Puppet module.
103     end
104 end
105
106 # Shared function that handles generic validations
107 # These should be common for all odl class param combos
108 def generic_validations()
109   # Verify ODL's directory
110   describe file('/opt/opendaylight/') do
111     it { should be_directory }
112     it { should be_owned_by 'odl' }
113     it { should be_grouped_into 'odl' }
114   end
115
116   # Verify ODL's systemd service
117   describe service('opendaylight') do
118     it { should be_enabled }
119     it { should be_enabled.with_level(3) }
120     it { should be_running.under('systemd') }
121   end
122
123   # Creation handled by RPM or Deb
124   describe user('odl') do
125     it { should exist }
126     it { should belong_to_group 'odl' }
127     # NB: This really shouldn't have a slash at the end!
128     #     The home dir set by the RPM is `/opt/opendaylight`.
129     #     Since we use the trailing slash elsewhere else, this
130     #     may look like a style issue. It isn't! It will make
131     #     Beaker tests fail if it ends with a `/`. A future
132     #     version of the ODL RPM may change this.
133     it { should have_home_directory '/opt/opendaylight' }
134   end
135
136   # Creation handled by RPM or Deb
137   describe group('odl') do
138     it { should exist }
139   end
140
141   # This should not be the odl user's home dir
142   describe file('/home/odl') do
143     # Home dir shouldn't be created for odl user
144     it { should_not be_directory }
145   end
146
147   # OpenDaylight will appear as a Java process
148   describe process('java') do
149     it { should be_running }
150   end
151
152   # Should contain Karaf features config file
153   describe file('/opt/opendaylight/etc/org.apache.karaf.features.cfg') do
154     it { should be_file }
155     it { should be_owned_by 'odl' }
156     it { should be_grouped_into 'odl' }
157   end
158
159   # Should contain ODL NB port config file
160   describe file('/opt/opendaylight/etc/jetty.xml') 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 log level config file
167   describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
168     it { should be_file }
169     it { should be_owned_by 'odl' }
170     it { should be_grouped_into 'odl' }
171   end
172
173   if ['centos-7', 'centos-7-docker'].include? ENV['RS_SET']
174     # Validations for modern Red Hat family OSs
175
176     # Verify ODL systemd .service file
177     describe file('/usr/lib/systemd/system/opendaylight.service') do
178       it { should be_file }
179       it { should be_owned_by 'root' }
180       it { should be_grouped_into 'root' }
181       it { should be_mode '644' }
182     end
183
184     # Java 8 should be installed
185     describe package('java-1.8.0-openjdk') do
186       it { should be_installed }
187     end
188
189   # Ubuntu 16.04 specific validation
190   elsif ['ubuntu-16', 'ubuntu-16-docker'].include? ENV['RS_SET']
191
192     # Verify ODL systemd .service file
193     describe file('/lib/systemd/system/opendaylight.service') do
194       it { should be_file }
195       it { should be_owned_by 'root' }
196       it { should be_grouped_into 'root' }
197       it { should be_mode '644' }
198     end
199
200     # Java 8 should be installed
201     describe package('openjdk-8-jre-headless') do
202       it { should be_installed }
203     end
204
205   else
206     fail("Unexpected RS_SET (host OS): #{ENV['RS_SET']}")
207   end
208 end
209
210 # Shared function for validations related to the Karaf config file
211 def karaf_config_validations(options = {})
212   # NB: These param defaults should match the ones used by the opendaylight
213   #   class, which are defined in opendaylight::params
214   # TODO: Remove this possible source of bugs^^
215   extra_features = options.fetch(:extra_features, [])
216   default_features = options.fetch(:default_features, ['config', 'standard', 'region',
217                                   'package', 'kar', 'ssh', 'management'])
218
219   # Create one list of all of the features
220   features = default_features + extra_features
221
222   describe file('/opt/opendaylight/etc/org.apache.karaf.features.cfg') do
223     it { should be_file }
224     it { should be_owned_by 'odl' }
225     it { should be_grouped_into 'odl' }
226     its(:content) { should match /^featuresBoot=#{features.join(",")}/ }
227   end
228 end
229
230 # Shared function for validations related to the ODL REST port config file
231 def port_config_validations(options = {})
232   # NB: This param default should match the one used by the opendaylight
233   #   class, which is defined in opendaylight::params
234   # TODO: Remove this possible source of bugs^^
235   odl_rest_port = options.fetch(:odl_rest_port, 8080)
236
237   describe file('/opt/opendaylight/etc/jetty.xml') 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 /Property name="jetty.port" default="#{odl_rest_port}"/ }
242   end
243 end
244
245 # Shared function for validations related to custom logging verbosity
246 def log_level_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   log_levels = options.fetch(:log_levels, {})
251
252   if log_levels.empty?
253     # Should contain log level config file
254     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
255       it { should be_file }
256       it { should be_owned_by 'odl' }
257       it { should be_grouped_into 'odl' }
258     end
259     # Should not contain custom log level config
260     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
261       it { should be_file }
262       it { should be_owned_by 'odl' }
263       it { should be_grouped_into 'odl' }
264       its(:content) { should_not match /# Log level config added by puppet-opendaylight/ }
265     end
266   else
267     # Should contain log level config file
268     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
269       it { should be_file }
270       it { should be_owned_by 'odl' }
271       it { should be_grouped_into 'odl' }
272     end
273     # Should not contain custom log level config
274     describe file('/opt/opendaylight/etc/org.ops4j.pax.logging.cfg') do
275       it { should be_file }
276       it { should be_owned_by 'odl' }
277       it { should be_grouped_into 'odl' }
278       its(:content) { should match /# Log level config added by puppet-opendaylight/ }
279     end
280     # Verify each custom log level config entry
281     log_levels.each_pair do |logger, level|
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         its(:content) { should match /^log4j.logger.#{logger} = #{level}/ }
287       end
288     end
289   end
290 end
291
292 # Shared function for validations related to ODL OVSDB HA config
293 def enable_ha_validations(options = {})
294   # NB: This param default should match the one used by the opendaylight
295   #   class, which is defined in opendaylight::params
296   # TODO: Remove this possible source of bugs^^
297   enable_ha = options.fetch(:enable_ha, false)
298   ha_node_ips = options.fetch(:ha_node_ips, [])
299   ha_node_index = options.fetch(:ha_node_index, 0)
300   # HA_NODE_IPS size
301   ha_node_count = ha_node_ips.size
302
303   if (enable_ha) && (ha_node_count < 2)
304     # Check for HA_NODE_COUNT < 2
305     fail("Number of HA nodes less than 2: #{ha_node_count} and HA Enabled")
306   end
307 end
308
309 # Shared function that handles validations specific to RPM-type installs
310 def rpm_validations()
311   rpm_repo = ENV['RPM_REPO']
312
313   describe yumrepo(rpm_repo) do
314     it { should exist }
315     it { should be_enabled }
316   end
317
318   describe package('opendaylight') do
319     it { should be_installed }
320   end
321 end
322
323 # Shared function that handles validations specific to Deb-type installs
324 def deb_validations()
325   deb_repo = ENV['DEB_REPO']
326   # Check ppa
327   # Docs: http://serverspec.org/resource_types.html#ppa
328   describe ppa(deb_repo) do
329     it { should exist }
330     it { should be_enabled }
331   end
332
333   describe package('opendaylight') do
334     it { should be_installed }
335   end
336 end
337
338 # Shared function for validations related to username/password
339 def username_password_validations(options = {})
340   # NB: This param default should match the one used by the opendaylight
341   #   class, which is defined in opendaylight::params
342   # TODO: Remove this possible source of bugs^^
343   odl_username = options.fetch(:username, 'admin')
344   odl_password = options.fetch(:password, 'admin')
345   odl_check_url = 'http://127.0.0.1:8080/restconf'
346
347   describe file('/opt/opendaylight/idmlight.db.mv.db') do
348     it { should be_file }
349   end
350
351   describe command("sleep 60 && curl -o /dev/null --fail --silent --head -u #{odl_username}:#{odl_password} #{odl_check_url}") do
352     its(:exit_status) { should eq 0 }
353   end
354 end