def test_len(self): # test batch = Batch() batch.items = [1, 2] # validation self.assertEqual(len(batch), len(batch.items))
def test_reset(self): # test batch = Batch() batch.items = {'A': 1} batch.reset() # validation self.assertEqual(batch.items, {})
def test_len(self): # test batch = Batch() batch.items['A'] = 12 batch.items['B'] = 18 # validation self.assertEqual(len(batch), len(batch.items))
def test_call(self, relink, migrate, reset): # test batch = Batch() batch.items['A'] = 12 batch() # validate relink.assert_called_once_with() migrate.assert_called_once_with() reset.assert_called_once_with()
def test_call(self, relink, migrate, reset): # test batch = Batch() batch.items = [12] batch() # validate relink.assert_called_once_with() migrate.assert_called_once_with() reset.assert_called_once_with()
def test_add(self): unit = Mock(plan=1, id=2, storage_path=3, new_path=4) # test batch = Batch() batch.add(unit) # validation item = batch.items.values()[0] self.assertEqual(len(batch), 1) self.assertEqual(batch.items.keys()[0], unit.storage_path) self.assertEqual(item.plan, unit.plan) self.assertEqual(item.unit_id, unit.id) self.assertEqual(item.storage_path, unit.storage_path)
def test_relink(self, islink, symlink, unlink, readlink, walk): def read_link(path): if os.path.basename(path) == 'l-invalid': raise OSError() return path.upper() readlink.side_effect = read_link def is_link(path): return os.path.basename(path)[0] == 'l' islink.side_effect = is_link paths = { 'D0/L2': Item(1, '1', 'D0/L2', 'new-path-1', []), 'D1/L3': Item(2, '2', 'D1/L3', 'new-path-2', []), 'D2/L6': Item(3, '3', 'D2/L6', 'new-path-3', []), 'D3/L7': Item(4, '4', 'D3', 'new-path-4', ['L7']), } walk.return_value = [ ('d0', ['d1', 'd2'], ['f1', 'l2']), ('d1', [], ['l3', 'f4']), ('d2', [], ['f5', 'l6', 'l7', 'l-invalid']), ('d3', [], ['l7']), ] # test batch = Batch() batch.paths = paths batch._relink() # validation self.assertEqual( unlink.call_args_list, [ call('d0/l2'), call('d1/l3'), call('d2/l6'), call('d3/l7') ]) self.assertEqual( symlink.call_args_list, [ call('new-path-1', 'd0/l2'), call('new-path-2', 'd1/l3'), call('new-path-3', 'd2/l6'), call('new-path-4/L7', 'd3/l7') ])
def test_add(self): unit = Mock(plan=1, id=2, storage_path='3', new_path='4', files=['4', '5']) # test batch = Batch() batch.add(unit) # validation item = batch.items[0] self.assertEqual(len(batch), 1) self.assertEqual( sorted(batch.paths.keys()), sorted([unit.storage_path] + [os.path.join(unit.storage_path, f) for f in unit.files])) self.assertEqual(batch.paths.values(), [item, item, item]) self.assertEqual(item.plan, unit.plan) self.assertEqual(item.unit_id, unit.id) self.assertEqual(item.storage_path, unit.storage_path)
def test_migrate(self): plan = Mock() items = [ Item(plan, '1', 'path-1', 'new-path-1', []), Item(plan, '2', 'path-2', 'new-path-2', []), Item(plan, '3', 'path-3', 'new-path-3', []), ] # test batch = Batch() batch.items = items batch._migrate() # validate for i in items: expected_call = call(i.unit_id, i.storage_path, i.new_path) self.assertIn(expected_call, plan.migrate.call_args_list)
def test_migrate(self): plan = Mock() items = [ Item(plan, '1', 'path-1', 'new-path-1', []), Item(plan, '2', 'path-2', 'new-path-2', []), Item(plan, '3', 'path-3', 'new-path-3', []), ] # test batch = Batch() batch.items = items batch._migrate() # validate for i in items: expected_call = call(i.unit_id, i.storage_path, i.new_path) self.assertTrue(expected_call in plan.migrate.call_args_list)
def test_migrate(self): plan = Mock() items = [ Item(plan, '1', 'path-1', 'new-path-1', []), Item(plan, '2', 'path-2', 'new-path-2', []), Item(plan, '3', 'path-3', 'new-path-3', []), ] # test batch = Batch() batch.items = items batch._migrate() # validate self.assertEqual( plan.migrate.call_args_list, [ call(i.unit_id, i.storage_path, i.new_path) for i in items ])
def test_migrate(self, unit_migrate): items = [ Item(Mock(), '1', 'path-1', 'new-path-1'), Item(Mock(), '2', 'path-2', 'new-path-2'), Item(Mock(), '3', 'path-3', 'new-path-3'), ] _dict = Mock() _dict.itervalues.return_value = iter(items) # test batch = Batch() batch.items = _dict batch._migrate() # validate self.assertEqual( unit_migrate.call_args_list, [ call(i.plan, i.unit_id, i.storage_path, i.new_path) for i in items ])
def test_relink(self, islink, symlink, unlink, readlink, walk): def read_link(path): return path.upper() readlink.side_effect = read_link def is_link(path): return os.path.basename(path)[0] == 'l' islink.side_effect = is_link items = { 'D0/L2': Item(1, '1', 'd0/l2', 'new-path-1'), 'D1/L3': Item(2, '2', 'd1/l3', 'new-path-2'), 'D2/L6': Item(3, '3', 'd2/l6', 'new-path-3'), } walk.return_value = [ ('d0', ['d1', 'd2'], ['f1', 'l2']), ('d1', [], ['l3', 'f4']), ('d2', [], ['f5', 'l6', 'l7']) ] # test batch = Batch() batch.items = items batch._relink() # validation self.assertEqual( unlink.call_args_list, [ call(items[k].storage_path) for k in sorted(items) ]) self.assertEqual( symlink.call_args_list, [ call(items[k].new_path, k.lower()) for k in sorted(items) ])
def test_relink(self, symlink, unlink, readlink, walk): links = { '/pub/zoo/cat/lion/f1': '/content/a/f1', '/pub/zoo/cat/tiger/f2': '/content/b/f2', '/pub/zoo/zebra/f3': '/content/c/f3', '/pub/zoo/bear/f4': '/content/d/f4', '/pub/zoo/wolf/f5': '/content/e/f5', '/pub/zoo/wolf/.f5': '/content/e/.f5', '/pub/zoo/wolf/f6': '/content/e/f6', '/pub/unknown/f0': '/content/unknown/f0', } def read_link(path): try: return links[path] except KeyError: raise OSError() readlink.side_effect = read_link items = [ Item(1, '1', '/content/a/f1', '/content/new/a/f1', []), Item(2, '2', '/content/b/f2', '/content/new/b/f2', []), Item(3, '3', '/content/c/f3', '/content/new/c/f3', []), Item(4, '4', '/content/d/f4', '/content/new/d/f4', []), Item(5, '5', '/content/e', '/content/new/e', ['f5', '.f5', 'f6']), ] paths = {} for i in items: paths[i.storage_path] = i for f in i.files: paths[os.path.join(i.storage_path, f)] = i walk.return_value = [ ('/pub', ['zoo', 'other'], []), ('/pub/zoo', ['cat'], []), ('/pub/zoo/cat', ['lion', 'tiger'], []), ('/pub/zoo/cat/lion', [], ['f1']), ('/pub/zoo/cat/tiger', [], ['f2']), ('/pub/zoo/zebra', [], ['f3']), ('/pub/zoo/bear', [], ['f4']), ('/pub/zoo/wolf', [], ['.f5', 'f5', 'f6']), ('/pub/unknown', [], ['f0', 'f1']), ] # test batch = Batch() batch.paths = paths batch._relink() # validation expected_unlink_calls = [ call('/pub/zoo/cat/lion/f1'), call('/pub/zoo/cat/tiger/f2'), call('/pub/zoo/zebra/f3'), call('/pub/zoo/bear/f4'), call('/pub/zoo/wolf/.f5'), call('/pub/zoo/wolf/f5'), call('/pub/zoo/wolf/f6'), ] expected_link_calls = [ call('/content/new/a/f1', '/pub/zoo/cat/lion/f1'), call('/content/new/b/f2', '/pub/zoo/cat/tiger/f2'), call('/content/new/c/f3', '/pub/zoo/zebra/f3'), call('/content/new/d/f4', '/pub/zoo/bear/f4'), call('/content/new/e/.f5', '/pub/zoo/wolf/.f5'), call('/content/new/e/f5', '/pub/zoo/wolf/f5'), call('/content/new/e/f6', '/pub/zoo/wolf/f6'), ] for link_call, unlink_call in zip(expected_link_calls, expected_unlink_calls): self.assertTrue(link_call in symlink.call_args_list) self.assertTrue(unlink_call in unlink.call_args_list)
def test_relink(self, symlink, unlink, readlink, walk): links = { '/pub/zoo/cat/lion/f1': '/content/a/f1', '/pub/zoo/cat/tiger/f2': '/content/b/f2', '/pub/zoo/zebra/f3': '/content/c/f3', '/pub/zoo/bear/f4': '/content/d/f4', '/pub/zoo/wolf/f5': '/content/e/f5', '/pub/zoo/wolf/.f5': '/content/e/.f5', '/pub/zoo/wolf/f6': '/content/e/f6', '/pub/unknown/f0': '/content/unknown/f0', } def read_link(path): try: return links[path] except KeyError: raise OSError() readlink.side_effect = read_link items = [ Item(1, '1', '/content/a/f1', '/content/new/a/f1', []), Item(2, '2', '/content/b/f2', '/content/new/b/f2', []), Item(3, '3', '/content/c/f3', '/content/new/c/f3', []), Item(4, '4', '/content/d/f4', '/content/new/d/f4', []), Item(5, '5', '/content/e', '/content/new/e', ['f5', '.f5', 'f6']), ] paths = {} for i in items: paths[i.storage_path] = i for f in i.files: paths[os.path.join(i.storage_path, f)] = i walk.return_value = [ ('/pub', ['zoo', 'other'], []), ('/pub/zoo', ['cat'], []), ('/pub/zoo/cat', ['lion', 'tiger'], []), ('/pub/zoo/cat/lion', [], ['f1']), ('/pub/zoo/cat/tiger', [], ['f2']), ('/pub/zoo/zebra', [], ['f3']), ('/pub/zoo/bear', [], ['f4']), ('/pub/zoo/wolf', [], ['.f5', 'f5', 'f6']), ('/pub/unknown', [], ['f0', 'f1']), ] # test batch = Batch() batch.paths = paths batch._relink() # validation expected_unlink_calls = [ call('/pub/zoo/cat/lion/f1'), call('/pub/zoo/cat/tiger/f2'), call('/pub/zoo/zebra/f3'), call('/pub/zoo/bear/f4'), call('/pub/zoo/wolf/.f5'), call('/pub/zoo/wolf/f5'), call('/pub/zoo/wolf/f6'), ] expected_link_calls = [ call('/content/new/a/f1', '/pub/zoo/cat/lion/f1'), call('/content/new/b/f2', '/pub/zoo/cat/tiger/f2'), call('/content/new/c/f3', '/pub/zoo/zebra/f3'), call('/content/new/d/f4', '/pub/zoo/bear/f4'), call('/content/new/e/.f5', '/pub/zoo/wolf/.f5'), call('/content/new/e/f5', '/pub/zoo/wolf/f5'), call('/content/new/e/f6', '/pub/zoo/wolf/f6'), ] for link_call, unlink_call in zip(expected_link_calls, expected_unlink_calls): self.assertIn(link_call, symlink.call_args_list) self.assertIn(unlink_call, unlink.call_args_list)