forked from googleapis/google-cloud-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.rb
More file actions
621 lines (575 loc) · 23.2 KB
/
helper.rb
File metadata and controls
621 lines (575 loc) · 23.2 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# Copyright 2016 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "simplecov"
gem "minitest"
require "minitest/autorun"
require "minitest/focus"
require "minitest/rg"
require "ostruct"
require "json"
require "base64"
require "uri"
require "google/cloud/storage"
##
# Monkey-Patch Google API Client to support Mocks
module Google::Apis::Core::Hashable
##
# Minitest Mock depends on === to match same-value objects.
# By default, the Google API Client objects do not match with ===.
# Therefore, we must add this capability.
# This module seems like as good a place as any...
def === other
return(to_h === other.to_h) if other.respond_to? :to_h
super
end
end
class MockStorage < Minitest::Spec
let(:project) { "test" }
let(:credentials) { OpenStruct.new(client: OpenStruct.new(updater_proc: Proc.new {})) }
let(:storage) { Google::Cloud::Storage::Project.new(Google::Cloud::Storage::Service.new(project, credentials, upload_chunk_size: 5 * 1024 * 1024 )) }
let(:pubsub_topic_name) { "my-topic-name" }
let(:file_obj) { StringIO.new("My test file") }
let(:file_name) { "my_test_file.txt" }
let(:acl) { "authenticated_read" }
# Register this spec type for when :mock_storage is used.
register_spec_type(self) do |desc, *addl|
addl.include? :mock_storage
end
def random_bucket_hash name: random_bucket_name,
url_root: "https://www.googleapis.com/storage/v1",
location: "US",
storage_class: "STANDARD",
versioning: nil,
logging_bucket: nil,
logging_prefix: nil,
website_main: nil,
website_404: nil,
cors: [],
requester_pays: nil,
lifecycle: nil,
location_type: "multi-region",
rpo: "DEFAULT",
autoclass_enabled: nil,
autoclass_terminal_storage_class: nil,
enable_object_retention: nil,
effective_time: DateTime.now,
retention_duration_seconds: 604_800, # 7 days
soft_deleted: nil,
hierarchical_namespace: nil,
generation: "1733393981548601746"
versioning_config = { "enabled" => versioning } if versioning
data = {
"kind" => "storage#bucket",
"id" => name,
"selfLink" => "#{url_root}/b/#{name}",
"projectNumber" => "1234567890",
"name" => name,
"timeCreated" => Time.now,
"generation" => generation,
"metageneration" => "1",
"owner" => { "entity" => "project-owners-1234567890" },
"location" => location,
"locationType" => location_type,
"rpo" => rpo,
"cors" => cors,
"lifecycle" => lifecycle,
"logging" => logging_hash(logging_bucket, logging_prefix),
"storageClass" => storage_class,
"versioning" => versioning_config,
"website" => website_hash(website_main, website_404),
"billing" => billing_hash(requester_pays),
"etag" => "CAE=",
"autoclass" => autoclass_config_hash(autoclass_enabled, autoclass_terminal_storage_class),
"enableObjectRetention" => enable_object_retention,
"softDeletePolicy" => soft_delete_policy_object(retention_duration_seconds: retention_duration_seconds),
"hierarchicalNamespace" => hierarchical_namespace
}
if soft_deleted
data.merge! soft_delete_bucket_hash
end
data.delete_if { |_, v| v.nil? }
end
def soft_delete_policy_object retention_duration_seconds: 604_800
Google::Apis::StorageV1::Bucket::SoftDeletePolicy.new(
effective_time: DateTime.now,
retention_duration_seconds: retention_duration_seconds
)
end
def soft_delete_policy_bucket retention_duration_seconds: 604_800
Google::Apis::StorageV1::Bucket::SoftDeletePolicy.new(
effective_time: DateTime.now,
retention_duration_seconds: retention_duration_seconds
)
end
def soft_delete_bucket_hash
soft_delete_policy = soft_delete_policy_bucket
{
"softDeleteTime" => soft_delete_policy.effective_time,
"hardDeleteTime" => soft_delete_policy.effective_time.to_time + soft_delete_policy.retention_duration_seconds
}
end
def hierarchical_namespace_object enabled: true
Google::Apis::StorageV1::Bucket::HierarchicalNamespace.new(
enabled: enabled
)
end
def autoclass_config_hash(enabled, terminal_storage_class)
{ "enabled" => enabled,
"terminalStorageClass" => terminal_storage_class
}.delete_if { |_, v| v.nil? } if !enabled.nil? || terminal_storage_class
end
def logging_hash(bucket, prefix)
{ "logBucket" => bucket,
"logObjectPrefix" => prefix,
}.delete_if { |_, v| v.nil? } if bucket || prefix
end
def website_hash(website_main, website_404)
{ "mainPageSuffix" => website_main,
"notFoundPage" => website_404,
}.delete_if { |_, v| v.nil? } if website_main || website_404
end
def billing_hash(requester_pays)
{ "requesterPays" => requester_pays} unless requester_pays.nil?
end
def file_retention_hash(retention_params)
{ "mode" => retention_params[:mode],
"retainUntilTime" => retention_params[:retain_until_time]
}.delete_if { |_, v| v.nil? } if !retention_params.nil? && !retention_params.empty?
end
def random_file_hash bucket=random_bucket_name,
name=random_file_path,
generation="1234567890",
kms_key_name="path/to/encryption_key_name",
custom_time: nil,
retention_params: nil,
override_unlocked_retention: nil,
soft_delete_time: nil,
hard_delete_time: nil
{ "kind" => "storage#object",
"id" => "#{bucket}/#{name}/1234567890",
"selfLink" => "https://www.googleapis.com/storage/v1/b/#{bucket}/o/#{name}",
"name" => "#{name}",
"timeCreated" => Time.now,
"bucket" => "#{bucket}",
"generation" => generation,
"metageneration" => "1",
"cacheControl" => "public, max-age=3600",
"contentDisposition" => "attachment; filename=filename.ext",
"contentEncoding" => "gzip",
"contentLanguage" => "en",
"contentType" => "text/plain",
"customTime" => custom_time,
"updated" => Time.now,
"storageClass" => "STANDARD",
"size" => rand(10_000),
"md5Hash" => "HXB937GQDFxDFqUGi//weQ==",
"mediaLink" => "https://www.googleapis.com/download/storage/v1/b/#{bucket}/o/#{name}?generation=1234567890&alt=media",
"metadata" => { "player" => "Alice", "score" => "101" },
"owner" => { "entity" => "user-1234567890", "entityId" => "abc123" },
"crc32c" => "Lm1F3g==",
"etag" => "CKih16GjycICEAE=",
"kmsKeyName" => kms_key_name,
"temporaryHold" => true,
"eventBasedHold" => true,
"retentionExpirationTime" => Time.now,
"retention" => file_retention_hash(retention_params),
"overrideUnlockedRetention" => override_unlocked_retention,
"softDeleteTime" => soft_delete_time,
"hardDeleteTime" => hard_delete_time }
end
def random_bucket_name
(0...50).map { ("a".."z").to_a[rand(26)] }.join
end
def random_file_path
[(0...10).map { ("a".."z").to_a[rand(26)] }.join,
(0...10).map { ("a".."z").to_a[rand(26)] }.join,
(0...10).map { ("a".."z").to_a[rand(26)] }.join + ".txt"].join "/"
end
def done_rewrite gapi
Google::Apis::StorageV1::RewriteResponse.new done: true, resource: gapi
end
def undone_rewrite token
Google::Apis::StorageV1::RewriteResponse.new done: false, rewrite_token: token
end
def random_notification_gapi id: "1", topic: "//pubsub.googleapis.com/projects/test/topics/my-topic", payload: "NONE"
gapi = Google::Apis::StorageV1::Notification.new(
custom_attributes: { "foo" => "bar" },
event_types: ["OBJECT_FINALIZE"],
object_name_prefix: "my-prefix",
payload_format: payload,
topic: topic
)
gapi.id = id if id
gapi
end
# Stub for the `http_res` that is returned in the Apis::Core::DownloadCommand monkey-patch in Service.
def download_http_resp gzip: nil
headers = {}
headers["Content-Encoding"] = ["gzip"] if gzip
OpenStruct.new(header: headers)
end
def encryption_gapi key_name
Google::Apis::StorageV1::Bucket::Encryption.new default_kms_key_name: key_name
end
def lifecycle_gapi *rules
Google::Apis::StorageV1::Bucket::Lifecycle.new rule: Array(rules)
end
def lifecycle_rule_gapi action,
storage_class: nil,
age: nil,
created_before: nil,
custom_time_before: nil,
days_since_custom_time: nil,
days_since_noncurrent_time: nil,
is_live: nil,
matches_storage_class: nil,
noncurrent_time_before: nil,
num_newer_versions: nil,
matches_prefix: nil,
matches_suffix: nil
Google::Apis::StorageV1::Bucket::Lifecycle::Rule.new(
action: Google::Apis::StorageV1::Bucket::Lifecycle::Rule::Action.new(
storage_class: storage_class,
type: action
),
condition: Google::Apis::StorageV1::Bucket::Lifecycle::Rule::Condition.new(
age: age,
created_before: created_before,
custom_time_before: custom_time_before,
days_since_custom_time: days_since_custom_time,
days_since_noncurrent_time: days_since_noncurrent_time,
is_live: is_live,
matches_storage_class: Array(matches_storage_class),
noncurrent_time_before: noncurrent_time_before,
num_newer_versions: num_newer_versions,
matches_prefix: Array(matches_prefix),
matches_suffix: Array(matches_suffix)
)
)
end
def iam_configuration_gapi uniform_bucket_level_access: nil, locked_time: nil, public_access_prevention: nil
raise "uniform_bucket_level_access must be provided with locked_time" if !locked_time.nil? && uniform_bucket_level_access.nil?
gapi = Google::Apis::StorageV1::Bucket::IamConfiguration.new
if uniform_bucket_level_access
ubla = Google::Apis::StorageV1::Bucket::IamConfiguration::UniformBucketLevelAccess.new(
enabled: uniform_bucket_level_access
)
ubla.locked_time = (Date.today + 1).to_datetime if locked_time
gapi.uniform_bucket_level_access = ubla
end
gapi.public_access_prevention = public_access_prevention if public_access_prevention
gapi
end
def policy_gapi etag: "CAE=", version: 1, bindings: []
Google::Apis::StorageV1::Policy.new etag: etag, version: version, bindings: bindings
end
def get_bucket_args if_metageneration_match: nil,
if_metageneration_not_match: nil,
user_project: nil,
generation: nil,
soft_deleted: nil,
options: {}
{
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
user_project: user_project,
generation: generation,
soft_deleted: soft_deleted,
options: options
}
end
def update_bucket_args if_metageneration_match: nil,
if_metageneration_not_match: nil,
predefined_acl: nil,
predefined_default_object_acl: nil,
user_project: nil,
options: {}
{
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
predefined_acl: predefined_acl,
predefined_default_object_acl: predefined_default_object_acl,
user_project: user_project,
options: options
}
end
def patch_bucket_args if_metageneration_match: nil,
if_metageneration_not_match: nil,
predefined_acl: nil,
predefined_default_object_acl: nil,
user_project: nil,
options: {}
{
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
predefined_acl: predefined_acl,
predefined_default_object_acl: predefined_default_object_acl,
user_project: user_project,
options: options
}
end
def delete_bucket_args if_metageneration_match: nil,
if_metageneration_not_match: nil,
user_project: nil,
options: {}
{
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
user_project: user_project,
options: options
}
end
def insert_object_args name: nil,
predefined_acl: nil,
upload_source: nil,
content_encoding: nil,
content_type: "text/plain",
kms_key_name: nil,
if_generation_match: nil,
if_generation_not_match: nil,
if_metageneration_match: nil,
if_metageneration_not_match: nil,
user_project: nil,
options: {}
{
name: name,
predefined_acl: predefined_acl,
upload_source: upload_source,
content_encoding: content_encoding,
content_type: content_type,
kms_key_name: kms_key_name,
if_generation_match: if_generation_match,
if_generation_not_match: if_generation_not_match,
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
user_project: user_project,
options: options
}
end
def resumable_upload_args options: {}
{
options: options
}
end
def get_object_args generation: nil,
if_generation_match: nil,
if_generation_not_match: nil,
if_metageneration_match: nil,
if_metageneration_not_match: nil,
user_project: nil,
soft_deleted: nil,
options: {}
{
generation: generation,
if_generation_match: if_generation_match,
if_generation_not_match: if_generation_not_match,
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
user_project: user_project,
soft_deleted: soft_deleted,
options: options
}
end
def list_objects_args delimiter: nil,
max_results: nil,
page_token: nil,
prefix: nil,
versions: nil,
user_project: nil,
match_glob: nil,
include_folders_as_prefixes: nil,
soft_deleted: nil,
options: {}
{
delimiter: delimiter,
max_results: max_results,
page_token: page_token,
prefix: prefix,
versions: versions,
user_project: user_project,
match_glob: match_glob,
include_folders_as_prefixes: include_folders_as_prefixes,
soft_deleted: soft_deleted,
options: options
}
end
def patch_object_args generation: nil,
if_generation_match: nil,
if_generation_not_match: nil,
if_metageneration_match: nil,
if_metageneration_not_match: nil,
predefined_acl: nil,
user_project: nil,
override_unlocked_retention: nil,
options: {}
opts = {
generation: generation,
if_generation_match: if_generation_match,
if_generation_not_match: if_generation_not_match,
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
predefined_acl: predefined_acl,
user_project: user_project,
override_unlocked_retention: override_unlocked_retention,
options: options
}
end
def move_object_args if_generation_match: nil,
if_generation_not_match: nil,
if_metageneration_match: nil,
if_metageneration_not_match: nil,
if_source_generation_match: nil,
if_source_generation_not_match: nil,
if_source_metageneration_match: nil,
if_source_metageneration_not_match: nil,
user_project: nil,
fields: nil,
quota_user: nil,
user_ip: nil,
options: {}
{
if_generation_match: if_generation_match,
if_generation_not_match: if_generation_not_match,
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
if_source_generation_match: if_source_generation_match,
if_source_generation_not_match: if_source_generation_not_match,
if_source_metageneration_match: if_source_metageneration_match,
if_source_metageneration_not_match: if_source_metageneration_not_match,
user_project: user_project,
fields: fields,
quota_user: quota_user,
user_ip: user_ip,
options: options
}
end
def delete_object_args generation: nil,
if_generation_match: nil,
if_generation_not_match: nil,
if_metageneration_match: nil,
if_metageneration_not_match: nil,
user_project: nil,
options: {}
{
generation: generation,
if_generation_match: if_generation_match,
if_generation_not_match: if_generation_not_match,
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
user_project: user_project,
options: options
}
end
def rewrite_object_args destination_kms_key_name: nil,
destination_predefined_acl: nil,
if_generation_match: nil,
if_generation_not_match: nil,
if_metageneration_match: nil,
if_metageneration_not_match: nil,
if_source_generation_match: nil,
if_source_generation_not_match: nil,
if_source_metageneration_match: nil,
if_source_metageneration_not_match: nil,
source_generation: nil,
rewrite_token: nil,
user_project: nil,
options: {}
{
destination_kms_key_name: destination_kms_key_name,
destination_predefined_acl: destination_predefined_acl,
if_generation_match: if_generation_match,
if_generation_not_match: if_generation_not_match,
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
if_source_generation_match: if_source_generation_match,
if_source_generation_not_match: if_source_generation_not_match,
if_source_metageneration_match: if_source_metageneration_match,
if_source_metageneration_not_match: if_source_metageneration_not_match,
source_generation: source_generation,
rewrite_token: rewrite_token,
user_project: user_project,
options: options
}
end
def compose_object_args destination_predefined_acl: nil,
if_generation_match: nil,
if_metageneration_match: nil,
user_project: nil,
options: {}
{
destination_predefined_acl: destination_predefined_acl,
if_generation_match: if_generation_match,
if_metageneration_match: if_metageneration_match,
user_project: user_project,
options: options
}
end
def restore_object_args copy_source_acl: nil,
if_generation_match: nil,
if_generation_not_match: nil,
if_metageneration_match: nil,
if_metageneration_not_match: nil,
projection: nil,
user_project: nil,
fields: nil,
options: {}
{
copy_source_acl: copy_source_acl,
if_generation_match: if_generation_match,
if_generation_not_match: if_generation_not_match,
if_metageneration_match: if_metageneration_match,
if_metageneration_not_match: if_metageneration_not_match,
projection: projection,
user_project: user_project,
fields: fields,
options: options
}
end
def compose_request source_files, destination_gapi = nil, if_source_generation_match: nil
source_objects = source_files.map do |file|
if file.is_a? String
Google::Apis::StorageV1::ComposeRequest::SourceObject.new \
name: file
else
Google::Apis::StorageV1::ComposeRequest::SourceObject.new \
name: file.name,
generation: file.generation
end
end
if if_source_generation_match
if source_files.count != if_source_generation_match.count
raise ArgumentError, "if provided, if_source_generation_match length must match sources length"
end
if_source_generation_match.each_with_index do |generation, i|
next unless generation
object_preconditions = Google::Apis::StorageV1::ComposeRequest::SourceObject::ObjectPreconditions.new(
if_generation_match: generation
)
source_objects[i].object_preconditions = object_preconditions
end
end
Google::Apis::StorageV1::ComposeRequest.new(
destination: destination_gapi,
source_objects: source_objects
)
end
def list_files_gapi count = 2, token = nil, prefixes = nil
files = count.times.map { Google::Apis::StorageV1::Object.from_json random_file_hash.to_json }
Google::Apis::StorageV1::Objects.new kind: "storage#objects", items: files, next_page_token: token, prefixes: prefixes
end
def restore_file_gapi bucket, file_name, generation=nil
file_hash = random_file_hash(bucket, file_name, generation).to_json
Google::Apis::StorageV1::Object.from_json file_hash
end
end