tfe-cloudfiles 1.4.7

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 = "YOUR_USERNAME"
8
+ apikey = "YOUR_API_KEY"
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 = "RubyCFTest"
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(:ttl => 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,48 @@
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, :snet? => false)
13
+ result = CloudFiles::Authentication.new(@connection)
14
+ assert_equal result.class, CloudFiles::Authentication
15
+ end
16
+
17
+ def test_snet_authentication
18
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}
19
+ response.stubs(:code).returns('204')
20
+ server = mock(:use_ssl= => true, :verify_mode= => true, :start => true, :finish => true)
21
+ server.stubs(:get).returns(response)
22
+ Net::HTTP.stubs(:new).returns(server)
23
+ @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, :snet? => true)
24
+ result = CloudFiles::Authentication.new(@connection)
25
+ assert_equal result.class, CloudFiles::Authentication
26
+ end
27
+
28
+ def test_bad_authentication
29
+ response = mock()
30
+ response.stubs(:code).returns('499')
31
+ server = mock(:use_ssl= => true, :verify_mode= => true, :start => true)
32
+ server.stubs(:get).returns(response)
33
+ Net::HTTP.stubs(:new).returns(server)
34
+ @connection = stub(:authuser => 'bad_user', :authkey => 'bad_key', :authok= => true, :authtoken= => true)
35
+ assert_raises(AuthenticationException) do
36
+ result = CloudFiles::Authentication.new(@connection)
37
+ end
38
+ end
39
+
40
+ def test_bad_hostname
41
+ Net::HTTP.stubs(:new).raises(ConnectionException)
42
+ @connection = stub(:authuser => 'bad_user', :authkey => 'bad_key', :authok= => true, :authtoken= => true)
43
+ assert_raises(ConnectionException) do
44
+ result = CloudFiles::Authentication.new(@connection)
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,286 @@
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_snet
27
+ # This would normally be set in CloudFiles::Authentication
28
+ assert_equal @connection.snet?, false
29
+ @connection.expects(:snet?).returns(true)
30
+ assert_equal @connection.snet?, true
31
+ end
32
+
33
+ def test_cfreq_get
34
+ build_net_http_object
35
+ assert_nothing_raised do
36
+ response = @connection.cfreq("GET", "test.server.example", "/dummypath", "80", "http")
37
+ end
38
+ end
39
+
40
+ def test_cfreq_post
41
+ build_net_http_object
42
+ assert_nothing_raised do
43
+ response = @connection.cfreq("POST", "test.server.example", "/dummypath", "80", "http")
44
+ end
45
+ end
46
+
47
+ def test_cfreq_put
48
+ build_net_http_object
49
+ assert_nothing_raised do
50
+ response = @connection.cfreq("PUT", "test.server.example", "/dummypath", "80", "http")
51
+ end
52
+ end
53
+
54
+ def test_cfreq_delete
55
+ build_net_http_object
56
+ assert_nothing_raised do
57
+ response = @connection.cfreq("DELETE", "test.server.example", "/dummypath", "80", "http")
58
+ end
59
+ end
60
+
61
+ def test_cfreq_with_static_data
62
+ build_net_http_object
63
+ assert_nothing_raised do
64
+ response = @connection.cfreq("PUT", "test.server.example", "/dummypath", "80", "http", {}, "This is string data")
65
+ end
66
+ end
67
+
68
+ def test_cfreq_with_stream_data
69
+ build_net_http_object
70
+ require 'tempfile'
71
+ file = Tempfile.new("test")
72
+ assert_nothing_raised do
73
+ response = @connection.cfreq("PUT", "test.server.example", "/dummypath", "80", "http", {}, file)
74
+ end
75
+ end
76
+
77
+ def test_cfreq_head
78
+ build_net_http_object
79
+ assert_nothing_raised do
80
+ response = @connection.cfreq("HEAD", "test.server.example", "/dummypath", "80", "http")
81
+ end
82
+ end
83
+
84
+ def test_net_http_raises_connection_exception
85
+ Net::HTTP.expects(:new).raises(ConnectionException)
86
+ assert_raises(ConnectionException) do
87
+ response = @connection.cfreq("GET", "test.server.example", "/dummypath", "80", "http")
88
+ end
89
+ end
90
+
91
+ def test_net_http_raises_one_eof_exception
92
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}
93
+ response.stubs(:code).returns('204')
94
+ server = stub(:use_ssl= => true, :verify_mode= => true, :start => true, :finish => true)
95
+ server.stubs(:request).raises(EOFError).then.returns(response)
96
+ Net::HTTP.stubs(:new).returns(server)
97
+ assert_nothing_raised do
98
+ response = @connection.cfreq("GET", "test.server.example", "/dummypath", "443", "https")
99
+ end
100
+ end
101
+
102
+ def test_net_http_raises_one_expired_token
103
+ CloudFiles::Authentication.expects(:new).returns(true)
104
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}
105
+ response.stubs(:code).returns('401').then.returns('204')
106
+ server = stub(:use_ssl= => true, :verify_mode= => true, :start => true)
107
+ server.stubs(:request).returns(response)
108
+ Net::HTTP.stubs(:new).returns(server)
109
+ assert_nothing_raised do
110
+ response = @connection.cfreq("GET", "test.server.example", "/dummypath", "80", "http")
111
+ end
112
+ end
113
+
114
+ def test_net_http_raises_continual_eof_exceptions
115
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}
116
+ response.stubs(:code).returns('204')
117
+ server = stub(:use_ssl= => true, :verify_mode= => true, :start => true)
118
+ server.stubs(:finish).returns(true)
119
+ server.stubs(:request).raises(EOFError)
120
+ Net::HTTP.stubs(:new).returns(server)
121
+ assert_raises(ConnectionException) do
122
+ response = @connection.cfreq("GET", "test.server.example", "/dummypath", "80", "http")
123
+ end
124
+ end
125
+
126
+ def test_get_info
127
+ build_net_http_object(:response => {'x-account-bytes-used' => '9999', 'x-account-container-count' => '5'}, :code => '204')
128
+ @connection.get_info
129
+ assert_equal @connection.bytes, 9999
130
+ assert_equal @connection.count, 5
131
+ end
132
+
133
+ def test_get_info_fails
134
+ build_net_http_object(:response => {'x-account-bytes-used' => '9999', 'x-account-container-count' => '5'}, :code => '999')
135
+ assert_raises(InvalidResponseException) do
136
+ @connection.get_info
137
+ end
138
+ end
139
+
140
+ def test_public_containers
141
+ build_net_http_object(:body => "foo\nbar\nbaz", :code => '200', :response => {})
142
+ public_containers = @connection.public_containers
143
+ assert_equal public_containers.size, 3
144
+ assert_equal public_containers.first, 'foo'
145
+ end
146
+
147
+ def test_public_containers_empty
148
+ build_net_http_object
149
+ public_containers = @connection.public_containers
150
+ assert_equal public_containers.size, 0
151
+ assert_equal public_containers.class, Array
152
+ end
153
+
154
+ def test_public_containers_exception
155
+ build_net_http_object(:code => '999')
156
+ assert_raises(InvalidResponseException) do
157
+ public_containers = @connection.public_containers
158
+ end
159
+ end
160
+
161
+ def test_delete_container
162
+ build_net_http_object
163
+ response = @connection.delete_container("good_container")
164
+ assert_equal response, true
165
+ end
166
+
167
+ def test_delete_nonempty_container
168
+ build_net_http_object(:code => '409')
169
+ assert_raises(NonEmptyContainerException) do
170
+ response = @connection.delete_container("not_empty")
171
+ end
172
+ end
173
+
174
+ def test_delete_unknown_container
175
+ build_net_http_object(:code => '999')
176
+ assert_raises(NoSuchContainerException) do
177
+ response = @connection.delete_container("not_empty")
178
+ end
179
+ end
180
+
181
+ def test_create_container
182
+ CloudFiles::Container.any_instance.stubs(:populate)
183
+ build_net_http_object(:code => '201')
184
+ container = @connection.create_container('good_container')
185
+ assert_equal container.name, 'good_container'
186
+ end
187
+
188
+ def test_create_container_with_invalid_name
189
+ CloudFiles::Container.stubs(:new)
190
+ assert_raise(SyntaxException) do
191
+ container = @connection.create_container('a'*300)
192
+ end
193
+ end
194
+
195
+ def test_create_container_name_filter
196
+ CloudFiles::Container.any_instance.stubs(:populate)
197
+ build_net_http_object(:code => '201')
198
+ assert_raises(SyntaxException) do
199
+ container = @connection.create_container('this/has/bad?characters')
200
+ end
201
+ end
202
+
203
+ def test_container_exists_true
204
+ build_net_http_object
205
+ assert_equal @connection.container_exists?('this_container_exists'), true
206
+ end
207
+
208
+ def test_container_exists_false
209
+ build_net_http_object(:code => '999')
210
+ assert_equal @connection.container_exists?('this_does_not_exist'), false
211
+ end
212
+
213
+ def test_fetch_exisiting_container
214
+ CloudFiles::Container.any_instance.stubs(:populate)
215
+ build_net_http_object
216
+ container = @connection.container('good_container')
217
+ assert_equal container.name, 'good_container'
218
+ end
219
+
220
+ def test_fetch_nonexistent_container
221
+ CloudFiles::Container.any_instance.stubs(:populate).raises(NoSuchContainerException)
222
+ build_net_http_object
223
+ assert_raise(NoSuchContainerException) do
224
+ container = @connection.container('bad_container')
225
+ end
226
+ end
227
+
228
+ def test_containers
229
+ build_net_http_object(:body => "foo\nbar\nbaz\nboo", :code => '200')
230
+ containers = @connection.containers
231
+ assert_equal containers.size, 4
232
+ assert_equal containers.first, 'foo'
233
+ end
234
+
235
+ def test_no_containers_yet
236
+ build_net_http_object
237
+ containers = @connection.containers
238
+ assert_equal containers.size, 0
239
+ assert_equal containers.class, Array
240
+ end
241
+
242
+ def test_containers_bad_result
243
+ build_net_http_object(:code => '999')
244
+ assert_raises(InvalidResponseException) do
245
+ containers = @connection.containers
246
+ end
247
+ end
248
+
249
+ def test_containers_detail
250
+ body = %{<?xml version="1.0" encoding="UTF-8"?>
251
+ <account name="MossoCloudFS_012559c7-030d-4bbd-8538-7468896b0e7f">
252
+ <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>}
253
+ build_net_http_object(:body => body, :code => '200')
254
+ details = @connection.containers_detail
255
+ assert_equal details['CWX'][:count], "3"
256
+ end
257
+
258
+ def test_empty_containers_detail
259
+ build_net_http_object
260
+ details = @connection.containers_detail
261
+ assert_equal details, {}
262
+ end
263
+
264
+ def test_containers_detail_bad_response
265
+ build_net_http_object(:code => '999')
266
+ assert_raises(InvalidResponseException) do
267
+ details = @connection.containers_detail
268
+ end
269
+ end
270
+
271
+ private
272
+
273
+ def build_net_http_object(args={:code => '204' })
274
+ args[:response] = {} unless args[:response]
275
+ response = {'x-cdn-management-url' => 'http://cdn.example.com/path', 'x-storage-url' => 'http://cdn.example.com/storage', 'authtoken' => 'dummy_token'}.merge(args[:response])
276
+ response.stubs(:code).returns(args[:code])
277
+ response.stubs(:body).returns args[:body] || nil
278
+ server = mock()
279
+ server.stubs(:verify_mode= => true)
280
+ server.stubs(:start => true)
281
+ server.stubs(:use_ssl=).returns(true)
282
+ server.stubs(:request).returns(response)
283
+ Net::HTTP.stubs(:new).returns(server)
284
+ end
285
+
286
+ end