Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Unit tests for replication_util.""" |
| 6 | |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 7 | import json |
| 8 | import os |
| 9 | |
Mike Frysinger | 2c02406 | 2021-05-22 15:43:22 -0400 | [diff] [blame] | 10 | from chromite.third_party.google.protobuf import json_format |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 11 | |
| 12 | from chromite.api.gen.config.replication_config_pb2 import ( |
Mike Frysinger | 807d828 | 2022-04-28 22:45:17 -0400 | [diff] [blame] | 13 | FILE_TYPE_OTHER, |
| 14 | FileReplicationRule, |
| 15 | REPLICATION_TYPE_COPY, |
| 16 | ReplicationConfig, |
| 17 | ) |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 18 | from chromite.lib import constants |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_test_lib |
| 20 | from chromite.lib import osutils |
| 21 | from chromite.scripts import replication_util |
| 22 | |
Mike Frysinger | f8d9ba5 | 2020-02-10 23:48:22 -0500 | [diff] [blame] | 23 | |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 24 | D = cros_test_lib.Directory |
| 25 | |
| 26 | |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 27 | class RunTest(cros_test_lib.MockTempDirTestCase): |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 28 | """Tests of the run command. |
| 29 | |
| 30 | Note that detailed tests of replication behavior should be done in |
| 31 | replication_lib_unittest. |
| 32 | """ |
| 33 | |
| 34 | def setUp(self): |
| 35 | file_layout = (D('src', ['audio_file']),) |
| 36 | cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout) |
| 37 | |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 38 | self.audio_path = os.path.join('src', 'audio_file') |
| 39 | self.WriteTempFile(self.audio_path, '[Speaker A Settings]') |
| 40 | |
| 41 | self.PatchObject(constants, 'SOURCE_ROOT', new=self.tempdir) |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 42 | |
| 43 | def testRun(self): |
Mike Frysinger | df1d0b0 | 2019-11-12 17:44:12 -0500 | [diff] [blame] | 44 | """Basic test of the 'run' command.""" |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 45 | audio_dst_path = os.path.join('dst', 'audio_file') |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 46 | |
| 47 | replication_config = ReplicationConfig(file_replication_rules=[ |
| 48 | FileReplicationRule( |
| 49 | source_path=self.audio_path, |
| 50 | destination_path=audio_dst_path, |
| 51 | file_type=FILE_TYPE_OTHER, |
| 52 | replication_type=REPLICATION_TYPE_COPY, |
| 53 | ), |
| 54 | ]) |
| 55 | |
| 56 | replication_config_path = os.path.join(self.tempdir, |
| 57 | 'replication_config.jsonpb') |
| 58 | osutils.WriteFile(replication_config_path, |
| 59 | json_format.MessageToJson(replication_config)) |
| 60 | |
| 61 | replication_util.main(['run', replication_config_path]) |
| 62 | |
| 63 | expected_file_layout = ( |
| 64 | D('src', ['audio_file']), |
| 65 | D('dst', ['audio_file']), |
| 66 | 'replication_config.jsonpb', |
| 67 | ) |
| 68 | |
| 69 | cros_test_lib.VerifyOnDiskHierarchy(self.tempdir, expected_file_layout) |
| 70 | |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 71 | self.assertTempFileContents(audio_dst_path, '[Speaker A Settings]') |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 72 | |
| 73 | def testUnknownFieldInConfig(self): |
| 74 | """Test that unknown fields in the ReplicationConfig cause an error.""" |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 75 | audio_dst_path = os.path.join('dst', 'audio_file') |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 76 | |
| 77 | replication_config = ReplicationConfig(file_replication_rules=[ |
| 78 | FileReplicationRule( |
| 79 | source_path=self.audio_path, |
| 80 | destination_path=audio_dst_path, |
| 81 | file_type=FILE_TYPE_OTHER, |
| 82 | replication_type=REPLICATION_TYPE_COPY, |
| 83 | ), |
| 84 | ]) |
| 85 | |
| 86 | replication_config_path = os.path.join(self.tempdir, |
| 87 | 'replication_config.jsonpb') |
| 88 | replication_config_dict = json_format.MessageToDict(replication_config) |
| 89 | replication_config_dict['new_field'] = 1 |
| 90 | osutils.WriteFile(replication_config_path, |
| 91 | json.dumps(replication_config_dict)) |
| 92 | |
| 93 | with self.assertRaises(json_format.ParseError): |
| 94 | replication_util.main(['run', replication_config_path]) |