buildpack-support 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +202 -0
- data/NOTICE +2 -0
- data/docs/cache.md +77 -0
- data/docs/component.md +1 -0
- data/docs/configuration.md +27 -0
- data/docs/logging.md +54 -0
- data/docs/other.md +1 -0
- data/docs/rake.md +1 -0
- data/docs/repository.md +116 -0
- data/docs/test.md +1 -0
- data/lib/buildpack_support.rb +18 -0
- data/lib/buildpack_support/base_buildpack.rb +166 -0
- data/lib/buildpack_support/buildpack_version.rb +124 -0
- data/lib/buildpack_support/cache.rb +24 -0
- data/lib/buildpack_support/cache/application_cache.rb +41 -0
- data/lib/buildpack_support/cache/cached_file.rb +103 -0
- data/lib/buildpack_support/cache/download_cache.rb +280 -0
- data/lib/buildpack_support/cache/inferred_network_failure.rb +26 -0
- data/lib/buildpack_support/cache/internet_availability.rb +64 -0
- data/lib/buildpack_support/component.rb +24 -0
- data/lib/buildpack_support/component/application.rb +76 -0
- data/lib/buildpack_support/component/base_component.rb +78 -0
- data/lib/buildpack_support/component/base_droplet.rb +96 -0
- data/lib/buildpack_support/component/downloads.rb +88 -0
- data/lib/buildpack_support/component/services.rb +84 -0
- data/lib/buildpack_support/component/versioned_dependency_component.rb +71 -0
- data/lib/buildpack_support/component/versioned_downloads.rb +57 -0
- data/lib/buildpack_support/component/with_timing.rb +40 -0
- data/lib/buildpack_support/configuration_utils.rb +58 -0
- data/lib/buildpack_support/constantize.rb +46 -0
- data/lib/buildpack_support/dash_case.rb +29 -0
- data/lib/buildpack_support/directory_finder.rb +45 -0
- data/lib/buildpack_support/filtering_pathname.rb +227 -0
- data/lib/buildpack_support/format_duration.rb +57 -0
- data/lib/buildpack_support/logging.rb +22 -0
- data/lib/buildpack_support/logging/delegating_logger.rb +48 -0
- data/lib/buildpack_support/logging/logger_factory.rb +148 -0
- data/lib/buildpack_support/qualify_path.rb +36 -0
- data/lib/buildpack_support/rake.rb +22 -0
- data/lib/buildpack_support/rake/buildpack_stage_task.rb +86 -0
- data/lib/buildpack_support/rake/cached_artifact_finder.rb +99 -0
- data/lib/buildpack_support/rake/check_api_doc_task.rb +70 -0
- data/lib/buildpack_support/rake/dependency_cache_task.rb +87 -0
- data/lib/buildpack_support/rake/disable_remote_downloads_task.rb +80 -0
- data/lib/buildpack_support/rake/package_task.rb +133 -0
- data/lib/buildpack_support/rake/package_zip_task.rb +80 -0
- data/lib/buildpack_support/rake/repository_configuration_finder.rb +66 -0
- data/lib/buildpack_support/rake/write_version_file_task.rb +82 -0
- data/lib/buildpack_support/repository.rb +24 -0
- data/lib/buildpack_support/repository/configured_item.rb +81 -0
- data/lib/buildpack_support/repository/repository_index.rb +98 -0
- data/lib/buildpack_support/repository/wildcard_version_resolver.rb +75 -0
- data/lib/buildpack_support/shell.rb +41 -0
- data/lib/buildpack_support/snake_case.rb +30 -0
- data/lib/buildpack_support/space_case.rb +29 -0
- data/lib/buildpack_support/test/application_helper.rb +41 -0
- data/lib/buildpack_support/test/base_component_helper.rb +59 -0
- data/lib/buildpack_support/test/base_droplet_helper.rb +36 -0
- data/lib/buildpack_support/test/console_helper.rb +57 -0
- data/lib/buildpack_support/test/environment_helper.rb +32 -0
- data/lib/buildpack_support/test/internet_availability_helper.rb +29 -0
- data/lib/buildpack_support/test/logging_helper.rb +50 -0
- data/lib/buildpack_support/test/scratch_helper.rb +32 -0
- data/lib/buildpack_support/test/versioned_dependency_component_helper.rb +32 -0
- data/lib/buildpack_support/test/with_load_path_helper.rb +27 -0
- data/lib/buildpack_support/to_b.rb +38 -0
- data/lib/buildpack_support/tokenized_version.rb +157 -0
- data/lib/buildpack_support/version.rb +23 -0
- data/spec/buildpack_support/base_buildpack_spec.rb +112 -0
- data/spec/buildpack_support/buildpack_version_spec.rb +122 -0
- data/spec/buildpack_support/cache/application_cache_spec.rb +56 -0
- data/spec/buildpack_support/cache/cached_file_spec.rb +94 -0
- data/spec/buildpack_support/cache/download_cache_spec.rb +293 -0
- data/spec/buildpack_support/cache/internet_availability_spec.rb +57 -0
- data/spec/buildpack_support/cache/yield_file_with_content.rb +30 -0
- data/spec/buildpack_support/component/application_spec.rb +81 -0
- data/spec/buildpack_support/component/base_component_spec.rb +81 -0
- data/spec/buildpack_support/component/base_droplet_spec.rb +72 -0
- data/spec/buildpack_support/component/downloads_spec.rb +63 -0
- data/spec/buildpack_support/component/services_spec.rb +80 -0
- data/spec/buildpack_support/component/versioned_dependency_component_spec.rb +58 -0
- data/spec/buildpack_support/component/versioned_downloads_spec.rb +58 -0
- data/spec/buildpack_support/component/with_timing_spec.rb +30 -0
- data/spec/buildpack_support/configuration_utils_spec.rb +39 -0
- data/spec/buildpack_support/constantize_spec.rb +34 -0
- data/spec/buildpack_support/dash_case_spec.rb +41 -0
- data/spec/buildpack_support/directory_finder_spec.rb +41 -0
- data/spec/buildpack_support/filtering_pathname_spec.rb +443 -0
- data/spec/buildpack_support/format_duration_spec.rb +60 -0
- data/spec/buildpack_support/logging/delegating_logger_spec.rb +62 -0
- data/spec/buildpack_support/logging/logger_factory_spec.rb +262 -0
- data/spec/buildpack_support/qualify_path_spec.rb +42 -0
- data/spec/buildpack_support/rake/buildpack_stage_task_spec.rb +88 -0
- data/spec/buildpack_support/rake/cached_artifact_finder_spec.rb +73 -0
- data/spec/buildpack_support/rake/check_api_doc_task_spec.rb +69 -0
- data/spec/buildpack_support/rake/dependency_cache_task_spec.rb +133 -0
- data/spec/buildpack_support/rake/disable_remote_downloads_task_spec.rb +91 -0
- data/spec/buildpack_support/rake/package_task_spec.rb +335 -0
- data/spec/buildpack_support/rake/package_zip_task_spec.rb +91 -0
- data/spec/buildpack_support/rake/repository_configuration_finder_spec.rb +61 -0
- data/spec/buildpack_support/rake/write_version_file_task_spec.rb +96 -0
- data/spec/buildpack_support/repository/configured_item_spec.rb +78 -0
- data/spec/buildpack_support/repository/repository_index_spec.rb +118 -0
- data/spec/buildpack_support/repository/wildcard_version_resolver_spec.rb +73 -0
- data/spec/buildpack_support/shell_spec.rb +32 -0
- data/spec/buildpack_support/snake_case_spec.rb +45 -0
- data/spec/buildpack_support/space_case_spec.rb +41 -0
- data/spec/buildpack_support/to_b_spec.rb +41 -0
- data/spec/buildpack_support/tokenized_version_spec.rb +132 -0
- data/spec/fixtures/application/test-file +0 -0
- data/spec/fixtures/config/found-config.yml +2 -0
- data/spec/fixtures/droplet-resources/droplet-resource +0 -0
- data/spec/fixtures/stub-download-with-top-level.zip +0 -0
- data/spec/fixtures/stub-download.tar.gz +0 -0
- data/spec/fixtures/stub-download.zip +0 -0
- data/spec/fixtures/test-cache.yml +18 -0
- data/spec/fixtures/test-index.yml +2 -0
- data/spec/fixtures/test_component.rb +0 -0
- data/spec/fixtures/zip-contents/test-directory/test-deep-file +0 -0
- data/spec/fixtures/zip-contents/test-file +0 -0
- data/spec/spec_helper.rb +30 -0
- metadata +416 -0
@@ -0,0 +1,56 @@
|
|
1
|
+
# Encoding: utf-8
|
2
|
+
# Copyright 2013-2014 the original author or authors.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
require 'spec_helper'
|
17
|
+
require 'buildpack_support/cache/application_cache'
|
18
|
+
require 'buildpack_support/test/internet_availability_helper'
|
19
|
+
require 'buildpack_support/test/logging_helper'
|
20
|
+
require 'buildpack_support/test/scratch_helper'
|
21
|
+
|
22
|
+
describe BuildpackSupport::Cache::ApplicationCache do
|
23
|
+
include_context 'internet_availability_helper'
|
24
|
+
include_context 'logging_helper'
|
25
|
+
include_context 'scratch_helper'
|
26
|
+
|
27
|
+
previous_arg_value = ARGV[1]
|
28
|
+
|
29
|
+
before do
|
30
|
+
ARGV[1] = nil
|
31
|
+
|
32
|
+
stub_request(:get, 'http://foo-uri/').with(headers: { 'Accept' => '*/*', 'User-Agent' => 'Ruby' })
|
33
|
+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
|
34
|
+
|
35
|
+
stub_request(:head, 'http://foo-uri/')
|
36
|
+
.with(headers: { 'Accept' => '*/*', 'If-Modified-Since' => 'foo-last-modified', 'If-None-Match' => 'foo-etag', 'User-Agent' => 'Ruby' })
|
37
|
+
.to_return(status: 304, body: '', headers: {})
|
38
|
+
end
|
39
|
+
|
40
|
+
after do
|
41
|
+
ARGV[1] = previous_arg_value
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should raise an error if ARGV[1] is not defined' do
|
45
|
+
expect { described_class.new }.to raise_error
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should use ARGV[1] directory' do
|
49
|
+
ARGV[1] = scratch_dir
|
50
|
+
|
51
|
+
described_class.new.get('http://foo-uri/') {}
|
52
|
+
|
53
|
+
expect(Pathname.glob(scratch_dir + '*.cached').size).to eq(1)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# Encoding: utf-8
|
2
|
+
# Copyright 2013-2014 the original author or authors.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
require 'spec_helper'
|
17
|
+
require 'buildpack_support/cache/cached_file'
|
18
|
+
require 'buildpack_support/cache/yield_file_with_content'
|
19
|
+
require 'buildpack_support/test/scratch_helper'
|
20
|
+
require 'fileutils'
|
21
|
+
|
22
|
+
describe BuildpackSupport::Cache::CachedFile do
|
23
|
+
include_context 'scratch_helper'
|
24
|
+
|
25
|
+
let(:file_cache) { described_class.new(scratch_dir, 'http://foo-uri/') }
|
26
|
+
|
27
|
+
it 'should not create any files on initialization' do
|
28
|
+
%w(cached etag last_modified).each { |extension| expect(cache_file(extension)).not_to exist }
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should not detect cached file' do
|
32
|
+
expect(file_cache.cached?).not_to be
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should not detect etag file' do
|
36
|
+
expect(file_cache.etag?).not_to be
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should not detect last_modified file' do
|
40
|
+
expect(file_cache.last_modified?).not_to be
|
41
|
+
end
|
42
|
+
|
43
|
+
context do
|
44
|
+
|
45
|
+
before do
|
46
|
+
touch('cached', 'foo-cached')
|
47
|
+
touch('etag', 'foo-etag')
|
48
|
+
touch('last_modified', 'foo-last-modified')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should call the block with the content of the cache file' do
|
52
|
+
expect { |b| file_cache.cached(File::RDONLY, 'test-arg', &b) }.to yield_file_with_content(/foo-cached/)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should detect cached file' do
|
56
|
+
expect(file_cache.cached?).to be
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should destroy all files' do
|
60
|
+
file_cache.destroy
|
61
|
+
|
62
|
+
%w(cached etag last_modified).each { |extension| expect(cache_file(extension)).not_to exist }
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should call the block with the content of the etag file' do
|
66
|
+
expect { |b| file_cache.etag(File::RDONLY, 'test-arg', &b) }.to yield_file_with_content(/foo-etag/)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should detect etag file' do
|
70
|
+
expect(file_cache.etag?).to be
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should call the block with the content of the last_modified file' do
|
74
|
+
expect { |b| file_cache.last_modified(File::RDONLY, 'test-arg', &b) }.to yield_file_with_content(/foo-last-modified/)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should detect last_modified file' do
|
78
|
+
expect(file_cache.last_modified?).to be
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def cache_file(extension)
|
83
|
+
scratch_dir + "http:%2F%2Ffoo-uri%2F.#{extension}"
|
84
|
+
end
|
85
|
+
|
86
|
+
def touch(extension, content = '')
|
87
|
+
file = cache_file extension
|
88
|
+
FileUtils.mkdir_p file.dirname
|
89
|
+
file.open('w') { |f| f.write content }
|
90
|
+
|
91
|
+
file
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
@@ -0,0 +1,293 @@
|
|
1
|
+
# Encoding: utf-8
|
2
|
+
# Copyright 2013-2014 the original author or authors.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
require 'spec_helper'
|
17
|
+
require 'buildpack_support/cache/download_cache'
|
18
|
+
require 'buildpack_support/test/environment_helper'
|
19
|
+
require 'buildpack_support/test/internet_availability_helper'
|
20
|
+
require 'buildpack_support/test/logging_helper'
|
21
|
+
require 'buildpack_support/test/scratch_helper'
|
22
|
+
require 'fileutils'
|
23
|
+
require 'net/http'
|
24
|
+
|
25
|
+
describe BuildpackSupport::Cache::DownloadCache do
|
26
|
+
include_context 'environment_helper'
|
27
|
+
include_context 'internet_availability_helper'
|
28
|
+
include_context 'logging_helper'
|
29
|
+
include_context 'scratch_helper'
|
30
|
+
|
31
|
+
let(:mutable_cache_root) { scratch_dir + 'mutable' }
|
32
|
+
|
33
|
+
let(:immutable_cache_root) { scratch_dir + 'immutable' }
|
34
|
+
|
35
|
+
let(:uri) { 'http://foo-uri/' }
|
36
|
+
|
37
|
+
let(:uri_credentials) { 'http://test-username:test-password@foo-uri/' }
|
38
|
+
|
39
|
+
let(:uri_secure) { 'https://foo-uri/' }
|
40
|
+
|
41
|
+
let(:download_cache) { described_class.new(mutable_cache_root, immutable_cache_root) }
|
42
|
+
|
43
|
+
it 'should raise error if file cannot be found',
|
44
|
+
:disable_internet do
|
45
|
+
|
46
|
+
expect { download_cache.get uri }.to raise_error('Unable to find cached file for http://foo-uri/')
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should return file from immutable cache if internet is disabled',
|
50
|
+
:disable_internet do
|
51
|
+
|
52
|
+
touch immutable_cache_root, 'cached', 'foo-cached'
|
53
|
+
|
54
|
+
expect { |b| download_cache.get uri, &b }.to yield_file_with_content(/foo-cached/)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should return file from mutable cache if internet is disabled',
|
58
|
+
:disable_internet do
|
59
|
+
|
60
|
+
touch mutable_cache_root, 'cached', 'foo-cached'
|
61
|
+
|
62
|
+
expect { |b| download_cache.get uri, &b }.to yield_file_with_content(/foo-cached/)
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should download if cached file does not exist' do
|
66
|
+
stub_request(:get, uri)
|
67
|
+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
|
68
|
+
|
69
|
+
allow(Net::HTTP).to receive(:Proxy).and_call_original
|
70
|
+
expect(Net::HTTP).not_to receive(:Proxy).with('proxy', 9000, nil, nil)
|
71
|
+
|
72
|
+
expect { |b| download_cache.get uri, &b }.to yield_file_with_content(/foo-cached/)
|
73
|
+
expect_complete_cache mutable_cache_root
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should download with credentials if cached file does not exist' do
|
77
|
+
stub_request(:get, uri_credentials)
|
78
|
+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
|
79
|
+
|
80
|
+
allow(Net::HTTP).to receive(:Proxy).and_call_original
|
81
|
+
expect(Net::HTTP).not_to receive(:Proxy).with('proxy', 9000, nil, nil)
|
82
|
+
|
83
|
+
expect { |b| download_cache.get uri_credentials, &b }.to yield_file_with_content(/foo-cached/)
|
84
|
+
expect_complete_credential_cache mutable_cache_root
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should follow redirects' do
|
88
|
+
stub_request(:get, uri)
|
89
|
+
.to_return(status: 301, headers: { Location: uri_secure })
|
90
|
+
stub_request(:get, uri_secure)
|
91
|
+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
|
92
|
+
|
93
|
+
expect { |b| download_cache.get uri, &b }.to yield_file_with_content(/foo-cached/)
|
94
|
+
expect_complete_cache mutable_cache_root
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should retry failed downloads' do
|
98
|
+
stub_request(:get, uri)
|
99
|
+
.to_raise(SocketError)
|
100
|
+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
|
101
|
+
|
102
|
+
expect { |b| download_cache.get uri, &b }.to yield_file_with_content(/foo-cached/)
|
103
|
+
expect_complete_cache mutable_cache_root
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should return cached data if unknown error occurs' do
|
107
|
+
stub_request(:get, uri)
|
108
|
+
.to_raise('DNS Error')
|
109
|
+
|
110
|
+
touch immutable_cache_root, 'cached', 'foo-cached'
|
111
|
+
|
112
|
+
expect { |b| download_cache.get uri, &b }.to yield_file_with_content(/foo-cached/)
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should return cached data if retry limit is reached' do
|
116
|
+
stub_request(:get, uri)
|
117
|
+
.to_return(status: 500)
|
118
|
+
|
119
|
+
touch immutable_cache_root, 'cached', 'foo-cached'
|
120
|
+
|
121
|
+
expect { |b| download_cache.get uri, &b }.to yield_file_with_content(/foo-cached/)
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should not overwrite existing information if 304 is received' do
|
125
|
+
stub_request(:get, uri)
|
126
|
+
.with(headers: { 'If-None-Match' => 'foo-etag', 'If-Modified-Since' => 'foo-last-modified' })
|
127
|
+
.to_return(status: 304, body: '', headers: {})
|
128
|
+
|
129
|
+
touch mutable_cache_root, 'cached', 'foo-cached'
|
130
|
+
touch mutable_cache_root, 'etag', 'foo-etag'
|
131
|
+
touch mutable_cache_root, 'last_modified', 'foo-last-modified'
|
132
|
+
|
133
|
+
expect { |b| download_cache.get uri, &b }.to yield_file_with_content(/foo-cached/)
|
134
|
+
expect_complete_cache mutable_cache_root
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'should overwrite existing information if 304 is not received' do
|
138
|
+
stub_request(:get, uri)
|
139
|
+
.with(headers: { 'If-None-Match' => 'old-foo-etag', 'If-Modified-Since' => 'old-foo-last-modified' })
|
140
|
+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
|
141
|
+
|
142
|
+
touch mutable_cache_root, 'cached', 'old-foo-cached'
|
143
|
+
touch mutable_cache_root, 'etag', 'old-foo-etag'
|
144
|
+
touch mutable_cache_root, 'last_modified', 'old-foo-last-modified'
|
145
|
+
|
146
|
+
expect { |b| download_cache.get uri, &b }.to yield_with_args(be_a(File), true)
|
147
|
+
|
148
|
+
touch mutable_cache_root, 'cached', 'old-foo-cached'
|
149
|
+
touch mutable_cache_root, 'etag', 'old-foo-etag'
|
150
|
+
touch mutable_cache_root, 'last_modified', 'old-foo-last-modified'
|
151
|
+
|
152
|
+
expect { |b| download_cache.get uri, &b }.to yield_file_with_content(/foo-cached/)
|
153
|
+
|
154
|
+
expect_complete_cache mutable_cache_root
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'should discard content with incorrect size' do
|
158
|
+
stub_request(:get, uri)
|
159
|
+
.to_return(status: 200, body: 'foo-cac', headers: { Etag: 'foo-etag',
|
160
|
+
'Last-Modified' => 'foo-last-modified',
|
161
|
+
'Content-Length' => 10 })
|
162
|
+
|
163
|
+
touch immutable_cache_root, 'cached', 'old-foo-cached'
|
164
|
+
|
165
|
+
expect { |b| download_cache.get uri, &b }.to yield_file_with_content(/foo-cached/)
|
166
|
+
end
|
167
|
+
|
168
|
+
context do
|
169
|
+
|
170
|
+
let(:environment) { { 'http_proxy' => 'http://proxy:9000' } }
|
171
|
+
|
172
|
+
it 'should use http_proxy if specified' do
|
173
|
+
stub_request(:get, uri)
|
174
|
+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
|
175
|
+
|
176
|
+
allow(Net::HTTP).to receive(:Proxy).and_call_original
|
177
|
+
expect(Net::HTTP).to receive(:Proxy).with('proxy', 9000, nil, nil).and_call_original
|
178
|
+
|
179
|
+
download_cache.get(uri) {}
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
context do
|
185
|
+
|
186
|
+
let(:environment) { { 'HTTP_PROXY' => 'http://proxy:9000' } }
|
187
|
+
|
188
|
+
it 'should use HTTP_PROXY if specified' do
|
189
|
+
stub_request(:get, uri)
|
190
|
+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
|
191
|
+
|
192
|
+
allow(Net::HTTP).to receive(:Proxy).and_call_original
|
193
|
+
expect(Net::HTTP).to receive(:Proxy).with('proxy', 9000, nil, nil).and_call_original
|
194
|
+
|
195
|
+
download_cache.get(uri) {}
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
context do
|
201
|
+
|
202
|
+
let(:environment) { { 'https_proxy' => 'http://proxy:9000' } }
|
203
|
+
|
204
|
+
it 'should use https_proxy if specified and URL is secure' do
|
205
|
+
stub_request(:get, uri_secure)
|
206
|
+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
|
207
|
+
|
208
|
+
allow(Net::HTTP).to receive(:Proxy).and_call_original
|
209
|
+
expect(Net::HTTP).to receive(:Proxy).with('proxy', 9000, nil, nil).and_call_original
|
210
|
+
|
211
|
+
download_cache.get(uri_secure) {}
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
context do
|
217
|
+
|
218
|
+
let(:environment) { { 'HTTPS_PROXY' => 'http://proxy:9000' } }
|
219
|
+
|
220
|
+
it 'should use HTTPS_PROXY if specified and URL is secure' do
|
221
|
+
stub_request(:get, uri_secure)
|
222
|
+
.to_return(status: 200, body: 'foo-cached', headers: { Etag: 'foo-etag', 'Last-Modified' => 'foo-last-modified' })
|
223
|
+
|
224
|
+
allow(Net::HTTP).to receive(:Proxy).and_call_original
|
225
|
+
expect(Net::HTTP).to receive(:Proxy).with('proxy', 9000, nil, nil).and_call_original
|
226
|
+
|
227
|
+
download_cache.get(uri_secure) {}
|
228
|
+
end
|
229
|
+
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'should delete the cached file if it exists' do
|
233
|
+
expect_file_deleted 'cached'
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'should delete the etag file if it exists' do
|
237
|
+
expect_file_deleted 'etag'
|
238
|
+
end
|
239
|
+
|
240
|
+
it 'should delete the last_modified file if it exists' do
|
241
|
+
expect_file_deleted 'last_modified'
|
242
|
+
end
|
243
|
+
|
244
|
+
def cache_file(root, extension)
|
245
|
+
root + "http:%2F%2Ffoo-uri%2F.#{extension}"
|
246
|
+
end
|
247
|
+
|
248
|
+
def credential_cache_file(root, extension)
|
249
|
+
root + "http:%2F%2Ftest-username:test-password@foo-uri%2F.#{extension}"
|
250
|
+
end
|
251
|
+
|
252
|
+
def expect_complete_cache(root)
|
253
|
+
expect_file_content root, 'cached', 'foo-cached'
|
254
|
+
expect_file_content root, 'etag', 'foo-etag'
|
255
|
+
expect_file_content root, 'last_modified', 'foo-last-modified'
|
256
|
+
end
|
257
|
+
|
258
|
+
def expect_complete_credential_cache(root)
|
259
|
+
expect_credential_file_content root, 'cached', 'foo-cached'
|
260
|
+
expect_credential_file_content root, 'etag', 'foo-etag'
|
261
|
+
expect_credential_file_content root, 'last_modified', 'foo-last-modified'
|
262
|
+
end
|
263
|
+
|
264
|
+
def expect_credential_file_content(root, extension, content = '')
|
265
|
+
file = credential_cache_file root, extension
|
266
|
+
expect(file).to exist
|
267
|
+
expect(file.read).to eq(content)
|
268
|
+
end
|
269
|
+
|
270
|
+
def expect_file_content(root, extension, content = '')
|
271
|
+
file = cache_file root, extension
|
272
|
+
expect(file).to exist
|
273
|
+
expect(file.read).to eq(content)
|
274
|
+
end
|
275
|
+
|
276
|
+
def expect_file_deleted(extension)
|
277
|
+
file = touch mutable_cache_root, extension
|
278
|
+
expect(file).to exist
|
279
|
+
|
280
|
+
download_cache.evict('http://foo-uri/')
|
281
|
+
|
282
|
+
expect(file).not_to exist
|
283
|
+
end
|
284
|
+
|
285
|
+
def touch(root, extension, content = '')
|
286
|
+
file = cache_file root, extension
|
287
|
+
FileUtils.mkdir_p file.dirname
|
288
|
+
file.open('w') { |f| f.write content }
|
289
|
+
|
290
|
+
file
|
291
|
+
end
|
292
|
+
|
293
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Encoding: utf-8
|
2
|
+
# Copyright 2013-2014 the original author or authors.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
require 'spec_helper'
|
17
|
+
require 'buildpack_support/cache/internet_availability'
|
18
|
+
require 'buildpack_support/configuration_utils'
|
19
|
+
require 'buildpack_support/test/internet_availability_helper'
|
20
|
+
require 'buildpack_support/test/logging_helper'
|
21
|
+
|
22
|
+
describe BuildpackSupport::Cache::InternetAvailability do
|
23
|
+
include_context 'internet_availability_helper'
|
24
|
+
include_context 'logging_helper'
|
25
|
+
|
26
|
+
it 'should use internet by default' do
|
27
|
+
expect(described_class.instance.available?).to be
|
28
|
+
end
|
29
|
+
|
30
|
+
context do
|
31
|
+
|
32
|
+
before do
|
33
|
+
allow_any_instance_of(BuildpackSupport::ConfigurationUtils).to receive(:load).with('cache')
|
34
|
+
.and_return('remote_downloads' => 'disabled')
|
35
|
+
described_class.instance.send :initialize
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should not use internet if remote downloads are disabled' do
|
39
|
+
expect(described_class.instance.available?).not_to be
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should record availability' do
|
44
|
+
described_class.instance.available false
|
45
|
+
|
46
|
+
expect(described_class.instance.available?).not_to be
|
47
|
+
expect(log_contents).not_to match(/Internet availability set to false/)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should record availability with message' do
|
51
|
+
described_class.instance.available false, 'test message'
|
52
|
+
|
53
|
+
expect(described_class.instance.available?).not_to be
|
54
|
+
expect(log_contents).to match(/Internet availability set to false: test message/)
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|