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