def test_retrieve_by_rev_hash(self): n = 1000 for i in xrange(n): self.node.update('/', VolthaInstance(instance_id='id%d' % i)) self.node.update('/', self.other) hashes = self.node.revisions self.assertEqual(self.node[hashes[0]].data, self.empty) self.assertEqual(self.node[hashes[10]].data, VolthaInstance(instance_id='id9')) self.assertEqual(self.node[hashes[-1]].data, self.other)
def start(self): log.debug('starting') self.root = ConfigRoot(VolthaInstance(**self.init_kw)) registry('grpc_server').register( add_VolthaLocalServiceServicer_to_server, self) log.info('started') return self
def test_many_simple_updates(self): n = 1000 for i in xrange(n): self.node.update('/', VolthaInstance(instance_id='id%d' % i)) self.node.update('/', self.other) self.assertEqual(len(self.node.revisions), 1002) self.assertEqual(self.node.latest.data, self.other)
def test_update(self): hash0 = self.node.latest.hash self.node.update('/', copy(self.other)) hash1 = self.node.latest.hash self.assertEqual(len(self.node.revisions), 2) self.assertNotEqual(hash0, hash1) self.assertEqual(self.node.latest.data, VolthaInstance(instance_id='other'))
def test_tagging(self): self.node.tag('original') hash1 = self.node.latest.hash # add a bunch of changes for a in xrange(10): self.node.update('/', VolthaInstance(instance_id=str(a))) hash2 = self.node.latest.hash # apply tag to latest self.node.tag('latest') # apply another tag to latest self.node.tag('other') # apply tag to specific rev hash self.node.tag('yetmore', hash2) # invalid hash self.assertRaises(KeyError, self.node.tag, 'sometag', 'badhash') # retrieve data based on tag self.assertEqual(self.node.by_tag('original').hash, hash1) self.assertEqual(self.node.by_tag('latest').hash, hash2) self.assertEqual(self.node.by_tag('other').hash, hash2) self.assertEqual(self.node.by_tag('yetmore').hash, hash2) # generate diff from tags self.assertEqual( self.node.diff_by_tag('original', 'latest').patch, [dict(op='replace', path='/instance_id', value='9')]) # move tags to given revision self.node.tag('original', self.node.revisions[2]) self.node.tag('latest', self.node.revisions[9]) # add another tag self.node.tag('another', self.node.revisions[7]) # list tags self.assertEqual(self.node.tags, ['another', 'latest', 'original', 'other', 'yetmore']) # delete a tag self.node.delete_tag('another') self.node.delete_tags('yetmore', 'other') self.assertEqual(self.node.tags, ['latest', 'original']) # prune untagged revisions from revision list self.node.prune_untagged() self.assertEqual(len(self.node.revisions), 3) # latest is always kept # retrieve and compare working tagged revs self.assertEqual( self.node.diff_by_tag('original', 'latest').patch, [dict(op='replace', path='/instance_id', value='8')])
def start(self, config_backend=None): log.debug('starting') if config_backend: if 'root' in config_backend: # This is going to block the entire reactor until loading is completed log.info('loading config from persisted backend') self.root = ConfigRoot.load(VolthaInstance, kv_store=config_backend) else: log.info('initializing new config') self.root = ConfigRoot(VolthaInstance(**self.init_kw), kv_store=config_backend) else: self.root = ConfigRoot(VolthaInstance(**self.init_kw)) registry('grpc_server').register( add_VolthaLocalServiceServicer_to_server, self) log.info('started') return self
def test_strict_read_only(self): # it shall not be possible to change a read-only field self.assertRaises(ValueError, self.node.update, '/', VolthaInstance(version='foo'), strict=True) self.assertRaises(ValueError, self.node.update, '/adapters/1', Adapter(version='foo'), strict=True)
def test_top_level_update(self): # test that top-level update retains children self.node.update('/', VolthaInstance(version='1.2.3')) hash_new = self.node.latest.hash self.assertNotEqual(self.hash_orig, hash_new) self.assertEqual(self.node.get(hash=self.hash_orig, deep=1), self.base_deep) latest = self.node.get(deep=1) self.assertNotEqual(latest, self.base_deep) self.assertEqual(len(latest.adapters), 5) self.assertEqual(len(latest.logical_devices), 0) self.assertEqual(latest.version, '1.2.3')
def setUp(self): gc.collect() _rev_cache.clear() self.health = HealthStatus(state=HealthStatus.DYING) self.base_shallow = VolthaInstance(instance_id='1') self.base_deep = copy(self.base_shallow) self.base_deep.health.state = HealthStatus.DYING # = self.health for i in xrange(5): self.base_deep.adapters.add().MergeFrom( Adapter(id=str(i), config=AdapterConfig(log_level=3))) self.node = ConfigRoot(self.base_deep) self.hash_orig = self.node.latest.hash
def GetVolthaInstance(self, request, context): log.info('grpc-request', request=request) instance_id = request.id try: return self.dispatcher.dispatch(instance_id, VolthaLocalServiceStub, 'GetVolthaInstance', Empty(), context) except KeyError: context.set_details( 'Voltha instance \'{}\' not found'.format(instance_id)) context.set_code(StatusCode.NOT_FOUND) return VolthaInstance()
def test_inmemory_kv_store(self): t0 = [time()] def pt(msg=''): t1 = time() print '%20.8f ms - %s' % (1000 * (t1 - t0[0]), msg) t0[0] = t1 kv_store = dict() # create node and pump data node = ConfigRoot(VolthaInstance(), kv_store=kv_store) node.tag('original') pt('init') self.pump_some_data(node) pt('pump') node.tag('pumped') # check that content of kv_store looks ok size1 = len(kv_store) self.assertEqual(size1, 14 + 3 * (n_adapters + n_logical_nodes)) # this should actually drop if we pune node.prune_untagged() pt('prunning') size2 = len(kv_store) self.assertEqual(size2, 7 + 2 * (1 + 1 + n_adapters + n_logical_nodes) + 2) all_latest_data = node.get('/', deep=1) pt('deep get') # save dict so that deleting the node will not wipe it latest_hash = node.latest.hash kv_store = copy(kv_store) pt('copy kv store') del node pt('delete node') # self.assertEqual(size2, 1 + 2 * (1 + 1 + n_adapters + n_logical_nodes)) self.assertEqual(json.loads(kv_store['root'])['latest'], latest_hash) # recreate tree from persistence node = ConfigRoot.load(VolthaInstance, kv_store) pt('load from kv store') self.assertEqual(node.get('/', deep=1), all_latest_data) pt('deep get') self.assertEqual(latest_hash, node.latest.hash) self.assertEqual(node.tags, ['original', 'pumped'])
def test_pruning_after_shallow_change(self): self.node.update('/', VolthaInstance(version='10.1')) # sanity check self.assertEqual(len(self.node.revisions), 2) # prune self.node.prune_untagged() self.assertEqual(len(self.node.revisions), 1) # we can nevertheless access the whole tree new = self.node.get('/', deep=1) self.assertEqual(new.adapters, self.base_deep.adapters) self.assertEqual(new.version, '10.1')
def pump_some_data(self, node): seed(0) node.update('/', VolthaInstance(instance_id='1', version='42', log_level=1)) node.update('/health', HealthStatus(state=HealthStatus.OVERLOADED)) for i in xrange(n_adapters): node.add( '/adapters', Adapter(id=str(i), vendor='cord', version=str(randint(1, 10)), config=AdapterConfig(log_level=0))) for i in xrange(n_logical_nodes): node.add( '/logical_devices', LogicalDevice(id=str(i), datapath_id=randint(1, 100000), desc=ofp_desc(mfr_desc='foo', hw_desc='bar', sw_desc='zoo')))
def test_immutability(self): self.assertEqual(self.node.latest.data, self.empty) self.empty.instance_id = 'overwritten id' self.assertEqual(self.node.latest.data, VolthaInstance())
def setUp(self): self.empty = VolthaInstance() self.other = VolthaInstance(instance_id='other') self.node = ConfigRoot(VolthaInstance())
def test_update_handle_invalid_type(self): self.assertRaises(ValueError, self.node.update, '/', Adapter()) self.assertRaises(ValueError, self.node.update, '/health', Adapter()) self.assertRaises(ValueError, self.node.update, '/adapters/1', VolthaInstance())
def test_reject_duplicate_keys(self): data = VolthaInstance( instance_id='42', adapters=[Adapter(id='same') for _ in xrange(5)]) self.assertRaises(ValueError, ConfigRoot, data)
def __init__(self): self.root = ConfigRoot(VolthaInstance())