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
|
require 'spec_helper_unit'
describe RSpec::Puppet::ManifestMatchers::CountGeneric do
subject(:matcher) { described_class.new(type, expected, method) }
let(:actual) do
lambda { test_double(Puppet::Resource::Catalog, :resources => resource_objects) }
end
let(:resource_objects) do
resources.map do |type, title|
test_double(Puppet::Resource, :ref => "#{type}[#{title}]", :type => type)
end
end
let(:resources) { [] }
let(:type) { nil }
let(:expected) { 0 }
let(:method) { nil }
let(:default_resources) do
[
['Class', 'main'],
['Class', 'Settings'],
['Stage', 'main'],
]
end
it 'is not a diffable matcher' do
pending 'method not implemented'
expect(matcher).not_to be_diffable
end
describe '#initialize' do
context 'when initialised with a specified type' do
context 'and the type is a single namespace segment' do
let(:type) { 'class' }
it 'capitalises the type' do
expect(matcher.resource_type).to eq('Class')
end
end
context 'and the type is multiple namespaced segments' do
let(:type) { 'test::type' }
it 'capitalises each segment of the type' do
pending 'bug - not implemented'
expect(matcher.resource_type).to eq('Test::Type')
end
end
end
context 'when initialised with a method name via method_missing' do
let(:type) { nil }
context 'and the type is a single namespace segment' do
let(:method) { 'have_class_resource_count' }
it 'extracts the type from the method name and capitalises it' do
expect(matcher.resource_type).to eq('Class')
end
end
context 'and the type is multiple namespaced segments' do
let(:method) { 'have_test__type_resource_count' }
it 'extracts the type from the method name and capitalises each segment' do
expect(matcher.resource_type).to eq('Test::Type')
end
end
end
end
describe '#description' do
subject(:description) { matcher.description }
context 'when counting classes in the catalogue' do
let(:type) { 'class' }
context 'and only a single class is expected' do
let(:expected) { 1 }
it 'describes an expectation of a singular class' do
expect(description).to eq('contain exactly 1 class')
end
end
context 'and more than one class is expected' do
let(:expected) { 2 }
it 'describes an expectation of plural classes' do
expect(description).to eq('contain exactly 2 classes')
end
end
end
context 'when counting all resources' do
let(:type) { 'resource' }
context 'and only a single resource is expected' do
let(:expected) { 1 }
it 'describes an expectation of a singular resource' do
expect(description).to eq('contain exactly 1 resource')
end
end
context 'and more than one resource is expected' do
let(:expected) { 2 }
it 'describes an expectation of plural resources' do
expect(description).to eq('contain exactly 2 resources')
end
end
end
context 'when counting resources of a particular type' do
let(:type) { 'exec' }
context 'and only a single resource is expected' do
let(:expected) { 1 }
it 'describes an expectation of a singular resource type' do
expect(description).to eq('contain exactly 1 Exec resource')
end
end
context 'and more than one resource is expected' do
let(:expected) { 2 }
it 'describes an expectation of plural resources of a type' do
expect(description).to eq('contain exactly 2 Exec resources')
end
end
end
end
describe '#matches?' do
subject(:match) { matcher.matches?(actual) }
context 'when counting all resources' do
let(:type) { 'resource' }
let(:expected) { 0 }
let(:resources) do
[
['Class', 'test'],
['Node', 'testhost.test.com'],
]
end
it 'does not include Class, Node or default resources in the count' do
expect(match).to be_truthy
end
context 'and the catalogue contains a number of countable resources' do
let(:resources) do
super() + [
['File', '/tmp/testfile'],
['Exec', 'some command'],
['Service', 'a service'],
]
end
context 'and the expected value matches the resource count' do
let(:expected) { 3 }
it 'returns true' do
expect(match).to be_truthy
end
end
context 'and the expected value does not match the resource count' do
let(:expected) { 4 }
it 'returns false' do
expect(match).to be_falsey
end
end
end
end
context 'and counting resources of a particular type' do
let(:type) { 'class' }
let(:expected) { 1 }
let(:resources) do
[
['Class', 'test'],
['File', 'testfile'],
]
end
it 'does not include default resources of that type in the resource count' do
expect(match).to be_truthy
end
end
end
describe '#failure_message' do
let(:expected) { 999 }
let(:type) { 'class' }
it 'provides the description of the failure and the actual value' do
matcher.matches?(actual)
msg = 'expected that the catalogue would contain exactly 999 classes but it contains 0'
expect(matcher.failure_message).to eq(msg)
end
end
describe '#failure_message_when_negated' do
let(:expected) { 999 }
let(:type) { 'class' }
it 'provides the description of the failure' do
matcher.matches?(actual)
msg = 'expected that the catalogue would not contain exactly 999 classes but it does'
expect(matcher.failure_message_when_negated).to eq(msg)
end
end
end
|