def testCreateFilteredDictWithOverlappingSubFields(self): """Tests selecting overlapping sub-fields.""" original_dict = { 'a': { 'b': { 'c': 'val1', 'd': 2, 'e': 3, }, }, } field_mask = field_mask_pb2.FieldMask(paths=['a.b.c', 'a.b.e']) expected_dict = { 'a': { 'b': { 'c': 'val1', 'e': 3, }, }, } merged_dict = field_mask_util.CreateFilteredDict( field_mask, original_dict) self.assertDictEqual(expected_dict, merged_dict)
def Replicate(replication_config): """Run the replication described in replication_config. Args: replication_config: (ReplicationConfig) Describes the replication to run. """ # Validate all rules before any of them are run, to decrease chance of ending # with a partial replication. for rule in replication_config.file_replication_rules: _ValidateFileReplicationRule(rule) for rule in replication_config.file_replication_rules: logging.info('Processing FileReplicationRule: %s', rule) src = os.path.join(constants.SOURCE_ROOT, rule.source_path) dst = os.path.join(constants.SOURCE_ROOT, rule.destination_path) osutils.SafeMakedirs(os.path.dirname(dst)) if rule.file_type == replication_config_pb2.FILE_TYPE_JSON: assert (rule.replication_type == replication_config_pb2.REPLICATION_TYPE_FILTER) assert rule.destination_fields.paths with open(src, 'r') as f: source_json = json.load(f) try: source_device_configs = source_json['chromeos']['configs'] except KeyError: raise NotImplementedError(( 'Currently only ChromeOS Configs are supported (expected file %s ' 'to have a list at "$.chromeos.configs")') % src) destination_device_configs = [] for source_device_config in source_device_configs: destination_device_configs.append( field_mask_util.CreateFilteredDict(rule.destination_fields, source_device_config)) destination_json = { 'chromeos': { 'configs': destination_device_configs } } logging.info('Writing filtered JSON source to %s', dst) pformat.json(destination_json, fp=dst) else: assert rule.file_type == replication_config_pb2.FILE_TYPE_OTHER assert (rule.replication_type == replication_config_pb2.REPLICATION_TYPE_COPY) assert not rule.destination_fields.paths logging.info('Copying full file from %s to %s', src, dst) shutil.copy2(src, dst) if rule.string_replacement_rules: _ApplyStringReplacementRules(dst, rule.string_replacement_rules)
def testCreateFilteredDictWithEmptyPath(self): """Tests a FieldMask with the empty string as a path.""" original_dict = {'a': 1} field_mask = field_mask_pb2.FieldMask(paths=['']) with self.assertRaisesRegex(ValueError, 'Field cannot be empty string'): field_mask_util.CreateFilteredDict(field_mask, original_dict)
def testCreateFilteredDictWithEmptyFieldMask(self): """Tests a FieldMask with no paths.""" original_dict = {'a': 1} field_mask = field_mask_pb2.FieldMask() merged_dict = field_mask_util.CreateFilteredDict(field_mask, original_dict) self.assertDictEqual({}, merged_dict)
def testCreateFilteredDictWithInvalidPath(self): """Tests a FieldMask with an invalid path.""" original_dict = {'a': 1, 'c': 2} # Note that 'b' is not a path in the dict. field_mask = field_mask_pb2.FieldMask(paths=['a', 'b']) expected_dict = {'a': 1} with cros_test_lib.LoggingCapturer() as logging_capturer: merged_dict = field_mask_util.CreateFilteredDict( field_mask, original_dict) # The merged dict should contain only field 'a' ('b' does not exist, 'c' # is not in the field mask). A warning about 'b' was logged. self.assertTrue(logging_capturer.LogsContain('Field b not found.')) self.assertDictEqual(expected_dict, merged_dict)
def testCreateFilteredDictWithFieldAndSubField(self): """Tests selecting top-level fields and sub-fields.""" original_dict = { 'a': { 'b': { 'c': 'val1', 'd': 2, } }, 'e': { 'f': { 'g': 'val2', 'h': 3, } }, 'i': 3, 'j': 'val3', } field_mask = field_mask_pb2.FieldMask(paths=['a.b.c', 'e.f', 'i']) expected_dict = { 'a': { 'b': { 'c': 'val1', } }, 'e': { 'f': { 'g': 'val2', 'h': 3, } }, 'i': 3, } merged_dict = field_mask_util.CreateFilteredDict( field_mask, original_dict) self.assertDictEqual(expected_dict, merged_dict)
def testCreateFilteredDictWithListSubFields(self): """Tests selecting a list that is not the last position in a path.""" original_dict = { 'a': [1, 2], 'b': [3, 4], 'c': { 'd': [5, 6], 'e': [7, 8], }, 'f': [{ 'g': 9, 'h': 10, }, { 'g': 11, 'h': 12, }], } field_mask = field_mask_pb2.FieldMask(paths=['f.g']) with self.assertRaisesRegex( ValueError, 'Field f is a list and cannot have sub-fields'): field_mask_util.CreateFilteredDict(field_mask, original_dict)
def testCreateFilteredDictWithLists(self): """Tests selecting fields that are lists.""" original_dict = { 'a': [1, 2], 'b': [3, 4], 'c': { 'd': [5, 6], 'e': [7, 8], }, 'f': [{ 'g': 9, 'h': 10, }, { 'g': 11, 'h': 12, }], } field_mask = field_mask_pb2.FieldMask(paths=['a', 'c.d', 'f']) expected_dict = { 'a': [1, 2], 'c': { 'd': [5, 6], }, 'f': [{ 'g': 9, 'h': 10, }, { 'g': 11, 'h': 12, }], } merged_dict = field_mask_util.CreateFilteredDict( field_mask, original_dict) self.assertDictEqual(expected_dict, merged_dict)