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
|
# frozen_string_literal: true
require 'spec_helper_rspec'
shared_examples 'keystore instance' do |instance|
describe "instance #{instance}" do
subject { described_class.instances.find { |x| x.name == instance } }
it { expect(subject).to be_exists }
it { expect(subject.name).to eq(instance) }
it {
expect(subject.settings).
to eq(['node.name', 'cloud.aws.access_key'])
}
end
end
describe Puppet::Type.type(:elasticsearch_keystore).provider(:elasticsearch_keystore) do
let(:executable) { '/usr/share/elasticsearch/bin/elasticsearch-keystore' }
let(:instances) { [] }
before do
Facter.clear
Facter.add('osfamily') { setcode { 'Debian' } }
allow(described_class).
to receive(:command).
with(:keystore).
and_return(executable)
allow(File).to receive(:exist?).
with('/etc/elasticsearch/scripts/elasticsearch.keystore').
and_return(false)
end
describe 'instances' do
before do
allow(Dir).to receive(:[]).
with('/etc/elasticsearch/*').
and_return((['scripts'] + instances).map do |directory|
"/etc/elasticsearch/#{directory}"
end)
instances.each do |instance|
instance_dir = "/etc/elasticsearch/#{instance}"
defaults_file = "/etc/default/elasticsearch-#{instance}"
allow(File).to receive(:exist?).
with("#{instance_dir}/elasticsearch.keystore").
and_return(true)
allow(described_class).
to receive(:execute).
with(
[executable, 'list'],
{
custom_environment: {
'ES_INCLUDE' => defaults_file,
'ES_PATH_CONF' => "/etc/elasticsearch/#{instance}"
},
uid: 'elasticsearch',
gid: 'elasticsearch',
failonfail: true
}
).
and_return(
Puppet::Util::Execution::ProcessOutput.new(
"node.name\ncloud.aws.access_key\n", 0
)
)
end
end
it 'has an instance method' do
expect(described_class).to respond_to(:instances)
end
context 'without any keystores' do
it 'returns no resources' do
expect(described_class.instances.size).to eq(0)
end
end
context 'with one instance' do
let(:instances) { ['es-01'] }
it { expect(described_class.instances.length).to eq(instances.length) }
include_examples 'keystore instance', 'es-01'
end
context 'with multiple instances' do
let(:instances) { %w[es-01 es-02] }
it { expect(described_class.instances.length).to eq(instances.length) }
include_examples 'keystore instance', 'es-01'
include_examples 'keystore instance', 'es-02'
end
end
describe 'prefetch' do
it 'has a prefetch method' do
expect(described_class).to respond_to :prefetch
end
end
describe 'flush' do
let(:provider) { described_class.new(name: 'es-03') }
let(:resource) do
Puppet::Type.type(:elasticsearch_keystore).new(
name: 'es-03',
provider: provider
)
end
it 'creates the keystore' do
allow(described_class).to(
receive(:execute).
with(
[executable, 'create'],
{
custom_environment: {
'ES_INCLUDE' => '/etc/default/elasticsearch-es-03',
'ES_PATH_CONF' => '/etc/elasticsearch/es-03'
},
uid: 'elasticsearch',
gid: 'elasticsearch',
failonfail: true
}
).
and_return(Puppet::Util::Execution::ProcessOutput.new('', 0))
)
resource[:ensure] = :present
provider.create
provider.flush
expect(described_class).to(
have_received(:execute).
with(
[executable, 'create'],
{
custom_environment: {
'ES_INCLUDE' => '/etc/default/elasticsearch-es-03',
'ES_PATH_CONF' => '/etc/elasticsearch/es-03'
},
uid: 'elasticsearch',
gid: 'elasticsearch',
failonfail: true
}
)
)
end
it 'deletes the keystore' do
allow(File).to(
receive(:delete).
with(File.join(%w[/ etc elasticsearch es-03 elasticsearch.keystore]))
)
resource[:ensure] = :absent
provider.destroy
provider.flush
expect(File).to(
have_received(:delete).
with(File.join(%w[/ etc elasticsearch es-03 elasticsearch.keystore]))
)
end
it 'updates settings' do
settings = {
'cloud.aws.access_key' => 'AKIAFOOBARFOOBAR',
'cloud.aws.secret_key' => 'AKIAFOOBARFOOBAR'
}
settings.each do |setting, value|
allow(provider.class).to(
receive(:run_keystore).
with(['add', '--force', '--stdin', setting], 'es-03', '/etc/elasticsearch', value).
and_return(Puppet::Util::Execution::ProcessOutput.new('', 0))
)
end
# Note that the settings hash is passed in wrapped in an array to mimic
# the behavior in real-world puppet runs.
resource[:ensure] = :present
resource[:settings] = [settings]
provider.settings = [settings]
provider.flush
settings.each do |setting, value|
expect(provider.class).to(
have_received(:run_keystore).
with(['add', '--force', '--stdin', setting], 'es-03', '/etc/elasticsearch', value)
)
end
end
end
end
|