|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'webmock/rspec' |
| 5 | +require_relative '../../tasks/provision_service' |
| 6 | + |
| 7 | +ENV['GITHUB_RUN_ID'] = '1234567890' |
| 8 | +ENV['GITHUB_URL'] = 'https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890' |
| 9 | + |
| 10 | +describe 'ProvisionService' do |
| 11 | + describe '.run' do |
| 12 | + context 'when inputs are invalid' do |
| 13 | + it 'return exception' do |
| 14 | + json_input = '{}' |
| 15 | + allow($stdin).to receive(:read).and_return(json_input) |
| 16 | + |
| 17 | + expect { ProvisionService.run }.to( |
| 18 | + raise_error(SystemExit) { |e| |
| 19 | + expect(e.status).to eq(0) |
| 20 | + }.and( |
| 21 | + output(%r{Unknown action}).to_stdout, |
| 22 | + ), |
| 23 | + ) |
| 24 | + end |
| 25 | + |
| 26 | + it 'return exception about invalid action' do |
| 27 | + json_input = '{"action":"foo","platform":"bar"}' |
| 28 | + allow($stdin).to receive(:read).and_return(json_input) |
| 29 | + |
| 30 | + expect { ProvisionService.run }.to( |
| 31 | + raise_error(SystemExit) { |e| |
| 32 | + expect(e.status).to eq(0) |
| 33 | + }.and( |
| 34 | + output(%r{Unknown action 'foo'}).to_stdout, |
| 35 | + ), |
| 36 | + ) |
| 37 | + end |
| 38 | + |
| 39 | + it 'return exception for missing platform' do |
| 40 | + json_input = '{"action":"provision"}' |
| 41 | + allow($stdin).to receive(:read).and_return(json_input) |
| 42 | + |
| 43 | + expect { ProvisionService.run }.to( |
| 44 | + raise_error(SystemExit) { |e| |
| 45 | + expect(e.status).to eq(1) |
| 46 | + }.and( |
| 47 | + output(%r{specify a platform when provisioning}).to_stdout, |
| 48 | + ), |
| 49 | + ) |
| 50 | + end |
| 51 | + |
| 52 | + it 'return exception for missing node_name' do |
| 53 | + json_input = '{"action":"tear_down"}' |
| 54 | + allow($stdin).to receive(:read).and_return(json_input) |
| 55 | + |
| 56 | + expect { ProvisionService.run }.to( |
| 57 | + raise_error(SystemExit) { |e| |
| 58 | + expect(e.status).to eq(1) |
| 59 | + }.and( |
| 60 | + output(%r{specify a node_name when tearing down}).to_stdout, |
| 61 | + ), |
| 62 | + ) |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + describe '#provision' do |
| 68 | + let(:inventory_location) { "#{Dir.pwd}/litmus_inventory.yaml" } |
| 69 | + let(:vars) { nil } |
| 70 | + let(:platform) { 'centos-8' } |
| 71 | + let(:retry_attempts) { 8 } |
| 72 | + let(:response_body) do |
| 73 | + { |
| 74 | + 'groups' => [ |
| 75 | + 'targets' => { |
| 76 | + 'uri' => '127.0.0.1' |
| 77 | + }, |
| 78 | + ] |
| 79 | + } |
| 80 | + end |
| 81 | + let(:provision_service) { ProvisionService.new } |
| 82 | + |
| 83 | + context 'when response is empty' do |
| 84 | + it 'return exception' do |
| 85 | + stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision') |
| 86 | + .with( |
| 87 | + body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}', |
| 88 | + headers: { |
| 89 | + 'Accept' => 'application/json', |
| 90 | + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', |
| 91 | + 'Content-Type' => 'application/json', |
| 92 | + 'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app', |
| 93 | + 'User-Agent' => 'Ruby' |
| 94 | + }, |
| 95 | + ) |
| 96 | + .to_return(status: 200, body: '', headers: {}) |
| 97 | + expect { provision_service.provision(platform, inventory_location, vars, retry_attempts) }.to raise_error(RuntimeError) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context 'when successive retry success' do |
| 102 | + it 'return valid response' do |
| 103 | + stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision') |
| 104 | + .with( |
| 105 | + body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}', |
| 106 | + headers: { |
| 107 | + 'Accept' => 'application/json', |
| 108 | + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', |
| 109 | + 'Content-Type' => 'application/json', |
| 110 | + 'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app', |
| 111 | + 'User-Agent' => 'Ruby' |
| 112 | + }, |
| 113 | + ) |
| 114 | + .to_return(status: 200, body: '', headers: {}) |
| 115 | + expect { provision_service.provision(platform, inventory_location, vars, retry_attempts) }.to raise_error(RuntimeError) |
| 116 | + stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision') |
| 117 | + .with( |
| 118 | + body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}', |
| 119 | + headers: { |
| 120 | + 'Accept' => 'application/json', |
| 121 | + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', |
| 122 | + 'Content-Type' => 'application/json', |
| 123 | + 'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app', |
| 124 | + 'User-Agent' => 'Ruby' |
| 125 | + }, |
| 126 | + ) |
| 127 | + .to_return(status: 200, body: response_body.to_json, headers: {}) |
| 128 | + allow(File).to receive(:open) |
| 129 | + expect(provision_service.provision(platform, inventory_location, vars, retry_attempts)[:status]).to eq('ok') |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + context 'when response is avlid' do |
| 134 | + it 'return valid response' do |
| 135 | + stub_request(:post, 'https://facade-release-6f3kfepqcq-ew.a.run.app/v1/provision') |
| 136 | + .with( |
| 137 | + body: '{"url":"https://api.github.com/repos/puppetlabs/puppetlabs-iis/actions/runs/1234567890","VMs":[{"cloud":null,"region":null,"zone":null,"images":["centos-8"]}]}', |
| 138 | + headers: { |
| 139 | + 'Accept' => 'application/json', |
| 140 | + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', |
| 141 | + 'Content-Type' => 'application/json', |
| 142 | + 'Host' => 'facade-release-6f3kfepqcq-ew.a.run.app', |
| 143 | + 'User-Agent' => 'Ruby' |
| 144 | + }, |
| 145 | + ) |
| 146 | + .to_return(status: 200, body: response_body.to_json, headers: {}) |
| 147 | + |
| 148 | + allow(File).to receive(:open) |
| 149 | + expect(provision_service.provision(platform, inventory_location, vars, retry_attempts)[:status]).to eq('ok') |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | +end |
0 commit comments