def test_tree_del_repeated_one_key(self): data = RPD_DB() data.data.Clear() self._create_repeated_list(data) data.del_tree({ 'cfg': { 'DsOfdmProfile': { 2: { 'DsOfdmSubcarrierModulation': { 21: None } } } } }) value = data.get_val(['cfg']) self.assertEqual(len(value.ListFields()), 1) value = data.get_val(['cfg', 'DsOfdmProfile']) self.assertEqual(len(value), 2) value = data.get_val( ['cfg', 'DsOfdmProfile', 1, 'DsOfdmSubcarrierModulation']) self.assertEqual(len(value), 1) value = data.get_val( ['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation']) self.assertListEqual([x.StartSubcarrierId for x in value], [22])
def test_update_repeated_elm(self): """Update element in repeated list.""" data = RPD_DB() t = data.data.cfg.RpdCapabilities.LcceChannelReachability.add() t.EnetPortIndex = 5 t.ChannelType = 2 t.RfPortIndex = 7 t.StartChannelIndex = 14 t.EndChannelIndex = 20 rep = t_RpdCapabilities.t_LcceChannelReachability() rep.EnetPortIndex = 5 rep.ChannelType = 2 rep.RfPortIndex = 7 rep.StartChannelIndex = 15 data.set_val(['cfg', 'RpdCapabilities', 'LcceChannelReachability'], rep) value = data.get_val([ 'cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 2, 7, 'StartChannelIndex' ]) self.assertEqual(value, 15) # Repeated element is replaced, not merged => value should be dropped value = data.get_val([ 'cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 2, 7, 'EndChannelIndex' ]) self.assertIsNone(value) self.assertEqual( len(data.data.cfg.RpdCapabilities.LcceChannelReachability), 1)
def test_db_malformed(self): """Create DB file, remove mandatory data and try to parse again.""" data = RPD_DB() core = db.operational.t_CCAPCapabilities() core.is_active = True core.is_principal = True core.ip_addr = "1.1.1.1" data.set_val(['oper', 'CCAPCapabilities'], core) data.set_val(['cfg', 'RpdCapabilities', 'NumBdirPorts'], 5) # DB file is now ready with values filled # Remove oper keyword with open(data.DB_FNAME, 'r+') as db_file: lines = db_file.readlines() db_file.seek(0) for line in lines: if not line.strip().startswith('"oper"'): db_file.write(line) db_file.truncate() # Try to parse malformed file -> load default values db2 = RPD_DB(load_all=True, init_file=data.DB_FNAME) # Check if malformed classes were removed self.assertIsNone( db2.get_val(['oper', 'CCAPCapabilities', '1.1.1.1', 'is_active'])) # Check if also other values (valid) values were dropped self.assertIsNone( db2.get_val(['cfg', 'RpdCapabilities', 'NumBdirPorts']))
def test_trees_merge_two_levels(self): data = RPD_DB() c = self._create_filled_cfg() data.set_val(['cfg'], c) c2 = cfg_pb2.config() d1 = c2.DsOfdmProfile.add() d1.ProfileId = 1 # Add another subcarrier under existing profileId d11 = d1.DsOfdmSubcarrierModulation.add() d11.StartSubcarrierId = 12 d11.EndSubcarrierId = 12 d11.Modulation = 1 # Change leaf which is not leaf d2 = c2.DsOfdmProfile.add() d2.ProfileId = 2 d21 = d2.DsOfdmSubcarrierModulation.add() d21.StartSubcarrierId = 21 d21.EndSubcarrierId = 21 d21.Modulation = 3 data.merge_from_tree(['cfg'], c2) value = data.get_val(['cfg', 'DsOfdmProfile']) self.assertEqual(len(value), 2) value = data.get_val( ['cfg', 'DsOfdmProfile', 1, 'DsOfdmSubcarrierModulation']) self.assertListEqual([x.StartSubcarrierId for x in value], [11, 12]) value = data.get_val( ['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation']) # One from two subcarriers specified - check if second was not removed self.assertEqual(len(value), 2) self.assertListEqual([x.Modulation for x in value], [3, 3])
def test_get_elm_from_repeated_list(self): """Get one element from repeated object list.""" data = RPD_DB() t = data.data.cfg.RpdCapabilities.LcceChannelReachability.add() t.EnetPortIndex = 5 t.ChannelType = 2 t.RfPortIndex = 7 # Completely different values t2 = data.data.cfg.RpdCapabilities.LcceChannelReachability.add() t2.EnetPortIndex = 4 t2.ChannelType = 3 t2.RfPortIndex = 5 # One value difference t3 = data.data.cfg.RpdCapabilities.LcceChannelReachability.add() t3.EnetPortIndex = 5 t3.ChannelType = 2 t3.RfPortIndex = 6 # Get element by numeric indices t_copy = data.get_val( ['cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 2, 7]) self.assertIs(t, t_copy) t2_copy = data.get_val( ['cfg', 'RpdCapabilities', 'LcceChannelReachability', 4, 3, 5]) self.assertIs(t2, t2_copy) t3_copy = data.get_val( ['cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 2, 6]) self.assertIs(t3, t3_copy)
def test_get_repeated_invalid(self): data = RPD_DB() t = data.data.cfg.RpdCapabilities.LcceChannelReachability.add() t.EnetPortIndex = 5 t.ChannelType = 2 t.RfPortIndex = 7 t.StartChannelIndex = 14 value = data.get_val([ 'cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 2, 8, 'StartChannelIndex' ]) self.assertIsNone(value) with self.assertRaises(DBKeyError): data.get_val([ 'cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 2, 7, 'test' ]) value = data.get_val( ['cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 2, 8]) self.assertIsNone(value)
def test_fill_trees_one_repeated(self): data = RPD_DB() self._create_repeated_list(data) ret = data.fill_tree({'cfg': {'DsOfdmProfile': {2: None}}}) self.assertIsInstance(ret, db) # Use get_val to extract profiles from returned object value = data.get_val(['cfg', 'DsOfdmProfile'], ret) self.assertEqual(len(value), 1) value = data.get_val( ['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation'], ret) self.assertEqual(len(value), 2)
def test_tree_del_container(self): data = RPD_DB() data.data.cfg.RpdCapabilities.NumBdirPorts = 5 data.data.cfg.DsScQamChannelConfig.PowerAdjust = 2 data.del_tree({'cfg': {'DsScQamChannelConfig': None}}) value = data.get_val(['cfg', 'DsScQamChannelConfig']) self.assertIsNone(value) value = data.get_val(['cfg']) # RpdCapabilities subtree should stay here self.assertEqual(len(value.ListFields()), 1)
def test_db_reload(self): """Fill some data to DB, save, load DB content from file.""" self.test_write_obj() # Create another DB instance to load values filled (& saved to file) db2 = RPD_DB(load_all=True, init_file=RPD_DB.DB_FNAME) path_to_bools = ['oper', 'CCAPCapabilities', '2.2.2.2'] self.assertTrue(db2.get_val(path_to_bools + ['is_active'])) # By default operational data are dropped during DB init db3 = RPD_DB(init_file=RPD_DB.DB_FNAME) self.assertIsNone(db3.get_val(path_to_bools + ['is_active']))
def test_del_unset(self): # Create one value and delete another value, which was not set data = RPD_DB() core = db.operational.t_CCAPCapabilities() core.is_principal = True core.ip_addr = '2.2.2.2' data.set_val(['oper', 'CCAPCapabilities'], core) data.del_val(['oper', 'CCAPCapabilities', '2.2.2.2', 'is_active']) self.assertIsNone( data.get_val(['oper', 'CCAPCapabilities', '2.2.2.2', 'is_active'])) self.assertTrue( data.get_val( ['oper', 'CCAPCapabilities', '2.2.2.2', 'is_principal']))
def test_tree_del_leaf(self): data = RPD_DB() data.data.Clear() data.data.cfg.RpdCapabilities.NumBdirPorts = 5 data.data.cfg.RpdCapabilities.NumDsRfPorts = 2 data.del_tree({'cfg': {'RpdCapabilities': {'NumBdirPorts': None}}}) value = data.get_val(['cfg', 'RpdCapabilities']) self.assertEqual(len(value.ListFields()), 1) self.assertIsNone( data.get_val(['cfg', 'RpdCapabilities', 'NumBdirPorts'])) self.assertEqual( data.get_val(['cfg', 'RpdCapabilities', 'NumDsRfPorts']), 2)
def test_get_elm_from_repeated(self): """Get one element from repeated object by both - numeric, string idx""" data = RPD_DB() t = data.data.cfg.RpdCapabilities.LcceChannelReachability.add() t.EnetPortIndex = 5 t.ChannelType = 2 t.RfPortIndex = 7 # Get element by numeric indices t2 = data.get_val( ['cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 2, 7]) self.assertIs(t, t2) # Get element by string indices t3 = data.get_val([ 'cfg', 'RpdCapabilities', 'LcceChannelReachability', '5', '2', '7' ]) self.assertIs(t, t3)
def test_add_elm_to_not_empty_list(self): data = RPD_DB() t = data.data.cfg.RpdCapabilities.LcceChannelReachability.add() t.EnetPortIndex = 5 t.ChannelType = 2 t.RfPortIndex = 7 t2 = data.data.cfg.RpdCapabilities.LcceChannelReachability.add() t2.EnetPortIndex = 4 t2.ChannelType = 3 t2.RfPortIndex = 5 rep_obj = t_RpdCapabilities.t_LcceChannelReachability() rep_obj.EnetPortIndex = 5 rep_obj.ChannelType = 4 rep_obj.RfPortIndex = 3 rep_obj.StartChannelIndex = 15 data.set_val(['cfg', 'RpdCapabilities', 'LcceChannelReachability'], [rep_obj]) clone = data.get_val( ['cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 4, 3]) self.assertEqual(clone.StartChannelIndex, 15) self.assertEqual(clone.EnetPortIndex, 5) self.assertEqual( len(data.data.cfg.RpdCapabilities.LcceChannelReachability), 3)
def test_add_more_elms_to_not_empty_list(self): data = RPD_DB() t = data.data.cfg.RpdCapabilities.LcceChannelReachability.add() t.EnetPortIndex = 5 t.ChannelType = 2 t.RfPortIndex = 7 rep = t_RpdCapabilities.t_LcceChannelReachability() rep.EnetPortIndex = 5 rep.ChannelType = 4 rep.RfPortIndex = 3 rep.StartChannelIndex = 15 rep2 = t_RpdCapabilities.t_LcceChannelReachability() rep2.EnetPortIndex = 5 rep2.ChannelType = 4 rep2.RfPortIndex = 2 data.set_val(['cfg', 'RpdCapabilities', 'LcceChannelReachability'], [rep, rep2]) clone = data.get_val( ['cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 4, 3]) self.assertEqual(clone.EnetPortIndex, 5) self.assertEqual( len(data.data.cfg.RpdCapabilities.LcceChannelReachability), 3)
def test_tree_del_repeated_all_elms(self): data = RPD_DB() data.data.Clear() self._create_repeated_list(data) data.del_tree({'cfg': {'DsOfdmProfile': {2: None, 1: None}}}) value = data.get_val(['cfg']) self.assertIsNone(value)
def test_two_repeated_in_path(self): """Check functionality with path + key + path + key + path.""" data = RPD_DB() self._create_repeated_list(data) # Check if values are really here value = data.get_val([ 'cfg', 'DsOfdmProfile', 1, 'DsOfdmSubcarrierModulation', 11, 'Modulation' ]) self.assertEqual(value, 1) value = data.get_val( ['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation', 21]) self.assertEqual(value.Modulation, 2) # Add next value to second level repeated list rep = cfg_pb2.config.t_DsOfdmProfile. \ t_DsOfdmSubcarrierModulation() rep.StartSubcarrierId = 23 rep.EndSubcarrierId = 23 rep.Modulation = 4 data.set_val(['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation'], [rep]) # Check if value was inserted to right place self.assertEqual(len(data.data.cfg.DsOfdmProfile), 2) value = data.get_val( ['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation']) self.assertEqual(len(value), 3) # Update one from original values rep = cfg_pb2.config.t_DsOfdmProfile. \ t_DsOfdmSubcarrierModulation() rep.StartSubcarrierId = 21 rep.EndSubcarrierId = 21 rep.Modulation = 5 data.set_val(['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation'], [rep]) # Check if update was successful value = data.get_val( ['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation']) self.assertEqual(len(value), 3) # Update changed values from 2,3,4 to 3,4,5, because first element was # deleted and new one was appended to end of the list self.assertListEqual([x.Modulation for x in value], [3, 4, 5])
def test_del_valid(self): # Write on value and delete it data = RPD_DB() core = db.operational.t_CCAPCapabilities() core.is_principal = True core.ip_addr = '2.2.2.2' core.is_active = True data.set_val(['oper', 'CCAPCapabilities'], core) data.del_val(['oper', 'CCAPCapabilities', '2.2.2.2', 'is_active']) self.assertIsNone( data.get_val(['oper', 'CCAPCapabilities', 'is_active']))
def test_write_leaf(self): """Use set_val to write value to one leaf.""" data = RPD_DB() path = ['oper', 'HwVersion'] data.set_val(path, 'ver1.2.3') self.assertEqual(data.get_val(path), 'ver1.2.3') str1 = data.data.SerializeToString() db2 = RPD_DB(init_file=RPD_DB.DB_FNAME, load_all=True) str2 = db2.data.SerializeToString() self.assertEqual(str1, str2)
def test_write_obj(self): """Fill GPB, set it to correct position and check value in DB.""" data = RPD_DB() core = db.operational.t_CCAPCapabilities() core.is_principal = True core.ip_addr = '2.2.2.2' core.is_active = True data.set_val(['oper', 'CCAPCapabilities'], core) self.assertTrue( data.get_val( ['oper', 'CCAPCapabilities', '2.2.2.2', 'is_principal']))
def test_tree_del_more_values(self): data = RPD_DB() data.data.Clear() data.data.cfg.RpdCapabilities.NumBdirPorts = 5 data.data.cfg.DsScQamChannelConfig.PowerAdjust = 2 data.data.cfg.RpdCapabilities.NumDsRfPorts = 4 data.del_tree({ 'cfg': { 'DsScQamChannelConfig': { 'PowerAdjust': None }, 'RpdCapabilities': { 'NumDsRfPorts': None } } }) value = data.get_val(['cfg']) self.assertEqual(len(value.ListFields()), 1) value = data.get_val(['cfg', 'RpdCapabilities']) self.assertEqual(len(value.ListFields()), 1)
def test_path_invalid(self): # Delete with wrong path data = RPD_DB() # If parent (oper) has no children, then exception is not raised, # because parent exists and we don't have reason to check children data.del_val(['oper', 'CCAPCapabilities', 'test']) self.assertIsNone(data.get_val(['oper', 'CCAPCapabilities', 'test'])) core = db.operational.t_CCAPCapabilities() core.is_active = True core.ip_addr = '2.2.2.2' data.set_val(['oper', 'CCAPCapabilities'], core) # Deleting from repeated list, element, which does not exist data.del_val(['oper', 'CCAPCapabilities', 'test']) # Deleting from path, which is invalid with self.assertRaises(DBKeyError): data.del_val(['oper', 'test']) # Getting value from repeated list, path is valid data.del_val(['oper', 'CCAPCapabilities', 'test']) # Getting value from invalid path with self.assertRaises(DBKeyError): data.get_val(['oper', 'test']) with self.assertRaises(DBKeyError): data.set_val(['oper', 'CCAPCapabilities', 'test'], True)
def test_fill_trees_list_and_leaf(self): data = RPD_DB() self._create_repeated_list(data) data.data.cfg.RpdCapabilities.NumBdirPorts = 5 data.data.cfg.RpdCapabilities.NumTenGeNsPorts = 14 ret = data.fill_tree({ 'cfg': { 'DsOfdmProfile': None, 'RpdCapabilities': { 'NumTenGeNsPorts': None } } }) # Reuse get_val to extract profiles from returned object value = data.get_val(['cfg', 'DsOfdmProfile'], ret) self.assertEqual(len(value), 2) self.assertListEqual([x.ProfileId for x in value], [1, 2]) value = data.get_val( ['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation'], ret) self.assertEqual(len(value), 2) value = data.get_val(['cfg', 'RpdCapabilities'], ret) self.assertEqual(len(value.ListFields()), 1) self.assertEqual(value.NumTenGeNsPorts, 14)
def test_trees_merge_no_conflicts(self): data = RPD_DB() data.data.Clear() c = self._create_filled_cfg() # Add one element, merge again and check if value was not overwritten data.data.cfg.RpdCapabilities.NumBdirPorts = 5 data.merge_from_tree(['cfg'], c) str1 = data.data.cfg.SerializeToString() str2 = c.SerializeToString() self.assertNotEqual(str1, str2) self.assertEqual( data.get_val(['cfg', 'RpdCapabilities', 'NumBdirPorts']), 5) # Delete additional item, check if buffers are same now data.del_val(['cfg', 'RpdCapabilities', 'NumBdirPorts']) str1 = data.data.cfg.SerializeToString() self.assertEqual(str1, str2)
def test_add_elm_to_repeated_list(self): """Add element to empty list and read it back.""" data = RPD_DB() rep_obj = t_RpdCapabilities.t_LcceChannelReachability() rep_obj.EnetPortIndex = 5 rep_obj.ChannelType = 4 rep_obj.RfPortIndex = 3 rep_obj.StartChannelIndex = 15 data.set_val(['cfg', 'RpdCapabilities', 'LcceChannelReachability'], [rep_obj]) clone = data.get_val( ['cfg', 'RpdCapabilities', 'LcceChannelReachability', 5, 4, 3]) # Set val is doing deep copy, so object should not be same self.assertIsNot(rep_obj, clone) self.assertEqual(clone.StartChannelIndex, 15) self.assertEqual( len(data.data.cfg.RpdCapabilities.LcceChannelReachability), 1)
def test_trees_merge_repeated_no_conflict(self): data = RPD_DB() c = self._create_filled_cfg() data.set_val(['cfg'], c) # DB is prepared, create another copy with added element on level 1 c2 = self._create_filled_cfg() d3 = c2.DsOfdmProfile.add() d3.ProfileId = 4 d11 = d3.DsOfdmSubcarrierModulation.add() d11.StartSubcarrierId = 15 d11.EndSubcarrierId = 15 d11.Modulation = 6 data.merge_from_tree(['cfg'], c2) value = data.get_val(['cfg', 'DsOfdmProfile']) self.assertEqual(len(value), 3) self.assertListEqual([x.ProfileId for x in value], [1, 2, 4])
def test_delete_repeated(self): """Delete repeated object & leaves in it.""" data = RPD_DB() self._create_repeated_list(data) # Delete leaf data.del_val([ 'cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation', 21, 'Modulation' ]) self.assertIsNone( data.get_val([ 'cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation', 21, 'Modulation' ])) self.assertIsNotNone( data.get_val( ['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation', 21])) # Delete key leaf => forbidden with self.assertRaises(DBKeyError): data.del_val([ 'cfg', 'DsOfdmProfile', 1, 'DsOfdmSubcarrierModulation', 11, 'StartSubcarrierId' ]) # Delete last (one) element from repeated list data.del_val( ['cfg', 'DsOfdmProfile', 1, 'DsOfdmSubcarrierModulation', 11]) value = data.get_val( ['cfg', 'DsOfdmProfile', 1, 'DsOfdmSubcarrierModulation']) self.assertIsNone(value) # Delete list of elements value = data.get_val( ['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation']) self.assertEqual(len(value), 2) data.del_val(['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation']) value = data.get_val( ['cfg', 'DsOfdmProfile', 2, 'DsOfdmSubcarrierModulation']) self.assertIsNone(value) # Delete config data.del_val(['cfg']) value = data.get_val(['cfg']) self.assertIsNone(value)
def test_trees_merge_conflict(self): data = RPD_DB() caps = t_RpdCapabilities() caps.NumBdirPorts = 5 caps.NumDsRfPorts = 6 data.set_val(['cfg', 'RpdCapabilities'], caps) # Create second instance with conflict caps2 = t_RpdCapabilities() caps2.NumDsRfPorts = 7 caps2.NumTenGeNsPorts = 8 # Merge tree to DB data.merge_from_tree(['cfg', 'RpdCapabilities'], caps2) value = data.get_val(['cfg', 'RpdCapabilities']) self.assertEqual(value.NumBdirPorts, 5) self.assertEqual(value.NumDsRfPorts, 7) self.assertEqual(value.NumTenGeNsPorts, 8) self.assertEqual(len(value.ListFields()), 3)
def test_read_unset_leaf(self): """Read unset leaves, expected value is default.""" data = RPD_DB() self.assertIsNone(data.get_val(['oper', 'HwVersion'])) self.assertIsNone(data.get_val(['oper', 'CCAPCapabilities']))