File: shared_examples.rb

package info (click to toggle)
puppet-module-keystone 25.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,428 kB
  • sloc: ruby: 9,684; pascal: 295; python: 38; makefile: 10; sh: 10
file content (129 lines) | stat: -rw-r--r-- 4,491 bytes parent folder | download
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
shared_examples_for "a Puppet::Error" do |description|
  it "with message matching #{description.inspect}" do
    expect { is_expected.to have_class_count(1) }.to raise_error(Puppet::Error, description)
  end
end

shared_examples_for 'parse title correctly' do |result|
  let(:title) do |example|
    example.metadata[:example_group][:description]
  end
  let(:resource) { described_class.new(:title => title) }
  it 'should parse this title correctly' do
    expect(resource.to_hash).to include(result)
  end
end

shared_examples_for 'croak on the title' do
  let(:title) do |example|
    example.metadata[:example_group][:description]
  end
  let(:resource) { described_class.new(:title => title) }
  it 'croak on the title' do
    expect { resource }.to raise_error(Puppet::Error, /No set of title patterns matched the title/)
  end
end

shared_examples_for 'croak on the required parameter' do |attr|
  let(:title) do |example|
    example.metadata[:example_group][:description]
  end
  prefix = attr.is_a?(String) ? attr : ''

  let(:resource) { described_class.new(:title => title) }
  it 'croak on the missing required parameter' do
    expect { resource }
      .to raise_error(Puppet::ResourceError, "#{prefix} Required parameter.")
  end
end

shared_examples_for 'croak on read-only parameter' do |resource|
  prefix = resource.delete(:_prefix)
  it 'should raise an error' do
    expect { described_class.new(resource) }
      .to raise_error(Puppet::ResourceError, "#{prefix} Read-only property.")
  end
end

shared_examples_for 'succeed with the required parameters' do |extra_params|
  let(:title) do |example|
    example.metadata[:example_group][:description]
  end
  extra_params_to_merge = extra_params || {}
  let(:resource) { described_class.new({ :title => title }.merge(extra_params_to_merge)) }
  it 'has all required parameters' do
    expect { resource }.not_to raise_error
  end
end

# Let resources to [<tested_resource>, <required>, <required>, ..., <not_required>]
shared_examples_for 'autorequire the correct resources' do
  let(:catalog) { Puppet::Resource::Catalog.new }
  it 'should autorequire correctly' do
    resource = resources[0]
    resources_good = resources[1...resources.count-1]
    catalog.add_resource(*resources)

    dependency = resource.autorequire
    expect(dependency.size).to eq(resources_good.count)
    resources_good.each_with_index do |good, idx|
      expect(dependency[idx].target).to eq(resource)
      expect(dependency[idx].source).to eq(good)
    end
  end
end

# Let resources to [<existing>, <non_existing>]
shared_examples_for 'prefetch the resources' do
  it 'should correctly prefetch the existing resource' do
    existing     = resources[0]
    non_existing = resources[1]
    resource = double
    r = []
    r << existing

    catalog = Puppet::Resource::Catalog.new
    r.each { |res| catalog.add_resource(res) }
    m_value = double
    m_first = double
    expect(resource).to receive(:values).and_return(m_value)
    expect(m_value).to receive(:first).and_return(m_first)
    expect(m_first).to receive(:catalog).and_return(catalog)
    expect(m_first).to receive(:class).and_return(described_class.resource_type)
    described_class.prefetch(resource)

    # found and not found
    expect(existing.provider.ensure).to eq(:present)
    expect(non_existing.provider.ensure).to eq(:absent)
  end
end

# attribute [Array[Hash]]
# - the first hash are the expected result
# - second are the combination of attributes you want to test
# The provider must be build from resource_attrs
# see examples in keystone_{user/user_role/tenant/service}
shared_examples_for 'create the correct resource' do |attributes|
  expected_results = attributes.shift['expected_results']
  attributes.each do |attribute|
    context 'test' do
      let(:resource_attrs) { attribute.values[0] }
      it "should correctly create the resource when #{attribute.keys[0]}" do
        provider.create
        expect(provider.exists?).to be_truthy
        expected_results.each do |key, value|
          expect(provider.send(key)).to eq(value)
        end
      end
    end
  end
end

# Let resources to [<resource_1>, <duplicate>]
shared_examples_for 'detect duplicate resource' do
  let(:catalog) { Puppet::Resource::Catalog.new }
  it 'should detect the duplicate' do
    expect { catalog.add_resource(resources[0]) }.not_to raise_error
    expect { catalog.add_resource(resources[1]) }.to raise_error(ArgumentError,/Cannot alias/)
  end
end