buildpack-utils 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bda82697d95d1aa6cb76a0e785aea130753f4130
4
+ data.tar.gz: 549cfc1b63231a311e065ef914f5e42265a7e758
5
+ SHA512:
6
+ metadata.gz: dad9181bdc893d7c291fbf4b21dd2368f9b000978942971c603f1e362f90d579851354a70e2864065308f73b4a594e2bbdeaaf823d530c1f1e1c823d330661e5
7
+ data.tar.gz: d7c6b770d92c71502a4a583204ed560c1d4f810c47bcd938cc83611eb0deaf0d8eac7c7dc439d7f1b3f892f04c6498521a70c1046e2587fcb11e73cd98ccb4a1
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/NOTICE ADDED
@@ -0,0 +1,2 @@
1
+ Cloud Foundry Buildpack Utilities
2
+ Copyright 2013 the original author or authors.
@@ -0,0 +1,9 @@
1
+ # Cloud Foundry Buildpack Utilities
2
+ [![Build Status](https://travis-ci.org/cloudfoundry/buildpack-utils.png?branch=master)](https://travis-ci.org/cloudfoundry/buildpack-utils)
3
+ [![Gem Version](https://badge.fury.io/rb/buildpack-utils.png)](http://badge.fury.io/rb/buildpack-utils)
4
+ [![Dependency Status](https://gemnasium.com/cloudfoundry/buildpack-utils.png)](http://gemnasium.com/cloudfoundry/buildpack-utils)
5
+ [![Code Climate](https://codeclimate.com/github/cloudfoundry/buildpack-utils.png)](https://codeclimate.com/github/cloudfoundry/buildpack-utils)
6
+
7
+ `buildpack-utils` provides a set of additional utilities for use in [Cloud Foundry][cf] buildpacks
8
+
9
+ [cf]: http://www.cloudfoundry.com
@@ -0,0 +1,34 @@
1
+ # Cloud Foundry Buildpack Utilities
2
+ # Copyright (c) 2013 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 'buildpack-utils/download_cache'
17
+
18
+ # An extension of {BuildpackUtils::DownloadCache} that is configured to use the application cache. The application
19
+ # cache location is defined by the second argument (<tt>ARGV[1]</tt>) to the +compile+ script.
20
+ #
21
+ # <b>WARNING: This cache should only by used by code run by the +compile+ script</b>
22
+ class BuildpackUtils::ApplicationCache < BuildpackUtils::DownloadCache
23
+
24
+ # Creates an instance that is configured to use the application cache. The application cache location is defined by
25
+ # the second argument (<tt>ARGV[1]</tt>) to the +compile+ script.
26
+ #
27
+ # @raise if the second argument (<tt>ARGV[1]</tt>) to the +compile+ script is +nil+
28
+ def initialize
29
+ application_cache_directory = ARGV[1]
30
+ raise 'Application cache directory is undefined' if application_cache_directory.nil?
31
+ super(application_cache_directory)
32
+ end
33
+
34
+ end
@@ -0,0 +1,161 @@
1
+ # Cloud Foundry Buildpack Utilities
2
+ # Copyright (c) 2013 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 'net/http'
17
+ require 'tmpdir'
18
+
19
+ # A cache for downloaded files that is configured to use a filesystem as the backing store. This cache uses standard
20
+ # file locking (<tt>File.flock()</tt>) in order ensure that mutation of files in the cache is non-concurrent across
21
+ # processes. Reading files (once they've been downloaded) happens concurrently so read performance is not impacted.
22
+ class BuildpackUtils::DownloadCache
23
+
24
+ # Creates an instance of the cache that is backed by the filesystem rooted at +cache_root+
25
+ #
26
+ # @param [String] cache_root the filesystem root for downloaded files to be cached in
27
+ def initialize(cache_root = Dir.tmpdir)
28
+ @cache_root = cache_root
29
+ end
30
+
31
+ # Retrieves an item from the cache. Retrieval of the item uses the following algorithm:
32
+ #
33
+ # 1. Obtain an exclusive lock based on the key of the item. This allows concurrency for different items, but not for
34
+ # the same item.
35
+ # 2. If the the cached item does not exist, download from +uri+ and cache it, its +Etag+, and its +Last-Modified+
36
+ # values if they exist
37
+ # 3. If the cached file does exist, and the original download had an +Etag+ or a +Last-Modified+ value, attempt to
38
+ # download from +uri+ again. If the result is +304+ (+Not-Modified+), then proceed without changing the cached
39
+ # item. If it is anything else, overwrite the cached file and its +Etag+ and +Last-Modified+ values if they exist.
40
+ # 4. Downgrade the lock to a shared lock as no further mutation of the cache is possible. This allows concurrency for
41
+ # read access of the item.
42
+ # 5. Yield the cached file (opened read-only) to the passed in block. Once the block is complete, the file is closed
43
+ # and the lock is released.
44
+ #
45
+ # @param [String] key the key of the item to retrieve
46
+ # @param [String] uri the uri to download if the item is not already in the cache. Also used in the case where the
47
+ # item is already in the cache, to validate that the item is up to date
48
+ # @yieldparam [File] file the file representing the cached item. In order to ensure that the file is not changed or
49
+ # deleted while it is being used, the cached item can only be accessed as part of a block.
50
+ # @return [void]
51
+ def get(key, uri)
52
+ filenames = filenames(key)
53
+ File.open(filenames[:lock], File::CREAT) do |lock_file|
54
+ lock_file.flock(File::LOCK_EX)
55
+
56
+ if should_update(filenames)
57
+ update(filenames, uri)
58
+ elsif should_download(filenames)
59
+ download(filenames, uri)
60
+ end
61
+
62
+ lock_file.flock(File::LOCK_SH)
63
+
64
+ File.open(filenames[:cached], File::RDONLY) do |cached_file|
65
+ yield cached_file
66
+ end
67
+ end
68
+ end
69
+
70
+ # Remove an item from the cache
71
+ #
72
+ # @param [String] key the key of the item to remove
73
+ # @return [void]
74
+ def evict(key)
75
+ filenames = filenames(key)
76
+ File.open(filenames[:lock], File::CREAT) do |lock_file|
77
+ lock_file.flock(File::LOCK_EX)
78
+
79
+ delete_file filenames[:cached]
80
+ delete_file filenames[:etag]
81
+ delete_file filenames[:last_modified]
82
+ delete_file filenames[:lock]
83
+ end
84
+ end
85
+
86
+ def delete_file(filename)
87
+ File.delete filename if File.exists? filename
88
+ end
89
+ private
90
+
91
+
92
+ def download(filenames, uri)
93
+ rich_uri = URI(uri)
94
+
95
+ Net::HTTP.start(rich_uri.host, rich_uri.port) do |http|
96
+ request = Net::HTTP::Get.new(uri)
97
+ http.request request do |response|
98
+ write_response(filenames, response)
99
+ end
100
+ end
101
+ end
102
+
103
+ def filenames(key)
104
+ {
105
+ cached: File.join(@cache_root, "#{key}.cached"),
106
+ etag: File.join(@cache_root, "#{key}.etag"),
107
+ last_modified: File.join(@cache_root, "#{key}.last_modified"),
108
+ lock: File.join(@cache_root, "#{key}.lock"),
109
+ }
110
+ end
111
+
112
+ def persist_header(response, header, filename)
113
+ unless response[header].nil?
114
+ File.open(filename, File::CREAT|File::WRONLY) do |file|
115
+ file.write(response[header])
116
+ end
117
+ end
118
+ end
119
+
120
+ def set_header(request, header, filename)
121
+ if File.exists?(filename)
122
+ File.open(filename, File::RDONLY) do |file|
123
+ request[header] = file.read
124
+ end
125
+ end
126
+ end
127
+
128
+ def should_download(filenames)
129
+ !File.exists?(filenames[:cached])
130
+ end
131
+
132
+ def should_update(filenames)
133
+ File.exists?(filenames[:cached]) && (File.exists?(filenames[:etag]) || File.exists?(filenames[:last_modified]))
134
+ end
135
+
136
+ def update(filenames, uri)
137
+ rich_uri = URI(uri)
138
+
139
+ Net::HTTP.start(rich_uri.host, rich_uri.port) do |http|
140
+ request = Net::HTTP::Get.new(uri)
141
+ set_header request, 'If-None-Match', filenames[:etag]
142
+ set_header request, 'If-Modified-Since', filenames[:last_modified]
143
+
144
+ http.request request do |response|
145
+ write_response(filenames, response) unless response.code == '304'
146
+ end
147
+ end
148
+ end
149
+
150
+ def write_response(filenames, response)
151
+ persist_header response, 'Etag', filenames[:etag]
152
+ persist_header response, 'Last-Modified', filenames[:last_modified]
153
+
154
+ File.open(filenames[:cached], File::CREAT|File::WRONLY) do |cached_file|
155
+ response.read_body do |chunk|
156
+ cached_file.write(chunk)
157
+ end
158
+ end
159
+ end
160
+
161
+ end
@@ -0,0 +1,32 @@
1
+ # Cloud Foundry Buildpack Utilities
2
+ # Copyright (c) 2013 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 'buildpack-utils/download_cache'
17
+
18
+ # An extension of {BuildpackUtils::DownloadCache} that is configured to use the global cache. The global cache location
19
+ # is defined by the +BUILDPACK_CACHE+ environment variable
20
+ class BuildpackUtils::GlobalCache < BuildpackUtils::DownloadCache
21
+
22
+ # Creates an instance that is configured to use the global cache. The global cache location is defined by the
23
+ # +BUILDPACK_CACHE+ environment variable
24
+ #
25
+ # @raise if the +BUILDPACK_CACHE+ environment variable is +nil+
26
+ def initialize
27
+ global_cache_directory = ENV['BUILDPACK_CACHE']
28
+ raise 'Global cache directory is undefined' if global_cache_directory.nil?
29
+ super(global_cache_directory)
30
+ end
31
+
32
+ end
@@ -0,0 +1,22 @@
1
+ # Cloud Foundry Buildpack Utilities
2
+ # Copyright (c) 2013 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
+ # A collection of utilities that are useful when implementing Cloud Foundry buildpacks.
17
+ module BuildpackUtils
18
+ end
19
+
20
+ require 'buildpack-utils/application_cache'
21
+ require 'buildpack-utils/download_cache'
22
+ require 'buildpack-utils/global_cache'
@@ -0,0 +1,53 @@
1
+ # Cloud Foundry Buildpack Utilities
2
+ # Copyright (c) 2013 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 'tmpdir'
18
+
19
+ describe BuildpackUtils::ApplicationCache do
20
+
21
+ before do
22
+ @previous_value = ARGV[1]
23
+ ARGV[1] = nil
24
+ end
25
+
26
+ after do
27
+ ARGV[1] = @previous_value
28
+ end
29
+
30
+ it 'should raise an error if ARGV[1] is not defined' do
31
+ lambda { BuildpackUtils::ApplicationCache.new }.should raise_error
32
+ end
33
+
34
+ it 'should use ARGV[1] directory' do
35
+ stub_request(:get, 'http://foo-uri/').to_return(
36
+ :status => 200,
37
+ :body => 'foo-cached',
38
+ :headers => {
39
+ :Etag => 'foo-etag',
40
+ 'Last-Modified' => 'foo-last-modified'
41
+ }
42
+ )
43
+
44
+ Dir.mktmpdir do |root|
45
+ ARGV[1] = root
46
+
47
+ BuildpackUtils::ApplicationCache.new().get('foo', 'http://foo-uri/') {}
48
+
49
+ expect(File.exists?(File.join(root, 'foo.cached'))).to be_true
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,245 @@
1
+ # Cloud Foundry Buildpack Utilities
2
+ # Copyright (c) 2013 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 'tmpdir'
18
+
19
+ describe BuildpackUtils::DownloadCache do
20
+
21
+ it 'should download from a uri if the cached file does not exist' do
22
+ stub_request(:get, 'http://foo-uri/').to_return(
23
+ :status => 200,
24
+ :body => 'foo-cached',
25
+ :headers => {
26
+ :Etag => 'foo-etag',
27
+ 'Last-Modified' => 'foo-last-modified'
28
+ }
29
+ )
30
+
31
+ Dir.mktmpdir do |root|
32
+ BuildpackUtils::DownloadCache.new(root).get('foo', 'http://foo-uri/') {}
33
+
34
+ expect_file_content root, 'cached', 'foo-cached'
35
+ expect_file_content root, 'etag', 'foo-etag'
36
+ expect_file_content root, 'last_modified', 'foo-last-modified'
37
+ end
38
+ end
39
+
40
+ it 'should download from a uri if the cached file exists and etag exists' do
41
+ stub_request(:get, 'http://foo-uri/').with(
42
+ :headers => {
43
+ 'If-None-Match' => 'foo-etag'
44
+ }
45
+ ).to_return(
46
+ :status => 200,
47
+ :body => 'foo-cached',
48
+ :headers => {
49
+ :Etag => 'foo-etag',
50
+ 'Last-Modified' => 'foo-last-modified'
51
+ }
52
+ )
53
+
54
+ Dir.mktmpdir do |root|
55
+ touch root, 'cached', 'foo-cached'
56
+ touch root, 'etag', 'foo-etag'
57
+
58
+ BuildpackUtils::DownloadCache.new(root).get('foo', 'http://foo-uri/') {}
59
+
60
+ expect_file_content root, 'cached', 'foo-cached'
61
+ expect_file_content root, 'etag', 'foo-etag'
62
+ expect_file_content root, 'last_modified', 'foo-last-modified'
63
+ end
64
+ end
65
+
66
+ it 'should download from a uri if the cached file exists and last modified exists' do
67
+ stub_request(:get, 'http://foo-uri/').with(
68
+ :headers => {
69
+ 'If-Modified-Since' => 'foo-last-modified'
70
+ }
71
+ ).to_return(
72
+ :status => 200,
73
+ :body => 'foo-cached',
74
+ :headers => {
75
+ :Etag => 'foo-etag',
76
+ 'Last-Modified' => 'foo-last-modified'
77
+ }
78
+ )
79
+
80
+ Dir.mktmpdir do |root|
81
+ touch root, 'cached', 'foo-cached'
82
+ touch root, 'last_modified', 'foo-last-modified'
83
+
84
+ BuildpackUtils::DownloadCache.new(root).get('foo', 'http://foo-uri/') {}
85
+
86
+ expect_file_content root, 'cached', 'foo-cached'
87
+ expect_file_content root, 'etag', 'foo-etag'
88
+ expect_file_content root, 'last_modified', 'foo-last-modified'
89
+ end
90
+ end
91
+
92
+ it 'should download from a uri if the cached file exists, etag exists, and last modified exists' do
93
+ stub_request(:get, 'http://foo-uri/').with(
94
+ :headers => {
95
+ 'If-None-Match' => 'foo-etag',
96
+ 'If-Modified-Since' => 'foo-last-modified'
97
+ }
98
+ ).to_return(
99
+ :status => 200,
100
+ :body => 'foo-cached',
101
+ :headers => {
102
+ :Etag => 'foo-etag',
103
+ 'Last-Modified' => 'foo-last-modified'
104
+ }
105
+ )
106
+
107
+ Dir.mktmpdir do |root|
108
+ touch root, 'cached', 'foo-cached'
109
+ touch root, 'etag', 'foo-etag'
110
+ touch root, 'last_modified', 'foo-last-modified'
111
+
112
+ BuildpackUtils::DownloadCache.new(root).get('foo', 'http://foo-uri/') {}
113
+
114
+ expect_file_content root, 'cached', 'foo-cached'
115
+ expect_file_content root, 'etag', 'foo-etag'
116
+ expect_file_content root, 'last_modified', 'foo-last-modified'
117
+ end
118
+ end
119
+
120
+ it 'should not download from a uri if the cached file exists and the etag and last modified do not exist' do
121
+ Dir.mktmpdir do |root|
122
+ touch root, 'cached', 'foo-cached'
123
+
124
+ BuildpackUtils::DownloadCache.new(root).get('foo', 'http://foo-uri/') {}
125
+
126
+ expect_file_content root, 'cached', 'foo-cached'
127
+ end
128
+ end
129
+
130
+ it 'should not overwrite existing information if 304 is received' do
131
+ stub_request(:get, 'http://foo-uri/').with(
132
+ :headers => {
133
+ 'If-None-Match' => 'foo-etag',
134
+ 'If-Modified-Since' => 'foo-last-modified'
135
+ }
136
+ ).to_return(
137
+ :status => 304,
138
+ :body => 'bar-cached',
139
+ :headers => {
140
+ :Etag => 'bar-etag',
141
+ 'Last-Modified' => 'bar-last-modified'
142
+ }
143
+ )
144
+
145
+ Dir.mktmpdir do |root|
146
+ touch root, 'cached', 'foo-cached'
147
+ touch root, 'etag', 'foo-etag'
148
+ touch root, 'last_modified', 'foo-last-modified'
149
+
150
+ BuildpackUtils::DownloadCache.new(root).get('foo', 'http://foo-uri/') {}
151
+
152
+ expect_file_content root, 'cached', 'foo-cached'
153
+ expect_file_content root, 'etag', 'foo-etag'
154
+ expect_file_content root, 'last_modified', 'foo-last-modified'
155
+ end
156
+ end
157
+
158
+ it 'should overwrite existing information if 304 is not received' do
159
+ stub_request(:get, 'http://foo-uri/').with(
160
+ :headers => {
161
+ 'If-None-Match' => 'foo-etag',
162
+ 'If-Modified-Since' => 'foo-last-modified'
163
+ }
164
+ ).to_return(
165
+ :status => 200,
166
+ :body => 'bar-cached',
167
+ :headers => {
168
+ :Etag => 'bar-etag',
169
+ 'Last-Modified' => 'bar-last-modified'
170
+ }
171
+ )
172
+
173
+ Dir.mktmpdir do |root|
174
+ touch root, 'cached', 'foo-cached'
175
+ touch root, 'etag', 'foo-etag'
176
+ touch root, 'last_modified', 'foo-last-modified'
177
+
178
+ BuildpackUtils::DownloadCache.new(root).get('foo', 'http://foo-uri/') {}
179
+
180
+ expect_file_content root, 'cached', 'bar-cached'
181
+ expect_file_content root, 'etag', 'bar-etag'
182
+ expect_file_content root, 'last_modified', 'bar-last-modified'
183
+ end
184
+ end
185
+
186
+ it 'should pass read-only file to block' do
187
+ stub_request(:get, 'http://foo-uri/').to_return(
188
+ :status => 200,
189
+ :body => 'foo-cached',
190
+ :headers => {
191
+ :Etag => 'foo-etag',
192
+ 'Last-Modified' => 'foo-last-modified'
193
+ }
194
+ )
195
+
196
+ Dir.mktmpdir do |root|
197
+ BuildpackUtils::DownloadCache.new(root).get('foo', 'http://foo-uri/') do |file|
198
+ expect(file.read).to eq('foo-cached')
199
+ lambda { file.write('bar') }.should raise_error
200
+ end
201
+ end
202
+ end
203
+
204
+ it 'should delete the cached file if it exists' do
205
+ expect_file_deleted 'cached'
206
+ end
207
+
208
+ it 'should delete the etag file if it exists' do
209
+ expect_file_deleted 'etag'
210
+ end
211
+
212
+ it 'should delete the last_modified file if it exists' do
213
+ expect_file_deleted 'last_modified'
214
+ end
215
+
216
+ it 'should delete the lock file if it exists' do
217
+ expect_file_deleted 'lock'
218
+ end
219
+
220
+ private
221
+
222
+ def touch(root, extension, content = '')
223
+ file = File.join(root, "foo.#{extension}")
224
+ File.open(file, 'w') { |f| f.write(content) }
225
+ file
226
+ end
227
+
228
+ def expect_file_deleted(extension)
229
+ Dir.mktmpdir do |root|
230
+ file = touch root, extension
231
+ expect(File.exists?(file)).to be_true
232
+
233
+ BuildpackUtils::DownloadCache.new(root).evict('foo')
234
+
235
+ expect(File.exists?(file)).to be_false
236
+ end
237
+ end
238
+
239
+ def expect_file_content(root, extension, content = '')
240
+ file = File.join(root, "foo.#{extension}")
241
+ expect(File.exists?(file)).to be_true
242
+ File.open(file, 'r') { |f| expect(f.read).to eq(content)}
243
+ end
244
+
245
+ end
@@ -0,0 +1,52 @@
1
+ # Cloud Foundry Buildpack Utilities
2
+ # Copyright (c) 2013 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 'tmpdir'
18
+
19
+ describe BuildpackUtils::GlobalCache do
20
+
21
+ before do
22
+ @previous_value = ENV.delete 'BUILDPACK_CACHE'
23
+ end
24
+
25
+ after do
26
+ ENV['BUILDPACK_CACHE'] = @previous_value
27
+ end
28
+
29
+ it 'should raise an error if BUILDPACK_CACHE is not defined' do
30
+ lambda { BuildpackUtils::GlobalCache.new }.should raise_error
31
+ end
32
+
33
+ it 'should use BUILDPACK_CACHE directory' do
34
+ stub_request(:get, 'http://foo-uri/').to_return(
35
+ :status => 200,
36
+ :body => 'foo-cached',
37
+ :headers => {
38
+ :Etag => 'foo-etag',
39
+ 'Last-Modified' => 'foo-last-modified'
40
+ }
41
+ )
42
+
43
+ Dir.mktmpdir do |root|
44
+ ENV['BUILDPACK_CACHE'] = root
45
+
46
+ BuildpackUtils::GlobalCache.new().get('foo', 'http://foo-uri/') {}
47
+
48
+ expect(File.exists?(File.join(root, 'foo.cached'))).to be_true
49
+ end
50
+ end
51
+
52
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: buildpack-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Hale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redcarpet
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '0.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '0.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.11'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.11'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ description: Provides a set of utilities for use in Cloud Foundry buildpacks
112
+ email: bhale@gopivotal.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - LICENSE
118
+ - NOTICE
119
+ - README.md
120
+ - lib/buildpack-utils/application_cache.rb
121
+ - lib/buildpack-utils/download_cache.rb
122
+ - lib/buildpack-utils/global_cache.rb
123
+ - lib/buildpack_utils.rb
124
+ - spec/buildpack-utils/application_cache_spec.rb
125
+ - spec/buildpack-utils/download_cache_spec.rb
126
+ - spec/buildpack-utils/global_cache_spec.rb
127
+ homepage: https://github.com/cloudfoundry/buildpack-utils
128
+ licenses:
129
+ - Apache-2.0
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: 1.9.3
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.0.0
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Utilities for use in Cloud Foundry buildpacks
151
+ test_files:
152
+ - spec/buildpack-utils/application_cache_spec.rb
153
+ - spec/buildpack-utils/download_cache_spec.rb
154
+ - spec/buildpack-utils/global_cache_spec.rb
155
+ has_rdoc: