configurationengine/source/cone/public/tests/unittest_storage.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    19 """
    19 """
    20 import unittest
    20 import unittest
    21 import string
    21 import string
    22 import sys,os
    22 import sys,os
    23 import pickle
    23 import pickle
    24 import __init__
       
    25 
    24 
    26 from cone.public import api, exceptions, utils
    25 from cone.public import api, exceptions, utils
    27 
    26 
    28 ROOT_PATH       = os.path.dirname(os.path.abspath(__file__))
    27 ROOT_PATH       = os.path.dirname(os.path.abspath(__file__))
    29 temp_dir        = os.path.join(ROOT_PATH, "temp/storage")
    28 temp_dir        = os.path.join(ROOT_PATH, "temp/storage")
   175         res1 = store.open_resource("test/foo.txt","a")
   174         res1 = store.open_resource("test/foo.txt","a")
   176         res1.write("Testing\n")
   175         res1.write("Testing\n")
   177         res1.close()
   176         res1.close()
   178         res1 = store.open_resource("test/foo.txt","w")
   177         res1 = store.open_resource("test/foo.txt","w")
   179         res1.close()
   178         res1.close()
   180         self.assertEquals(store.list_resources('', True),['test/foo.txt'])
   179         self.assertEquals(store.list_resources('', recurse=True),['test/foo.txt'])
   181 
   180 
   182     def test_open_many(self):
   181     def test_open_many(self):
   183         store = api.Storage.open(storage_path,"w")
   182         store = api.Storage.open(storage_path,"w")
   184         self.assertTrue(store)
   183         self.assertTrue(store)
   185         res1 = store.open_resource("test/foo.txt","a")
   184         res1 = store.open_resource("test/foo.txt","a")
   204         res2.write("Writing bar!")
   203         res2.write("Writing bar!")
   205         res2.close()
   204         res2.close()
   206         self.assertEquals(store.list_resources('/'), ['root.txt'])
   205         self.assertEquals(store.list_resources('/'), ['root.txt'])
   207         self.assertEquals(store.list_resources('/test'), ['test/bar.txt',
   206         self.assertEquals(store.list_resources('/test'), ['test/bar.txt',
   208                                                           'test/foo.txt'])
   207                                                           'test/foo.txt'])
   209         self.assertEquals(store.list_resources('/',True), ['root.txt',
   208         self.assertEquals(store.list_resources('/',recurse=True), ['root.txt',
   210                                                            'test/bar.txt',
   209                                                            'test/bar.txt',
   211                                                            'test/foo.txt'])
   210                                                            'test/foo.txt'])
   212 
   211 
   213     def test_open_resource_and_delete(self):
   212     def test_open_resource_and_delete(self):
   214         store = api.Storage.open(storage_path,"w")
   213         store = api.Storage.open(storage_path,"w")
   365         res = store.open_resource("foo.txt","w")
   364         res = store.open_resource("foo.txt","w")
   366         res.write("foo")
   365         res.write("foo")
   367         res.close()
   366         res.close()
   368         self.assertEquals(store.list_resources(""), ["foo.txt"])
   367         self.assertEquals(store.list_resources(""), ["foo.txt"])
   369         store.set_current_path("/")
   368         store.set_current_path("/")
   370         self.assertEquals(store.list_resources("", True), ["subdir/foo.txt"])
   369         self.assertEquals(store.list_resources("", recurse=True), ["subdir/foo.txt"])
   371         store.close()
   370         store.close()
   372         os.unlink(temp_file)
   371         os.unlink(temp_file)
   373 
   372 
   374     def test_create_folder(self):
   373     def test_create_folder(self):
   375         temp_file = os.path.join(temp_dir, "subpath.pk")
   374         temp_file = os.path.join(temp_dir, "subpath.pk")
   404         layer = api.Folder(store, "foo")
   403         layer = api.Folder(store, "foo")
   405         self.assertTrue(layer)
   404         self.assertTrue(layer)
   406         res = layer.open_resource("confml/test.confml","w")
   405         res = layer.open_resource("confml/test.confml","w")
   407         res.write("foo.conf")
   406         res.write("foo.conf")
   408         res.close()
   407         res.close()
   409         self.assertEquals(layer.list_resources("", True),["confml/test.confml"])
   408         self.assertEquals(layer.list_resources("", recurse=True),["confml/test.confml"])
   410         self.assertEquals(store.list_resources("", True),["foo/confml/test.confml"])
   409         self.assertEquals(store.list_resources("", recurse=True),["foo/confml/test.confml"])
   411 
   410 
   412     def test_create_two_layers_and_open_resource(self):
   411     def test_create_two_layers_and_open_resource(self):
   413         store = api.Storage.open(storage_path,"w")
   412         store = api.Storage.open(storage_path,"w")
   414         foo_layer = api.Folder(store, "foo")
   413         foo_layer = api.Folder(store, "foo")
   415         bar_layer = api.Folder(store, "bar")
   414         bar_layer = api.Folder(store, "bar")
   419         res = foo_layer.open_resource("root.confml","w")
   418         res = foo_layer.open_resource("root.confml","w")
   420         res.close()
   419         res.close()
   421         res = bar_layer.open_resource("confml/root.confml","w")
   420         res = bar_layer.open_resource("confml/root.confml","w")
   422         res.write("foo.conf")
   421         res.write("foo.conf")
   423         res.close()
   422         res.close()
   424         self.assertEquals(foo_layer.list_resources("", True),['confml/test.confml', 'root.confml'])
   423         self.assertEquals(foo_layer.list_resources("", recurse=True),['root.confml', 'confml/test.confml'])
   425         self.assertEquals(store.list_resources("", True),['bar/confml/root.confml','foo/confml/test.confml','foo/root.confml'])
   424         self.assertEquals(store.list_resources("", recurse=True),['bar/confml/root.confml',
       
   425                                                                   'foo/root.confml',
       
   426                                                                   'foo/confml/test.confml'])
   426         
   427         
   427         foo_layer.delete_resource("confml/test.confml")
   428         foo_layer.delete_resource("confml/test.confml")
   428         self.assertEquals(foo_layer.list_resources("", True),["root.confml"])
   429         self.assertEquals(foo_layer.list_resources("", recurse=True),["root.confml"])
   429         self.assertEquals(store.list_resources("", True),["bar/confml/root.confml","foo/root.confml"])
   430         self.assertEquals(store.list_resources("", recurse=True),["bar/confml/root.confml",
       
   431                                                                   "foo/root.confml"])
   430 
   432 
   431 
   433 
   432 if __name__ == '__main__':
   434 if __name__ == '__main__':
   433     unittest.main()
   435     unittest.main()
   434       
   436