hackerdude-aws 2.3.25

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,87 @@
1
+ =begin
2
+ Copyright (c) 2007 RightScale, Inc.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ 'Software'), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ =end
23
+
24
+ # Stub extension/redefinition of RightHttpConnection for testing purposes.
25
+ require 'net/http'
26
+ require 'rubygems'
27
+ require 'right_http_connection'
28
+
29
+ module Net
30
+ class HTTPResponse
31
+ alias_method :real_body, :body
32
+ def setmsg(msg)
33
+ @mymsg = msg
34
+ end
35
+
36
+ def body
37
+ # defined?() helps us to get rid of a bunch of 'warnings'
38
+ (defined?(@mymsg) && @mymsg) ? @mymsg : real_body
39
+ end
40
+ end
41
+ end
42
+
43
+ module Rightscale
44
+
45
+ class HttpConnection
46
+ @@response_stack = []
47
+
48
+ alias_method :real_request, :request
49
+
50
+ def request(request_params, &block)
51
+ if(@@response_stack.length == 0)
52
+ return real_request(request_params, &block)
53
+ end
54
+
55
+ if(block)
56
+ # Do something special
57
+ else
58
+ next_response = HttpConnection::pop()
59
+ classname = Net::HTTPResponse::CODE_TO_OBJ["#{next_response[:code]}"]
60
+ response = classname.new("1.1", next_response[:code], next_response[:msg])
61
+ if(next_response[:msg])
62
+ response.setmsg(next_response[:msg])
63
+ end
64
+ response
65
+ end
66
+ end
67
+
68
+ def self.reset
69
+ @@response_stack = []
70
+ end
71
+
72
+ def self.push(code, msg=nil)
73
+ response = {:code => code, :msg => msg}
74
+ @@response_stack << response
75
+ end
76
+
77
+ def self.pop
78
+ @@response_stack.pop
79
+ end
80
+
81
+ def self.length
82
+ @@response_stack.length
83
+ end
84
+
85
+ end
86
+
87
+ end
@@ -0,0 +1,181 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/aws'
3
+ require 'rds/rds'
4
+ require 'pp'
5
+ require File.dirname(__FILE__) + '/../test_credentials.rb'
6
+
7
+ class TestRds < Test::Unit::TestCase
8
+
9
+ # Some of RightEc2 instance methods concerning instance launching and image registration
10
+ # are not tested here due to their potentially risk.
11
+
12
+ def setup
13
+ TestCredentials.get_credentials
14
+
15
+ @rds = Aws::Rds.new(TestCredentials.aws_access_key_id,
16
+ TestCredentials.aws_secret_access_key)
17
+
18
+ @identifier = 'test-db-instance1b'
19
+ # deleting this one....
20
+ #@identifier2 = 'my-db-instance2'
21
+ end
22
+
23
+
24
+ def test_00_describe_db_instances_empty
25
+ instances = @rds.describe_db_instances
26
+ # puts "instances_result=" + instances_result.inspect
27
+ # instances = instances_result["DescribeDBInstancesResult"]["DBInstances"]["DBInstance"]
28
+ puts "instances count = " + instances.count.to_s
29
+ puts 'instances=' + instances.inspect
30
+ assert instances.size == 0
31
+ end
32
+
33
+
34
+ def test_01_create_db_instance
35
+ begin
36
+ db_instance3 = @rds.create_db_instance('bad_test_key', "db.m1.small", 5, "master", "masterpass")
37
+ rescue => ex
38
+ #puts "msg=" + ex.message
39
+ #puts "response=" + ex.response
40
+ assert ex.message[0, "InvalidParameterValue".size] == "InvalidParameterValue"
41
+ end
42
+
43
+ db_instance = @rds.create_db_instance(@identifier, "db.m1.small", 5, "master", "masterpass")
44
+ assert db_instance[:db_instance_status] == "creating"
45
+
46
+ start = Time.now
47
+ tries=0
48
+ catch (:done) do
49
+ while tries < 100
50
+ instances = @rds.describe_db_instances
51
+
52
+ #puts "INSTANCES -----> " + instances.inspect
53
+
54
+ instances.each do |i|
55
+ db_status = i[:db_instance_status]
56
+ puts 'i=' + db_status.to_s
57
+ next unless i[:db_instance_identifier] == @identifier
58
+ throw :done if db_status == "available"
59
+ puts "Database not ready yet.... attempt #{tries.to_s} of 100, db state --> #{i[:db_instance_status].to_s}"
60
+ tries += 1
61
+ sleep 5
62
+ end
63
+ end
64
+ end
65
+ puts "Duration to start db instance: #{Time.now-start}"
66
+ end
67
+
68
+
69
+ def test_02_describe_db_instances
70
+ instances = @rds.describe_db_instances
71
+ # puts "instances_result=" + instances_result.inspect
72
+ # instances = instances_result["DescribeDBInstancesResult"]["DBInstances"]["DBInstance"]
73
+ puts "instances count = " + instances.count.to_s
74
+ puts 'instances=' + instances.inspect
75
+ assert instances.size > 0
76
+ i_describe = nil
77
+ instances.each do |rdi|
78
+ puts 'rdi=' + rdi.inspect
79
+ i_describe = rdi if rdi[:db_instance_identifier] == @identifier
80
+ end
81
+ assert i_describe
82
+
83
+ puts 'response_metadata=' + instances.response_metadata.inspect
84
+ assert instances.response_metadata
85
+ assert instances.response_metadata[:request_id]
86
+ end
87
+
88
+
89
+ def test_03_describe_security_groups
90
+ security_groups = @rds.describe_db_security_groups()
91
+ puts "security_groups=" + security_groups.inspect
92
+ default_present = false
93
+ assert security_groups.is_a?(Array)
94
+ security_groups.each do |security_group|
95
+ security_group.inspect
96
+ if security_group[:db_security_group_name] == "default"
97
+ default_present=true
98
+ end
99
+ assert security_group[:ec2_security_groups].is_a? Array
100
+ end
101
+ assert default_present
102
+ end
103
+
104
+
105
+ def test_04_authorize_security_groups_ingress
106
+ # Create
107
+ # security_groups = @rds.describe_db_security_groups
108
+ # @security_info = security_groups[0]
109
+ @rds.authorize_db_security_group_ingress_range("default", "122.122.122.122/12")
110
+
111
+ # Check
112
+ security_groups = @rds.describe_db_security_groups
113
+ @security_info = security_groups[0]
114
+
115
+ ip_found = @security_info.inspect.include? "122.122.122.122/12"
116
+ assert ip_found
117
+ end
118
+
119
+
120
+ def test_05_delete_db_instance
121
+ @rds.delete_db_instance(@identifier) # todo: can't delete unless it's in "available" state
122
+ #@rds.delete_db_instance(@identifier2)
123
+ sleep 3
124
+
125
+ instances = @rds.describe_db_instances(:db_instance_identifier=>@identifier)
126
+ #puts "instances_result=" + instances_result.inspect
127
+
128
+ instances.each do |i|
129
+ next unless i[:db_instance_identifier] == @identifier
130
+ db_status = i[:db_instance_status]
131
+ puts "Trying to delete and getting i[DBInstanceStatus] -----------> " + db_status
132
+ @rds.delete_db_instance(i[:db_instance_identifier]) if db_status == "available"
133
+ assert db_status == "deleting"
134
+ end
135
+ sleep 2
136
+
137
+ end
138
+
139
+
140
+ def test_06_create_security_groups
141
+ group_present=false
142
+
143
+ @rds.create_db_security_group("new_sample_group", "new_sample_group_description")
144
+
145
+ security_groups = @rds.describe_db_security_groups
146
+
147
+ security_groups.each do |security_group|
148
+ if (security_group[:db_security_group_name]=="new_sample_group")&&(security_group[:db_security_group_description]=="new_sample_group_description")
149
+ group_present = true
150
+ end
151
+ end
152
+
153
+ assert group_present
154
+ end
155
+
156
+
157
+ def test_07_revoking_security_groups_ingress
158
+ # sleep 15
159
+ @rds.revoke_db_security_group_ingress("default", "122.122.122.122/12")
160
+ sleep 2
161
+ security_groups = @rds.describe_db_security_groups
162
+ revoking = security_groups[0].inspect.include? "revoking"
163
+ assert revoking
164
+ end
165
+
166
+
167
+ def test_08_delete_security_group
168
+ group_present=false
169
+ @rds.delete_db_security_group("new_sample_group")
170
+ sleep 2
171
+ security_groups = @rds.describe_db_security_groups
172
+ security_groups.each do |security_group|
173
+ if (security_group[:db_security_group_name]=="new_sample_group")
174
+ group_present=true
175
+ end
176
+ end
177
+ assert !group_present
178
+ end
179
+
180
+
181
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/aws'
3
+
@@ -0,0 +1,425 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require File.dirname(__FILE__) + '/../test_credentials.rb'
3
+
4
+ class TestS3 < Test::Unit::TestCase
5
+
6
+ RIGHT_OBJECT_TEXT = 'Right test message'
7
+
8
+ def setup
9
+ TestCredentials.get_credentials
10
+ @s3 = Aws::S3Interface.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
11
+ @bucket = TestCredentials.config['amazon']['my_prefix'] + '_awesome_test_bucket_000A1'
12
+ @bucket2 = TestCredentials.config['amazon']['my_prefix'] + '_awesome_test_bucket_000A2'
13
+ @key1 = 'test/woohoo1/'
14
+ @key2 = 'test1/key/woohoo2'
15
+ @key3 = 'test2/A%B@C_D&E?F+G=H"I'
16
+ @key1_copy = 'test/woohoo1_2'
17
+ @key1_new_name = 'test/woohoo1_3'
18
+ @key2_new_name = 'test1/key/woohoo2_new'
19
+ @s = Aws::S3.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
20
+ end
21
+
22
+ def teardown
23
+
24
+ end
25
+
26
+ #---------------------------
27
+ # Aws::S3Interface
28
+ #---------------------------
29
+
30
+ def test_01_create_bucket
31
+ assert @s3.create_bucket(@bucket), 'Create_bucket fail'
32
+ end
33
+
34
+ def test_02_list_all_my_buckets
35
+ assert @s3.list_all_my_buckets.map{|bucket| bucket[:name]}.include?(@bucket), "#{@bucket} must exist in bucket list"
36
+ end
37
+
38
+ def test_03_list_empty_bucket
39
+ assert_equal 0, @s3.list_bucket(@bucket).size, "#{@bucket} isn't empty, arrgh!"
40
+ end
41
+
42
+ def test_04_put
43
+ assert @s3.put(@bucket, @key1, RIGHT_OBJECT_TEXT, 'x-amz-meta-family'=>'Woohoo1!'), 'Put bucket fail'
44
+ assert @s3.put(@bucket, @key2, RIGHT_OBJECT_TEXT, 'x-amz-meta-family'=>'Woohoo2!'), 'Put bucket fail'
45
+ assert @s3.put(@bucket, @key3, RIGHT_OBJECT_TEXT, 'x-amz-meta-family'=>'Woohoo3!'), 'Put bucket fail'
46
+ end
47
+
48
+ def test_05_get_and_get_object
49
+ assert_raise(Aws::AwsError) { @s3.get(@bucket, 'undefined/key') }
50
+ data1 = @s3.get(@bucket, @key1)
51
+ assert_equal RIGHT_OBJECT_TEXT, data1[:object], "Object text must be equal to '#{RIGHT_OBJECT_TEXT}'"
52
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key1), "Get_object text must return '#{RIGHT_OBJECT_TEXT}'"
53
+ assert_equal 'Woohoo1!', data1[:headers]['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
54
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key3), "Get_object text must return '#{RIGHT_OBJECT_TEXT}'"
55
+ end
56
+
57
+ def test_06_head
58
+ assert_equal 'Woohoo1!', @s3.head(@bucket, @key1)['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
59
+ end
60
+
61
+
62
+ def test_07_streaming_get
63
+ resp = String.new
64
+ assert_raise(Aws::AwsError) do
65
+ @s3.get(@bucket, 'undefined/key') do |chunk|
66
+ resp += chunk
67
+ end
68
+ end
69
+
70
+ resp = String.new
71
+ data1 = @s3.get(@bucket, @key1) do |chunk|
72
+ resp += chunk
73
+ end
74
+ assert_equal RIGHT_OBJECT_TEXT, resp, "Object text must be equal to '#{RIGHT_OBJECT_TEXT}'"
75
+ assert_equal @s3.get_object(@bucket, @key1), resp, "Streaming iface must return same as non-streaming"
76
+ assert_equal 'Woohoo1!', data1[:headers]['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
77
+ end
78
+
79
+ def test_08_keys
80
+ keys = @s3.list_bucket(@bucket).map{|b| b[:key]}
81
+ assert_equal keys.size, 3, "There should be 3 keys"
82
+ assert(keys.include?(@key1))
83
+ assert(keys.include?(@key2))
84
+ assert(keys.include?(@key3))
85
+ end
86
+
87
+ def test_09_copy_key
88
+ #--- test COPY
89
+ # copy a key
90
+ assert @s3.copy(@bucket, @key1, @bucket, @key1_copy)
91
+ # check it was copied well
92
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key1_copy), "copied object must have the same data"
93
+ # check meta-headers were copied
94
+ headers = @s3.head(@bucket, @key1_copy)
95
+ assert_equal 'Woohoo1!', headers['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
96
+ #--- test REPLACE
97
+ assert @s3.copy(@bucket, @key1, @bucket, @key1_copy, :replace, 'x-amz-meta-family' => 'oooops!')
98
+ # check it was copied well
99
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key1_copy), "copied object must have the same data"
100
+ # check meta-headers were overwrittenn
101
+ headers = @s3.head(@bucket, @key1_copy)
102
+ assert_equal 'oooops!', headers['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'oooops!'"
103
+ end
104
+
105
+ def test_10_move_key
106
+ # move a key
107
+ assert @s3.move(@bucket, @key1, @bucket, @key1_new_name)
108
+ # check it's data was moved correctly
109
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key1_new_name), "moved object must have the same data"
110
+ # check meta-headers were moved
111
+ headers = @s3.head(@bucket, @key1_new_name)
112
+ assert_equal 'Woohoo1!', headers['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
113
+ # check the original key is not exists any more
114
+ keys = @s3.list_bucket(@bucket).map{|b| b[:key]}
115
+ assert(!keys.include?(@key1))
116
+ end
117
+
118
+ def test_11_rename_key
119
+ # rename a key
120
+ assert @s3.rename(@bucket, @key2, @key2_new_name)
121
+ # check the new key data
122
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key2_new_name), "moved object must have the same data"
123
+ # check meta-headers
124
+ headers = @s3.head(@bucket, @key2_new_name)
125
+ assert_equal 'Woohoo2!', headers['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo2!'"
126
+ # check the original key is not exists any more
127
+ keys = @s3.list_bucket(@bucket).map{|b| b[:key]}
128
+ assert(!keys.include?(@key2))
129
+ end
130
+
131
+ def test_12_retrieve_object
132
+ assert_raise(Aws::AwsError) { @s3.retrieve_object(:bucket => @bucket, :key => 'undefined/key') }
133
+ data1 = @s3.retrieve_object(:bucket => @bucket, :key => @key1_new_name)
134
+ assert_equal RIGHT_OBJECT_TEXT, data1[:object], "Object text must be equal to '#{RIGHT_OBJECT_TEXT}'"
135
+ assert_equal 'Woohoo1!', data1[:headers]['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
136
+ end
137
+
138
+ def test_13_delete_folder
139
+ assert_equal 1, @s3.delete_folder(@bucket, 'test').size, "Only one key(#{@key1}) must be deleted!"
140
+ end
141
+
142
+ def test_14_delete_bucket
143
+ assert_raise(Aws::AwsError) { @s3.delete_bucket(@bucket) }
144
+ assert @s3.clear_bucket(@bucket), 'Clear_bucket fail'
145
+ assert_equal 0, @s3.list_bucket(@bucket).size, 'Bucket must be empty'
146
+ assert @s3.delete_bucket(@bucket)
147
+ assert !@s3.list_all_my_buckets.map{|bucket| bucket[:name]}.include?(@bucket), "#{@bucket} must not exist"
148
+ end
149
+
150
+
151
+ #---------------------------
152
+ # Aws::S3 classes
153
+ #---------------------------
154
+
155
+ def test_20_s3
156
+ # create bucket
157
+ bucket = @s.bucket(@bucket, true)
158
+ assert bucket
159
+ # check that the bucket exists
160
+ assert @s.buckets.map{|b| b.name}.include?(@bucket)
161
+ # delete bucket
162
+ assert bucket.clear
163
+ assert bucket.delete
164
+ end
165
+
166
+ def test_21_bucket_create_put_get_key
167
+ bucket = Aws::S3::Bucket.create(@s, @bucket, true)
168
+ # check that the bucket exists
169
+ assert @s.buckets.map{|b| b.name}.include?(@bucket)
170
+ assert bucket.keys.empty?
171
+ # put data
172
+ assert bucket.put(@key3, RIGHT_OBJECT_TEXT, {'family'=>'123456'})
173
+ # get data and compare
174
+ assert_equal RIGHT_OBJECT_TEXT, bucket.get(@key3)
175
+ # get key object
176
+ key = bucket.key(@key3, true)
177
+ assert_equal Aws::S3::Key, key.class
178
+ assert key.exists?
179
+ assert_equal '123456', key.meta_headers['family']
180
+ end
181
+
182
+ def test_22_keys
183
+ bucket = Aws::S3::Bucket.create(@s, @bucket, false)
184
+ # create first key
185
+ key3 = Aws::S3::Key.create(bucket, @key3)
186
+ key3.refresh
187
+ assert key3.exists?
188
+ assert_equal '123456', key3.meta_headers['family']
189
+ # create second key
190
+ key2 = Aws::S3::Key.create(bucket, @key2)
191
+ assert !key2.refresh
192
+ assert !key2.exists?
193
+ assert_raise(Aws::AwsError) { key2.head }
194
+ # store key
195
+ key2.meta_headers = {'family'=>'111222333'}
196
+ assert key2.put(RIGHT_OBJECT_TEXT)
197
+ # make sure that the key exists
198
+ assert key2.refresh
199
+ assert key2.exists?
200
+ assert key2.head
201
+ # get its data
202
+ assert_equal RIGHT_OBJECT_TEXT, key2.get
203
+ # drop key
204
+ assert key2.delete
205
+ assert !key2.exists?
206
+ end
207
+
208
+ def test_23_rename_key
209
+ bucket = Aws::S3::Bucket.create(@s, @bucket, false)
210
+ # -- 1 -- (key based rename)
211
+ # create a key
212
+ key = bucket.key('test/copy/1')
213
+ key.put(RIGHT_OBJECT_TEXT)
214
+ original_key = key.clone
215
+ assert key.exists?, "'test/copy/1' should exist"
216
+ # rename it
217
+ key.rename('test/copy/2')
218
+ assert_equal 'test/copy/2', key.name
219
+ assert key.exists?, "'test/copy/2' should exist"
220
+ # the original key should not exist
221
+ assert !original_key.exists?, "'test/copy/1' should not exist"
222
+ # -- 2 -- (bucket based rename)
223
+ bucket.rename_key('test/copy/2', 'test/copy/3')
224
+ assert bucket.key('test/copy/3').exists?, "'test/copy/3' should exist"
225
+ assert !bucket.key('test/copy/2').exists?, "'test/copy/2' should not exist"
226
+ end
227
+
228
+ def test_24_copy_key
229
+ bucket = Aws::S3::Bucket.create(@s, @bucket, false)
230
+ # -- 1 -- (key based copy)
231
+ # create a key
232
+ key = bucket.key('test/copy/10')
233
+ key.put(RIGHT_OBJECT_TEXT)
234
+ # make copy
235
+ new_key = key.copy('test/copy/11')
236
+ # make sure both the keys exist and have a correct data
237
+ assert key.exists?, "'test/copy/10' should exist"
238
+ assert new_key.exists?, "'test/copy/11' should exist"
239
+ assert_equal RIGHT_OBJECT_TEXT, key.get
240
+ assert_equal RIGHT_OBJECT_TEXT, new_key.get
241
+ # -- 2 -- (bucket based copy)
242
+ bucket.copy_key('test/copy/11', 'test/copy/12')
243
+ assert bucket.key('test/copy/11').exists?, "'test/copy/11' should exist"
244
+ assert bucket.key('test/copy/12').exists?, "'test/copy/12' should exist"
245
+ assert_equal RIGHT_OBJECT_TEXT, bucket.key('test/copy/11').get
246
+ assert_equal RIGHT_OBJECT_TEXT, bucket.key('test/copy/12').get
247
+ end
248
+
249
+ def test_25_move_key
250
+ bucket = Aws::S3::Bucket.create(@s, @bucket, false)
251
+ # -- 1 -- (key based copy)
252
+ # create a key
253
+ key = bucket.key('test/copy/20')
254
+ key.put(RIGHT_OBJECT_TEXT)
255
+ # move
256
+ new_key = key.move('test/copy/21')
257
+ # make sure both the keys exist and have a correct data
258
+ assert !key.exists?, "'test/copy/20' should not exist"
259
+ assert new_key.exists?, "'test/copy/21' should exist"
260
+ assert_equal RIGHT_OBJECT_TEXT, new_key.get
261
+ # -- 2 -- (bucket based copy)
262
+ bucket.copy_key('test/copy/21', 'test/copy/22')
263
+ assert bucket.key('test/copy/21').exists?, "'test/copy/21' should not exist"
264
+ assert bucket.key('test/copy/22').exists?, "'test/copy/22' should exist"
265
+ assert_equal RIGHT_OBJECT_TEXT, bucket.key('test/copy/22').get
266
+ end
267
+
268
+ def test_26_save_meta
269
+ bucket = Aws::S3::Bucket.create(@s, @bucket, false)
270
+ # create a key
271
+ key = bucket.key('test/copy/30')
272
+ key.put(RIGHT_OBJECT_TEXT)
273
+ assert key.meta_headers.blank?
274
+ # store some meta keys
275
+ meta = {'family' => 'oops', 'race' => 'troll'}
276
+ assert_equal meta, key.save_meta(meta)
277
+ # reload meta
278
+ assert_equal meta, key.reload_meta
279
+ end
280
+
281
+ def test_27_clear_delete
282
+ bucket = Aws::S3::Bucket.create(@s, @bucket, false)
283
+ # add another key
284
+ bucket.put(@key2, RIGHT_OBJECT_TEXT)
285
+ # delete 'folder'
286
+ assert_equal 1, bucket.delete_folder(@key1).size
287
+ # delete
288
+ assert_raise(Aws::AwsError) { bucket.delete }
289
+ bucket.delete(true)
290
+ end
291
+
292
+ # Grantees
293
+
294
+ def test_30_create_bucket
295
+ bucket = @s.bucket(@bucket, true, 'public-read')
296
+ assert bucket
297
+ end
298
+
299
+ def test_31_list_grantees
300
+ bucket = Aws::S3::Bucket.create(@s, @bucket, false)
301
+ # get grantees list
302
+ grantees = bucket.grantees
303
+ # check that the grantees count equal to 2 (root, AllUsers)
304
+ assert_equal 2, grantees.size
305
+ end
306
+
307
+ def test_32_grant_revoke_drop
308
+ bucket = Aws::S3::Bucket.create(@s, @bucket, false)
309
+ # Take 'AllUsers' grantee
310
+ grantee = Aws::S3::Grantee.new(bucket, 'http://acs.amazonaws.com/groups/global/AllUsers')
311
+ # Check exists?
312
+ assert grantee.exists?
313
+ # Add grant as String
314
+ assert grantee.grant('WRITE')
315
+ # Add grants as Array
316
+ assert grantee.grant(['READ_ACP', 'WRITE_ACP'])
317
+ # Check perms count
318
+ assert_equal 4, grantee.perms.size
319
+ # revoke 'WRITE_ACP'
320
+ assert grantee.revoke('WRITE_ACP')
321
+ # Check manual perm removal method
322
+ grantee.perms -= ['READ_ACP']
323
+ grantee.apply
324
+ assert_equal 2, grantee.perms.size
325
+ # Check grantee removal if it has no permissions
326
+ assert grantee.perms = []
327
+ assert grantee.apply
328
+ assert !grantee.exists?
329
+ # Check multiple perms assignment
330
+ assert grantee.grant('FULL_CONTROL', 'READ', 'WRITE')
331
+ assert_equal ['FULL_CONTROL', 'READ', 'WRITE'].sort, grantee.perms.sort
332
+ # Check multiple perms removal
333
+ assert grantee.revoke('FULL_CONTROL', 'WRITE')
334
+ assert_equal ['READ'], grantee.perms
335
+ # check 'Drop' method
336
+ assert grantee.drop
337
+ assert !grantee.exists?
338
+ assert_equal 1, bucket.grantees.size
339
+ # Delete bucket
340
+ bucket.delete(true)
341
+ end
342
+
343
+ def test_33_key_grantees
344
+ # Create bucket
345
+ bucket = @s.bucket(@bucket, true)
346
+ # Create key
347
+ key = bucket.key(@key1)
348
+ assert key.put(RIGHT_OBJECT_TEXT, 'public-read')
349
+ # Get grantees list (must be == 2)
350
+ grantees = key.grantees
351
+ assert grantees
352
+ assert_equal 2, grantees.size
353
+ # Take one of grantees and give him 'Write' perms
354
+ grantee = grantees[0]
355
+ assert grantee.grant('WRITE')
356
+ # Drop grantee
357
+ assert grantee.drop
358
+ # Drop bucket
359
+ bucket.delete(true)
360
+ end
361
+
362
+ def test_34_bucket_create_put_with_perms
363
+ bucket = Aws::S3::Bucket.create(@s, @bucket, true)
364
+ # check that the bucket exists
365
+ assert @s.buckets.map{|b| b.name}.include?(@bucket)
366
+ assert bucket.keys.empty?
367
+ # put data (with canned ACL)
368
+ assert bucket.put(@key1, RIGHT_OBJECT_TEXT, {'family'=>'123456'}, "public-read")
369
+ # get data and compare
370
+ assert_equal RIGHT_OBJECT_TEXT, bucket.get(@key1)
371
+ # get key object
372
+ key = bucket.key(@key1, true)
373
+ assert_equal Aws::S3::Key, key.class
374
+ assert key.exists?
375
+ assert_equal '123456', key.meta_headers['family']
376
+ end
377
+
378
+ def test_35_key_put_with_perms
379
+ bucket = Aws::S3::Bucket.create(@s, @bucket, false)
380
+ # create first key
381
+ key1 = Aws::S3::Key.create(bucket, @key1)
382
+ key1.refresh
383
+ assert key1.exists?
384
+ assert key1.put(RIGHT_OBJECT_TEXT, "public-read")
385
+ # get its data
386
+ assert_equal RIGHT_OBJECT_TEXT, key1.get
387
+ # drop key
388
+ assert key1.delete
389
+ assert !key1.exists?
390
+ end
391
+
392
+ def test_36_set_amazon_problems
393
+ original_problems = Aws::S3Interface.amazon_problems
394
+ assert(original_problems.length > 0)
395
+ Aws::S3Interface.amazon_problems= original_problems << "A New Problem"
396
+ new_problems = Aws::S3Interface.amazon_problems
397
+ assert_equal(new_problems, original_problems)
398
+
399
+ Aws::S3Interface.amazon_problems= nil
400
+ assert_nil(Aws::S3Interface.amazon_problems)
401
+ end
402
+
403
+ def test_37_access_logging
404
+ bucket = Aws::S3::Bucket.create(@s, @bucket, false)
405
+ targetbucket = Aws::S3::Bucket.create(@s, @bucket2, true)
406
+ # Take 'AllUsers' grantee
407
+ grantee = Aws::S3::Grantee.new(targetbucket, 'http://acs.amazonaws.com/groups/s3/LogDelivery')
408
+
409
+ assert grantee.grant(['READ_ACP', 'WRITE'])
410
+
411
+ assert bucket.enable_logging(:targetbucket => targetbucket, :targetprefix => "loggylogs/")
412
+
413
+ assert_equal(bucket.logging_info, {:enabled => true, :targetbucket => @bucket2, :targetprefix => "loggylogs/"})
414
+
415
+ assert bucket.disable_logging
416
+
417
+ # check 'Drop' method
418
+ assert grantee.drop
419
+
420
+ # Delete bucket
421
+ bucket.delete(true)
422
+ targetbucket.delete(true)
423
+ end
424
+
425
+ end