File: custom_insync_spec.rb

package info (click to toggle)
ruby-puppet-resource-api 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,232 kB
  • sloc: ruby: 9,573; sh: 4; makefile: 2
file content (273 lines) | stat: -rw-r--r-- 11,523 bytes parent folder | download | duplicates (2)
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
262
263
264
265
266
267
268
269
270
271
272
273
# frozen_string_literal: true

require 'spec_helper'
require 'tempfile'

RSpec.describe 'a provider using custom insync' do
  describe 'using `puppet apply`' do
    subject(:puppet_apply_stdout) do
      stdout_str, _status = Open3.capture2e("puppet apply --verbose --trace --strict=error --modulepath spec/fixtures -e \"#{manifest}\"")
      stdout_str
    end

    context 'when an error is raised during insync?' do
      let(:raising_resource_declaration) { "test_custom_insync { example: ensure => 'present', case_insensitive_string => 'RaiseError' }" }
      let(:manifest) { raising_resource_declaration.dup }

      it 'reports the error with stack trace and change failure' do
        expect(puppet_apply_stdout).to match %r{Error:}
        expect(puppet_apply_stdout).to match %r{block in def_custom_insync?}
        expect(puppet_apply_stdout).to match %r{change from 'FooBar' to 'RaiseError' failed}
      end

      context 'with a dependent resource' do
        let(:dependent_resource_declaration) do
          "test_custom_insync { dependent: ensure => 'present', case_insensitive_string => 'foobar', require => Test_custom_insync['example'] }"
        end
        let(:manifest) { "#{raising_resource_declaration} #{dependent_resource_declaration}" }

        it 'skips dependent resource because of failed dependencies' do
          expect(puppet_apply_stdout).to match %r{Test_custom_insync\[dependent\]: Skipping because of failed dependencies}
        end
      end
    end

    context 'when handling subset array comparisons' do
      let(:manifest) { "test_custom_insync { example: ensure => 'present', some_array => #{test_value} }" }

      context 'when the should array is an exact match for the actual value' do
        let(:test_value) { %w[a b] }

        it 'makes no changes and does not error' do
          expect(puppet_apply_stdout).not_to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end

      context 'when the should array members are all included in the actual value' do
        let(:test_value) { ['a'] }

        it 'makes no changes and does not error' do
          expect(puppet_apply_stdout).not_to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end

      context 'when the the actual value is missing a should array member' do
        let(:test_value) { %w[a b c] }

        it 'makes no changes and does not error' do
          expect(puppet_apply_stdout).to match %r{Adding missing members \["c"\]}
          expect(puppet_apply_stdout).to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end
    end

    context 'when handling forced array comparisons' do
      let(:manifest) { "test_custom_insync { example: ensure => 'present', some_array => #{test_value}, force => true }" }

      context 'when the should array is an exact match for the actual value' do
        let(:test_value) { %w[a b] }

        it 'makes no changes and does not error' do
          expect(puppet_apply_stdout).not_to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end

      context 'when the should array is an order insensitive match for the actual value' do
        let(:test_value) { %w[b a] }

        it 'makes no changes and does not error' do
          expect(puppet_apply_stdout).not_to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end

      context 'when the actual value includes members not found in the should array' do
        let(:test_value) { ['a'] }

        it 'removes the additional member from the array without erroring' do
          expect(puppet_apply_stdout).to match %r{some_array changed \['a', 'b'\] to \['a'\]}
          expect(puppet_apply_stdout).to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end

      context 'when the the actual value is missing a should array member' do
        let(:test_value) { %w[a b c] }

        it 'adds the missing member to the array without erroring' do
          expect(puppet_apply_stdout).to match %r{some_array changed \['a', 'b'\] to \['a', 'b', 'c'\]}
          expect(puppet_apply_stdout).to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end
    end

    context 'when handling case sensitive string comparisons' do
      let(:manifest) { "test_custom_insync { example: ensure => 'present', case_sensitive_string => '#{test_value}' }" }

      context 'when the strings match exactly' do
        let(:test_value) { 'FooBar' }

        it 'makes no changes and does not error' do
          expect(puppet_apply_stdout).not_to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end

      context 'when the strings differ' do
        let(:test_value) { 'foobar' }

        it 'changes the string value without erroring' do
          expect(puppet_apply_stdout).to match %r{case_sensitive_string changed 'FooBar' to 'foobar'}
          expect(puppet_apply_stdout).to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end
    end

    context 'when handling case insensitive string comparisons' do
      let(:manifest) { "test_custom_insync { example: ensure => 'present', case_insensitive_string => '#{test_value}' }" }

      context 'when the strings match exactly' do
        let(:test_value) { 'FooBar' }

        it 'makes no changes and does not error' do
          expect(puppet_apply_stdout).not_to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end

      context 'when the strings match exactly except for casing' do
        let(:test_value) { 'foobar' }

        it 'makes no changes and does not error' do
          expect(puppet_apply_stdout).not_to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end

      context 'when the strings differ' do
        let(:test_value) { 'FooBarBaz' }

        it 'changes the string value without erroring' do
          expect(puppet_apply_stdout).to match %r{case_insensitive_string changed 'FooBar' to 'FooBarBaz'}
          expect(puppet_apply_stdout).to match %r{Updating}
          expect(puppet_apply_stdout).not_to match %r{Error:}
        end
      end
    end

    context 'when handling versions' do
      let(:manifest) { "test_custom_insync { example: ensure => 'present', #{test_key_value_pairs} }" }

      context 'when handling exact version strings' do
        context 'when the should and actual versions are identical' do
          let(:test_key_value_pairs) { "version => '1.2.3'" }

          it 'makes no changes and does not error' do
            expect(puppet_apply_stdout).not_to match %r{Updating}
            expect(puppet_apply_stdout).not_to match %r{Error:}
          end
        end

        context 'when the should and actual versions differ' do
          let(:test_key_value_pairs) { "version => '3.2.1'" }

          it 'changes the version without erroring' do
            expect(puppet_apply_stdout).to match %r{version changed '1.2.3' to '3.2.1'}
            expect(puppet_apply_stdout).to match %r{Updating}
            expect(puppet_apply_stdout).not_to match %r{Error:}
          end
        end
      end

      context 'when handling custom version strings' do
        context 'when the custom version string comparison is satisfied by the actual value' do
          let(:test_key_value_pairs) { "version => '> 1.0.0'" }

          it 'makes no changes and does not error' do
            expect(puppet_apply_stdout).not_to match %r{Updating}
            expect(puppet_apply_stdout).not_to match %r{Error:}
          end
        end

        context 'when the custom version string comparison is not satisfied by the actual value' do
          let(:test_key_value_pairs) { "version => '> 2.0.0'" }

          it 'changes to a satisfactory version without erroring' do
            expect(puppet_apply_stdout).to match %r{The actual version \(1.2.3\) does not meet the custom version bound \(> 2.0.0\); updating to a version that does}
            expect(puppet_apply_stdout).to match %r{Updating}
            expect(puppet_apply_stdout).not_to match %r{Error:}
          end
        end
      end

      context 'when handling minimum version bounds' do
        context 'when the minimum version bound is satisfied by the actual value' do
          let(:test_key_value_pairs) { "minimum_version => '1.0.0'" }

          it 'makes no changes and does not error' do
            expect(puppet_apply_stdout).not_to match %r{Updating}
            expect(puppet_apply_stdout).not_to match %r{Error:}
          end
        end

        context 'when the minimum version bound is not satisfied by the actual value' do
          let(:test_key_value_pairs) { "minimum_version => '2.0.0'" }

          it 'changes to a satisfactory version without erroring' do
            expect(puppet_apply_stdout).to match %r{The actual version \(1.2.3\) does not meet the minimum version bound \(2.0.0\); updating to a version that does}
            expect(puppet_apply_stdout).to match %r{Updating}
            expect(puppet_apply_stdout).not_to match %r{Error:}
          end
        end
      end

      context 'when handling maximum version bounds' do
        context 'when the maximum version bound is satisfied by the actual value' do
          let(:test_key_value_pairs) { "maximum_version => '2.0.0'" }

          it 'makes no changes and does not error' do
            expect(puppet_apply_stdout).not_to match %r{Updating}
            expect(puppet_apply_stdout).not_to match %r{Error:}
          end
        end

        context 'when the maximum version bound is not satisfied by the actual value' do
          let(:test_key_value_pairs) { "maximum_version => '1.0.0'" }

          it 'changes to a satisfactory version without erroring' do
            expect(puppet_apply_stdout).to match %r{The actual version \(1.2.3\) does not meet the maximum version bound \(1.0.0\); updating to a version that does}
            expect(puppet_apply_stdout).to match %r{Updating}
            expect(puppet_apply_stdout).not_to match %r{Error:}
          end
        end
      end

      context 'when handling combined minimum and maximum version bounds' do
        context 'when the combined version bounds are satisfied by the actual value' do
          let(:test_key_value_pairs) { "minimum_version => '1.0.0', maximum_version => '2.0.0'" }

          it 'makes no changes and does not error' do
            expect(puppet_apply_stdout).not_to match %r{Updating}
            expect(puppet_apply_stdout).not_to match %r{Error:}
          end
        end

        context 'when the combined version bounds are not satisfied by the actual value' do
          let(:test_key_value_pairs) { "minimum_version => '1.0.0', maximum_version => '1.2.0'" }

          it 'changes to a satisfactory version without erroring' do
            expect(puppet_apply_stdout).to match %r{The actual version \(1.2.3\) does not meet the combined minimum \(1.0.0\) and maximum \(1.2.0\) bounds; updating to a version which does.}
            expect(puppet_apply_stdout).to match %r{Updating}
            expect(puppet_apply_stdout).not_to match %r{Error:}
          end
        end
      end
    end
  end
end