Skip to content

Commit 3625b64

Browse files
committed
Add tests for current provider.get call behavior
1 parent ef0f655 commit 3625b64

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed

spec/acceptance/get_calls_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'tempfile'
5+
6+
RSpec.describe 'minimizing provider get calls' do
7+
let(:common_args) { '--verbose --trace --strict=error --modulepath spec/fixtures' }
8+
9+
describe 'using `puppet resource` with a basic type' do
10+
it 'calls get 1 time' do
11+
stdout_str, status = Open3.capture2e("puppet resource #{common_args} test_get_calls_basic")
12+
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 1 times}
13+
expect(stdout_str).not_to match %r{Notice: test_get_calls_basic: Provider get called 2 times}
14+
expect(status).to eq 0
15+
end
16+
end
17+
18+
describe 'using `puppet apply` with a basic type' do
19+
it 'calls get 2 times' do
20+
stdout_str, _status = Open3.capture2e("puppet apply #{common_args} -e \"test_get_calls_basic { foo: } test_get_calls_basic { bar: }\"")
21+
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 1 times}
22+
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 2 times}
23+
expect(stdout_str).not_to match %r{Notice: test_get_calls_basic: Provider get called 3 times}
24+
expect(stdout_str).not_to match %r{Creating}
25+
end
26+
27+
it 'calls get 3 times with resource purging' do
28+
stdout_str, _status = Open3.capture2e("puppet apply #{common_args} -e \"test_get_calls_basic { foo: } test_get_calls_basic { bar: } resources { test_get_calls_basic: purge => true }\"")
29+
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 1 times}
30+
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 2 times}
31+
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 3 times}
32+
expect(stdout_str).not_to match %r{Notice: test_get_calls_basic: Provider get called 4 times}
33+
expect(stdout_str).not_to match %r{Creating}
34+
end
35+
end
36+
37+
describe 'using `puppet resource` with a simple_get_filter type' do
38+
it 'calls get 1 time' do
39+
stdout_str, status = Open3.capture2e("puppet resource #{common_args} test_get_calls_sgf")
40+
expect(stdout_str).to match %r{Notice: test_get_calls_sgf: Provider get called 1 times}
41+
expect(stdout_str).not_to match %r{Notice: test_get_calls_sgf: Provider get called 2 times}
42+
expect(status.exitstatus).to be_zero
43+
end
44+
end
45+
46+
describe 'using `puppet apply` with a type using simple_get_filter' do
47+
it 'calls get 2 times' do
48+
stdout_str, _status = Open3.capture2e("puppet apply #{common_args} -e \"test_get_calls_sgf { foo: } test_get_calls_sgf { bar: }\"")
49+
expect(stdout_str).to match %r{Notice: test_get_calls_sgf: Provider get called 1 times}
50+
expect(stdout_str).to match %r{Notice: test_get_calls_sgf: Provider get called 2 times}
51+
expect(stdout_str).not_to match %r{Notice: test_get_calls_sgf: Provider get called 3 times}
52+
expect(stdout_str).not_to match %r{Creating}
53+
end
54+
55+
it 'calls get 3 times when resource purging' do
56+
stdout_str, _status = Open3.capture2e("puppet apply #{common_args} -e \"test_get_calls_sgf { foo: } test_get_calls_sgf { bar: } resources { test_get_calls_sgf: purge => true }\"")
57+
expect(stdout_str).to match %r{Notice: test_get_calls_sgf: Provider get called 1 times}
58+
expect(stdout_str).to match %r{Notice: test_get_calls_sgf: Provider get called 2 times}
59+
expect(stdout_str).to match %r{Notice: test_get_calls_sgf: Provider get called 3 times}
60+
expect(stdout_str).not_to match %r{Notice: test_get_calls_sgf: Provider get called 4 times}
61+
expect(stdout_str).not_to match %r{Creating}
62+
end
63+
end
64+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require 'puppet/resource_api/simple_provider'
4+
5+
# Implementation for the test_get_calls_basic type using the Resource API.
6+
class Puppet::Provider::TestGetCallsBasic::TestGetCallsBasic < Puppet::ResourceApi::SimpleProvider
7+
def get(context)
8+
@count ||= 0
9+
@count += 1
10+
context.notice("Provider get called #{@count} times")
11+
[
12+
{
13+
name: 'foo',
14+
ensure: 'present',
15+
prop: 'fooprop',
16+
},
17+
{
18+
name: 'bar',
19+
ensure: 'present',
20+
prop: 'barprop',
21+
},
22+
]
23+
end
24+
25+
def create(context, name, should)
26+
context.notice("Creating '#{name}' with #{should.inspect}")
27+
end
28+
29+
def update(context, name, should)
30+
context.notice("Updating '#{name}' with #{should.inspect}")
31+
end
32+
33+
def delete(context, name)
34+
context.notice("Deleting '#{name}'")
35+
end
36+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require 'puppet/resource_api/simple_provider'
4+
5+
# Implementation for the test_get_calls_sgf type using the Resource API.
6+
class Puppet::Provider::TestGetCallsSgf::TestGetCallsSgf < Puppet::ResourceApi::SimpleProvider
7+
def get(context, names = nil)
8+
@count ||= 0
9+
@count += 1
10+
context.notice("Provider get called #{@count} times with names=#{names}")
11+
data = [
12+
{
13+
name: 'foo',
14+
ensure: 'present',
15+
},
16+
{
17+
name: 'bar',
18+
ensure: 'present',
19+
},
20+
]
21+
if names.nil?
22+
data
23+
else
24+
data.select { |r| names.include?(r[:name]) }
25+
end
26+
end
27+
28+
def create(context, name, should)
29+
context.notice("Creating '#{name}' with #{should.inspect}")
30+
end
31+
32+
def update(context, name, should)
33+
context.notice("Updating '#{name}' with #{should.inspect}")
34+
end
35+
36+
def delete(context, name)
37+
context.notice("Deleting '#{name}'")
38+
end
39+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require 'puppet/resource_api'
4+
5+
Puppet::ResourceApi.register_type(
6+
name: 'test_get_calls_basic',
7+
docs: <<-EOS,
8+
@summary a test_get_calls_basic type
9+
@example
10+
test_get_calls_basic { 'foo':
11+
ensure => 'present',
12+
}
13+
14+
This type provides Puppet with the capabilities to manage ...
15+
16+
If your type uses autorequires, please document as shown below, else delete
17+
these lines.
18+
**Autorequires**:
19+
* `Package[foo]`
20+
EOS
21+
features: [],
22+
attributes: {
23+
ensure: {
24+
type: 'Enum[present, absent]',
25+
desc: 'Whether this resource should be present or absent on the target system.',
26+
default: 'present',
27+
},
28+
name: {
29+
type: 'String',
30+
desc: 'The name of the resource you want to manage.',
31+
behaviour: :namevar,
32+
},
33+
prop: {
34+
type: 'Optional[String]',
35+
desc: 'A property to manage.',
36+
},
37+
},
38+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require 'puppet/resource_api'
4+
5+
Puppet::ResourceApi.register_type(
6+
name: 'test_get_calls_sgf',
7+
docs: <<-EOS,
8+
@summary a test_get_calls_sgf type
9+
@example
10+
test_get_calls_sgf { 'foo':
11+
ensure => 'present',
12+
}
13+
14+
This type provides Puppet with the capabilities to manage ...
15+
16+
If your type uses autorequires, please document as shown below, else delete
17+
these lines.
18+
**Autorequires**:
19+
* `Package[foo]`
20+
EOS
21+
features: ['simple_get_filter'],
22+
attributes: {
23+
ensure: {
24+
type: 'Enum[present, absent]',
25+
desc: 'Whether this resource should be present or absent on the target system.',
26+
default: 'present',
27+
},
28+
name: {
29+
type: 'String',
30+
desc: 'The name of the resource you want to manage.',
31+
behaviour: :namevar,
32+
},
33+
prop: {
34+
type: 'Optional[String]',
35+
desc: 'A property to manage.',
36+
},
37+
},
38+
)

0 commit comments

Comments
 (0)