dmarkov-right_aws 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/right_aws'
@@ -0,0 +1,146 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestAcf < Test::Unit::TestCase
4
+
5
+ RIGHT_OBJECT_TEXT = 'Right test message'
6
+
7
+ STDOUT.sync = true
8
+
9
+ def setup
10
+ @acf= Rightscale::AcfInterface.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
11
+ @s3 = Rightscale::S3.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
12
+ @bucket_name = "right-acf-awesome-test-bucket-0001"
13
+ @bucket_domain = "#{@bucket_name}.s3.amazonaws.com"
14
+ end
15
+
16
+ def test_01_list_distributions_part1
17
+ distributions = nil
18
+ assert_nothing_raised(Rightscale::AwsError) do
19
+ distributions = @acf.list_distributions
20
+ end
21
+ assert distributions.is_a?(Array)
22
+ end
23
+
24
+ def test_02_try_to_create_for_bad_bucket
25
+ # a bucket does not exist
26
+ assert_raise(Rightscale::AwsError) do
27
+ @acf.create_distribution("right-cloudfront-awesome-test-bucket-not-exist", "Mustn't to be born", true)
28
+ end
29
+ # a bucket is not a domain naming complied guy
30
+ bucket_name = 'right_cloudfront_awesome_test_bucket_BAD'
31
+ @s3.bucket(bucket_name, :create)
32
+ assert_raise(Rightscale::AwsError) do
33
+ @acf.create_distribution(bucket_name, "Mustn't to be born", true)
34
+ end
35
+ end
36
+
37
+ def test_03_create
38
+ comment = 'WooHoo!!!'
39
+ # create a test bucket
40
+ @s3.bucket(@bucket_name, :create)
41
+ # create a distribution
42
+ distribution = @acf.create_distribution(@bucket_domain, comment, true)
43
+ assert_equal comment, distribution[:comment]
44
+ assert distribution[:cnames].size == 0
45
+ assert distribution[:enabled]
46
+ end
47
+
48
+ def test_04_list_distributions_part2
49
+ distributions = @acf.list_distributions
50
+ assert distributions.size > 0
51
+ end
52
+
53
+ def get_test_distribution
54
+ @acf.list_distributions.select{ |d| d[:origin] == @bucket_domain }.first
55
+ end
56
+
57
+ def test_05_get_distribution
58
+ old = get_test_distribution
59
+ assert_nothing_raised do
60
+ @acf.get_distribution(old[:aws_id])
61
+ end
62
+ end
63
+
64
+ def test_06_get_and_set_config
65
+ config = nil
66
+ old = get_test_distribution
67
+ assert_nothing_raised do
68
+ config = @acf.get_distribution_config(old[:aws_id])
69
+ end
70
+ # change a config
71
+ config[:enabled] = false
72
+ config[:cnames] << 'x1.myawesomesite.com'
73
+ config[:cnames] << 'x2.myawesomesite.com'
74
+ # set config
75
+ set_config_result = nil
76
+ assert_nothing_raised do
77
+ set_config_result = @acf.set_distribution_config(old[:aws_id], config)
78
+ end
79
+ assert set_config_result
80
+ # reget the config and check
81
+ new_config = nil
82
+ assert_nothing_raised do
83
+ new_config = @acf.get_distribution_config(old[:aws_id])
84
+ end
85
+ assert !new_config[:enabled]
86
+ assert_equal new_config[:cnames].sort, ['x1.myawesomesite.com', 'x2.myawesomesite.com']
87
+ assert_not_equal config[:e_tag], new_config[:e_tag]
88
+
89
+ # try to update the old config again (must fail because ETAG has changed)
90
+ assert_raise(Rightscale::AwsError) do
91
+ @acf.set_distribution_config(old[:aws_id], config)
92
+ end
93
+ end
94
+
95
+ def test_07_caching
96
+ # enable caching
97
+ @acf.params[:cache] = true
98
+ # list distributions
99
+ @acf.list_distributions
100
+ # list the distributions again - cache should hit
101
+ assert_raise(Rightscale::AwsNoChange) do
102
+ @acf.list_distributions
103
+ end
104
+ # disable caching
105
+ @acf.params[:cache] = true
106
+ end
107
+
108
+ def test_08_delete_distribution
109
+ # we need ETAG so use get_distribution
110
+ distribution = @acf.get_distribution(get_test_distribution[:aws_id])
111
+ # try to delete a distribution
112
+ # should fail because
113
+ if distribution[:status] == 'InProgress'
114
+ # should fail because the distribution is not deployed yet
115
+ assert_raise(Rightscale::AwsError) do
116
+ @acf.delete_distribution(distribution[:aws_id], distribution[:e_tag])
117
+ end
118
+ # wait for a deployed state
119
+ print "waiting up to 5 min while the distribution is being deployed: "
120
+ 100.times do
121
+ print '.'
122
+ distribution = @acf.get_distribution(distribution[:aws_id])
123
+ if distribution[:status] == 'Deployed'
124
+ print ' done'
125
+ break
126
+ end
127
+ sleep 3
128
+ end
129
+ puts
130
+ end
131
+
132
+ # only disabled and deployed distribution can be deleted
133
+ assert_equal 'Deployed', distribution[:status]
134
+ assert !distribution[:enabled]
135
+
136
+ # delete the distribution
137
+ assert_nothing_raised do
138
+ @acf.delete_distribution(distribution[:aws_id], distribution[:e_tag])
139
+ end
140
+ end
141
+
142
+ def test_09_drop_bucket
143
+ assert @s3.bucket(@bucket_name).delete
144
+ end
145
+
146
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/right_aws'
@@ -0,0 +1,108 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'pp'
3
+
4
+ class TestEc2 < Test::Unit::TestCase
5
+
6
+ # Some of RightEc2 instance methods concerning instance launching and image registration
7
+ # are not tested here due to their potentially risk.
8
+
9
+ def setup
10
+ @ec2 = Rightscale::Ec2.new(TestCredentials.aws_access_key_id,
11
+ TestCredentials.aws_secret_access_key)
12
+ @key = 'right_ec2_awesome_test_key'
13
+ @group = 'right_ec2_awesome_test_security_group'
14
+ end
15
+
16
+ def test_01_create_describe_key_pairs
17
+ new_key = @ec2.create_key_pair(@key)
18
+ assert new_key[:aws_material][/BEGIN RSA PRIVATE KEY/], "New key material is absent"
19
+ keys = @ec2.describe_key_pairs
20
+ assert keys.map{|key| key[:aws_key_name] }.include?(@key), "#{@key} must exist"
21
+ end
22
+
23
+ def test_02_create_security_group
24
+ assert @ec2.create_security_group(@group,'My awesone test group'), 'Create_security_group fail'
25
+ group = @ec2.describe_security_groups([@group])[0]
26
+ assert_equal @group, group[:aws_group_name], 'Group must be created but does not exist'
27
+ end
28
+
29
+ def test_03_perms_add
30
+ assert @ec2.authorize_security_group_named_ingress(@group, TestCredentials.account_number, 'default')
31
+ assert @ec2.authorize_security_group_IP_ingress(@group, 80,80,'udp','192.168.1.0/8')
32
+ end
33
+
34
+ def test_04_check_new_perms_exist
35
+ assert_equal 2, @ec2.describe_security_groups([@group])[0][:aws_perms].size
36
+ end
37
+
38
+ def test_05_perms_remove
39
+ assert @ec2.revoke_security_group_IP_ingress(@group, 80,80,'udp','192.168.1.0/8')
40
+ assert @ec2.revoke_security_group_named_ingress(@group,
41
+ TestCredentials.account_number, 'default')
42
+ end
43
+
44
+ def test_06_describe_images
45
+ images = @ec2.describe_images
46
+ assert images.size>0, 'Amazon must have at least some public images'
47
+ # unknown image
48
+ assert_raise(Rightscale::AwsError){ @ec2.describe_images(['ami-ABCDEFGH'])}
49
+ end
50
+
51
+ def test_07_describe_instanses
52
+ assert @ec2.describe_instances
53
+ # unknown image
54
+ assert_raise(Rightscale::AwsError){ @ec2.describe_instances(['i-ABCDEFGH'])}
55
+ end
56
+
57
+ def test_08_delete_security_group
58
+ assert @ec2.delete_security_group(@group), 'Delete_security_group fail'
59
+ end
60
+
61
+ def test_09_delete_key_pair
62
+ assert @ec2.delete_key_pair(@key), 'Delete_key_pair fail'
63
+ ## Hmmm... Amazon does not through the exception any more. It now just returns a 'true' if the key does not exist any more...
64
+ ## # key must be deleted already
65
+ ## assert_raise(Rightscale::AwsError) { @ec2.delete_key_pair(@key) }
66
+ end
67
+
68
+ def test_10_signature_version_0
69
+ ec2 = Rightscale::Ec2.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key, :signature_version => '0')
70
+ images = ec2.describe_images
71
+ assert images.size>0, 'Amazon must have at least some public images'
72
+ # check that the request has correct signature version
73
+ assert ec2.last_request.path.include?('SignatureVersion=0')
74
+ end
75
+
76
+ def test_11_regions
77
+ regions = nil
78
+ assert_nothing_raised do
79
+ regions = @ec2.describe_regions
80
+ end
81
+ # check we got more that 0 regions
82
+ assert regions.size > 0
83
+ # check an access to regions
84
+ regions.each do |region|
85
+ regional_ec2 = Rightscale::Ec2.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key, :region => region)
86
+ # do we have a correct endpoint server?
87
+ assert_equal "#{region}.ec2.amazonaws.com", regional_ec2.params[:server]
88
+ # get a list of images from every region
89
+ images = nil
90
+ assert_nothing_raised do
91
+ images = regional_ec2.describe_regions
92
+ end
93
+ # every region must have images
94
+ assert images.size > 0
95
+ end
96
+ end
97
+
98
+ def test_12_endpoint_url
99
+ ec2 = Rightscale::Ec2.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key, :endpoint_url => 'a://b.c:0/d/', :region => 'z')
100
+ # :endpoint_url has a priority hence :region should be ommitted
101
+ assert_equal 'a', ec2.params[:protocol]
102
+ assert_equal 'b.c', ec2.params[:server]
103
+ assert_equal '/d/', ec2.params[:service]
104
+ assert_equal 0, ec2.params[:port]
105
+ assert_nil ec2.params[:region]
106
+ end
107
+
108
+ end
@@ -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,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/right_aws'
@@ -0,0 +1,419 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestS3 < Test::Unit::TestCase
4
+
5
+ RIGHT_OBJECT_TEXT = 'Right test message'
6
+
7
+ def setup
8
+ @s3 = Rightscale::S3Interface.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
9
+ @bucket = 'right_s3_awesome_test_bucket_000A1'
10
+ @bucket2 = 'right_s3_awesome_test_bucket_000A2'
11
+ @key1 = 'test/woohoo1/'
12
+ @key2 = 'test1/key/woohoo2'
13
+ @key3 = 'test2/A%B@C_D&E?F+G=H"I'
14
+ @key1_copy = 'test/woohoo1_2'
15
+ @key1_new_name = 'test/woohoo1_3'
16
+ @key2_new_name = 'test1/key/woohoo2_new'
17
+ @s = Rightscale::S3.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
18
+ end
19
+
20
+ #---------------------------
21
+ # Rightscale::S3Interface
22
+ #---------------------------
23
+
24
+ def test_01_create_bucket
25
+ assert @s3.create_bucket(@bucket), 'Create_bucket fail'
26
+ end
27
+
28
+ def test_02_list_all_my_buckets
29
+ assert @s3.list_all_my_buckets.map{|bucket| bucket[:name]}.include?(@bucket), "#{@bucket} must exist in bucket list"
30
+ end
31
+
32
+ def test_03_list_empty_bucket
33
+ assert_equal 0, @s3.list_bucket(@bucket).size, "#{@bucket} isn't empty, arrgh!"
34
+ end
35
+
36
+ def test_04_put
37
+ assert @s3.put(@bucket, @key1, RIGHT_OBJECT_TEXT, 'x-amz-meta-family'=>'Woohoo1!'), 'Put bucket fail'
38
+ assert @s3.put(@bucket, @key2, RIGHT_OBJECT_TEXT, 'x-amz-meta-family'=>'Woohoo2!'), 'Put bucket fail'
39
+ assert @s3.put(@bucket, @key3, RIGHT_OBJECT_TEXT, 'x-amz-meta-family'=>'Woohoo3!'), 'Put bucket fail'
40
+ end
41
+
42
+ def test_05_get_and_get_object
43
+ assert_raise(Rightscale::AwsError) { @s3.get(@bucket, 'undefined/key') }
44
+ data1 = @s3.get(@bucket, @key1)
45
+ assert_equal RIGHT_OBJECT_TEXT, data1[:object], "Object text must be equal to '#{RIGHT_OBJECT_TEXT}'"
46
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key1), "Get_object text must return '#{RIGHT_OBJECT_TEXT}'"
47
+ assert_equal 'Woohoo1!', data1[:headers]['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
48
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key3), "Get_object text must return '#{RIGHT_OBJECT_TEXT}'"
49
+ end
50
+
51
+ def test_06_head
52
+ assert_equal 'Woohoo1!', @s3.head(@bucket,@key1)['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
53
+ end
54
+
55
+
56
+ def test_07_streaming_get
57
+ resp = String.new
58
+ assert_raise(Rightscale::AwsError) do
59
+ @s3.get(@bucket, 'undefined/key') do |chunk|
60
+ resp += chunk
61
+ end
62
+ end
63
+
64
+ resp = String.new
65
+ data1 = @s3.get(@bucket, @key1) do |chunk|
66
+ resp += chunk
67
+ end
68
+ assert_equal RIGHT_OBJECT_TEXT, resp, "Object text must be equal to '#{RIGHT_OBJECT_TEXT}'"
69
+ assert_equal @s3.get_object(@bucket, @key1), resp, "Streaming iface must return same as non-streaming"
70
+ assert_equal 'Woohoo1!', data1[:headers]['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
71
+ end
72
+
73
+ def test_08_keys
74
+ keys = @s3.list_bucket(@bucket).map{|b| b[:key]}
75
+ assert_equal keys.size, 3, "There should be 3 keys"
76
+ assert(keys.include?(@key1))
77
+ assert(keys.include?(@key2))
78
+ assert(keys.include?(@key3))
79
+ end
80
+
81
+ def test_09_copy_key
82
+ #--- test COPY
83
+ # copy a key
84
+ assert @s3.copy(@bucket, @key1, @bucket, @key1_copy)
85
+ # check it was copied well
86
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key1_copy), "copied object must have the same data"
87
+ # check meta-headers were copied
88
+ headers = @s3.head(@bucket, @key1_copy)
89
+ assert_equal 'Woohoo1!', headers['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
90
+ #--- test REPLACE
91
+ assert @s3.copy(@bucket, @key1, @bucket, @key1_copy, :replace, 'x-amz-meta-family' => 'oooops!')
92
+ # check it was copied well
93
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key1_copy), "copied object must have the same data"
94
+ # check meta-headers were overwrittenn
95
+ headers = @s3.head(@bucket, @key1_copy)
96
+ assert_equal 'oooops!', headers['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'oooops!'"
97
+ end
98
+
99
+ def test_10_move_key
100
+ # move a key
101
+ assert @s3.move(@bucket, @key1, @bucket, @key1_new_name)
102
+ # check it's data was moved correctly
103
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key1_new_name), "moved object must have the same data"
104
+ # check meta-headers were moved
105
+ headers = @s3.head(@bucket, @key1_new_name)
106
+ assert_equal 'Woohoo1!', headers['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
107
+ # check the original key is not exists any more
108
+ keys = @s3.list_bucket(@bucket).map{|b| b[:key]}
109
+ assert(!keys.include?(@key1))
110
+ end
111
+
112
+ def test_11_rename_key
113
+ # rename a key
114
+ assert @s3.rename(@bucket, @key2, @key2_new_name)
115
+ # check the new key data
116
+ assert_equal RIGHT_OBJECT_TEXT, @s3.get_object(@bucket, @key2_new_name), "moved object must have the same data"
117
+ # check meta-headers
118
+ headers = @s3.head(@bucket, @key2_new_name)
119
+ assert_equal 'Woohoo2!', headers['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo2!'"
120
+ # check the original key is not exists any more
121
+ keys = @s3.list_bucket(@bucket).map{|b| b[:key]}
122
+ assert(!keys.include?(@key2))
123
+ end
124
+ def test_12_retrieve_object
125
+ assert_raise(Rightscale::AwsError) { @s3.retrieve_object(:bucket => @bucket, :key => 'undefined/key') }
126
+ data1 = @s3.retrieve_object(:bucket => @bucket, :key => @key1_new_name)
127
+ assert_equal RIGHT_OBJECT_TEXT, data1[:object], "Object text must be equal to '#{RIGHT_OBJECT_TEXT}'"
128
+ assert_equal 'Woohoo1!', data1[:headers]['x-amz-meta-family'], "x-amz-meta-family header must be equal to 'Woohoo1!'"
129
+ end
130
+ def test_13_delete_folder
131
+ assert_equal 1, @s3.delete_folder(@bucket, 'test').size, "Only one key(#{@key1}) must be deleted!"
132
+ end
133
+
134
+ def test_14_delete_bucket
135
+ assert_raise(Rightscale::AwsError) { @s3.delete_bucket(@bucket) }
136
+ assert @s3.clear_bucket(@bucket), 'Clear_bucket fail'
137
+ assert_equal 0, @s3.list_bucket(@bucket).size, 'Bucket must be empty'
138
+ assert @s3.delete_bucket(@bucket)
139
+ assert !@s3.list_all_my_buckets.map{|bucket| bucket[:name]}.include?(@bucket), "#{@bucket} must not exist"
140
+ end
141
+
142
+
143
+
144
+
145
+ #---------------------------
146
+ # Rightscale::S3 classes
147
+ #---------------------------
148
+
149
+ def test_20_s3
150
+ # create bucket
151
+ bucket = @s.bucket(@bucket, true)
152
+ assert bucket
153
+ # check that the bucket exists
154
+ assert @s.buckets.map{|b| b.name}.include?(@bucket)
155
+ # delete bucket
156
+ assert bucket.clear
157
+ assert bucket.delete
158
+ end
159
+
160
+ def test_21_bucket_create_put_get_key
161
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, true)
162
+ # check that the bucket exists
163
+ assert @s.buckets.map{|b| b.name}.include?(@bucket)
164
+ assert bucket.keys.empty?
165
+ # put data
166
+ assert bucket.put(@key3, RIGHT_OBJECT_TEXT, {'family'=>'123456'})
167
+ # get data and compare
168
+ assert_equal RIGHT_OBJECT_TEXT, bucket.get(@key3)
169
+ # get key object
170
+ key = bucket.key(@key3, true)
171
+ assert_equal Rightscale::S3::Key, key.class
172
+ assert key.exists?
173
+ assert_equal '123456', key.meta_headers['family']
174
+ end
175
+
176
+ def test_22_keys
177
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, false)
178
+ # create first key
179
+ key3 = Rightscale::S3::Key.create(bucket, @key3)
180
+ key3.refresh
181
+ assert key3.exists?
182
+ assert_equal '123456', key3.meta_headers['family']
183
+ # create second key
184
+ key2 = Rightscale::S3::Key.create(bucket, @key2)
185
+ assert !key2.refresh
186
+ assert !key2.exists?
187
+ assert_raise(Rightscale::AwsError) { key2.head }
188
+ # store key
189
+ key2.meta_headers = {'family'=>'111222333'}
190
+ assert key2.put(RIGHT_OBJECT_TEXT)
191
+ # make sure that the key exists
192
+ assert key2.refresh
193
+ assert key2.exists?
194
+ assert key2.head
195
+ # get its data
196
+ assert_equal RIGHT_OBJECT_TEXT, key2.get
197
+ # drop key
198
+ assert key2.delete
199
+ assert !key2.exists?
200
+ end
201
+
202
+ def test_23_rename_key
203
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, false)
204
+ # -- 1 -- (key based rename)
205
+ # create a key
206
+ key = bucket.key('test/copy/1')
207
+ key.put(RIGHT_OBJECT_TEXT)
208
+ original_key = key.clone
209
+ assert key.exists?, "'test/copy/1' should exist"
210
+ # rename it
211
+ key.rename('test/copy/2')
212
+ assert_equal 'test/copy/2', key.name
213
+ assert key.exists?, "'test/copy/2' should exist"
214
+ # the original key should not exist
215
+ assert !original_key.exists?, "'test/copy/1' should not exist"
216
+ # -- 2 -- (bucket based rename)
217
+ bucket.rename_key('test/copy/2', 'test/copy/3')
218
+ assert bucket.key('test/copy/3').exists?, "'test/copy/3' should exist"
219
+ assert !bucket.key('test/copy/2').exists?, "'test/copy/2' should not exist"
220
+ end
221
+
222
+ def test_24_copy_key
223
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, false)
224
+ # -- 1 -- (key based copy)
225
+ # create a key
226
+ key = bucket.key('test/copy/10')
227
+ key.put(RIGHT_OBJECT_TEXT)
228
+ # make copy
229
+ new_key = key.copy('test/copy/11')
230
+ # make sure both the keys exist and have a correct data
231
+ assert key.exists?, "'test/copy/10' should exist"
232
+ assert new_key.exists?, "'test/copy/11' should exist"
233
+ assert_equal RIGHT_OBJECT_TEXT, key.get
234
+ assert_equal RIGHT_OBJECT_TEXT, new_key.get
235
+ # -- 2 -- (bucket based copy)
236
+ bucket.copy_key('test/copy/11', 'test/copy/12')
237
+ assert bucket.key('test/copy/11').exists?, "'test/copy/11' should exist"
238
+ assert bucket.key('test/copy/12').exists?, "'test/copy/12' should exist"
239
+ assert_equal RIGHT_OBJECT_TEXT, bucket.key('test/copy/11').get
240
+ assert_equal RIGHT_OBJECT_TEXT, bucket.key('test/copy/12').get
241
+ end
242
+
243
+ def test_25_move_key
244
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, false)
245
+ # -- 1 -- (key based copy)
246
+ # create a key
247
+ key = bucket.key('test/copy/20')
248
+ key.put(RIGHT_OBJECT_TEXT)
249
+ # move
250
+ new_key = key.move('test/copy/21')
251
+ # make sure both the keys exist and have a correct data
252
+ assert !key.exists?, "'test/copy/20' should not exist"
253
+ assert new_key.exists?, "'test/copy/21' should exist"
254
+ assert_equal RIGHT_OBJECT_TEXT, new_key.get
255
+ # -- 2 -- (bucket based copy)
256
+ bucket.copy_key('test/copy/21', 'test/copy/22')
257
+ assert bucket.key('test/copy/21').exists?, "'test/copy/21' should not exist"
258
+ assert bucket.key('test/copy/22').exists?, "'test/copy/22' should exist"
259
+ assert_equal RIGHT_OBJECT_TEXT, bucket.key('test/copy/22').get
260
+ end
261
+
262
+ def test_26_save_meta
263
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, false)
264
+ # create a key
265
+ key = bucket.key('test/copy/30')
266
+ key.put(RIGHT_OBJECT_TEXT)
267
+ assert key.meta_headers.blank?
268
+ # store some meta keys
269
+ meta = {'family' => 'oops','race' => 'troll'}
270
+ assert_equal meta, key.save_meta(meta)
271
+ # reload meta
272
+ assert_equal meta, key.reload_meta
273
+ end
274
+
275
+ def test_27_clear_delete
276
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, false)
277
+ # add another key
278
+ bucket.put(@key2, RIGHT_OBJECT_TEXT)
279
+ # delete 'folder'
280
+ assert_equal 1, bucket.delete_folder(@key1).size
281
+ # delete
282
+ assert_raise(Rightscale::AwsError) { bucket.delete }
283
+ bucket.delete(true)
284
+ end
285
+
286
+ # Grantees
287
+
288
+ def test_30_create_bucket
289
+ bucket = @s.bucket(@bucket, true, 'public-read')
290
+ assert bucket
291
+ end
292
+
293
+ def test_31_list_grantees
294
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, false)
295
+ # get grantees list
296
+ grantees = bucket.grantees
297
+ # check that the grantees count equal to 2 (root, AllUsers)
298
+ assert_equal 2, grantees.size
299
+ end
300
+
301
+ def test_32_grant_revoke_drop
302
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, false)
303
+ # Take 'AllUsers' grantee
304
+ grantee = Rightscale::S3::Grantee.new(bucket,'http://acs.amazonaws.com/groups/global/AllUsers')
305
+ # Check exists?
306
+ assert grantee.exists?
307
+ # Add grant as String
308
+ assert grantee.grant('WRITE')
309
+ # Add grants as Array
310
+ assert grantee.grant(['READ_ACP', 'WRITE_ACP'])
311
+ # Check perms count
312
+ assert_equal 4, grantee.perms.size
313
+ # revoke 'WRITE_ACP'
314
+ assert grantee.revoke('WRITE_ACP')
315
+ # Check manual perm removal method
316
+ grantee.perms -= ['READ_ACP']
317
+ grantee.apply
318
+ assert_equal 2, grantee.perms.size
319
+ # Check grantee removal if it has no permissions
320
+ assert grantee.perms = []
321
+ assert grantee.apply
322
+ assert !grantee.exists?
323
+ # Check multiple perms assignment
324
+ assert grantee.grant('FULL_CONTROL', 'READ', 'WRITE')
325
+ assert_equal ['FULL_CONTROL','READ','WRITE'].sort, grantee.perms.sort
326
+ # Check multiple perms removal
327
+ assert grantee.revoke('FULL_CONTROL', 'WRITE')
328
+ assert_equal ['READ'], grantee.perms
329
+ # check 'Drop' method
330
+ assert grantee.drop
331
+ assert !grantee.exists?
332
+ assert_equal 1, bucket.grantees.size
333
+ # Delete bucket
334
+ bucket.delete(true)
335
+ end
336
+
337
+ def test_33_key_grantees
338
+ # Create bucket
339
+ bucket = @s.bucket(@bucket, true)
340
+ # Create key
341
+ key = bucket.key(@key1)
342
+ assert key.put(RIGHT_OBJECT_TEXT, 'public-read')
343
+ # Get grantees list (must be == 2)
344
+ grantees = key.grantees
345
+ assert grantees
346
+ assert_equal 2, grantees.size
347
+ # Take one of grantees and give him 'Write' perms
348
+ grantee = grantees[0]
349
+ assert grantee.grant('WRITE')
350
+ # Drop grantee
351
+ assert grantee.drop
352
+ # Drop bucket
353
+ bucket.delete(true)
354
+ end
355
+
356
+ def test_34_bucket_create_put_with_perms
357
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, true)
358
+ # check that the bucket exists
359
+ assert @s.buckets.map{|b| b.name}.include?(@bucket)
360
+ assert bucket.keys.empty?
361
+ # put data (with canned ACL)
362
+ assert bucket.put(@key1, RIGHT_OBJECT_TEXT, {'family'=>'123456'}, "public-read")
363
+ # get data and compare
364
+ assert_equal RIGHT_OBJECT_TEXT, bucket.get(@key1)
365
+ # get key object
366
+ key = bucket.key(@key1, true)
367
+ assert_equal Rightscale::S3::Key, key.class
368
+ assert key.exists?
369
+ assert_equal '123456', key.meta_headers['family']
370
+ end
371
+
372
+ def test_35_key_put_with_perms
373
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, false)
374
+ # create first key
375
+ key1 = Rightscale::S3::Key.create(bucket, @key1)
376
+ key1.refresh
377
+ assert key1.exists?
378
+ assert key1.put(RIGHT_OBJECT_TEXT, "public-read")
379
+ # get its data
380
+ assert_equal RIGHT_OBJECT_TEXT, key1.get
381
+ # drop key
382
+ assert key1.delete
383
+ assert !key1.exists?
384
+ end
385
+
386
+ def test_36_set_amazon_problems
387
+ original_problems = RightAws::S3Interface.amazon_problems
388
+ assert(original_problems.length > 0)
389
+ RightAws::S3Interface.amazon_problems= original_problems << "A New Problem"
390
+ new_problems = RightAws::S3Interface.amazon_problems
391
+ assert_equal(new_problems, original_problems)
392
+
393
+ RightAws::S3Interface.amazon_problems= nil
394
+ assert_nil(RightAws::S3Interface.amazon_problems)
395
+ end
396
+
397
+ def test_37_access_logging
398
+ bucket = Rightscale::S3::Bucket.create(@s, @bucket, false)
399
+ targetbucket = Rightscale::S3::Bucket.create(@s, @bucket2, true)
400
+ # Take 'AllUsers' grantee
401
+ grantee = Rightscale::S3::Grantee.new(targetbucket,'http://acs.amazonaws.com/groups/s3/LogDelivery')
402
+
403
+ assert grantee.grant(['READ_ACP', 'WRITE'])
404
+
405
+ assert bucket.enable_logging(:targetbucket => targetbucket, :targetprefix => "loggylogs/")
406
+
407
+ assert_equal(bucket.logging_info, {:enabled => true, :targetbucket => @bucket2, :targetprefix => "loggylogs/"})
408
+
409
+ assert bucket.disable_logging
410
+
411
+ # check 'Drop' method
412
+ assert grantee.drop
413
+
414
+ # Delete bucket
415
+ bucket.delete(true)
416
+ targetbucket.delete(true)
417
+ end
418
+
419
+ end