gitlab-fog-azure-rm 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +7 -0
  2. data/.arclint +8 -0
  3. data/.codeclimate.yml +17 -0
  4. data/.gitignore +6 -0
  5. data/.gitlab-ci.yml +17 -0
  6. data/.hound.yml +2 -0
  7. data/.rubocop.yml +5 -0
  8. data/.rubocop_todo.yml +274 -0
  9. data/.travis.yml +49 -0
  10. data/CHANGELOG.md +414 -0
  11. data/CONTRIBUTING.md +40 -0
  12. data/CONTRIBUTORS.md +12 -0
  13. data/Gemfile +3 -0
  14. data/LICENSE.md +20 -0
  15. data/README.md +100 -0
  16. data/gitlab-fog-azure-rm.gemspec +31 -0
  17. data/lib/fog/azurerm.rb +30 -0
  18. data/lib/fog/azurerm/async_response.rb +44 -0
  19. data/lib/fog/azurerm/config.rb +1 -0
  20. data/lib/fog/azurerm/constants.rb +72 -0
  21. data/lib/fog/azurerm/credentials.rb +40 -0
  22. data/lib/fog/azurerm/custom_fog_errors.rb +52 -0
  23. data/lib/fog/azurerm/docs/storage.md +398 -0
  24. data/lib/fog/azurerm/docs/structure.md +53 -0
  25. data/lib/fog/azurerm/models/resources/azure_resource.rb +25 -0
  26. data/lib/fog/azurerm/models/resources/azure_resources.rb +40 -0
  27. data/lib/fog/azurerm/models/resources/dependency.rb +27 -0
  28. data/lib/fog/azurerm/models/resources/deployment.rb +65 -0
  29. data/lib/fog/azurerm/models/resources/deployments.rb +30 -0
  30. data/lib/fog/azurerm/models/resources/provider.rb +27 -0
  31. data/lib/fog/azurerm/models/resources/provider_resource_type.rb +22 -0
  32. data/lib/fog/azurerm/models/resources/resource_group.rb +34 -0
  33. data/lib/fog/azurerm/models/resources/resource_groups.rb +29 -0
  34. data/lib/fog/azurerm/models/storage/directories.rb +75 -0
  35. data/lib/fog/azurerm/models/storage/directory.rb +151 -0
  36. data/lib/fog/azurerm/models/storage/file.rb +265 -0
  37. data/lib/fog/azurerm/models/storage/files.rb +183 -0
  38. data/lib/fog/azurerm/models/storage/kind.rb +12 -0
  39. data/lib/fog/azurerm/models/storage/sku_name.rb +15 -0
  40. data/lib/fog/azurerm/models/storage/sku_tier.rb +12 -0
  41. data/lib/fog/azurerm/models/storage/storage_account.rb +92 -0
  42. data/lib/fog/azurerm/models/storage/storage_accounts.rb +47 -0
  43. data/lib/fog/azurerm/requests/storage/acquire_blob_lease.rb +32 -0
  44. data/lib/fog/azurerm/requests/storage/acquire_container_lease.rb +32 -0
  45. data/lib/fog/azurerm/requests/storage/check_container_exists.rb +30 -0
  46. data/lib/fog/azurerm/requests/storage/commit_blob_blocks.rb +30 -0
  47. data/lib/fog/azurerm/requests/storage/compare_container_blobs.rb +148 -0
  48. data/lib/fog/azurerm/requests/storage/copy_blob.rb +30 -0
  49. data/lib/fog/azurerm/requests/storage/copy_blob_from_uri.rb +30 -0
  50. data/lib/fog/azurerm/requests/storage/copy_object.rb +20 -0
  51. data/lib/fog/azurerm/requests/storage/create_block_blob.rb +108 -0
  52. data/lib/fog/azurerm/requests/storage/create_container.rb +40 -0
  53. data/lib/fog/azurerm/requests/storage/create_disk.rb +62 -0
  54. data/lib/fog/azurerm/requests/storage/create_page_blob.rb +31 -0
  55. data/lib/fog/azurerm/requests/storage/delete_blob.rb +32 -0
  56. data/lib/fog/azurerm/requests/storage/delete_blob_https_url.rb +39 -0
  57. data/lib/fog/azurerm/requests/storage/delete_container.rb +31 -0
  58. data/lib/fog/azurerm/requests/storage/delete_disk.rb +36 -0
  59. data/lib/fog/azurerm/requests/storage/delete_object_url.rb +30 -0
  60. data/lib/fog/azurerm/requests/storage/get_blob.rb +163 -0
  61. data/lib/fog/azurerm/requests/storage/get_blob_http_url.rb +39 -0
  62. data/lib/fog/azurerm/requests/storage/get_blob_https_url.rb +39 -0
  63. data/lib/fog/azurerm/requests/storage/get_blob_properties.rb +55 -0
  64. data/lib/fog/azurerm/requests/storage/get_blob_url.rb +28 -0
  65. data/lib/fog/azurerm/requests/storage/get_container_acl.rb +30 -0
  66. data/lib/fog/azurerm/requests/storage/get_container_properties.rb +41 -0
  67. data/lib/fog/azurerm/requests/storage/get_container_url.rb +34 -0
  68. data/lib/fog/azurerm/requests/storage/get_object_url.rb +21 -0
  69. data/lib/fog/azurerm/requests/storage/list_blobs.rb +168 -0
  70. data/lib/fog/azurerm/requests/storage/list_containers.rb +73 -0
  71. data/lib/fog/azurerm/requests/storage/multipart_save_block_blob.rb +110 -0
  72. data/lib/fog/azurerm/requests/storage/put_blob_block.rb +30 -0
  73. data/lib/fog/azurerm/requests/storage/put_blob_https_url.rb +39 -0
  74. data/lib/fog/azurerm/requests/storage/put_blob_metadata.rb +31 -0
  75. data/lib/fog/azurerm/requests/storage/put_blob_pages.rb +30 -0
  76. data/lib/fog/azurerm/requests/storage/put_blob_properties.rb +31 -0
  77. data/lib/fog/azurerm/requests/storage/put_container_acl.rb +31 -0
  78. data/lib/fog/azurerm/requests/storage/put_container_metadata.rb +31 -0
  79. data/lib/fog/azurerm/requests/storage/put_object_url.rb +30 -0
  80. data/lib/fog/azurerm/requests/storage/release_blob_lease.rb +30 -0
  81. data/lib/fog/azurerm/requests/storage/release_container_lease.rb +30 -0
  82. data/lib/fog/azurerm/requests/storage/save_page_blob.rb +111 -0
  83. data/lib/fog/azurerm/requests/storage/wait_blob_copy_operation_to_finish.rb +56 -0
  84. data/lib/fog/azurerm/storage.rb +126 -0
  85. data/lib/fog/azurerm/utilities/general.rb +186 -0
  86. data/lib/fog/azurerm/utilities/logger_messages.yml +19 -0
  87. data/lib/fog/azurerm/version.rb +5 -0
  88. data/rake-script.sh +10 -0
  89. data/rakefile +29 -0
  90. data/test/api_stub.rb +147 -0
  91. data/test/api_stub/models/storage/directory.rb +178 -0
  92. data/test/api_stub/models/storage/file.rb +181 -0
  93. data/test/api_stub/requests/storage/directory.rb +89 -0
  94. data/test/api_stub/requests/storage/file.rb +252 -0
  95. data/test/integration/README.md +14 -0
  96. data/test/integration/blob.rb +297 -0
  97. data/test/integration/container.rb +160 -0
  98. data/test/integration/credentials/azure.yml +5 -0
  99. data/test/integration/storage_account.rb +135 -0
  100. data/test/models/storage/test_directories.rb +74 -0
  101. data/test/models/storage/test_directory.rb +178 -0
  102. data/test/models/storage/test_file.rb +294 -0
  103. data/test/models/storage/test_files.rb +234 -0
  104. data/test/requests/storage/test_acquire_blob_lease.rb +36 -0
  105. data/test/requests/storage/test_acquire_container_lease.rb +36 -0
  106. data/test/requests/storage/test_check_container_exists.rb +18 -0
  107. data/test/requests/storage/test_commit_blob_blocks.rb +34 -0
  108. data/test/requests/storage/test_compare_container_blobs.rb +36 -0
  109. data/test/requests/storage/test_copy_blob.rb +39 -0
  110. data/test/requests/storage/test_copy_blob_from_uri.rb +36 -0
  111. data/test/requests/storage/test_create_block_blob.rb +80 -0
  112. data/test/requests/storage/test_create_container.rb +36 -0
  113. data/test/requests/storage/test_create_disk.rb +70 -0
  114. data/test/requests/storage/test_create_page_blob.rb +34 -0
  115. data/test/requests/storage/test_delete_blob.rb +42 -0
  116. data/test/requests/storage/test_delete_blob_https_url.rb +32 -0
  117. data/test/requests/storage/test_delete_container.rb +42 -0
  118. data/test/requests/storage/test_delete_disk.rb +30 -0
  119. data/test/requests/storage/test_get_blob.rb +141 -0
  120. data/test/requests/storage/test_get_blob_http_url.rb +30 -0
  121. data/test/requests/storage/test_get_blob_https_url.rb +32 -0
  122. data/test/requests/storage/test_get_blob_properties.rb +47 -0
  123. data/test/requests/storage/test_get_blob_url.rb +75 -0
  124. data/test/requests/storage/test_get_container_acl.rb +37 -0
  125. data/test/requests/storage/test_get_container_properties.rb +45 -0
  126. data/test/requests/storage/test_get_container_url.rb +33 -0
  127. data/test/requests/storage/test_list_blobs.rb +77 -0
  128. data/test/requests/storage/test_list_containers.rb +54 -0
  129. data/test/requests/storage/test_multipart_save_block_blob.rb +105 -0
  130. data/test/requests/storage/test_put_blob_block.rb +34 -0
  131. data/test/requests/storage/test_put_blob_https_url.rb +32 -0
  132. data/test/requests/storage/test_put_blob_metadata.rb +36 -0
  133. data/test/requests/storage/test_put_blob_pages.rb +34 -0
  134. data/test/requests/storage/test_put_blob_properties.rb +39 -0
  135. data/test/requests/storage/test_put_container_acl.rb +34 -0
  136. data/test/requests/storage/test_put_container_metadata.rb +36 -0
  137. data/test/requests/storage/test_release_blob_lease.rb +34 -0
  138. data/test/requests/storage/test_release_container_lease.rb +34 -0
  139. data/test/requests/storage/test_save_page_blob.rb +115 -0
  140. data/test/requests/storage/test_wait_blob_copy_operation_to_finish.rb +148 -0
  141. data/test/smoke_tests/compute/test_resource_group_smoke.rb +37 -0
  142. data/test/test_credentials.rb +63 -0
  143. data/test/test_helper.rb +846 -0
  144. metadata +352 -0
