Add Docker-based Beaker tests for Ubuntu
[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     else
11       install_puppet
12     end
13   end
14 end
15
16 RSpec.configure do |c|
17   # Project root
18   proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
19
20   # Readable test descriptions
21   c.formatter = :documentation
22
23   # Configure all nodes in nodeset
24   c.before :suite do
25     # Install opendaylight module on any/all Beaker hosts
26     # TODO: Should this be done in host.each loop?
27     puppet_module_install(:source => proj_root, :module_name => 'opendaylight')
28     hosts.each do |host|
29       # Install stdlib, a dependency of the odl mod
30       on host, puppet('module', 'install', 'puppetlabs-stdlib'), { :acceptable_exit_codes => [0] }
31       # Install archive, a dependency of the odl mod use for tarball-type installs
32       on host, puppet('module', 'install', 'camptocamp-archive'), { :acceptable_exit_codes => [0] }
33       # Install Java Puppet mod, a dependency of the tarball install method
34       on host, puppet('module', 'install', 'puppetlabs-java'), { :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   # NB: These param defaults should match the ones used by the opendaylight
50   #   class, which are defined in opendaylight::params
51   # TODO: Remove this possible source of bugs^^
52   # Extract params if given, defaulting to odl class defaults if not
53   # Default install method is passed via environment var, set in Rakefile
54   install_method = options.fetch(:install_method, ENV['INSTALL_METHOD'])
55   extra_features = options.fetch(:extra_features, [])
56   default_features = options.fetch(:default_features,
57     ['config', 'standard', 'region', 'package', 'kar', 'ssh', 'management'])
58   odl_rest_port = options.fetch(:odl_rest_port, 8080)
59
60   # Build script for consumption by Puppet apply
61   it 'should work idempotently with no errors' do
62     pp = <<-EOS
63     class { 'opendaylight':
64       install_method => #{install_method},
65       default_features => #{default_features},
66       extra_features => #{extra_features},
67       odl_rest_port=> #{odl_rest_port},
68     }
69     EOS
70
71     # Apply our Puppet manifest on the Beaker host
72     apply_manifest(pp, :catch_failures => true)
73
74     # The tarball extract isn't idempotent, can't do this check
75     # See: https://github.com/dfarrell07/puppet-opendaylight/issues/45#issuecomment-78135725
76     if install_method != 'tarball'
77       # Run it twice to test for idempotency
78       apply_manifest(pp, :catch_changes  => true)
79     end
80   end
81 end
82
83 # Shared function that handles generic validations
84 # These should be common for all odl class param combos
85 def generic_validations()
86   # Verify ODL's directory
87   describe file('/opt/opendaylight/') do
88     it { should be_directory }
89     it { should be_owned_by 'odl' }
90     it { should be_grouped_into 'odl' }
91   end
92
93   # Verify ODL's systemd service
94   describe service('opendaylight') do
95     it { should be_enabled }
96     it { should be_enabled.with_level(3) }
97     it { should be_running }
98   end
99
100   # Creation handled by RPM, or Puppet during tarball installs
101   describe user('odl') do
102     it { should exist }
103     it { should belong_to_group 'odl' }
104     # NB: This really shouldn't have a slash at the end!
105     #     The home dir set by the RPM is `/opt/opendaylight`.
106     #     Since we use the trailing slash elsewhere else, this
107     #     may look like a style issue. It isn't! It will make
108     #     Beaker tests fail if it ends with a `/`. A future
109     #     version of the ODL RPM may change this.
110     it { should have_home_directory '/opt/opendaylight' }
111   end
112
113   # Creation handled by RPM, or Puppet during tarball installs
114   describe group('odl') do
115     it { should exist }
116   end
117
118   # This should not be the odl user's home dir
119   describe file('/home/odl') do
120     # Home dir shouldn't be created for odl user
121     it { should_not be_directory }
122   end
123
124   # OpenDaylight will appear as a Java process
125   describe process('java') do
126     it { should be_running }
127   end
128
129   # Validations specific to the host OS
130   if ['fedora-20'].include? ENV['RS_SET']
131     # Validations for (legecy) Fedora 20 checks
132     # NB: Fedora 20 support will be removed soon, it's EOL
133
134     # Verify ODL systemd .service file
135     describe file('/usr/lib/systemd/system/opendaylight.service') do
136       it { should be_file }
137       it { should be_owned_by 'root' }
138       it { should be_grouped_into 'root' }
139       it { should be_mode '644' }
140     end
141
142     # Java 7 should be installed
143     describe package('java-1.7.0-openjdk') do
144       it { should be_installed }
145     end
146   elsif ['centos-7', 'centos-7-docker', 'fedora-21'].include? ENV['RS_SET']
147     # Validations for modern Red Hat family OSs
148
149     # Verify ODL systemd .service file
150     describe file('/usr/lib/systemd/system/opendaylight.service') do
151       it { should be_file }
152       it { should be_owned_by 'root' }
153       it { should be_grouped_into 'root' }
154       it { should be_mode '644' }
155     end
156
157     # Java 8 should be installed
158     describe package('java-1.8.0-openjdk') do
159       it { should be_installed }
160     end
161   elsif ['ubuntu-1404', 'ubuntu-1404-docker'].include? ENV['RS_SET']
162     # Ubuntu-specific validations
163
164     # Verify ODL Upstart config file
165     describe file('/etc/init/opendaylight.conf') do
166       it { should be_file }
167       it { should be_owned_by 'root' }
168       it { should be_grouped_into 'root' }
169       it { should be_mode '644' }
170     end
171
172     # Java 7 should be installed
173     describe package('openjdk-7-jdk') do
174       it { should be_installed }
175     end
176   else
177     fail("Unexpected RS_SET (host OS): #{ENV['RS_SET']}")
178   end
179 end
180
181 # Shared function for validations related to the Karaf config file
182 def karaf_config_validations(options = {})
183   # NB: These param defaults should match the ones used by the opendaylight
184   #   class, which are defined in opendaylight::params
185   # TODO: Remove this possible source of bugs^^
186   extra_features = options.fetch(:extra_features, [])
187   default_features = options.fetch(:default_features, ['config', 'standard', 'region',
188                                   'package', 'kar', 'ssh', 'management'])
189
190   # Create one list of all of the features
191   features = default_features + extra_features
192
193   describe file('/opt/opendaylight/etc/org.apache.karaf.features.cfg') do
194     it { should be_file }
195     it { should be_owned_by 'odl' }
196     it { should be_grouped_into 'odl' }
197     its(:content) { should match /^featuresBoot=#{features.join(",")}/ }
198   end
199 end
200
201 # Shared function for validations related to the ODL REST port config file
202 def port_config_validations(options = {})
203   # NB: This param default should match the one used by the opendaylight
204   #   class, which is defined in opendaylight::params
205   # TODO: Remove this possible source of bugs^^
206   odl_rest_port = options.fetch(:odl_rest_port, 8080)
207
208   describe file('/opt/opendaylight/etc/jetty.xml') do
209     it { should be_file }
210     it { should be_owned_by 'odl' }
211     it { should be_grouped_into 'odl' }
212     its(:content) { should match /Property name="jetty.port" default="#{odl_rest_port}"/ }
213   end
214 end
215
216
217 # Shared function that handles validations specific to RPM-type installs
218 def rpm_validations()
219   describe yumrepo('opendaylight-3-candidate') do
220     it { should exist }
221     it { should be_enabled }
222   end
223
224   describe package('opendaylight') do
225     it { should be_installed }
226   end
227 end
228
229 # Shared function that handles validations specific to tarball-type installs
230 def tarball_validations()
231   describe package('opendaylight') do
232     it { should_not be_installed }
233   end
234
235   # Repo checks break (not fail) when yum doesn't make sense (Ubuntu)
236   if ['centos-7', 'fedora-20', 'fedora-21'].include? ENV['RS_SET']
237     describe yumrepo('opendaylight-3-candidate') do
238       it { should_not exist }
239       it { should_not be_enabled }
240     end
241   end
242 end