cloudfiles 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,157 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Major's live tests that go against the real Cloud Files system. Requires a valid username and API key to function.
4
+
5
+ require File.dirname(__FILE__) + '/../lib/cloudfiles'
6
+
7
+ username = "XXXXX"
8
+ apikey = "XXXXX"
9
+
10
+ def assert_test(testtext,bool)
11
+ booltext = (bool)? " PASS" : "*FAIL*" ;
12
+ (testtext+"... ").ljust(50)+booltext
13
+ end
14
+
15
+ filename = File.dirname(__FILE__) + '/../lib/cloudfiles.rb'
16
+
17
+ # Test initial connection
18
+ cf = CloudFiles::Connection.new(username,apikey)
19
+ puts assert_test("Connecting to CloudFiles",cf.class == CloudFiles::Connection)
20
+
21
+ # Test container creation
22
+ testingcontainer = "CloudFiles Ruby API Testing Container"
23
+ cntnr = cf.create_container(testingcontainer)
24
+ puts assert_test("Creating test container",cntnr.class == CloudFiles::Container)
25
+
26
+ # Checking container size
27
+ bytes = cntnr.bytes
28
+ puts assert_test(" Checking container size",bytes == 0)
29
+
30
+ # Checking container count
31
+ count = cntnr.count
32
+ puts assert_test(" Checking container count",count == 0)
33
+
34
+ # Add a file to the container - standard method
35
+ cloudfilesfilesize = File.read(filename).length
36
+ cloudfilesmd5 = Digest::MD5.hexdigest(File.read(filename))
37
+ headers = { "ETag" => cloudfilesmd5, "Content-Type" => "text/ruby", "X-Object-Meta-Testmeta" => "value" }
38
+ myobj = cntnr.create_object("cloudfiles-standard.rb")
39
+ myobj.write(File.read(filename), headers)
40
+ puts assert_test(" Uploading object (read into memory)",myobj.class == CloudFiles::StorageObject)
41
+ cntnr.refresh
42
+
43
+ # Check if object exists
44
+ bool = cntnr.object_exists?("cloudfiles-standard.rb")
45
+ puts assert_test(" Checking for object existence",bool)
46
+
47
+ # Checking container size
48
+ bytes = cntnr.bytes
49
+ puts assert_test(" Checking container size with #{bytes} vs #{cloudfilesfilesize}",bytes == cloudfilesfilesize)
50
+
51
+ # Checking container count
52
+ count = cntnr.count
53
+ puts assert_test(" Checking container count",count == 1)
54
+
55
+ # Add a file to the container - stream method
56
+ headers = { "ETag" => cloudfilesmd5, "Content-Type" => "text/ruby", "X-Object-Meta-Testmeta" => "value" }
57
+ f = IO.read(filename)
58
+ myobj = cntnr.create_object("cloudfiles-stream.rb")
59
+ myobj.write(File.read(filename), headers)
60
+ puts assert_test(" Uploading object (read from stream)",myobj.class == CloudFiles::StorageObject)
61
+ cntnr.refresh
62
+
63
+ # Check if object exists
64
+ bool = cntnr.object_exists?("cloudfiles-stream.rb")
65
+ puts assert_test(" Checking for object existence",bool)
66
+
67
+ # Checking container size
68
+ bytes = cntnr.bytes
69
+ puts assert_test(" Checking container size",bytes == (cloudfilesfilesize*2))
70
+
71
+ # Checking container count
72
+ count = cntnr.count
73
+ puts assert_test(" Checking container count",count == 2)
74
+
75
+ # Check file size
76
+ bytes = myobj.bytes.to_i
77
+ puts assert_test(" Checking object size",bytes == cloudfilesfilesize)
78
+
79
+ # Check content type
80
+ content_type = myobj.content_type
81
+ puts assert_test(" Checking object content type",content_type == "text/ruby")
82
+
83
+ # Check metadata
84
+ metadata = myobj.metadata
85
+ puts assert_test(" Checking object metadata",metadata["testmeta"] == "value")
86
+
87
+ # Set new metadata
88
+ bool = myobj.set_metadata({ "testmeta2" => "differentvalue"})
89
+ puts assert_test(" Setting new object metadata",bool)
90
+
91
+ # Check new metadata
92
+ myobj.refresh
93
+ metadata = myobj.metadata
94
+ puts assert_test(" Checking new object metadata",metadata["testmeta2"] == "differentvalue")
95
+
96
+ # Get data via standard method
97
+ data = myobj.data
98
+ puts assert_test(" Retrieving object data (read into memory)",Digest::MD5.hexdigest(data) == cloudfilesmd5)
99
+
100
+ # Get data via stream
101
+ data = ""
102
+ myobj.data_stream { |chunk|
103
+ data += chunk.to_s
104
+ }
105
+ puts assert_test(" Retrieving object data (read from stream)",Digest::MD5.hexdigest(data) == cloudfilesmd5)
106
+
107
+ # Check md5sum
108
+ etag = myobj.etag
109
+ puts assert_test(" Checking object's md5sum",etag == cloudfilesmd5)
110
+
111
+ # Make container public
112
+ bool = cntnr.make_public
113
+ puts assert_test(" Making container public",bool)
114
+
115
+ # Verify that container is public
116
+ bool = cntnr.public?
117
+ puts assert_test(" Verifying container is public",bool)
118
+
119
+ # Getting CDN URL
120
+ cdnurl = cntnr.cdn_url
121
+ puts assert_test(" Getting CDN URL",cdnurl)
122
+
123
+ # Setting CDN URL
124
+ bool = cntnr.make_public(7200)
125
+ puts assert_test(" Setting CDN TTL",bool)
126
+
127
+ # Make container private
128
+ bool = cntnr.make_private
129
+ puts assert_test(" Making container private",bool)
130
+
131
+ # Check if container is empty
132
+ bool = cntnr.empty?
133
+ puts assert_test(" Checking if container empty",bool == false)
134
+
135
+ # Remove standard object
136
+ bool = cntnr.delete_object("cloudfiles-standard.rb")
137
+ puts assert_test(" Deleting first object",bool)
138
+
139
+ # Remove stream object
140
+ bool = cntnr.delete_object("cloudfiles-stream.rb")
141
+ puts assert_test(" Deleting second object",bool)
142
+ cntnr.refresh
143
+
144
+ # Check if container is empty
145
+ bool = cntnr.empty?
146
+ puts assert_test(" Checking if container empty",bool)
147
+
148
+ # Remove testing container
149
+ bool = cf.delete_container(testingcontainer)
150
+ puts assert_test("Removing container",bool)
151
+
152
+ # Check to see if container exists
153
+ bool = cf.container_exists?(testingcontainer)
154
+ puts assert_test("Checking container existence",bool == false)
155
+
156
+
157
+
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CloudfilesAuthenticationTest < Test::Unit::TestCase
4
+
5
+
6
+ def test_good_authentication
7
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}
8
+ response.stubs(:code).returns('204')
9
+ server = mock(:use_ssl= => true, :verify_mode= => true, :start => true, :finish => true)
10
+ server.stubs(:get).returns(response)
11
+ Net::HTTP.stubs(:new).returns(server)
12
+ @connection = stub(:authuser => 'dummy_user', :authkey => 'dummy_key', :cdnmgmthost= => true, :cdnmgmtpath= => true, :cdnmgmtport= => true, :cdnmgmtscheme= => true, :storagehost= => true, :storagepath= => true, :storageport= => true, :storagescheme= => true, :authtoken= => true, :authok= => true)
13
+ result = CloudFiles::Authentication.new(@connection)
14
+ assert_equal result.class, CloudFiles::Authentication
15
+ end
16
+
17
+ def test_bad_authentication
18
+ response = mock()
19
+ response.stubs(:code).returns('499')
20
+ server = mock(:use_ssl= => true, :verify_mode= => true, :start => true)
21
+ server.stubs(:get).returns(response)
22
+ Net::HTTP.stubs(:new).returns(server)
23
+ @connection = stub(:authuser => 'bad_user', :authkey => 'bad_key', :authok= => true, :authtoken= => true)
24
+ assert_raises(AuthenticationException) do
25
+ result = CloudFiles::Authentication.new(@connection)
26
+ end
27
+ end
28
+
29
+ def test_bad_hostname
30
+ Net::HTTP.stubs(:new).raises(ConnectionException)
31
+ @connection = stub(:authuser => 'bad_user', :authkey => 'bad_key', :authok= => true, :authtoken= => true)
32
+ assert_raises(ConnectionException) do
33
+ result = CloudFiles::Authentication.new(@connection)
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,279 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CloudfilesConnectionTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ CloudFiles::Authentication.expects(:new).returns(true)
7
+ @connection = CloudFiles::Connection.new('dummy_user', 'dummy_key')
8
+ @connection.storagehost = "test.setup.example"
9
+ @connection.storagepath = "/dummypath/setup"
10
+ @connection.cdnmgmthost = "test.cdn.example"
11
+ @connection.cdnmgmtpath = "/dummycdnpath/setup"
12
+ end
13
+
14
+ def test_initialize
15
+ assert_equal @connection.authuser, 'dummy_user'
16
+ assert_equal @connection.authkey, 'dummy_key'
17
+ end
18
+
19
+ def test_authok
20
+ # This would normally be set in CloudFiles::Authentication
21
+ assert_equal @connection.authok?, false
22
+ @connection.expects(:authok?).returns(true)
23
+ assert_equal @connection.authok?, true
24
+ end
25
+
26
+ def test_cfreq_get
27
+ build_net_http_object
28
+ assert_nothing_raised do
29
+ response = @connection.cfreq("GET", "test.server.example", "/dummypath", "80", "http")
30
+ end
31
+ end
32
+
33
+ def test_cfreq_post
34
+ build_net_http_object
35
+ assert_nothing_raised do
36
+ response = @connection.cfreq("POST", "test.server.example", "/dummypath", "80", "http")
37
+ end
38
+ end
39
+
40
+ def test_cfreq_put
41
+ build_net_http_object
42
+ assert_nothing_raised do
43
+ response = @connection.cfreq("PUT", "test.server.example", "/dummypath", "80", "http")
44
+ end
45
+ end
46
+
47
+ def test_cfreq_delete
48
+ build_net_http_object
49
+ assert_nothing_raised do
50
+ response = @connection.cfreq("DELETE", "test.server.example", "/dummypath", "80", "http")
51
+ end
52
+ end
53
+
54
+ def test_cfreq_with_static_data
55
+ build_net_http_object
56
+ assert_nothing_raised do
57
+ response = @connection.cfreq("PUT", "test.server.example", "/dummypath", "80", "http", {}, "This is string data")
58
+ end
59
+ end
60
+
61
+ def test_cfreq_with_stream_data
62
+ build_net_http_object
63
+ require 'tempfile'
64
+ file = Tempfile.new("test")
65
+ assert_nothing_raised do
66
+ response = @connection.cfreq("PUT", "test.server.example", "/dummypath", "80", "http", {}, file)
67
+ end
68
+ end
69
+
70
+ def test_cfreq_head
71
+ build_net_http_object
72
+ assert_nothing_raised do
73
+ response = @connection.cfreq("HEAD", "test.server.example", "/dummypath", "80", "http")
74
+ end
75
+ end
76
+
77
+ def test_net_http_raises_connection_exception
78
+ Net::HTTP.expects(:new).raises(ConnectionException)
79
+ assert_raises(ConnectionException) do
80
+ response = @connection.cfreq("GET", "test.server.example", "/dummypath", "80", "http")
81
+ end
82
+ end
83
+
84
+ def test_net_http_raises_one_eof_exception
85
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}
86
+ response.stubs(:code).returns('204')
87
+ server = stub(:use_ssl= => true, :verify_mode= => true, :start => true, :finish => true)
88
+ server.stubs(:request).raises(EOFError).then.returns(response)
89
+ Net::HTTP.stubs(:new).returns(server)
90
+ assert_nothing_raised do
91
+ response = @connection.cfreq("GET", "test.server.example", "/dummypath", "443", "https")
92
+ end
93
+ end
94
+
95
+ def test_net_http_raises_one_expired_token
96
+ CloudFiles::Authentication.expects(:new).returns(true)
97
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}
98
+ response.stubs(:code).returns('401').then.returns('204')
99
+ server = stub(:use_ssl= => true, :verify_mode= => true, :start => true)
100
+ server.stubs(:request).returns(response)
101
+ Net::HTTP.stubs(:new).returns(server)
102
+ assert_nothing_raised do
103
+ response = @connection.cfreq("GET", "test.server.example", "/dummypath", "80", "http")
104
+ end
105
+ end
106
+
107
+ def test_net_http_raises_continual_eof_exceptions
108
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}
109
+ response.stubs(:code).returns('204')
110
+ server = stub(:use_ssl= => true, :verify_mode= => true, :start => true)
111
+ server.stubs(:finish).returns(true)
112
+ server.stubs(:request).raises(EOFError)
113
+ Net::HTTP.stubs(:new).returns(server)
114
+ assert_raises(ConnectionException) do
115
+ response = @connection.cfreq("GET", "test.server.example", "/dummypath", "80", "http")
116
+ end
117
+ end
118
+
119
+ def test_get_info
120
+ build_net_http_object(:response => {'x-account-bytes-used' => '9999', 'x-account-container-count' => '5'}, :code => '204')
121
+ @connection.get_info
122
+ assert_equal @connection.bytes, 9999
123
+ assert_equal @connection.count, 5
124
+ end
125
+
126
+ def test_get_info_fails
127
+ build_net_http_object(:response => {'x-account-bytes-used' => '9999', 'x-account-container-count' => '5'}, :code => '999')
128
+ assert_raises(InvalidResponseException) do
129
+ @connection.get_info
130
+ end
131
+ end
132
+
133
+ def test_public_containers
134
+ build_net_http_object(:body => "foo\nbar\nbaz", :code => '200', :response => {})
135
+ public_containers = @connection.public_containers
136
+ assert_equal public_containers.size, 3
137
+ assert_equal public_containers.first, 'foo'
138
+ end
139
+
140
+ def test_public_containers_empty
141
+ build_net_http_object
142
+ public_containers = @connection.public_containers
143
+ assert_equal public_containers.size, 0
144
+ assert_equal public_containers.class, Array
145
+ end
146
+
147
+ def test_public_containers_exception
148
+ build_net_http_object(:code => '999')
149
+ assert_raises(InvalidResponseException) do
150
+ public_containers = @connection.public_containers
151
+ end
152
+ end
153
+
154
+ def test_delete_container
155
+ build_net_http_object
156
+ response = @connection.delete_container("good_container")
157
+ assert_equal response, true
158
+ end
159
+
160
+ def test_delete_nonempty_container
161
+ build_net_http_object(:code => '409')
162
+ assert_raises(NonEmptyContainerException) do
163
+ response = @connection.delete_container("not_empty")
164
+ end
165
+ end
166
+
167
+ def test_delete_unknown_container
168
+ build_net_http_object(:code => '999')
169
+ assert_raises(NoSuchContainerException) do
170
+ response = @connection.delete_container("not_empty")
171
+ end
172
+ end
173
+
174
+ def test_create_container
175
+ CloudFiles::Container.any_instance.stubs(:populate)
176
+ build_net_http_object(:code => '201')
177
+ container = @connection.create_container('good_container')
178
+ assert_equal container.name, 'good_container'
179
+ end
180
+
181
+ def test_create_container_with_invalid_name
182
+ CloudFiles::Container.stubs(:new)
183
+ assert_raise(SyntaxException) do
184
+ container = @connection.create_container('a'*300)
185
+ end
186
+ end
187
+
188
+ def test_create_container_name_filter
189
+ CloudFiles::Container.any_instance.stubs(:populate)
190
+ build_net_http_object(:code => '201')
191
+ assert_raises(SyntaxException) do
192
+ container = @connection.create_container('this/has/bad?characters')
193
+ end
194
+ end
195
+
196
+ def test_container_exists_true
197
+ build_net_http_object
198
+ assert_equal @connection.container_exists?('this_container_exists'), true
199
+ end
200
+
201
+ def test_container_exists_false
202
+ build_net_http_object(:code => '999')
203
+ assert_equal @connection.container_exists?('this_does_not_exist'), false
204
+ end
205
+
206
+ def test_fetch_exisiting_container
207
+ CloudFiles::Container.any_instance.stubs(:populate)
208
+ build_net_http_object
209
+ container = @connection.container('good_container')
210
+ assert_equal container.name, 'good_container'
211
+ end
212
+
213
+ def test_fetch_nonexistent_container
214
+ CloudFiles::Container.any_instance.stubs(:populate).raises(NoSuchContainerException)
215
+ build_net_http_object
216
+ assert_raise(NoSuchContainerException) do
217
+ container = @connection.container('bad_container')
218
+ end
219
+ end
220
+
221
+ def test_containers
222
+ build_net_http_object(:body => "foo\nbar\nbaz\nboo", :code => '200')
223
+ containers = @connection.containers
224
+ assert_equal containers.size, 4
225
+ assert_equal containers.first, 'foo'
226
+ end
227
+
228
+ def test_no_containers_yet
229
+ build_net_http_object
230
+ containers = @connection.containers
231
+ assert_equal containers.size, 0
232
+ assert_equal containers.class, Array
233
+ end
234
+
235
+ def test_containers_bad_result
236
+ build_net_http_object(:code => '999')
237
+ assert_raises(InvalidResponseException) do
238
+ containers = @connection.containers
239
+ end
240
+ end
241
+
242
+ def test_containers_detail
243
+ body = %{<?xml version="1.0" encoding="UTF-8"?>
244
+ <account name="MossoCloudFS_012559c7-030d-4bbd-8538-7468896b0e7f">
245
+ <container><name>Books</name><count>0</count><bytes>0</bytes></container><container><name>cftest</name><count>0</count><bytes>0</bytes></container><container><name>cszsa</name><count>1</count><bytes>82804</bytes></container><container><name>CWX</name><count>3</count><bytes>3645134</bytes></container><container><name>test</name><count>2</count><bytes>20</bytes></container><container><name>video</name><count>2</count><bytes>34141298</bytes></container><container><name>webpics</name><count>1</count><bytes>177496</bytes></container></account>}
246
+ build_net_http_object(:body => body, :code => '200')
247
+ details = @connection.containers_detail
248
+ assert_equal details['CWX'][:count], "3"
249
+ end
250
+
251
+ def test_empty_containers_detail
252
+ build_net_http_object
253
+ details = @connection.containers_detail
254
+ assert_equal details, {}
255
+ end
256
+
257
+ def test_containers_detail_bad_response
258
+ build_net_http_object(:code => '999')
259
+ assert_raises(InvalidResponseException) do
260
+ details = @connection.containers_detail
261
+ end
262
+ end
263
+
264
+ private
265
+
266
+ def build_net_http_object(args={:code => '204' })
267
+ args[:response] = {} unless args[:response]
268
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}.merge(args[:response])
269
+ response.stubs(:code).returns(args[:code])
270
+ response.stubs(:body).returns args[:body] || nil
271
+ server = mock()
272
+ server.stubs(:verify_mode= => true)
273
+ server.stubs(:start => true)
274
+ server.stubs(:use_ssl=).returns(true)
275
+ server.stubs(:request).returns(response)
276
+ Net::HTTP.stubs(:new).returns(server)
277
+ end
278
+
279
+ end