@@ -0,0 +1,5 @@
1
+ client_id: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
2
+ client_secret: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
3
+ subscription_id: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
4
+ tenant_id: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
5
+ environment: XXXXXXXX
@@ -0,0 +1,135 @@
1
+ require 'fog/azurerm'
2
+ require 'yaml'
3
+
4
+ ########################################################################################################################
5
+ ###################### Services object required by all actions ######################
6
+ ###################### Keep it Uncommented! ######################
7
+ ########################################################################################################################
8
+
9
+ azure_credentials = YAML.load_file(File.expand_path('credentials/azure.yml', __dir__))
10
+
11
+ rs = Fog::Resources::AzureRM.new(
12
+ tenant_id: azure_credentials['tenant_id'],
13
+ client_id: azure_credentials['client_id'],
14
+ client_secret: azure_credentials['client_secret'],
15
+ subscription_id: azure_credentials['subscription_id']
16
+ )
17
+
18
+ storage = Fog::Storage::AzureRM.new(
19
+ tenant_id: azure_credentials['tenant_id'],
20
+ client_id: azure_credentials['client_id'],
21
+ client_secret: azure_credentials['client_secret'],
22
+ subscription_id: azure_credentials['subscription_id'],
23
+ environment: azure_credentials['environment']
24
+ )
25
+
26
+ ########################################################################################################################
27
+ ###################### Resource names #####################
28
+ ########################################################################################################################
29
+
30
+ time = current_time
31
+ resource_group_name = "TestRG-SA-#{time}"
32
+ storage_account_name = "teststorage#{time}"
33
+ lrs_storage_account = "lrs#{time}"
34
+ grs_storage_account = "grs#{time}"
35
+ premium_storage_acc = "premsa#{time}"
36
+
37
+ ########################################################################################################################
38
+ ###################### Prerequisites ######################
39
+ ########################################################################################################################
40
+
41
+ begin
42
+ resource_group = rs.resource_groups.create(
43
+ name: resource_group_name,
44
+ location: LOCATION
45
+ )
46
+
47
+ ########################################################################################################################
48
+ ###################### Check Storage Account name Availability ######################
49
+ ########################################################################################################################
50
+
51
+ storage_account_avail = storage.storage_accounts.check_name_availability(storage_account_name)
52
+ puts storage_account_avail ? 'Storage Account name available' : 'Storage Account name unavailable'
53
+
54
+ ########################################################################################################################
55
+ ###################### Check Subnet Exists? ######################
56
+ ########################################################################################################################
57
+
58
+ flag = storage.storage_accounts.check_storage_account_exists(resource_group_name, lrs_storage_account)
59
+ puts "Storage Account doesn't exist." unless flag
60
+
61
+ ########################################################################################################################
62
+ ############### Create A Standard Storage Account of Replication: LRS (Locally-redundant storage) ###############
63
+ ########################################################################################################################
64
+
65
+ tags = { key1: 'value1', key2: 'value2' }
66
+
67
+ storage_account = storage.storage_accounts.create(
68
+ name: lrs_storage_account,
69
+ location: LOCATION,
70
+ resource_group: resource_group_name,
71
+ tags: tags
72
+ )
73
+ puts "Created storage account for standard lrs replication: #{storage_account.name}"
74
+
75
+ ########################################################################################################################
76
+ ############### Create A Standard Storage Account of Replication: GRS (Geo-redundant storage) #################
77
+ ########################################################################################################################
78
+
79
+ storage_account = storage.storage_accounts.create(
80
+ name: grs_storage_account,
81
+ location: LOCATION,
82
+ resource_group: resource_group_name,
83
+ sku_name: Fog::ARM::Storage::Models::SkuTier::Standard,
84
+ replication: 'GRS',
85
+ encryption: true,
86
+ tags: tags
87
+ )
88
+ puts "Created storage account for standard grs replication: #{storage_account.name}"
89
+
90
+ ########################################################################################################################
91
+ ########### Create A Premium(SSD) Storage Account of its only Replication: LRS (Locally-redundant storage) ##########
92
+ ########################################################################################################################
93
+
94
+ storage_account = storage.storage_accounts.create(
95
+ name: premium_storage_acc,
96
+ location: LOCATION,
97
+ resource_group: resource_group_name,
98
+ sku_name: Fog::ARM::Storage::Models::SkuTier::Premium,
99
+ replication: 'LRS',
100
+ tags: tags
101
+ )
102
+ puts "Created storage account for premium lrs replication: #{storage_account.name}"
103
+
104
+ ########################################################################################################################
105
+ ###################### Get and Update Storage Account ######################
106
+ ########################################################################################################################
107
+
108
+ premium_storage_account = storage.storage_accounts.get(resource_group_name, premium_storage_acc)
109
+ puts "Get storage account: #{premium_storage_account.name}"
110
+ premium_storage_account.update(encryption: true)
111
+ puts 'Updated encryption of storage account'
112
+
113
+ ########################################################################################################################
114
+ ###################### Get and Delete Storage Account ######################
115
+ ########################################################################################################################
116
+
117
+ standard_storage_account = storage.storage_accounts.get(resource_group_name, lrs_storage_account)
118
+ puts "Deleted storage account for standard lrs replication: #{standard_storage_account.destroy}"
119
+ standard_storage_account = storage.storage_accounts.get(resource_group_name, grs_storage_account)
120
+ puts "Deleted storage account for standard grs replication: #{standard_storage_account.destroy}"
121
+ premium_storage_account = storage.storage_accounts.get(resource_group_name, premium_storage_acc)
122
+ puts "Deleted storage account for premium lrs replication: #{premium_storage_account.destroy}"
123
+
124
+ ########################################################################################################################
125
+ ###################### CleanUp ######################
126
+ ########################################################################################################################
127
+
128
+ resource_group = rs.resource_groups.get(resource_group_name)
129
+ resource_group.destroy
130
+
131
+ puts 'Integration test for storage account ran successfully!'
132
+ rescue
133
+ puts 'Integration Test for storage account is failing'
134
+ resource_group.destroy unless resource_group.nil?
135
+ end
@@ -0,0 +1,74 @@
1
+ require File.expand_path '../../test_helper', __dir__
2
+
3
+ # Test class for Container Collection
4
+ class TestDirectories < Minitest::Test
5
+ def setup
6
+ @service = Fog::Storage::AzureRM.new(storage_account_credentials)
7
+ @directories = Fog::Storage::AzureRM::Directories.new(service: @service)
8
+
9
+ @container_list = ApiStub::Models::Storage::Directory.container_list
10
+ @container = ApiStub::Models::Storage::Directory.container
11
+ @blob_list = ApiStub::Models::Storage::Directory.blob_list
12
+ end
13
+
14
+ def test_collection_methods
15
+ methods = [
16
+ :all,
17
+ :get,
18
+ :check_container_exists
19
+ ]
20
+ methods.each do |method|
21
+ assert_respond_to @directories, method
22
+ end
23
+ end
24
+
25
+ def test_all_method_success
26
+ @service.stub :list_containers, @container_list do
27
+ dirs = @directories.all
28
+ assert_instance_of Fog::Storage::AzureRM::Directories, dirs
29
+ assert_equal @container_list.size, dirs.size
30
+ dirs.each do |directory|
31
+ assert_instance_of Fog::Storage::AzureRM::Directory, directory
32
+ assert_equal 'unknown', directory.attributes[:acl]
33
+ end
34
+ end
35
+ end
36
+
37
+ def test_get_method_success
38
+ @service.stub :get_container_properties, @container do
39
+ @service.stub :list_blobs, @blob_list do
40
+ directory = @directories.get('test_container')
41
+ assert_instance_of Fog::Storage::AzureRM::Directory, directory
42
+ assert_equal 'unknown', directory.attributes[:acl]
43
+ end
44
+ end
45
+ end
46
+
47
+ def test_get_method_container_not_found
48
+ exception = ->(*) { raise 'NotFound' }
49
+ @service.stub :get_container_properties, exception do
50
+ assert @directories.get('test_container').nil?
51
+ end
52
+ end
53
+
54
+ def test_get_method_exception
55
+ exception = ->(*) { raise 'Error' }
56
+ @service.stub :get_container_properties, exception do
57
+ assert_raises(RuntimeError) do
58
+ @directories.get('test_container')
59
+ end
60
+ end
61
+ end
62
+
63
+ def test_check_container_exists_true_response
64
+ @service.stub :check_container_exists, true do
65
+ assert @directories.check_container_exists('test_container')
66
+ end
67
+ end
68
+
69
+ def test_check_container_exists_false_response
70
+ @service.stub :check_container_exists, false do
71
+ assert !@directories.check_container_exists('test_container')
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,178 @@
1
+ require File.expand_path '../../test_helper', __dir__
2
+
3
+ # Test class for Storage Container Model
4
+ class TestDirectory < Minitest::Test
5
+ def setup
6
+ @service = Fog::Storage::AzureRM.new(storage_account_credentials)
7
+ @directory = directory(@service)
8
+
9
+ @container = ApiStub::Models::Storage::Directory.container
10
+ @container_acl = ApiStub::Models::Storage::Directory.container_acl
11
+ @container_https_url = ApiStub::Models::Storage::Directory.container_https_url
12
+ end
13
+
14
+ def test_model_methods
15
+ methods = [
16
+ :acl=,
17
+ :acl,
18
+ :destroy,
19
+ :files,
20
+ :public=,
21
+ :public_url,
22
+ :save,
23
+ :persisted?
24
+ ]
25
+ methods.each do |method|
26
+ assert_respond_to @directory, method
27
+ end
28
+ end
29
+
30
+ def test_model_attributes
31
+ attributes = [
32
+ :key,
33
+ :etag,
34
+ :last_modified,
35
+ :lease_duration,
36
+ :lease_state,
37
+ :lease_status,
38
+ :metadata
39
+ ]
40
+ @service.stub :create_container, @container do
41
+ attributes.each do |attribute|
42
+ assert_respond_to @directory, attribute
43
+ end
44
+ end
45
+ end
46
+
47
+ def test_set_acl_method_success
48
+ result = @directory.acl = 'container'
49
+ assert_equal 'container', result
50
+ assert_equal 'container', @directory.attributes[:acl]
51
+ end
52
+
53
+ def test_set_acl_method_exception
54
+ assert_raises(ArgumentError) do
55
+ @directory.acl = 'invalid-acl'
56
+ end
57
+ end
58
+
59
+ def test_get_acl_method_success
60
+ # Return 'unknonw' when directory is not persist and acl is 'unknown'
61
+ @directory.acl = 'unknown'
62
+ @directory.stub :persisted?, false do
63
+ assert_equal 'unknown', @directory.acl
64
+ end
65
+
66
+ # Return actual value directly when directory is not persist and acl is not 'unknown'
67
+ @directory.acl = 'blob'
68
+ @directory.stub :persisted?, false do
69
+ assert_equal 'blob', @directory.acl
70
+ end
71
+
72
+ # Query actual value and return it when directory is persist and acl is 'unknown'
73
+ @directory.acl = 'unknown'
74
+ @directory.stub :persisted?, true do
75
+ @service.stub :get_container_acl, @container_acl do
76
+ assert_equal 'container', @directory.acl
77
+ assert_equal 'container', @directory.attributes[:acl]
78
+ end
79
+ end
80
+ end
81
+
82
+ def test_get_acl_method_without_key_exception
83
+ @directory.attributes.delete(:key)
84
+ assert_raises(ArgumentError) do
85
+ @directory.acl
86
+ end
87
+ end
88
+
89
+ def test_destroy_method_success
90
+ @service.stub :delete_container, true do
91
+ assert @directory.destroy
92
+ end
93
+ end
94
+
95
+ def test_destroy_method_without_key_exception
96
+ @directory.attributes.delete(:key)
97
+ assert_raises(ArgumentError) do
98
+ @directory.destroy
99
+ end
100
+ end
101
+
102
+ def test_files_method_success
103
+ assert_instance_of Fog::Storage::AzureRM::Files, @directory.files
104
+ end
105
+
106
+ def test_set_public_method_success
107
+ # Set public
108
+ result = @directory.public = true
109
+ assert result
110
+ assert_equal 'container', @directory.attributes[:acl]
111
+
112
+ # Set private
113
+ result = @directory.public = false
114
+ assert !result
115
+ assert_equal nil, @directory.attributes[:acl]
116
+ end
117
+
118
+ def test_public_url_method_with_public_success
119
+ @directory.stub :acl, 'container' do
120
+ @service.stub :get_container_url, @container_https_url do
121
+ assert_equal @container_https_url, @directory.public_url
122
+ end
123
+ end
124
+ end
125
+
126
+ def test_public_url_method_with_private_success
127
+ @directory.stub :acl, nil do
128
+ assert @directory.public_url.nil?
129
+ end
130
+ end
131
+
132
+ def test_public_url_method_without_key_exception
133
+ @directory.attributes.delete(:key)
134
+ assert_raises(ArgumentError) do
135
+ @directory.public_url
136
+ end
137
+ end
138
+
139
+ def test_save_method_create_success
140
+ @service.stub :create_container, @container do
141
+ assert @directory.save
142
+ assert @directory.attributes[:is_persisted]
143
+ end
144
+ end
145
+
146
+ def test_save_method_update_success
147
+ options = { is_create: false }
148
+ @service.stub :put_container_acl, true do
149
+ @service.stub :put_container_metadata, true do
150
+ @service.stub :get_container_properties, @container do
151
+ assert @directory.save(options)
152
+ assert @directory.attributes[:is_persisted]
153
+ end
154
+ end
155
+ end
156
+ end
157
+
158
+ def test_save_method_without_key_exception
159
+ @directory.attributes.delete(:key)
160
+ assert_raises(ArgumentError) do
161
+ @directory.save
162
+ end
163
+ end
164
+
165
+ def test_persisted_method_success
166
+ @directory.attributes[:is_persisted] = true
167
+ @directory.attributes[:last_modified] = nil
168
+ assert @directory.persisted?
169
+
170
+ @directory.attributes[:is_persisted] = false
171
+ @directory.attributes[:last_modified] = 'datetime'
172
+ assert @directory.persisted?
173
+
174
+ @directory.attributes[:is_persisted] = false
175
+ @directory.attributes[:last_modified] = nil
176
+ assert !@directory.persisted?
177
+ end
178
+ end
@@ -0,0 +1,294 @@
1
+ require File.expand_path '../../test_helper', __dir__
2
+
3
+ # Test class for Storage Container Model
4
+ class TestFile < Minitest::Test
5
+ def setup
6
+ @service = Fog::Storage::AzureRM.new(storage_account_credentials)
7
+ @directory = directory(@service)
8
+ @file = file(@service)
9
+ @raw_cloud_blob = storage_blob
10
+
11
+ @mocked_response = mocked_storage_http_error
12
+ @blob = ApiStub::Models::Storage::File.blob
13
+ @metadata = ApiStub::Models::Storage::File.blob_metadata
14
+ @blob_list = ApiStub::Models::Storage::File.blob_list
15
+ @blob_https_url = ApiStub::Models::Storage::File.blob_https_url
16
+ end
17
+
18
+ def test_model_methods
19
+ methods = [
20
+ :save,
21
+ :body,
22
+ :body=,
23
+ :copy,
24
+ :copy_from_uri,
25
+ :destroy,
26
+ :public?,
27
+ :public_url,
28
+ :url
29
+ ]
30
+ methods.each do |method|
31
+ assert_respond_to @file, method
32
+ end
33
+ end
34
+
35
+ def test_model_attributes
36
+ attributes = [
37
+ :key,
38
+ :body,
39
+ :directory,
40
+ :accept_ranges,
41
+ :content_length,
42
+ :content_type,
43
+ :content_md5,
44
+ :content_encoding,
45
+ :content_language,
46
+ :cache_control,
47
+ :content_disposition,
48
+ :copy_completion_time,
49
+ :copy_status,
50
+ :copy_status_description,
51
+ :copy_id,
52
+ :copy_progress,
53
+ :copy_source,
54
+ :etag,
55
+ :last_modified,
56
+ :lease_duration,
57
+ :lease_state,
58
+ :lease_status,
59
+ :sequence_number,
60
+ :blob_type,
61
+ :metadata
62
+ ]
63
+ attributes.each do |attribute|
64
+ assert_respond_to @file, attribute
65
+ end
66
+ end
67
+
68
+ def test_save_method_with_small_block_blob_success
69
+ @file.body = 'd' * 1025 # SINGLE_BLOB_PUT_THRESHOLD is 32 * 1024 * 1024
70
+
71
+ @service.stub :create_block_blob, @raw_cloud_blob do
72
+ assert @file.save
73
+ end
74
+ end
75
+
76
+ def test_save_method_with_large_block_blob_success
77
+ @file.body = 'd' * (32 * 1024 * 1024 + 1) # SINGLE_BLOB_PUT_THRESHOLD is 32 * 1024 * 1024
78
+
79
+ @service.stub :multipart_save_block_blob, true do
80
+ @service.stub :get_blob_properties, @raw_cloud_blob do
81
+ assert @file.save
82
+ end
83
+ end
84
+ end
85
+
86
+ def test_save_method_with_page_blob_success
87
+ options = { blob_type: 'PageBlob' }
88
+ @file.metadata = @metadata
89
+ @file.body = 'd' * 5 * 1024 * 1024 # MAXIMUM_CHUNK_SIZE is 4 * 1024 * 1024
90
+
91
+ @service.stub :save_page_blob, true do
92
+ @service.stub :get_blob_properties, @raw_cloud_blob do
93
+ assert @file.save(options)
94
+ end
95
+ end
96
+ end
97
+
98
+ def test_save_method_with_not_update_body_success
99
+ options = { update_body: false }
100
+ @file.metadata = @metadata
101
+
102
+ @service.stub :put_blob_metadata, true do
103
+ @service.stub :put_blob_properties, true do
104
+ @service.stub :get_blob_properties, @raw_cloud_blob do
105
+ assert @file.save(options)
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ def test_save_method_without_directory_exception
112
+ assert_raises(ArgumentError) do
113
+ @file.attributes.delete(:directory)
114
+ @file.save
115
+ end
116
+ end
117
+
118
+ def test_save_method_without_key_exception
119
+ assert_raises(ArgumentError) do
120
+ @file.attributes.delete(:key)
121
+ @file.save
122
+ end
123
+ end
124
+
125
+ def test_save_method_create_without_body_exception
126
+ assert_raises(ArgumentError) do
127
+ @file.attributes.delete(:body)
128
+ @file.save
129
+ end
130
+ end
131
+
132
+ def test_get_body_method_local_success
133
+ @file.attributes[:body] = 'body'
134
+ assert_equal 'body', @file.body
135
+
136
+ @file.attributes[:body] = nil
137
+ @file.attributes[:last_modified] = nil
138
+ assert_equal '', @file.body
139
+ end
140
+
141
+ def test_get_body_method_remote_success
142
+ @file.attributes[:body] = nil
143
+ remote_file = @file.dup
144
+ body_content = 'data'
145
+ @file.collection.stub :get, remote_file do
146
+ remote_file.stub :body, body_content do
147
+ body = @file.body
148
+ assert_equal body_content, body
149
+ assert_equal body_content, @file.attributes[:body]
150
+ end
151
+ end
152
+ end
153
+
154
+ def test_get_body_method_remote_not_found_success
155
+ @file.attributes[:body] = nil
156
+ @file.collection.stub :get, nil do
157
+ body = @file.body
158
+ assert_equal '', body
159
+ assert_equal '', @file.attributes[:body]
160
+ end
161
+ end
162
+
163
+ def test_set_body_method_success
164
+ @file.body = 'new_body'
165
+ assert_equal 'new_body', @file.attributes[:body]
166
+ end
167
+
168
+ def test_copy_method_success
169
+ copy_id = @raw_cloud_blob.properties[:copy_id]
170
+ copy_status = 'pending'
171
+
172
+ @service.stub :get_blob_properties, @raw_cloud_blob do
173
+ @service.stub :copy_blob, [copy_id, copy_status] do
174
+ @service.stub :wait_blob_copy_operation_to_finish, true do
175
+ target_file = @file.copy('target_container', 'target_blob')
176
+ assert_instance_of Fog::Storage::AzureRM::File, target_file
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ def test_copy_from_uri_method_success
183
+ copy_id = @raw_cloud_blob.properties[:copy_id]
184
+ copy_status = 'pending'
185
+
186
+ @service.stub :get_blob_properties, @raw_cloud_blob do
187
+ @service.stub :copy_blob_from_uri, [copy_id, copy_status] do
188
+ @service.stub :wait_blob_copy_operation_to_finish, true do
189
+ assert @file.copy_from_uri('source_uri')
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+ def test_copy_from_uri_method_without_directory_exception
196
+ assert_raises(ArgumentError) do
197
+ @file.attributes.delete(:directory)
198
+ @file.copy_from_uri('source_uri')
199
+ end
200
+ end
201
+
202
+ def test_copy_from_uri_method_without_key_exception
203
+ assert_raises(ArgumentError) do
204
+ @file.attributes.delete(:key)
205
+ @file.copy_from_uri('source_uri')
206
+ end
207
+ end
208
+
209
+ def test_destroy_method_success
210
+ @service.stub :delete_blob, true do
211
+ assert @file.destroy
212
+ assert @file.attributes[:body].nil?
213
+ end
214
+ end
215
+
216
+ def test_destroy_method_without_directory_exception
217
+ assert_raises(ArgumentError) do
218
+ @file.attributes.delete(:directory)
219
+ @file.destroy
220
+ end
221
+ end
222
+
223
+ def test_destroy_method_without_key_exception
224
+ assert_raises(ArgumentError) do
225
+ @file.attributes.delete(:key)
226
+ @file.destroy
227
+ end
228
+ end
229
+
230
+ def test_public_method_success
231
+ @file.directory.acl = 'container'
232
+ assert @file.public?
233
+
234
+ @file.directory.acl = 'blob'
235
+ assert @file.public?
236
+
237
+ @file.directory.acl = nil
238
+ assert !@file.public?
239
+ end
240
+
241
+ def test_public_method_without_directory_exception
242
+ assert_raises(ArgumentError) do
243
+ @file.attributes.delete(:directory)
244
+ @file.public?
245
+ end
246
+ end
247
+
248
+ def test_public_url_method_success
249
+ @file.stub :public?, false do
250
+ assert @file.public_url.nil?
251
+ end
252
+
253
+ @file.stub :public?, true do
254
+ @service.stub :get_blob_url, @blob_https_url do
255
+ assert @file.public_url, @blob_https_url
256
+ end
257
+ end
258
+
259
+ http_url = @blob_https_url.gsub('https:', 'http:')
260
+ options = { scheme: 'https' }
261
+ @file.stub :public?, true do
262
+ @service.stub :get_blob_url, http_url do
263
+ assert @file.public_url(options), http_url
264
+ end
265
+ end
266
+ end
267
+
268
+ def test_public_url_method_without_directory_exception
269
+ assert_raises(ArgumentError) do
270
+ @file.attributes.delete(:directory)
271
+ @file.public_url
272
+ end
273
+ end
274
+
275
+ def test_public_url_method_without_key_exception
276
+ assert_raises(ArgumentError) do
277
+ @file.attributes.delete(:key)
278
+ @file.public_url
279
+ end
280
+ end
281
+
282
+ def test_url_method_success
283
+ @file.collection.stub :get_url, @blob_https_url do
284
+ assert @file.url(Time.now + 3600), @blob_https_url
285
+ end
286
+ end
287
+
288
+ def test_url_method_without_key_exception
289
+ assert_raises(ArgumentError) do
290
+ @file.attributes.delete(:key)
291
+ @file.url(Time.now + 3600)
292
+ end
293
+ end
294
+ end