|
26 | 26 | @storage = @gcloud.storage |
27 | 27 | @bucket = @storage.bucket @bucket_name |
28 | 28 | @local_file_path = File.expand_path "resources/file.txt", __dir__ |
29 | | - |
30 | | - # TODO: Delete all files in bucket before each test. |
31 | | - # These tests work well when the default state |
32 | | - # is an existing bucket with no files. |
33 | 29 | end |
34 | 30 |
|
35 | 31 | # Delete given file in Cloud Storage test bucket if it exists |
@@ -138,252 +134,4 @@ def capture &block |
138 | 134 | local_file.unlink |
139 | 135 | end |
140 | 136 | end |
141 | | - |
142 | | - it "can delete a file in a bucket" |
143 | | - it "can print metadata for a file in a bucket" |
144 | | - it "can make a file in a bucket public" |
145 | | - it "can generate a signed URL to access a file" |
146 | | - it "can rename a file in a bucket" |
147 | | - it "can copy a file to another bucket" |
148 | | - |
149 | | -end |
150 | | - |
151 | | -__END__ |
152 | | - |
153 | | -require "net/http" |
154 | | -require "tempfile" |
155 | | - |
156 | | -describe "Google Cloud Storage sample" do |
157 | | - |
158 | | - # Returns the text content of a given Gcloud storage file |
159 | | - def content_of storage_file |
160 | | - temp_file = Tempfile.new |
161 | | - storage_file.download temp_file.path |
162 | | - File.read temp_file.path |
163 | | - end |
164 | | - |
165 | | - # Capture and return STDOUT output by block |
166 | | - def capture &block |
167 | | - real_stdout = $stdout |
168 | | - $stdout = StringIO.new |
169 | | - block.call |
170 | | - $stdout.string |
171 | | - ensure |
172 | | - $stdout = real_stdout |
173 | | - end |
174 | | - |
175 | | - # Tests require environment variables: |
176 | | - # |
177 | | - # GCLOUD_PROJECT ID of your Google Cloud Platform project |
178 | | - # BUCKET Name of Google Cloud Storage bucket to use for tests |
179 | | - # ALT_BUCKET Name of an alternative bucket to also use for tests |
180 | | - # |
181 | | - before :all do |
182 | | - @project_id = ENV["GCLOUD_PROJECT"] |
183 | | - @gcloud = Gcloud.new @project_id |
184 | | - @storage = @gcloud.storage |
185 | | - @bucket = @storage.bucket ENV["BUCKET"] |
186 | | - @alt_bucket = @storage.bucket ENV["ALT_BUCKET"] |
187 | | - @test_file_path = File.expand_path "test-file.txt", __dir__ |
188 | | - end |
189 | | - |
190 | | - # Sample code uses project ID "my-gcp-project-id" and bucket "my-bucket" |
191 | | - # |
192 | | - # Stub calls to Gcloud library to use our test project and storage buckets |
193 | | - before :each do |
194 | | - allow(Gcloud).to receive(:new).with("my-project-id").and_return(@gcloud) |
195 | | - allow(@gcloud).to receive(:storage).and_return(@storage) |
196 | | - allow(@storage).to receive(:bucket).with("my-bucket-name"). |
197 | | - and_return(@bucket) |
198 | | - allow(@storage).to receive(:bucket).with("other-bucket-name"). |
199 | | - and_return(@alt_bucket) |
200 | | - |
201 | | - # File uploads use the fake path "/path/to/my-file.txt" |
202 | | - # When samples upload files, upload the spec/test-file.txt |
203 | | - # file instead for testing |
204 | | - allow(@bucket).to receive(:create_file). |
205 | | - with("/path/to/my-file.txt", "my-file.txt"). |
206 | | - and_wrap_original do |create_file, file_path, obj_path| |
207 | | - create_file.call @test_file_path, obj_path |
208 | | - end |
209 | | - |
210 | | - cleanup! |
211 | | - end |
212 | | - |
213 | | - # Delete files in bucket used for tests |
214 | | - def cleanup! |
215 | | - @bucket.file("my-file.txt").delete if @bucket.file "my-file.txt" |
216 | | - @bucket.file("renamed-file.txt").delete if @bucket.file "renamed-file.txt" |
217 | | - @alt_bucket.file("my-file.txt").delete if @alt_bucket.file "my-file.txt" |
218 | | - end |
219 | | - |
220 | | - it "list buckets" |
221 | | - |
222 | | - it "create bucket" |
223 | | - # it "create bucket" do |
224 | | - # # Google Cloud Storage bucket IDs are unique |
225 | | - # # |
226 | | - # # To prevent creating unique buckets, this test simply verifies |
227 | | - # # that the correct `#create_bucket` method is called by the sample |
228 | | - # expect(@storage).to receive(:create_bucket).with("my-bucket-name"). |
229 | | - # and_return(@bucket) |
230 | | - |
231 | | - # expect { create_bucket }.to output("Created bucket: #{@bucket.name}\n"). |
232 | | - # to_stdout |
233 | | - # end |
234 | | - |
235 | | - it "upload file" do |
236 | | - expect(@bucket.file "my-file.txt").to be nil |
237 | | - |
238 | | - expect { upload_file }.to output("Uploaded my-file.txt\n").to_stdout |
239 | | - |
240 | | - expect(@bucket.file "my-file.txt").not_to be nil |
241 | | - |
242 | | - expect(content_of @bucket.file("my-file.txt")).to eq( |
243 | | - "Content of test file\n" |
244 | | - ) |
245 | | - end |
246 | | - |
247 | | - it "download file" do |
248 | | - upload_file |
249 | | - |
250 | | - # This sample downloads to the fake path "/path/to/my-file.txt" |
251 | | - # When the file download method is called, download to a |
252 | | - # temporary file instead |
253 | | - uploaded_file = @bucket.file "my-file.txt" |
254 | | - temp_file = Tempfile.new |
255 | | - |
256 | | - expect(@bucket).to receive(:file).with("my-file.txt"). |
257 | | - and_return(uploaded_file) |
258 | | - |
259 | | - expect(uploaded_file).to receive(:download).with("/path/to/my-file.txt"). |
260 | | - and_wrap_original do |download, path| |
261 | | - download.call temp_file.path |
262 | | - end |
263 | | - |
264 | | - expect(File.read temp_file).to eq "" |
265 | | - |
266 | | - expect { download_file }.to output("Downloaded my-file.txt\n").to_stdout |
267 | | - |
268 | | - expect(File.read temp_file).to eq "Content of test file\n" |
269 | | - end |
270 | | - |
271 | | - it "make file public" do |
272 | | - upload_file |
273 | | - uploaded_file = @bucket.file "my-file.txt" |
274 | | - |
275 | | - response = Net::HTTP.get_response URI.parse(uploaded_file.public_url) |
276 | | - expect(response).to be_a Net::HTTPForbidden |
277 | | - expect(response.code.to_i).to eq 403 |
278 | | - |
279 | | - expect { make_file_public }.to output( |
280 | | - "my-file.txt is publicly accessible at #{uploaded_file.public_url}\n" |
281 | | - ).to_stdout |
282 | | - |
283 | | - response = Net::HTTP.get_response URI.parse(uploaded_file.public_url) |
284 | | - expect(response).to be_a Net::HTTPOK |
285 | | - expect(response.code.to_i).to eq 200 |
286 | | - expect(response.body).to eq "Content of test file\n" |
287 | | - end |
288 | | - |
289 | | - it "rename file" do |
290 | | - upload_file |
291 | | - |
292 | | - expect(@bucket.file "my-file.txt").not_to be nil |
293 | | - expect(@bucket.file "renamed-file.txt").to be nil |
294 | | - |
295 | | - expect { rename_file }.to output( |
296 | | - "my-file.txt has been renamed to renamed-file.txt\n" |
297 | | - ).to_stdout |
298 | | - |
299 | | - expect(@bucket.file "my-file.txt").to be nil |
300 | | - expect(@bucket.file "renamed-file.txt").not_to be nil |
301 | | - |
302 | | - expect(content_of @bucket.file("renamed-file.txt")).to eq( |
303 | | - "Content of test file\n" |
304 | | - ) |
305 | | - end |
306 | | - |
307 | | - it "copy file between buckets" do |
308 | | - upload_file |
309 | | - |
310 | | - expect(@alt_bucket.file "my-file.txt").to be nil |
311 | | - |
312 | | - expect { copy_file_between_buckets }.to output( |
313 | | - "my-file.txt in #{@bucket.name} copied to " + |
314 | | - "my-file.txt in #{@alt_bucket.name}\n" |
315 | | - ).to_stdout |
316 | | - |
317 | | - expect(@alt_bucket.file "my-file.txt").not_to be nil |
318 | | - expect(content_of @alt_bucket.file("my-file.txt")).to eq( |
319 | | - "Content of test file\n" |
320 | | - ) |
321 | | - end |
322 | | - |
323 | | - it "list bucket contents" do |
324 | | - upload_file |
325 | | - |
326 | | - expect { list_bucket_contents }.to output(/my-file\.txt/).to_stdout |
327 | | - end |
328 | | - |
329 | | - it "list file details" do |
330 | | - upload_file |
331 | | - uploaded_file = @bucket.file "my-file.txt" |
332 | | - |
333 | | - uploaded_file.cache_control = "Cache-Control:public, max-age=3600" |
334 | | - uploaded_file.content_disposition = "attachment; filename=my-file.txt" |
335 | | - uploaded_file.content_language = "en" |
336 | | - uploaded_file.metadata = { foo: "bar" } |
337 | | - |
338 | | - output = capture { list_file_details } |
339 | | - |
340 | | - expect(output).to include("Name: my-file.txt") |
341 | | - expect(output).to include("Bucket: #{@bucket.name}") |
342 | | - expect(output).to include("Storage class: STANDARD") |
343 | | - expect(output).to include("ID: #{uploaded_file.id}") |
344 | | - expect(output).to include("Size: 23 bytes") |
345 | | - expect(output).to match(/Created: \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/) |
346 | | - expect(output).to match(/Updated: \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/) |
347 | | - expect(output).to include("Generation: #{uploaded_file.generation}") |
348 | | - expect(output).to include( |
349 | | - "Metageneration: #{uploaded_file.metageneration}" |
350 | | - ) |
351 | | - expect(output).to include("Etag: #{uploaded_file.etag}") |
352 | | - expect(output).to include("Owners: #{uploaded_file.acl.owners.join ","}") |
353 | | - expect(output).to include("Crc32c: #{uploaded_file.crc32c}") |
354 | | - expect(output).to include("md5_hash: #{uploaded_file.md5}") |
355 | | - expect(output).to include( |
356 | | - "Cache-control: Cache-Control:public, max-age=3600" |
357 | | - ) |
358 | | - expect(output).to include("Content-type: text/plain") |
359 | | - expect(output).to include( |
360 | | - "Content-disposition: attachment; filename=my-file.txt" |
361 | | - ) |
362 | | - expect(output).to include("Content-encoding:") |
363 | | - expect(output).to include("Content-language: en") |
364 | | - expect(output).to include("Metadata:\n - foo = bar") |
365 | | - end |
366 | | - |
367 | | - it "delete file" do |
368 | | - upload_file |
369 | | - |
370 | | - expect(@bucket.file "my-file.txt").not_to be nil |
371 | | - |
372 | | - expect { delete_file }.to output("Deleted my-file.txt\n").to_stdout |
373 | | - |
374 | | - expect(@bucket.file "my-file.txt").to be nil |
375 | | - end |
376 | | - |
377 | | - it "delete bucket" |
378 | | - # it "delete bucket" do |
379 | | - # # Google Cloud Storage bucket IDs are unique |
380 | | - # # |
381 | | - # # To prevent deleting the bucket that is used for testing, |
382 | | - # # this test simply verifies that the correct `#delete_bucket` |
383 | | - # # method is called by the sample |
384 | | - # expect(@bucket).to receive(:delete) |
385 | | - |
386 | | - # expect { delete_bucket }.to output("Deleted bucket: #{@bucket.name}\n"). |
387 | | - # to_stdout |
388 | | - # end |
389 | 137 | end |
0 commit comments