1
1
# _*_ coding: utf-8 -*-
2
2
import os
3
3
import shutil
4
+ import json
4
5
import unittest
5
6
6
7
import ipfsApi
@@ -84,6 +85,19 @@ def tearDown(self):
84
85
# TESTS #
85
86
#########
86
87
88
+ def test_version (self ):
89
+ expected = ['Repo' , 'Commit' , 'Version' ]
90
+ resp_version = self .api .version ()
91
+ for key in expected :
92
+ self .assertTrue (key in resp_version )
93
+
94
+ def test_id (self ):
95
+ expected = ['PublicKey' , 'ProtocolVersion' ,
96
+ 'ID' , 'AgentVersion' , 'Addresses' ]
97
+ resp_id = self .api .id ()
98
+ for key in expected :
99
+ self .assertTrue (key in resp_id )
100
+
87
101
def test_add_single_from_str (self ):
88
102
res = self .api .add (self .fake_file )
89
103
self .assertEqual (res , self .fake_file_only_res )
@@ -104,7 +118,6 @@ def test_add_multiple_from_dirname(self):
104
118
sorted (self .fake_dir_res ,
105
119
key = lambda x : x ['Name' ]))
106
120
107
-
108
121
def test_add_recursive (self ):
109
122
res = self .api .add (self .fake_dir , recursive = True )
110
123
self .assertEqual (sorted (res ,
@@ -170,6 +183,117 @@ def test_cat_single_file_str(self):
170
183
self .assertEqual ("dsadsad\n " , res )
171
184
172
185
186
+ class IpfsApiLogTest (unittest .TestCase ):
187
+
188
+ def setUp (self ):
189
+ self .api = ipfsApi .Client ()
190
+
191
+ def test_log_ls_level (self ):
192
+ """
193
+ Unfortunately there is no way of knowing the logging levels prior
194
+ to this test. This makes it impossible to guarantee that the logging
195
+ levels are the same as before the test was run.
196
+ """
197
+ # Retrieves the list of logging subsystems for a running daemon.
198
+ resp_ls = self .api .log_ls ()
199
+ # The response should be a dictionary with only one key ('Strings').
200
+ self .assertTrue ('Strings' in resp_ls )
201
+
202
+ # Sets the logging level to 'error' for the first subsystem found.
203
+ sub = resp_ls ['Strings' ][0 ]
204
+ resp_level = self .api .log_level (sub , 'error' )
205
+ self .assertEqual (resp_level ['Message' ],
206
+ "Changed log level of \' %s\' to 'error'\n " % sub )
207
+
208
+ def test_log_tail (self ):
209
+ # Gets the response object.
210
+ tail = self .api .log_tail ()
211
+
212
+ # Takes the first log received.
213
+ line = tail .readline ()
214
+ log = json .loads (line .decode ())
215
+
216
+ # Closes the response object.
217
+ tail .close ()
218
+
219
+ # The log should have been parsed into a dictionary object with
220
+ # various keys depending on the event that occured.
221
+ self .assertTrue (type (log ) is dict )
222
+
223
+
224
+ class IpfsApiPinTest (unittest .TestCase ):
225
+
226
+ fake_dir_hash = 'QmYqqgRahxbZvudnzDu2ZzUS1vFSNEuCrxghM8hgT8uBFY'
227
+
228
+ def setUp (self ):
229
+ self .api = ipfsApi .Client ()
230
+ # Add resources to be pinned.
231
+ self .resource = self .api .add_str (u'Mary had a little lamb' )
232
+ resp_add = self .api .add ('fake_dir' , recursive = True )
233
+ self .fake_dir_hashes = [el ['Hash' ] for el in resp_add if 'Hash' in el ]
234
+
235
+ def test_pin_ls_add_rm_single (self ):
236
+ # Get pinned objects at start.
237
+ pins_begin = self .api .pin_ls ()['Keys' ]
238
+
239
+ # Unpin the resource if already pinned.
240
+ if self .resource in pins_begin .keys ():
241
+ self .api .pin_rm (self .resource )
242
+
243
+ # No matter what, the resource should not be pinned at this point.
244
+ self .assertFalse (self .resource in self .api .pin_ls ()['Keys' ])
245
+
246
+ for option in [True , False ]:
247
+ # Pin the resource.
248
+ resp_add = self .api .pin_add (self .resource ,
249
+ opts = {"recursive" :str (option )})
250
+ pins_afer_add = self .api .pin_ls ()['Keys' ]
251
+ self .assertEqual (resp_add ['Pins' ], [self .resource ])
252
+ self .assertTrue (self .resource in pins_afer_add )
253
+ self .assertEqual (pins_afer_add [self .resource ]['Type' ] == 'recursive' ,
254
+ option )
255
+
256
+ # Unpin the resource.
257
+ resp_rm = self .api .pin_rm (self .resource )
258
+ pins_afer_rm = self .api .pin_ls ()['Keys' ]
259
+ self .assertEqual (resp_rm ['Pins' ], [self .resource ])
260
+ self .assertFalse (self .resource in pins_afer_rm )
261
+
262
+ # Get pinned objects at end.
263
+ pins_end = self .api .pin_ls ()['Keys' ]
264
+
265
+ # Compare pinned items from start to finish of test.
266
+ self .assertFalse (self .resource in pins_end .keys ())
267
+
268
+ def test_pin_ls_add_rm_directory (self ):
269
+ # Get pinned objects at start.
270
+ pins_begin = self .api .pin_ls ()['Keys' ]
271
+
272
+ # Remove fake_dir if it had previously been pinned.
273
+ if self .fake_dir_hash in pins_begin .keys () and \
274
+ pins_begin [self .fake_dir_hash ]['Type' ] == 'recursive' :
275
+ self .api .pin_rm (self .fake_dir_hash )
276
+
277
+ # Make sure I removed it
278
+ pins_after_rm = self .api .pin_ls ()['Keys' ]
279
+ self .assertFalse (self .fake_dir_hash in pins_after_rm .keys () and \
280
+ pins_after_rm [self .fake_dir_hash ]['Type' ] == 'recursive' )
281
+
282
+ # Add 'fake_dir' recursively.
283
+ self .api .pin_add (self .fake_dir_hash )
284
+
285
+ # Make sure all appear on the list of pinned objects.
286
+ pins_after_add = self .api .pin_ls ()['Keys' ].keys ()
287
+ for el in self .fake_dir_hashes :
288
+ self .assertTrue (el in pins_after_add )
289
+
290
+ # Clean up.
291
+ self .api .pin_rm (self .fake_dir_hash )
292
+ pins_end = self .api .pin_ls ()['Keys' ].keys ()
293
+ self .assertFalse (self .fake_dir_hash in pins_end and \
294
+ pins_after_rm [self .fake_dir_hash ]['Type' ] == 'recursive' )
295
+
296
+
173
297
class IpfsApiMFSTest (unittest .TestCase ):
174
298
175
299
test_files = {
@@ -269,6 +393,8 @@ def setUp(self):
269
393
self .api = ipfsApi .Client ()
270
394
self ._olddir = os .getcwd ()
271
395
os .chdir (HERE )
396
+ # Add a resource to get the stats for.
397
+ self .resource = self .api .add_str (u'Mary had a little lamb' )
272
398
273
399
def tearDown (self ):
274
400
os .chdir (self ._olddir )
@@ -279,6 +405,13 @@ def test_object_new(self):
279
405
for key in expected_keys :
280
406
self .assertTrue (key in res )
281
407
408
+ def test_object_stat (self ):
409
+ expected = ['Hash' , 'CumulativeSize' , 'DataSize' ,
410
+ 'NumLinks' , 'LinksSize' , 'BlockSize' ]
411
+ resp_stat = self .api .object_stat (self .resource )
412
+ for key in expected :
413
+ self .assertTrue (key in resp_stat )
414
+
282
415
def test_object_put_get (self ):
283
416
# Set paths to test json files
284
417
path_no_links = os .path .join (os .path .dirname (__file__ ),
0 commit comments