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_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_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_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_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_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_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_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_db_file_update(self): """Insert/delete CCAPCores and check if DB file was updated.""" data = RPD_DB() self.assertNotIn('is_active', open(data.DB_FNAME).read()) core = db.operational.t_CCAPCapabilities() core.is_active = True core.ip_addr = "1.1.1.1" data.set_val(['oper', 'CCAPCapabilities'], core) self.assertIn('is_active', open(data.DB_FNAME).read()) data.del_val(['oper', 'CCAPCapabilities', '1.1.1.1']) self.assertNotIn('is_active', open(data.DB_FNAME).read())
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_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_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_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_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_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_write_invalid(self): """Set invalid value to valid path.""" data = RPD_DB() with self.assertRaises(TypeError): data.set_val(['oper', 'CCAPCapabilities', 'is_active'], None)