038d909caa167a2fd272d833783ba083c235fc37
[integration/packaging/puppet-opendaylight.git] / spec / spec_helper.rb
1 require 'puppetlabs_spec_helper/module_spec_helper'
2
3 # Customize filters to ignore 3rd-party code
4 custom_filters = [
5   'Anchor[java::end]',
6   'Stage[setup]',
7   'Anchor[java::begin:]',
8   'Archive::Download[opendaylight.tar.gz]',
9   'Archive::Download[opendaylight-systemd.tar.gz]',
10   'Archive::Extract[opendaylight]',
11   'Archive::Extract[opendaylight-systemd]',
12   'Class[Java::Config]',
13   'Class[Java::Params]',
14   'Class[Stdlib::Stages]',
15   'Class[Stdlib]',
16   'Exec[download archive opendaylight.tar.gz and check sum]',
17   'Exec[download archive opendaylight-systemd.tar.gz and check sum]',
18   'Exec[opendaylight unpack]',
19   'Exec[opendaylight-systemd unpack]',
20   'Exec[rm-on-error-opendaylight.tar.gz]',
21   'Exec[rm-on-error-opendaylight-systemd.tar.gz]',
22   'Package[curl]',
23   'Stage[deploy]',
24   'Stage[deploy_app]',
25   'Stage[deploy_infra]',
26   'Stage[runtime]',
27   'Stage[setup_app]',
28   'Stage[setup_infra]',
29 ]
30 RSpec::Puppet::Coverage.filters.push(*custom_filters)
31
32 # Tests that are common to all possible configurations
33 def generic_tests()
34   # Confirm that module compiles
35   it { should compile }
36   it { should compile.with_all_deps }
37
38   # Confirm presence of classes
39   it { should contain_class('opendaylight') }
40   it { should contain_class('opendaylight::params') }
41   it { should contain_class('opendaylight::install') }
42   it { should contain_class('opendaylight::config') }
43   it { should contain_class('opendaylight::service') }
44
45   # Confirm relationships between classes
46   it { should contain_class('opendaylight::install').that_comes_before('opendaylight::config') }
47   it { should contain_class('opendaylight::config').that_requires('opendaylight::install') }
48   it { should contain_class('opendaylight::config').that_notifies('opendaylight::service') }
49   it { should contain_class('opendaylight::service').that_subscribes_to('opendaylight::config') }
50   it { should contain_class('opendaylight::service').that_comes_before('opendaylight') }
51   it { should contain_class('opendaylight').that_requires('opendaylight::service') }
52
53   # Confirm presence of generic resources
54   it { should contain_service('opendaylight') }
55   it { should contain_file('org.apache.karaf.features.cfg') }
56
57   # Confirm properties of generic resources
58   # NB: These hashes don't work with Ruby 1.8.7, but we
59   #   don't support 1.8.7 so that's okay. See issue #36.
60   it {
61     should contain_service('opendaylight').with(
62       'ensure'      => 'running',
63       'enable'      => 'true',
64       'hasstatus'   => 'true',
65       'hasrestart'  => 'true',
66     )
67   }
68   it {
69     should contain_file('org.apache.karaf.features.cfg').with(
70       'ensure'      => 'file',
71       'path'        => '/opt/opendaylight/etc/org.apache.karaf.features.cfg',
72     )
73   }
74 end
75
76 # Shared tests that specialize in testing Karaf feature installs
77 def karaf_feature_tests(features)
78   # Confirm properties of other resources
79   # NB: These hashes don't work with Ruby 1.8.7, but we
80   #   don't support 1.8.7 so that's okay. See issue #36.
81   it {
82     should contain_file('org.apache.karaf.features.cfg').with(
83       'ensure'      => 'file',
84       'path'        => '/opt/opendaylight/etc/org.apache.karaf.features.cfg',
85       'content'     => /^featuresBoot=#{features.join(",")}/
86     )
87   }
88 end
89
90 def tarball_install_tests(options = {})
91   # Extract params
92   tarball_url = options.fetch(:tarball_url, 'https://nexus.opendaylight.org/content/groups/public/org/opendaylight/integration/distribution-karaf/0.2.2-Helium-SR2/distribution-karaf-0.2.2-Helium-SR2.tar.gz')
93   unitfile_url = options.fetch(:unitfile_url, 'https://github.com/dfarrell07/opendaylight-systemd/archive/master/opendaylight-unitfile.tar.gz')
94   osfamily = options.fetch(:osfamily, 'RedHat')
95
96   # Confirm presence of tarball-related resources
97   it { should contain_archive('opendaylight') }
98   it { should contain_class('java') }
99   it { should contain_file('/opt/opendaylight/') }
100   it { should contain_user('odl') }
101   it { should contain_group('odl') }
102
103   # Confirm relationships between tarball-related resources
104   it { should contain_archive('opendaylight').that_comes_before('File[/opt/opendaylight/]') }
105   it { should contain_archive('opendaylight').that_comes_before('User[odl]') }
106   it { should contain_file('/opt/opendaylight/').that_requires('Archive[opendaylight]') }
107   it { should contain_file('/opt/opendaylight/').that_requires('Group[odl]') }
108   it { should contain_file('/opt/opendaylight/').that_requires('User[odl]') }
109   it { should contain_user('odl').that_comes_before('File[/opt/opendaylight/]') }
110   it { should contain_user('odl').that_requires('Archive[opendaylight]') }
111   it { should contain_user('odl').that_requires('Group[odl]') }
112   it { should contain_group('odl').that_comes_before('File[/opt/opendaylight/]') }
113   it { should contain_group('odl').that_comes_before('User[odl]') }
114
115   # Confirm properties of tarball-related resources
116   # NB: These hashes don't work with Ruby 1.8.7, but we
117   #   don't support 1.8.7 so that's okay. See issue #36.
118   it {
119     should contain_archive('opendaylight').with(
120       'ensure'           => 'present',
121       'url'              => tarball_url,
122       'target'           => '/opt/opendaylight/',
123       'checksum'         => false,
124       'strip_components' => 1,
125       'timeout'          => 600,
126     )
127   }
128   it {
129     should contain_file('/opt/opendaylight/').with(
130       'ensure'  => 'directory',
131       'recurse' => true,
132       'owner'   => 'odl',
133       'group'   => 'odl',
134     )
135   }
136   it {
137     should contain_user('odl').with(
138       'name'       => 'odl',
139       'ensure'     => 'present',
140       'home'       => '/opt/opendaylight/',
141       'membership' => 'minimum',
142       'groups'     => 'odl',
143     )
144   }
145   it {
146     should contain_group('odl').with(
147       'name'       => 'odl',
148       'ensure'     => 'present',
149     )
150   }
151
152   # OS-specific validations
153   case osfamily
154   when 'RedHat'
155     # Validations specific to Red Hat family OSs (RHEL/CentOS/Fedora)
156     it { should contain_archive('opendaylight-systemd') }
157     it { should contain_file('/usr/lib/systemd/system/opendaylight.service') }
158     it { should contain_archive('opendaylight-systemd').that_comes_before('File[/usr/lib/systemd/system/opendaylight.service]') }
159     it { should contain_file('/usr/lib/systemd/system/opendaylight.service').that_requires('Archive[opendaylight-systemd]') }
160
161     # NB: These hashes don't work with Ruby 1.8.7, but we
162     #   don't support 1.8.7 so that's okay. See issue #36.
163     it {
164       should contain_package('java').with(
165         'name' => 'java-1.7.0-openjdk',
166       )
167     }
168     it {
169       should contain_archive('opendaylight-systemd').with(
170         'ensure'           => 'present',
171         'url'              => unitfile_url,
172         'target'           => '/usr/lib/systemd/system/',
173         'root_dir'         => 'opendaylight.service',
174         'checksum'         => false,
175         'strip_components' => 1,
176         'follow_redirects' => true,
177       )
178     }
179     it {
180       should contain_file('/usr/lib/systemd/system/opendaylight.service').with(
181         'ensure'  => 'file',
182         'owner'   => 'root',
183         'group'   => 'root',
184         'mode'    => '0644',
185       )
186     }
187   when 'Debian'
188     # Validations specific to Debain family OSs (Ubuntu)
189     it {
190       should contain_package('java').with(
191         'name' => 'java7-jdk',
192       )
193     }
194     it {
195       should contain_file('/etc/init/opendaylight.conf').with(
196         'ensure'  => 'file',
197         'owner'   => 'root',
198         'group'   => 'root',
199         'mode'    => '0644',
200       )
201     }
202     expected_msg = 'Debian has limited support, is less stable, less tested.'
203     it {
204       expect {
205         # This could be any check, most (all?) will raise warning
206         should contain_file('/etc/init/opendaylight.conf').to(
207           raise_warning(Puppet::Warning, /#{expected_msg}/)
208         )
209       }
210     }
211   else
212     fail("Unexpected osfamily #{osfamily}")
213   end
214
215   # Verify that there are no unexpected resources from RPM-type installs
216   it { should_not contain_yumrepo('opendaylight') }
217   it { should_not contain_package('opendaylight') }
218 end
219
220 def rpm_install_tests(options = {})
221   # Extract params
222   # Default to CentOS 7 Yum repo URL
223   yum_repo = options.fetch(:yum_repo, 'https://copr-be.cloud.fedoraproject.org/results/dfarrell07/OpenDaylight/epel-7-$basearch/')
224
225   # Confirm presence of RPM-related resources
226   it { should contain_yumrepo('opendaylight') }
227   it { should contain_package('opendaylight') }
228
229   # Confirm relationships between RPM-related resources
230   it { should contain_package('opendaylight').that_requires('Yumrepo[opendaylight]') }
231   it { should contain_yumrepo('opendaylight').that_comes_before('Package[opendaylight]') }
232
233   # Confirm properties of RPM-related resources
234   # NB: These hashes don't work with Ruby 1.8.7, but we
235   #   don't support 1.8.7 so that's okay. See issue #36.
236   it {
237     should contain_yumrepo('opendaylight').with(
238       'enabled'     => '1',
239       'gpgcheck'    => '0',
240       'descr'       => 'OpenDaylight SDN controller',
241       'baseurl'     => yum_repo,
242     )
243   }
244   it {
245     should contain_package('opendaylight').with(
246       'ensure'   => 'present',
247     )
248   }
249 end
250
251 # Shared tests for unsupported OSs
252 def unsupported_os_tests(options = {})
253   # Extract params
254   expected_msg = options.fetch(:expected_msg)
255
256   # Confirm that classes fail on unsupported OSs
257   it { expect { should contain_class('opendaylight') }.to raise_error(Puppet::Error, /#{expected_msg}/) }
258   it { expect { should contain_class('opendaylight::install') }.to raise_error(Puppet::Error, /#{expected_msg}/) }
259   it { expect { should contain_class('opendaylight::config') }.to raise_error(Puppet::Error, /#{expected_msg}/) }
260   it { expect { should contain_class('opendaylight::service') }.to raise_error(Puppet::Error, /#{expected_msg}/) }
261
262   # Confirm that other resources fail on unsupported OSs
263   it { expect { should contain_yumrepo('opendaylight') }.to raise_error(Puppet::Error, /#{expected_msg}/) }
264   it { expect { should contain_package('opendaylight') }.to raise_error(Puppet::Error, /#{expected_msg}/) }
265   it { expect { should contain_service('opendaylight') }.to raise_error(Puppet::Error, /#{expected_msg}/) }
266   it { expect { should contain_file('org.apache.karaf.features.cfg') }.to raise_error(Puppet::Error, /#{expected_msg}/) }
267 end