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