|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require_relative '../../tasks/configure' |
| 5 | + |
| 6 | +# rubocop:disable RSpec/MessageSpies |
| 7 | +# rubocop:disable RSpec/StubbedMock |
| 8 | +describe 'openvox_bootstrap::configure' do |
| 9 | + let(:tmpdir) { Dir.mktmpdir('openvox_bootstrap-configure-spec') } |
| 10 | + let(:task) { OpenvoxBootstrap::Configure.new } |
| 11 | + |
| 12 | + around do |example| |
| 13 | + example.run |
| 14 | + ensure |
| 15 | + FileUtils.remove_entry_secure(tmpdir) |
| 16 | + end |
| 17 | + |
| 18 | + describe '#write_puppet_conf' do |
| 19 | + let(:puppet_conf) do |
| 20 | + { |
| 21 | + 'main' => { |
| 22 | + 'server' => 'puppet.spec', |
| 23 | + 'certname' => 'agent.spec', |
| 24 | + }, |
| 25 | + 'agent' => { |
| 26 | + 'environment' => 'test' |
| 27 | + } |
| 28 | + } |
| 29 | + end |
| 30 | + let(:puppet_conf_path) { File.join(tmpdir, 'puppet.conf') } |
| 31 | + let(:puppet_conf_contents) do |
| 32 | + <<~CONF |
| 33 | + [main] |
| 34 | + server = puppet.spec |
| 35 | + certname = agent.spec |
| 36 | +
|
| 37 | + [agent] |
| 38 | + environment = test |
| 39 | + CONF |
| 40 | + end |
| 41 | + |
| 42 | + it 'writes a puppet.conf ini' do |
| 43 | + expect(task.write_puppet_conf(puppet_conf, tmpdir)).to( |
| 44 | + eq( |
| 45 | + { |
| 46 | + puppet_conf: { |
| 47 | + path: puppet_conf_path, |
| 48 | + contents: puppet_conf_contents |
| 49 | + } |
| 50 | + } |
| 51 | + ) |
| 52 | + ) |
| 53 | + expect(File.read(puppet_conf_path)).to eq(puppet_conf_contents) |
| 54 | + end |
| 55 | + |
| 56 | + it 'does nothing if given an empty config' do |
| 57 | + expect(task.write_puppet_conf(nil)).to eq({}) |
| 58 | + expect(task.write_puppet_conf({})).to eq({}) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + describe '#write_csr_attributes' do |
| 63 | + let(:csr_attributes) do |
| 64 | + { |
| 65 | + 'custom_attributes' => { |
| 66 | + '1.2.840.113549.1.9.7' => 'bar', |
| 67 | + }, |
| 68 | + 'extension_requests' => { |
| 69 | + 'pp_role' => 'spec' |
| 70 | + } |
| 71 | + } |
| 72 | + end |
| 73 | + let(:csr_attributes_path) { File.join(tmpdir, 'csr_attributes.yaml') } |
| 74 | + let(:csr_attributes_contents) do |
| 75 | + <<~YAML |
| 76 | + --- |
| 77 | + custom_attributes: |
| 78 | + 1.2.840.113549.1.9.7: bar |
| 79 | + extension_requests: |
| 80 | + pp_role: spec |
| 81 | + YAML |
| 82 | + end |
| 83 | + |
| 84 | + it 'writes a csr_attributes.yaml file' do |
| 85 | + expect(task.write_csr_attributes(csr_attributes, tmpdir)).to( |
| 86 | + eq( |
| 87 | + { |
| 88 | + csr_attributes: { |
| 89 | + path: csr_attributes_path, |
| 90 | + contents: csr_attributes_contents, |
| 91 | + } |
| 92 | + } |
| 93 | + ) |
| 94 | + ) |
| 95 | + expect(File.read(csr_attributes_path)).to eq(csr_attributes_contents) |
| 96 | + expect(File.stat(csr_attributes_path).mode & 0o777).to eq(0o640) |
| 97 | + end |
| 98 | + |
| 99 | + it 'does nothing if given an empty config' do |
| 100 | + expect(task.write_csr_attributes(nil)).to eq({}) |
| 101 | + expect(task.write_csr_attributes({})).to eq({}) |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + describe '#manage_puppet_service' do |
| 106 | + def status(code = 0) |
| 107 | + instance_double(Process::Status, exitstatus: code) |
| 108 | + end |
| 109 | + |
| 110 | + it 'is successful for a 0 exit code' do |
| 111 | + command = [ |
| 112 | + 'puppet', |
| 113 | + 'apply', |
| 114 | + '--detailed-exitcodes', |
| 115 | + '-e', |
| 116 | + %(service { 'puppet': ensure => running, enable => true, }), |
| 117 | + ] |
| 118 | + expect(Open3).to receive(:capture2e).with(*command).and_return(['applied', status]) |
| 119 | + |
| 120 | + expect(task.manage_puppet_service(true, true)).to eq( |
| 121 | + { |
| 122 | + puppet_service: { |
| 123 | + command: command.join(' '), |
| 124 | + output: 'applied', |
| 125 | + successful: true, |
| 126 | + } |
| 127 | + } |
| 128 | + ) |
| 129 | + end |
| 130 | + |
| 131 | + it 'is successful for a 2 exit code' do |
| 132 | + expect(Open3).to receive(:capture2e).and_return(['applied', status(2)]) |
| 133 | + |
| 134 | + result = task.manage_puppet_service(true, true) |
| 135 | + expect(result.dig(:puppet_service, :successful)).to be true |
| 136 | + end |
| 137 | + |
| 138 | + it 'fails for a non 0, 2 exit code' do |
| 139 | + expect(Open3).to receive(:capture2e).and_return(['applied', status(1)]) |
| 140 | + |
| 141 | + result = task.manage_puppet_service(true, true) |
| 142 | + expect(result.dig(:puppet_service, :successful)).to be false |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + describe '#task' do |
| 147 | + it 'returns a result hash if puppet service is managed successfully' do |
| 148 | + expect(task).to receive(:manage_puppet_service).and_return({ puppet_service: { successful: true } }) |
| 149 | + |
| 150 | + expect(task.task).to eq( |
| 151 | + { |
| 152 | + puppet_service: { successful: true }, |
| 153 | + } |
| 154 | + ) |
| 155 | + end |
| 156 | + |
| 157 | + it 'prints results and exits 1 if puppet service fails' do |
| 158 | + expect(task).to( |
| 159 | + receive(:manage_puppet_service). |
| 160 | + and_return( |
| 161 | + { |
| 162 | + puppet_service: { |
| 163 | + successful: false, |
| 164 | + output: "apply failed\n", |
| 165 | + } |
| 166 | + } |
| 167 | + ) |
| 168 | + ) |
| 169 | + |
| 170 | + expect { task.task }.to( |
| 171 | + raise_error(SystemExit).and( |
| 172 | + output(<<~EOM).to_stdout |
| 173 | + { |
| 174 | + "puppet_service": { |
| 175 | + "successful": false, |
| 176 | + "output": "apply failed\\n" |
| 177 | + } |
| 178 | + } |
| 179 | +
|
| 180 | + Failed managing the puppet service: |
| 181 | +
|
| 182 | + apply failed |
| 183 | + EOM |
| 184 | + ).and(output('').to_stderr) |
| 185 | + ) |
| 186 | + end |
| 187 | + end |
| 188 | +end |
| 189 | +# rubocop:enable RSpec/MessageSpies |
| 190 | +# rubocop:enable RSpec/StubbedMock |
0 commit comments