| 1 |
require 'puppet' |
| 2 |
require 'spec_helper' |
| 3 |
require 'puppet/provider/nova_aggregate/openstack' |
| 4 |
|
| 5 |
describe Puppet::Type.type(:nova_aggregate).provider(:openstack) do |
| 6 |
|
| 7 |
let(:set_env) do |
| 8 |
ENV['OS_USERNAME'] = 'test' |
| 9 |
ENV['OS_PASSWORD'] = 'abc123' |
| 10 |
ENV['OS_PROJECT_NAME'] = 'test' |
| 11 |
ENV['OS_AUTH_URL'] = 'http://127.0.0.1:5000/v3' |
| 12 |
end |
| 13 |
|
| 14 |
describe 'managing aggregates' do |
| 15 |
|
| 16 |
let(:aggregate_attrs) do |
| 17 |
{ |
| 18 |
:name => 'just', |
| 19 |
:availability_zone => 'simple', |
| 20 |
:hosts => ['example'], |
| 21 |
:ensure => 'present', |
| 22 |
:metadata => {'nice' => 'cookie'}, |
| 23 |
} |
| 24 |
end |
| 25 |
|
| 26 |
let(:resource) do |
| 27 |
Puppet::Type::Nova_aggregate.new(aggregate_attrs) |
| 28 |
end |
| 29 |
|
| 30 |
let(:provider) do |
| 31 |
described_class.new(resource) |
| 32 |
end |
| 33 |
|
| 34 |
before(:each) do |
| 35 |
set_env |
| 36 |
end |
| 37 |
|
| 38 |
describe '#instances' do |
| 39 |
it 'finds existing aggregates' do |
| 40 |
expect(described_class).to receive(:openstack) |
| 41 |
.with('aggregate', 'list', '--quiet', '--format', 'csv', []) |
| 42 |
.and_return('"ID","Name","Availability Zone" |
| 43 |
just,"simple","just" |
| 44 |
') |
| 45 |
expect(described_class).to receive(:openstack) |
| 46 |
.with('aggregate', 'show', '--format', 'shell', 'simple') |
| 47 |
.and_return('"id="just" |
| 48 |
name="simple" |
| 49 |
availability_zone=just" |
| 50 |
properties="{\'key1\': \'tomato\', \'key2\':\'mushroom\'}" |
| 51 |
hosts="[]" |
| 52 |
') |
| 53 |
instances = described_class.instances |
| 54 |
expect(instances.count).to eq(1) |
| 55 |
expect(instances[0].name).to eq('simple') |
| 56 |
expect(instances[0].metadata).to eq({"key1"=>"tomato", "key2"=>"mushroom"}) |
| 57 |
end |
| 58 |
end |
| 59 |
|
| 60 |
describe '#create' do |
| 61 |
it 'creates aggregate' do |
| 62 |
expect(described_class).to receive(:openstack) |
| 63 |
.with('aggregate', 'create', '--format', 'shell', |
| 64 |
['just', '--zone', 'simple', '--property', 'nice=cookie' ]) |
| 65 |
.and_return('name="just" |
| 66 |
id="just" |
| 67 |
availability_zone="simple" |
| 68 |
properties="{\'nice\': \'cookie\'}" |
| 69 |
hosts="[]" |
| 70 |
') |
| 71 |
expect(described_class).to receive(:openstack) |
| 72 |
.with('aggregate', 'add host', ['just', 'example']) |
| 73 |
.and_return('name="just" |
| 74 |
id="just" |
| 75 |
availability_zone="simple" |
| 76 |
properties="{\'nice\': \'cookie\'}" |
| 77 |
hosts="[\'example\']" |
| 78 |
') |
| 79 |
|
| 80 |
provider.create |
| 81 |
expect(provider.exists?).to be_truthy |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
describe '#destroy' do |
| 86 |
it 'removes aggregate with hosts' do |
| 87 |
expect(described_class).to receive(:openstack) |
| 88 |
.with('aggregate', 'remove host', ['just', 'example']) |
| 89 |
expect(described_class).to receive(:openstack) |
| 90 |
.with('aggregate', 'delete', 'just') |
| 91 |
provider.instance_variable_set(:@property_hash, aggregate_attrs) |
| 92 |
provider.destroy |
| 93 |
expect(provider.exists?).to be_falsey |
| 94 |
end |
| 95 |
end |
| 96 |
end |
| 97 |
|
| 98 |
|
| 99 |
describe 'managing aggregates with filter_hosts toggled' do |
| 100 |
|
| 101 |
# instantiation attributes for the provider with filter_hosts set. |
| 102 |
let(:aggregate_attrs) do |
| 103 |
{ |
| 104 |
:name => 'just', |
| 105 |
:availability_zone => 'simple', |
| 106 |
:hosts => ['known'], |
| 107 |
:ensure => 'present', |
| 108 |
:metadata => {'nice' => 'cookie'}, |
| 109 |
:filter_hosts => 'true' |
| 110 |
} |
| 111 |
end |
| 112 |
|
| 113 |
let(:resource) do |
| 114 |
Puppet::Type::Nova_aggregate.new(aggregate_attrs) |
| 115 |
end |
| 116 |
|
| 117 |
let(:provider) do |
| 118 |
described_class.new(resource) |
| 119 |
end |
| 120 |
|
| 121 |
before(:each) do |
| 122 |
set_env |
| 123 |
end |
| 124 |
|
| 125 |
# create an aggregate and actually aggregate hosts to it |
| 126 |
describe 'create aggregate and add/remove hosts with filter_hosts toggled' do |
| 127 |
|
| 128 |
it 'creates aggregate with filter_hosts toggled' do |
| 129 |
|
| 130 |
allow(provider.class).to receive(:get_known_hosts) |
| 131 |
.and_return(['known', 'known_too']) |
| 132 |
|
| 133 |
# these expectations are the actual tests that check the provider's behaviour |
| 134 |
# and make sure only known hosts ('known' is the only known host) will be |
| 135 |
# aggregated. |
| 136 |
|
| 137 |
expect(described_class).to receive(:openstack) |
| 138 |
.with('aggregate', 'create', '--format', 'shell', ['just', '--zone', 'simple', "--property", "nice=cookie"]) |
| 139 |
.once |
| 140 |
.and_return('name="just" |
| 141 |
id="just" |
| 142 |
availability_zone="simple" |
| 143 |
properties="{\'nice\': \'cookie\'}" |
| 144 |
hosts="[]" |
| 145 |
') |
| 146 |
|
| 147 |
expect(described_class).to receive(:openstack) |
| 148 |
.with('aggregate', 'add host', ['just', 'known']) |
| 149 |
.once |
| 150 |
.and_return('name="just" |
| 151 |
id="just" |
| 152 |
availability_zone="simple" |
| 153 |
properties="{\'nice\': \'cookie\'}" |
| 154 |
hosts="[\'known\']" |
| 155 |
') |
| 156 |
|
| 157 |
expect(described_class).to receive(:openstack) |
| 158 |
.with('aggregate', 'add host', ['just', 'known_too']) |
| 159 |
.once |
| 160 |
.and_return('name="just" |
| 161 |
id="just" |
| 162 |
availability_zone="simple" |
| 163 |
properties="{\'nice\': \'cookie\'}" |
| 164 |
hosts="[\'known\', \'known_too\']" |
| 165 |
') |
| 166 |
|
| 167 |
expect(described_class).to receive(:openstack) |
| 168 |
.with('aggregate', 'remove host', ['just', 'known']) |
| 169 |
.once |
| 170 |
.and_return('name="just" |
| 171 |
id="just" |
| 172 |
availability_zone="simple" |
| 173 |
properties="{\'nice\': \'cookie\'}" |
| 174 |
hosts="[\'known_too\']" |
| 175 |
') |
| 176 |
|
| 177 |
# this creates a provider with the attributes defined above as 'aggregate_attrs' |
| 178 |
# and tries to add some hosts and then to remove some hosts. |
| 179 |
# the hosts will be filtered against known active hosts and the expectations |
| 180 |
# described above are the actual tests that check the provider's behaviour |
| 181 |
|
| 182 |
provider.create |
| 183 |
property_hash = provider.instance_variable_get(:@property_hash) |
| 184 |
property_hash[:hosts] = ['known'] |
| 185 |
provider.instance_variable_set(:@property_hash, property_hash) |
| 186 |
|
| 187 |
provider.hosts = ['known', 'known_too', 'unknown'] |
| 188 |
property_hash = provider.instance_variable_get(:@property_hash) |
| 189 |
property_hash[:hosts] = ['known', 'known_too'] |
| 190 |
provider.instance_variable_set(:@property_hash, property_hash) |
| 191 |
|
| 192 |
provider.hosts = ['known_too'] |
| 193 |
|
| 194 |
end |
| 195 |
end |
| 196 |
end |
| 197 |
|
| 198 |
end |