|
73 | 73 | @context = context |
74 | 74 | { |
75 | 75 | code: 200, |
76 | | - parsed: JSON.parse(File.read(OctocatalogDiff::Spec.fixture_path('catalogs/tiny-catalog.json'))) |
| 76 | + parsed: JSON.parse(File.read(OctocatalogDiff::Spec.fixture_path(fixture_catalog))) |
77 | 77 | } |
78 | 78 | end |
79 | 79 | end |
80 | 80 |
|
81 | 81 | let(:api_url) do |
82 | 82 | { |
83 | 83 | 2 => 'https://fake-puppetmaster.non-existent-domain.com:8140/foobranch/catalog/foo', |
84 | | - 3 => 'https://fake-puppetmaster.non-existent-domain.com:8140/puppet/v3/catalog/foo' |
| 84 | + 3 => 'https://fake-puppetmaster.non-existent-domain.com:8140/puppet/v3/catalog/foo', |
| 85 | + 4 => 'https://fake-puppetmaster.non-existent-domain.com:8140/puppet/v4/catalog' |
85 | 86 | } |
86 | 87 | end |
87 | 88 |
|
88 | 89 | let(:api_sets_environment) { { 2 => false, 3 => true } } |
| 90 | + let(:fixture_catalog) { 'catalogs/tiny-catalog.json' } |
89 | 91 |
|
90 | 92 | [2, 3].each do |api_version| |
91 | 93 | context "api v#{api_version}" do |
|
100 | 102 | expect(@url).to eq(api_url[api_version]) |
101 | 103 | end |
102 | 104 |
|
| 105 | + it 'should set the Accept header' do |
| 106 | + expect(@opts[:headers]['Accept']).to eq('text/pson') |
| 107 | + end |
| 108 | + |
103 | 109 | it 'should post the correct facts to HTTParty' do |
104 | 110 | answer = JSON.parse(File.read(OctocatalogDiff::Spec.fixture_path('facts/facts_esc.json'))) |
105 | 111 | answer.delete('_timestamp') |
|
136 | 142 | end |
137 | 143 | end |
138 | 144 |
|
| 145 | + context 'api v4' do |
| 146 | + let(:fixture_catalog) { 'catalogs/tiny-catalog-v4-api.json' } |
| 147 | + let(:extra_opts) { {} } |
| 148 | + |
| 149 | + before(:each) do |
| 150 | + opts = { puppet_master_api_version: 4 } |
| 151 | + @obj = OctocatalogDiff::Catalog::PuppetMaster.new(valid_options.merge(opts).merge(extra_opts)) |
| 152 | + @logger, @logger_str = OctocatalogDiff::Spec.setup_logger |
| 153 | + @obj.build(@logger) |
| 154 | + @parsed_data = JSON.parse(@post_data) |
| 155 | + end |
| 156 | + |
| 157 | + it 'should post to the correct URL' do |
| 158 | + expect(@url).to eq(api_url[4]) |
| 159 | + end |
| 160 | + |
| 161 | + it 'should set the Content-Type header correctly' do |
| 162 | + expect(@opts[:headers]['Content-Type']).to eq('application/json') |
| 163 | + end |
| 164 | + |
| 165 | + it 'should not set the X-Authentication header when no token is provided' do |
| 166 | + expect(@opts[:headers].key?('X-Authentication')).to eq false |
| 167 | + end |
| 168 | + |
| 169 | + it 'should post the correct facts to HTTParty' do |
| 170 | + answer = JSON.parse(File.read(OctocatalogDiff::Spec.fixture_path('facts/facts_esc.json'))) |
| 171 | + answer.delete('_timestamp') |
| 172 | + result = @parsed_data['facts']['values'] |
| 173 | + expect(result).to eq(answer) |
| 174 | + end |
| 175 | + |
| 176 | + it 'should set the environment in the parameters correctly for the API' do |
| 177 | + expect(@parsed_data['environment']).to eq('foobranch') |
| 178 | + end |
| 179 | + |
| 180 | + it 'should default to false for persistence' do |
| 181 | + expect(@parsed_data['persistence']['facts']).to eq false |
| 182 | + expect(@parsed_data['persistence']['catalog']).to eq false |
| 183 | + end |
| 184 | + |
| 185 | + it 'should parse the response and set instance variables correctly' do |
| 186 | + expect(@obj.catalog).to be_a_kind_of(Hash) |
| 187 | + expect(@obj.catalog_json).to be_a_kind_of(String) |
| 188 | + expect(@obj.error_message).to eq(nil) |
| 189 | + end |
| 190 | + |
| 191 | + it 'should log correctly' do |
| 192 | + logs = @logger_str.string |
| 193 | + expect(logs).to match(/Start retrieving facts for foo from OctocatalogDiff::Catalog::PuppetMaster/) |
| 194 | + expect(logs).to match(%r{Retrieving facts from.*fixtures/facts/facts_esc.yaml}) |
| 195 | + expect(logs).to match(%r{Retrieving facts from.*fixtures/facts/facts_esc.yaml}) |
| 196 | + |
| 197 | + answer = Regexp.new("Retrieve catalog from #{api_url[4]} environment foobranch") |
| 198 | + expect(logs).to match(answer) |
| 199 | + |
| 200 | + answer2 = Regexp.new("Response from #{api_url[4]} environment foobranch was 200") |
| 201 | + expect(logs).to match(answer2) |
| 202 | + end |
| 203 | + |
| 204 | + context 'when a RBAC token is passed' do |
| 205 | + let(:extra_opts) { { puppet_master_token: 'mytoken' } } |
| 206 | + |
| 207 | + it 'should set the token in the headers' do |
| 208 | + expect(@opts[:headers]['X-Authentication']).to eq 'mytoken' |
| 209 | + end |
| 210 | + end |
| 211 | + |
| 212 | + context 'when facts persistence is requested' do |
| 213 | + let(:extra_opts) { { puppet_master_update_facts: true } } |
| 214 | + |
| 215 | + it 'should set the request in the parameters' do |
| 216 | + expect(@parsed_data['persistence']['facts']).to eq true |
| 217 | + end |
| 218 | + end |
| 219 | + |
| 220 | + context 'when catalog persistence is requested' do |
| 221 | + let(:extra_opts) { { puppet_master_update_catalog: true } } |
| 222 | + |
| 223 | + it 'should set the request in the parameters' do |
| 224 | + expect(@parsed_data['persistence']['catalog']).to eq true |
| 225 | + end |
| 226 | + end |
| 227 | + end |
| 228 | + |
139 | 229 | context 'response is not 200' do |
140 | 230 | before(:each) do |
141 | 231 | allow(OctocatalogDiff::Util::HTTParty).to receive(:post) do |_url, _opts, _post_data, _context| |
|
0 commit comments