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 | |
| 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): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | """Tests of the run command. |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 29 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 30 | Note that detailed tests of replication behavior should be done in |
| 31 | replication_lib_unittest. |
| 32 | """ |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 33 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 34 | def setUp(self): |
| 35 | file_layout = (D("src", ["audio_file"]),) |
| 36 | cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout) |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 37 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | self.audio_path = os.path.join("src", "audio_file") |
| 39 | self.WriteTempFile(self.audio_path, "[Speaker A Settings]") |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 40 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir) |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 42 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | def testRun(self): |
| 44 | """Basic test of the 'run' command.""" |
| 45 | audio_dst_path = os.path.join("dst", "audio_file") |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | replication_config = ReplicationConfig( |
| 48 | file_replication_rules=[ |
| 49 | FileReplicationRule( |
| 50 | source_path=self.audio_path, |
| 51 | destination_path=audio_dst_path, |
| 52 | file_type=FILE_TYPE_OTHER, |
| 53 | replication_type=REPLICATION_TYPE_COPY, |
| 54 | ), |
| 55 | ] |
| 56 | ) |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 57 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | replication_config_path = os.path.join( |
| 59 | self.tempdir, "replication_config.jsonpb" |
| 60 | ) |
| 61 | osutils.WriteFile( |
| 62 | replication_config_path, |
| 63 | json_format.MessageToJson(replication_config), |
| 64 | ) |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 65 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | replication_util.main(["run", replication_config_path]) |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 67 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | expected_file_layout = ( |
| 69 | D("src", ["audio_file"]), |
| 70 | D("dst", ["audio_file"]), |
| 71 | "replication_config.jsonpb", |
| 72 | ) |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | cros_test_lib.VerifyOnDiskHierarchy(self.tempdir, expected_file_layout) |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | self.assertTempFileContents(audio_dst_path, "[Speaker A Settings]") |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 77 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | def testUnknownFieldInConfig(self): |
| 79 | """Test that unknown fields in the ReplicationConfig cause an error.""" |
| 80 | audio_dst_path = os.path.join("dst", "audio_file") |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 81 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | replication_config = ReplicationConfig( |
| 83 | file_replication_rules=[ |
| 84 | FileReplicationRule( |
| 85 | source_path=self.audio_path, |
| 86 | destination_path=audio_dst_path, |
| 87 | file_type=FILE_TYPE_OTHER, |
| 88 | replication_type=REPLICATION_TYPE_COPY, |
| 89 | ), |
| 90 | ] |
| 91 | ) |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 92 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | replication_config_path = os.path.join( |
| 94 | self.tempdir, "replication_config.jsonpb" |
| 95 | ) |
| 96 | replication_config_dict = json_format.MessageToDict(replication_config) |
| 97 | replication_config_dict["new_field"] = 1 |
| 98 | osutils.WriteFile( |
| 99 | replication_config_path, json.dumps(replication_config_dict) |
| 100 | ) |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 101 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 102 | with self.assertRaises(json_format.ParseError): |
| 103 | replication_util.main(["run", replication_config_path]) |