cloudfiles 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,198 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CloudfilesContainerTest < Test::Unit::TestCase
4
+
5
+ def test_object_creation
6
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
7
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5'}
8
+ response.stubs(:code).returns('204')
9
+ connection.stubs(:cfreq => response)
10
+ @container = CloudFiles::Container.new(connection, 'test_container')
11
+ assert_equal @container.name, 'test_container'
12
+ assert_equal @container.class, CloudFiles::Container
13
+ assert_equal @container.public?, false
14
+ assert_equal @container.cdn_url, false
15
+ assert_equal @container.cdn_ttl, false
16
+ end
17
+
18
+ def test_object_creation_no_such_container
19
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
20
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5'}
21
+ response.stubs(:code).returns('999')
22
+ connection.stubs(:cfreq => response)
23
+ assert_raise(NoSuchContainerException) do
24
+ @container = CloudFiles::Container.new(connection, 'test_container')
25
+ end
26
+ end
27
+
28
+ def test_object_creation_with_cdn
29
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
30
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'x-cdn-enabled' => 'True', 'x-cdn-uri' => 'http://cdn.test.example/container', 'x-ttl' => '86400'}
31
+ response.stubs(:code).returns('204')
32
+ connection.stubs(:cfreq => response)
33
+ @container = CloudFiles::Container.new(connection, 'test_container')
34
+ assert_equal @container.name, 'test_container'
35
+ assert_equal @container.cdn_enabled, true
36
+ assert_equal @container.public?, true
37
+ assert_equal @container.cdn_url, 'http://cdn.test.example/container'
38
+ assert_equal @container.cdn_ttl, '86400'
39
+ end
40
+
41
+ def test_to_s
42
+ build_net_http_object
43
+ assert_equal @container.to_s, 'test_container'
44
+ end
45
+
46
+ def test_make_private_succeeds
47
+ build_net_http_object(:code => '201')
48
+ assert_nothing_raised do
49
+ @container.make_private
50
+ end
51
+ end
52
+
53
+ def test_make_private_fails
54
+ build_net_http_object(:code => '999')
55
+ assert_raises(NoSuchContainerException) do
56
+ @container.make_private
57
+ end
58
+ end
59
+
60
+ def test_make_public_succeeds
61
+ build_net_http_object(:code => '201')
62
+ assert_nothing_raised do
63
+ @container.make_public
64
+ end
65
+ end
66
+
67
+ def test_make_public_fails
68
+ build_net_http_object(:code => '999')
69
+ assert_raises(NoSuchContainerException) do
70
+ @container.make_public
71
+ end
72
+ end
73
+
74
+ def test_empty_is_false
75
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
76
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5'}
77
+ response.stubs(:code).returns('204')
78
+ connection.stubs(:cfreq => response)
79
+ @container = CloudFiles::Container.new(connection, 'test_container')
80
+ assert_equal @container.empty?, false
81
+ end
82
+
83
+ def test_empty_is_true
84
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
85
+ response = {'x-container-bytes-used' => '0', 'x-container-object-count' => '0'}
86
+ response.stubs(:code).returns('204')
87
+ connection.stubs(:cfreq => response)
88
+ @container = CloudFiles::Container.new(connection, 'test_container')
89
+ assert_equal @container.empty?, true
90
+ end
91
+
92
+ def test_log_retention_is_true
93
+ connection = mock(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
94
+ response = {'x-container-bytes-used' => '0', 'x-container-object-count' => '0', 'x-cdn-enabled' => 'True', 'x-log-retention' => 'True'}
95
+ response.stubs(:code).returns('204')
96
+ connection.stubs(:cfreq => response)
97
+ @container = CloudFiles::Container.new(connection, 'test_container')
98
+ assert_equal @container.log_retention?, true
99
+ end
100
+
101
+ def test_object_fetch
102
+ build_net_http_object(:code => '204', :response => {'last-modified' => 'Wed, 28 Jan 2009 16:16:26 GMT'})
103
+ object = @container.object('good_object')
104
+ assert_equal object.class, CloudFiles::StorageObject
105
+ end
106
+
107
+ def test_create_object
108
+ build_net_http_object()
109
+ object = @container.create_object('new_object')
110
+ assert_equal object.class, CloudFiles::StorageObject
111
+ end
112
+
113
+ def test_object_exists_true
114
+ build_net_http_object
115
+ assert_equal @container.object_exists?('good_object'), true
116
+ end
117
+
118
+ def test_object_exists_false
119
+ build_net_http_object(:code => '999')
120
+ assert_equal @container.object_exists?('bad_object'), false
121
+ end
122
+
123
+ def test_delete_object_succeeds
124
+ build_net_http_object
125
+ assert_nothing_raised do
126
+ @container.delete_object('good_object')
127
+ end
128
+ end
129
+
130
+ def test_delete_invalid_object_fails
131
+ build_net_http_object(:code => '404')
132
+ assert_raise(NoSuchObjectException) do
133
+ @container.delete_object('nonexistent_object')
134
+ end
135
+ end
136
+
137
+ def test_delete_invalid_response_code_fails
138
+ build_net_http_object(:code => '999')
139
+ assert_raise(InvalidResponseException) do
140
+ @container.delete_object('broken_object')
141
+ end
142
+ end
143
+
144
+ def test_fetch_objects
145
+ build_net_http_object(:code => '200', :body => "foo\nbar\nbaz")
146
+ objects = @container.objects
147
+ assert_equal objects.class, Array
148
+ assert_equal objects.size, 3
149
+ assert_equal objects.first, 'foo'
150
+ end
151
+
152
+ def test_fetch_object_detail
153
+ body = %{<?xml version="1.0" encoding="UTF-8"?>
154
+ <container name="video">
155
+ <object><name>kisscam.mov</name><hash>96efd5a0d78b74cfe2a911c479b98ddd</hash><bytes>9196332</bytes><content_type>video/quicktime</content_type><last_modified>2008-12-18T10:34:43.867648</last_modified></object>
156
+ <object><name>penaltybox.mov</name><hash>d2a4c0c24d8a7b4e935bee23080e0685</hash><bytes>24944966</bytes><content_type>video/quicktime</content_type><last_modified>2008-12-18T10:35:19.273927</last_modified></object>
157
+ </container>
158
+ }
159
+ build_net_http_object(:code => '200', :body => body)
160
+ details = @container.objects_detail
161
+ assert_equal details.size, 2
162
+ assert_equal details['kisscam.mov'][:bytes], '9196332'
163
+ end
164
+
165
+ def test_fetch_object_detail_empty
166
+ build_net_http_object
167
+ details = @container.objects_detail
168
+ assert_equal details, {}
169
+ end
170
+
171
+ def test_fetch_object_detail_error
172
+ build_net_http_object(:code => '999')
173
+ assert_raise(InvalidResponseException) do
174
+ details = @container.objects_detail
175
+ end
176
+ end
177
+
178
+ def test_setting_log_retention
179
+ build_net_http_object(:code => '201')
180
+ assert(@container.log_retention='false')
181
+ end
182
+
183
+ private
184
+
185
+ def build_net_http_object(args={:code => '204' })
186
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
187
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
188
+ args[:response] = {} unless args[:response]
189
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token', 'last-modified' => Time.now.to_s}.merge(args[:response])
190
+ response.stubs(:code).returns(args[:code])
191
+ response.stubs(:body).returns args[:body] || nil
192
+ connection.stubs(:cfreq => response)
193
+ @container = CloudFiles::Container.new(connection, 'test_container')
194
+ @container.stubs(:connection).returns(connection)
195
+ end
196
+
197
+
198
+ end
@@ -0,0 +1,209 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CloudfilesStorageObjectTest < Test::Unit::TestCase
4
+
5
+ def test_object_creation
6
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
7
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'last-modified' => Time.now.to_s}
8
+ response.stubs(:code).returns('204')
9
+ connection.stubs(:cfreq => response)
10
+ container = CloudFiles::Container.new(connection, 'test_container')
11
+ @object = CloudFiles::StorageObject.new(container, 'test_object')
12
+ assert_equal @object.name, 'test_object'
13
+ assert_equal @object.class, CloudFiles::StorageObject
14
+ assert_equal @object.to_s, 'test_object'
15
+ end
16
+
17
+ def test_object_creation_with_invalid_name
18
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
19
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'last-modified' => Time.now.to_s}
20
+ response.stubs(:code).returns('204')
21
+ connection.stubs(:cfreq => response)
22
+ container = CloudFiles::Container.new(connection, 'test_container')
23
+ assert_raises SyntaxException do
24
+ @object = CloudFiles::StorageObject.new(container, 'test_object?')
25
+ end
26
+ end
27
+
28
+
29
+ def test_public_url_exists
30
+ build_net_http_object(:public => true, :name => 'test object')
31
+ assert_equal @object.public_url, "http://cdn.test.example/test%20object"
32
+ end
33
+
34
+ def test_public_url_does_not_exist
35
+ build_net_http_object
36
+ assert_equal @object.public_url, nil
37
+ end
38
+
39
+ def test_data_succeeds
40
+ build_net_http_object(:code => '200', :body => 'This is good data')
41
+ assert_equal @object.data, 'This is good data'
42
+ end
43
+
44
+ def test_data_with_offset_succeeds
45
+ build_net_http_object(:code => '200', :body => 'Thi')
46
+ assert_equal @object.data(3), 'Thi'
47
+ end
48
+
49
+ def test_data_fails
50
+ build_net_http_object(:code => '999', :body => 'This is bad data')
51
+ assert_raise(NoSuchObjectException) do
52
+ @object.data
53
+ end
54
+ end
55
+
56
+ def test_data_stream_succeeds
57
+ build_net_http_object(:code => '200', :body => 'This is good data')
58
+ data = ""
59
+ assert_nothing_raised do
60
+ @object.data_stream { |chunk|
61
+ data += chunk
62
+ }
63
+ end
64
+ end
65
+
66
+ def test_data_stream_with_offset_succeeds
67
+ build_net_http_object(:code => '200', :body => 'This ')
68
+ data = ""
69
+ assert_nothing_raised do
70
+ @object.data_stream(5) { |chunk|
71
+ data += chunk
72
+ }
73
+ end
74
+ end
75
+
76
+ # Need to find a way to simulate this properly
77
+ def data_stream_fails
78
+ build_net_http_object(:code => '999', :body => 'This is bad data')
79
+ data = ""
80
+ assert_raise(NoSuchObjectException) do
81
+ @object.data_stream { |chunk|
82
+ data += chunk
83
+ }
84
+ end
85
+ end
86
+
87
+ def test_set_metadata_succeeds
88
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
89
+ build_net_http_object(:code => '202')
90
+ assert_nothing_raised do
91
+ @object.set_metadata({'Foo' =>'bar'})
92
+ end
93
+ end
94
+
95
+ def test_set_metadata_invalid_object
96
+ build_net_http_object(:code => '404')
97
+ assert_raise(NoSuchObjectException) do
98
+ @object.set_metadata({'Foo' =>'bar'})
99
+ end
100
+ end
101
+
102
+ def test_set_metadata_fails
103
+ build_net_http_object(:code => '999')
104
+ assert_raise(InvalidResponseException) do
105
+ @object.set_metadata({'Foo' =>'bar'})
106
+ end
107
+ end
108
+
109
+ def test_read_metadata_succeeds
110
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
111
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'x-object-meta-foo' => 'Bar', 'last-modified' => Time.now.to_s}
112
+ response.stubs(:code).returns('204')
113
+ connection.stubs(:cfreq => response)
114
+ container = CloudFiles::Container.new(connection, 'test_container')
115
+ @object = CloudFiles::StorageObject.new(container, 'test_object')
116
+ assert_equal @object.metadata, {'foo' => 'Bar'}
117
+ end
118
+
119
+ def test_write_succeeds
120
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
121
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
122
+ build_net_http_object(:code => '201')
123
+ assert_nothing_raised do
124
+ @object.write("This is test data")
125
+ end
126
+ end
127
+
128
+ def test_write_with_make_path
129
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
130
+ response = {'x-container-bytes-used' => '42', 'x-container-object-count' => '5', 'last-modified' => Time.now.to_s}
131
+ response.stubs(:code).returns('204').then.returns('204').then.returns('201').then.returns('204')
132
+ connection.stubs(:cfreq => response)
133
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
134
+ container = CloudFiles::Container.new(connection, 'test_container')
135
+ @object = CloudFiles::StorageObject.new(container, 'path/to/my/test_object', false, true)
136
+ assert_nothing_raised do
137
+ @object.write("This is path test data")
138
+ end
139
+ end
140
+
141
+ def test_load_from_filename_succeeds
142
+ require 'tempfile'
143
+ out = Tempfile.new('test')
144
+ out.write("This is test data")
145
+ out.close
146
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
147
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
148
+ build_net_http_object(:code => '201')
149
+ assert_nothing_raised do
150
+ @object.load_from_filename(out.path)
151
+ end
152
+ end
153
+
154
+ def test_write_sets_mime_type
155
+ CloudFiles::StorageObject.any_instance.stubs(:populate).returns(true)
156
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
157
+ build_net_http_object(:name => 'myfile.xml', :code => '201')
158
+ assert_nothing_raised do
159
+ @object.write("This is test data")
160
+ end
161
+ end
162
+
163
+ def test_write_with_no_data_dies
164
+ build_net_http_object
165
+ assert_raise(SyntaxException) do
166
+ @object.write
167
+ end
168
+ end
169
+
170
+ def test_write_with_invalid_content_length_dies
171
+ build_net_http_object(:code => '412')
172
+ assert_raise(InvalidResponseException) do
173
+ @object.write('Test Data')
174
+ end
175
+ end
176
+
177
+ def test_write_with_mismatched_md5_dies
178
+ build_net_http_object(:code => '422')
179
+ assert_raise(MisMatchedChecksumException) do
180
+ @object.write('Test Data')
181
+ end
182
+ end
183
+
184
+ def test_write_with_invalid_response_dies
185
+ build_net_http_object(:code => '999')
186
+ assert_raise(InvalidResponseException) do
187
+ @object.write('Test Data')
188
+ end
189
+ end
190
+
191
+ private
192
+
193
+ def build_net_http_object(args={:code => '204' })
194
+ CloudFiles::Container.any_instance.stubs(:populate).returns(true)
195
+ connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdnmgmthost => 'cdm.test.example', :cdnmgmtpath => '/dummy/path', :cdnmgmtport => 443, :cdnmgmtscheme => 'https')
196
+ args[:response] = {} unless args[:response]
197
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token', 'last-modified' => Time.now.to_s}.merge(args[:response])
198
+ response.stubs(:code).returns(args[:code])
199
+ response.stubs(:body).returns args[:body] || nil
200
+ connection.stubs(:cfreq => response)
201
+ container = CloudFiles::Container.new(connection, 'test_container')
202
+ container.stubs(:connection).returns(connection)
203
+ container.stubs(:public?).returns(args[:public] || false)
204
+ container.stubs(:cdn_url).returns('http://cdn.test.example')
205
+ @object = CloudFiles::StorageObject.new(container, args[:name] || 'test_object')
206
+ end
207
+
208
+
209
+ end
@@ -0,0 +1,5 @@
1
+ require 'test/unit'
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'cloudfiles'
4
+ require 'rubygems'
5
+ require 'mocha'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudfiles
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.0
5
+ platform: ruby
6
+ authors:
7
+ - H. Wade Minter
8
+ - Rackspace Hosting
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-10-08 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: A Ruby version of the Rackspace Cloud Files API.
18
+ email: wade.minter@rackspace.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.rdoc
25
+ files:
26
+ - .gitignore
27
+ - COPYING
28
+ - Manifest
29
+ - README.rdoc
30
+ - Rakefile
31
+ - TODO
32
+ - VERSION
33
+ - cloudfiles.gemspec
34
+ - lib/cloudfiles.rb
35
+ - lib/cloudfiles/authentication.rb
36
+ - lib/cloudfiles/connection.rb
37
+ - lib/cloudfiles/container.rb
38
+ - lib/cloudfiles/storage_object.rb
39
+ - test/cf-testunit.rb
40
+ - test/cloudfiles_authentication_test.rb
41
+ - test/cloudfiles_connection_test.rb
42
+ - test/cloudfiles_container_test.rb
43
+ - test/cloudfiles_storage_object_test.rb
44
+ - test/test_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://www.rackspacecloud.com/cloud_hosting_products/files
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A Ruby API into Rackspace Cloud Files
73
+ test_files:
74
+ - test/cf-testunit.rb
75
+ - test/cloudfiles_authentication_test.rb
76
+ - test/cloudfiles_connection_test.rb
77
+ - test/cloudfiles_container_test.rb
78
+ - test/cloudfiles_storage_object_test.rb
79
+ - test/test_helper.rb