-
Notifications
You must be signed in to change notification settings - Fork 611
/
spec_helper_local.rb
261 lines (225 loc) · 6.65 KB
/
spec_helper_local.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# frozen_string_literal: true
RSpec.configure do |c|
c.mock_with :rspec
c.include PuppetlabsSpec::Files
c.after :each do
PuppetlabsSpec::Files.cleanup
end
end
if ENV['COVERAGE'] == 'yes'
require 'simplecov'
require 'simplecov-console'
require 'codecov'
SimpleCov.formatters = [
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::Console,
SimpleCov::Formatter::Codecov,
]
SimpleCov.start do
track_files 'lib/**/*.rb'
add_filter '/spec'
# do not track vendored files
add_filter '/vendor'
add_filter '/.vendor'
# do not track gitignored files
# this adds about 4 seconds to the coverage check
# this could definitely be optimized
add_filter do |f|
# system returns true if exit status is 0, which with git-check-ignore means file is ignored
system('git', 'check-ignore', '--quiet', f.filename)
end
end
end
# Convenience helper for returning parameters for a type from the
# catalogue.
def param(type, title, param)
param_value(catalogue, type, title, param)
end
shared_examples 'postgresql_password function' do
it { is_expected.not_to be_nil }
it {
expect(subject).to run.with_params('foo', 'bar').and_return(
'md596948aad3fcae80c08a35c9b5958cd89',
)
}
it {
expect(subject).to run.with_params('foo', 1234).and_return(
'md539a0e1b308278a8de5e007cd1f795920',
)
}
it {
expect(subject).to run.with_params('foo', 'bar', true).and_return(
sensitive(%(md596948aad3fcae80c08a35c9b5958cd89)),
)
}
it {
expect(subject).to run.with_params('foo', 'bar', false, 'scram-sha-256').and_return(
'SCRAM-SHA-256$4096:Zm9v$ea66ynZ8cS9Ty4ZkEYicwC72StsKLSwjcXIXKMgepTk=:dJYmOU6BMCaWkQOB3lrXH9OAF3lW2n3NJ26NO7Srq7U=',
)
}
it {
expect(subject).to run.with_params('foo', 'bar', true, 'scram-sha-256').and_return(
sensitive(%(SCRAM-SHA-256$4096:Zm9v$ea66ynZ8cS9Ty4ZkEYicwC72StsKLSwjcXIXKMgepTk=:dJYmOU6BMCaWkQOB3lrXH9OAF3lW2n3NJ26NO7Srq7U=)),
)
}
it {
expect(subject).to run.with_params('foo', 'bar', false, 'scram-sha-256', 'salt').and_return(
'SCRAM-SHA-256$4096:c2FsdA==$hl63wu9L6vKIjd/UGPfpRl/hIQRBnlkoCiJ9KgxzbX0=:3Q39uiwDZ51m3iPpV8rSgISgRiYqkbnpc+wScL2lSAU=',
)
}
it {
expect(subject).to run.with_params('foo', 'bar', true, 'scram-sha-256', 'salt').and_return(
sensitive(%(SCRAM-SHA-256$4096:c2FsdA==$hl63wu9L6vKIjd/UGPfpRl/hIQRBnlkoCiJ9KgxzbX0=:3Q39uiwDZ51m3iPpV8rSgISgRiYqkbnpc+wScL2lSAU=)),
)
}
it {
expect(subject).to run.with_params('foo', 'bar', false, nil, 'salt').and_return(
'md596948aad3fcae80c08a35c9b5958cd89',
)
}
it {
expect(subject).to run.with_params('foo', 'bar', true, nil, 'salt').and_return(
sensitive(%(md596948aad3fcae80c08a35c9b5958cd89)),
)
}
it {
expect(subject).to run.with_params('foo', 'md596948aad3fcae80c08a35c9b5958cd89', false).and_return(
'md596948aad3fcae80c08a35c9b5958cd89',
)
}
it {
expect(subject).to run.with_params('foo', sensitive(%(SCRAM-SHA-256$4096:c2FsdA==$hl63wu9L6vKIjd/UGPfpRl/hIQRBnlkoCiJ9KgxzbX0=:3Q39uiwDZ51m3iPpV8rSgISgRiYqkbnpc+wScL2lSAU=)), true).and_return(
sensitive(%(SCRAM-SHA-256$4096:c2FsdA==$hl63wu9L6vKIjd/UGPfpRl/hIQRBnlkoCiJ9KgxzbX0=:3Q39uiwDZ51m3iPpV8rSgISgRiYqkbnpc+wScL2lSAU=)),
)
}
it {
expect(subject).to run.with_params('foo', sensitive('md596948aad3fcae80c08a35c9b5958cd89'), false).and_return(
'md596948aad3fcae80c08a35c9b5958cd89',
)
}
it 'raises an error if there is only 1 argument' do
expect(subject).to run.with_params('foo').and_raise_error(StandardError)
end
end
shared_examples 'postgresql_escape function' do
it { is_expected.not_to be_nil }
it {
expect(subject).to run.with_params('foo')
.and_return('$$foo$$')
}
it {
expect(subject).to run.with_params('fo$$o')
.and_return('$ed$fo$$o$ed$')
}
it {
expect(subject).to run.with_params('foo$')
.and_return('$a$foo$$a$')
}
it 'raises an error if there is more than 1 argument' do
expect(subject).to run.with_params(['foo'], ['foo'])
.and_raise_error(StandardError)
end
end
# This duplicates spec_helper but we need it for add_custom_fact
include RspecPuppetFacts
# Rough conversion of grepping in the puppet source:
# grep defaultfor lib/puppet/provider/service/*.rb
# See https://github.com/voxpupuli/voxpupuli-test/blob/master/lib/voxpupuli/test/facts.rb
add_custom_fact :service_provider, ->(_os, facts) do
case facts[:osfamily].downcase
when 'archlinux', 'debian', 'redhat', 'suse'
'systemd'
when 'darwin'
'launchd'
when 'freebsd'
'freebsd'
when 'gentoo'
'openrc'
when 'openbsd'
'openbsd'
when 'windows'
'windows'
else
'init'
end
end
shared_context 'Debian 10' do
let(:facts) { on_supported_os['debian-10-x86_64'] }
end
shared_context 'Debian 11' do
let(:facts) { on_supported_os['debian-11-x86_64'] }
end
shared_context 'Ubuntu 18.04' do
let(:facts) { on_supported_os['ubuntu-18.04-x86_64'] }
end
shared_context 'RedHat 7' do
let(:facts) { on_supported_os['redhat-7-x86_64'] }
end
shared_context 'RedHat 8' do
let(:facts) { on_supported_os['redhat-8-x86_64'] }
end
shared_context 'Fedora 33' do
let(:facts) do
{
kernel: 'Linux',
id: 'root',
path: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
selinux: true,
os: {
'architecture' => 'x86_64',
'family' => 'RedHat',
'hardware' => 'x86_64',
'name' => 'Fedora',
'release' => {
'full' => '33',
'major' => '33',
'minor' => '33'
},
selinux: { 'enabled' => true }
},
operatingsystem: 'Fedora',
operatingsystemrelease: '33',
service_provider: 'systemd'
}
end
end
shared_context 'Amazon 1' do
let :facts do
{
os: {
family: 'RedHat',
name: 'Amazon',
release: {
'full' => '1.0',
'major' => '1'
},
selinux: { 'enabled' => true }
},
kernel: 'Linux',
id: 'root',
path: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
selinux: true,
service_provider: 'redhat'
}
end
end
shared_context 'Gentoo' do
let :facts do
{
os: {
family: 'Gentoo',
name: 'Gentoo',
release: {
'full' => 'unused',
'major' => 'unused'
},
selinux: { 'enabled' => false }
},
kernel: 'Linux',
id: 'root',
path: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
selinux: false,
service_provider: 'openrc'
}
end
end