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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
require 'spec_helper'
require 'puppet/http'
describe Puppet::HTTP::Service::FileServer do
include PuppetSpec::Files
let(:ssl_context) { Puppet::SSL::SSLContext.new }
let(:client) { Puppet::HTTP::Client.new(ssl_context: ssl_context) }
let(:subject) { client.create_session.route_to(:fileserver) }
let(:environment) { 'testing' }
let(:report) { Puppet::Transaction::Report.new }
before :each do
Puppet[:server] = 'www.example.com'
Puppet[:serverport] = 443
end
context 'when making requests' do
let(:uri) {"https://www.example.com:443/puppet/v3/file_content/:mount/:path?environment=testing"}
it 'includes default HTTP headers' do
stub_request(:get, uri).with do |request|
expect(request.headers).to include({'X-Puppet-Version' => /./, 'User-Agent' => /./})
expect(request.headers).to_not include('X-Puppet-Profiling')
end
subject.get_file_content(path: '/:mount/:path', environment: environment) { |data| }
end
end
context 'when routing to the file service' do
it 'defaults the server and port based on settings' do
Puppet[:server] = 'file.example.com'
Puppet[:serverport] = 8141
stub_request(:get, "https://file.example.com:8141/puppet/v3/file_content/:mount/:path?environment=testing")
subject.get_file_content(path: '/:mount/:path', environment: environment) { |data| }
end
end
context 'retrieving file metadata' do
let(:path) { tmpfile('get_file_metadata') }
let(:url) { "https://www.example.com/puppet/v3/file_metadata/:mount/#{path}?checksum_type=sha256&environment=testing&links=manage&source_permissions=ignore" }
let(:filemetadata) { Puppet::FileServing::Metadata.new(path) }
let(:request_path) { "/:mount/#{path}"}
it 'includes puppet headers set via the :http_extra_headers and :profile settings' do
stub_request(:get, url).with(headers: {'Example-Header' => 'real-thing', 'another' => 'thing', 'X-Puppet-Profiling' => 'true'}).
to_return(status: 200, body: filemetadata.render, headers: { 'Content-Type' => 'application/json' })
Puppet[:http_extra_headers] = 'Example-Header:real-thing,another:thing'
Puppet[:profile] = true
subject.get_file_metadata(path: request_path, environment: environment)
end
it 'submits a request for file metadata to the server' do
stub_request(:get, url).with(
headers: {'Accept'=>'application/json, application/x-msgpack, text/pson',}
).to_return(
status: 200, body: filemetadata.render, headers: { 'Content-Type' => 'application/json' }
)
_, metadata = subject.get_file_metadata(path: request_path, environment: environment)
expect(metadata.path).to eq(path)
end
it 'returns the request response' do
stub_request(:get, url).to_return(
status: 200, body: filemetadata.render, headers: { 'Content-Type' => 'application/json' }
)
resp, _ = subject.get_file_metadata(path: request_path, environment: environment)
expect(resp).to be_a(Puppet::HTTP::Response)
end
it 'raises a protocol error if the Content-Type header is missing from the response' do
stub_request(:get, url).to_return(status: 200, body: '', headers: {})
expect {
subject.get_file_metadata(path: request_path, environment: environment)
}.to raise_error(Puppet::HTTP::ProtocolError, "No content type in http response; cannot parse")
end
it 'raises an error if the Content-Type is unsupported' do
stub_request(:get, url).to_return(status: 200, body: '', headers: { 'Content-Type' => 'text/yaml' })
expect {
subject.get_file_metadata(path: request_path, environment: environment)
}.to raise_error(Puppet::HTTP::ProtocolError, "Content-Type is unsupported")
end
it 'raises response error if unsuccessful' do
stub_request(:get, url).to_return(status: [400, 'Bad Request'])
expect {
subject.get_file_metadata(path: request_path, environment: environment)
}.to raise_error do |err|
expect(err).to be_an_instance_of(Puppet::HTTP::ResponseError)
expect(err.message).to eq('Bad Request')
expect(err.response.code).to eq(400)
end
end
it 'raises a serialization error if serialization fails' do
stub_request(:get, url).to_return(
status: 200, body: '', headers: { 'Content-Type' => 'application/json' }
)
expect {
subject.get_file_metadata(path: request_path, environment: environment)
}.to raise_error(Puppet::HTTP::SerializationError, /Failed to deserialize Puppet::FileServing::Metadata from json/)
end
it 'raises response error if path is relative' do
expect {
subject.get_file_metadata(path: 'relative_path', environment: environment)
}.to raise_error(ArgumentError, 'Path must start with a slash')
end
end
context 'retrieving multiple file metadatas' do
let(:path) { tmpfile('get_file_metadatas') }
let(:url) { "https://www.example.com/puppet/v3/file_metadatas/:mount/#{path}?checksum_type=sha256&links=manage&recurse=false&source_permissions=ignore&environment=testing" }
let(:filemetadatas) { [Puppet::FileServing::Metadata.new(path)] }
let(:formatter) { Puppet::Network::FormatHandler.format(:json) }
let(:request_path) { "/:mount/#{path}"}
it 'includes puppet headers set via the :http_extra_headers and :profile settings' do
stub_request(:get, url).with(headers: {'Example-Header' => 'real-thing', 'another' => 'thing', 'X-Puppet-Profiling' => 'true'}).
to_return(status: 200, body: formatter.render_multiple(filemetadatas), headers: { 'Content-Type' => 'application/json' })
Puppet[:http_extra_headers] = 'Example-Header:real-thing,another:thing'
Puppet[:profile] = true
subject.get_file_metadatas(path: request_path, environment: environment)
end
it 'submits a request for file metadata to the server' do
stub_request(:get, url).with(
headers: {'Accept'=>'application/json, application/x-msgpack, text/pson',}
).to_return(
status: 200, body: formatter.render_multiple(filemetadatas), headers: { 'Content-Type' => 'application/json' }
)
_, metadatas = subject.get_file_metadatas(path: request_path, environment: environment)
expect(metadatas.first.path).to eq(path)
end
it 'returns the request response' do
stub_request(:get, url).to_return(
status: 200, body: formatter.render_multiple(filemetadatas), headers: { 'Content-Type' => 'application/json' }
)
resp, _ = subject.get_file_metadatas(path: request_path, environment: environment)
expect(resp).to be_a(Puppet::HTTP::Response)
end
it 'automatically converts an array of parameters to the stringified query' do
url = "https://www.example.com/puppet/v3/file_metadatas/:mount/#{path}?checksum_type=sha256&environment=testing&ignore=CVS&ignore=.git&ignore=.hg&links=manage&recurse=false&source_permissions=ignore"
stub_request(:get, url).with(
headers: {'Accept'=>'application/json, application/x-msgpack, text/pson',}
).to_return(
status: 200, body: formatter.render_multiple(filemetadatas), headers: { 'Content-Type' => 'application/json' }
)
_, metadatas = subject.get_file_metadatas(path: request_path, environment: environment, ignore: ['CVS', '.git', '.hg'])
expect(metadatas.first.path).to eq(path)
end
it 'raises a protocol error if the Content-Type header is missing from the response' do
stub_request(:get, url).to_return(status: 200, body: '', headers: {})
expect {
subject.get_file_metadatas(path: request_path, environment: environment)
}.to raise_error(Puppet::HTTP::ProtocolError, "No content type in http response; cannot parse")
end
it 'raises an error if the Content-Type is unsupported' do
stub_request(:get, url).to_return(status: 200, body: '', headers: { 'Content-Type' => 'text/yaml' })
expect {
subject.get_file_metadatas(path: request_path, environment: environment)
}.to raise_error(Puppet::HTTP::ProtocolError, "Content-Type is unsupported")
end
it 'raises response error if unsuccessful' do
stub_request(:get, url).to_return(status: [400, 'Bad Request'])
expect {
subject.get_file_metadatas(path: request_path, environment: environment)
}.to raise_error do |err|
expect(err).to be_an_instance_of(Puppet::HTTP::ResponseError)
expect(err.message).to eq('Bad Request')
expect(err.response.code).to eq(400)
end
end
it 'raises a serialization error if serialization fails' do
stub_request(:get, url).to_return(
status: 200, body: '', headers: { 'Content-Type' => 'application/json' }
)
expect {
subject.get_file_metadatas(path: request_path, environment: environment)
}.to raise_error(Puppet::HTTP::SerializationError, /Failed to deserialize multiple Puppet::FileServing::Metadata from json/)
end
it 'raises response error if path is relative' do
expect {
subject.get_file_metadatas(path: 'relative_path', environment: environment)
}.to raise_error(ArgumentError, 'Path must start with a slash')
end
end
context 'getting file content' do
let(:uri) {"https://www.example.com:443/puppet/v3/file_content/:mount/:path?environment=testing"}
it 'includes puppet headers set via the :http_extra_headers and :profile settings' do
stub_request(:get, uri).with(headers: {'Example-Header' => 'real-thing', 'another' => 'thing', 'X-Puppet-Profiling' => 'true'}).
to_return(status: 200, body: "and beyond")
Puppet[:http_extra_headers] = 'Example-Header:real-thing,another:thing'
Puppet[:profile] = true
expect { |b|
subject.get_file_content(path: '/:mount/:path', environment: environment, &b)
}.to yield_with_args("and beyond")
end
it 'yields file content' do
stub_request(:get, uri).with do |request|
expect(request.headers).to include({'Accept' => 'application/octet-stream'})
end.to_return(status: 200, body: "and beyond")
expect { |b|
subject.get_file_content(path: '/:mount/:path', environment: environment, &b)
}.to yield_with_args("and beyond")
end
it 'returns the request response' do
stub_request(:get, uri)
resp = subject.get_file_content(path: '/:mount/:path', environment: environment) { |b| b }
expect(resp).to be_a(Puppet::HTTP::Response)
end
it 'raises response error if unsuccessful' do
stub_request(:get, uri).to_return(status: [400, 'Bad Request'])
expect {
subject.get_file_content(path: '/:mount/:path', environment: environment) { |data| }
}.to raise_error do |err|
expect(err).to be_an_instance_of(Puppet::HTTP::ResponseError)
expect(err.message).to eq('Bad Request')
expect(err.response.code).to eq(400)
end
end
it 'raises response error if path is relative' do
expect {
subject.get_file_content(path: 'relative_path', environment: environment) { |data| }
}.to raise_error(ArgumentError, 'Path must start with a slash')
end
end
context 'getting static file content' do
let(:code_id) { "0fc72115-adc6-4b1a-aa50-8f31b3ece440" }
let(:uri) { "https://www.example.com:443/puppet/v3/static_file_content/:mount/:path?environment=testing&code_id=#{code_id}"}
it 'yields file content' do
stub_request(:get, uri).with do |request|
expect(request.headers).to include({'Accept' => 'application/octet-stream'})
end.to_return(status: 200, body: "and beyond")
expect { |b|
subject.get_static_file_content(path: '/:mount/:path', environment: environment, code_id: code_id, &b)
}.to yield_with_args("and beyond")
end
it 'returns the request response' do
stub_request(:get, uri)
resp = subject.get_static_file_content(path: '/:mount/:path', environment: environment, code_id: code_id) { |b| b }
expect(resp).to be_a(Puppet::HTTP::Response)
end
it 'raises response error if unsuccessful' do
stub_request(:get, uri).to_return(status: [400, 'Bad Request'])
expect {
subject.get_static_file_content(path: '/:mount/:path', environment: environment, code_id: code_id) { |data| }
}.to raise_error do |err|
expect(err).to be_an_instance_of(Puppet::HTTP::ResponseError)
expect(err.message).to eq('Bad Request')
expect(err.response.code).to eq(400)
end
end
it 'raises response error if path is relative' do
expect {
subject.get_static_file_content(path: 'relative_path', environment: environment, code_id: code_id) { |data| }
}.to raise_error(ArgumentError, 'Path must start with a slash')
end
end
end
|