blob: 7c89344aa74ded12ff53cd1a5e6cd906ece37318 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Andrew Lamb16f02bf2019-11-04 14:44:34 -07002# 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 Lamb16f02bf2019-11-04 14:44:34 -07007import json
8import os
9
Mike Frysinger2c024062021-05-22 15:43:22 -040010from chromite.third_party.google.protobuf import json_format
Andrew Lamb16f02bf2019-11-04 14:44:34 -070011
Trent Apted39e74d32023-09-04 11:24:40 +100012from chromite.api.gen.config import replication_config_pb2
Andrew Lambca1f35b2019-12-04 09:37:11 -070013from chromite.lib import constants
Andrew Lamb16f02bf2019-11-04 14:44:34 -070014from chromite.lib import cros_test_lib
15from chromite.lib import osutils
16from chromite.scripts import replication_util
17
Mike Frysingerf8d9ba52020-02-10 23:48:22 -050018
Andrew Lamb16f02bf2019-11-04 14:44:34 -070019D = cros_test_lib.Directory
Trent Apted39e74d32023-09-04 11:24:40 +100020FILE_TYPE_OTHER = replication_config_pb2.FILE_TYPE_OTHER
21FileReplicationRule = replication_config_pb2.FileReplicationRule
22REPLICATION_TYPE_COPY = replication_config_pb2.REPLICATION_TYPE_COPY
23ReplicationConfig = replication_config_pb2.ReplicationConfig
Andrew Lamb16f02bf2019-11-04 14:44:34 -070024
25
Andrew Lambca1f35b2019-12-04 09:37:11 -070026class RunTest(cros_test_lib.MockTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060027 """Tests of the run command.
Andrew Lamb16f02bf2019-11-04 14:44:34 -070028
Alex Klein1699fab2022-09-08 08:46:06 -060029 Note that detailed tests of replication behavior should be done in
30 replication_lib_unittest.
31 """
Andrew Lamb16f02bf2019-11-04 14:44:34 -070032
Alex Klein1699fab2022-09-08 08:46:06 -060033 def setUp(self):
34 file_layout = (D("src", ["audio_file"]),)
35 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Andrew Lamb16f02bf2019-11-04 14:44:34 -070036
Alex Klein1699fab2022-09-08 08:46:06 -060037 self.audio_path = os.path.join("src", "audio_file")
38 self.WriteTempFile(self.audio_path, "[Speaker A Settings]")
Andrew Lambca1f35b2019-12-04 09:37:11 -070039
Alex Klein1699fab2022-09-08 08:46:06 -060040 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Andrew Lamb16f02bf2019-11-04 14:44:34 -070041
Alex Klein1699fab2022-09-08 08:46:06 -060042 def testRun(self):
43 """Basic test of the 'run' command."""
44 audio_dst_path = os.path.join("dst", "audio_file")
Andrew Lamb16f02bf2019-11-04 14:44:34 -070045
Alex Klein1699fab2022-09-08 08:46:06 -060046 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 Lamb16f02bf2019-11-04 14:44:34 -070056
Alex Klein1699fab2022-09-08 08:46:06 -060057 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 Lamb16f02bf2019-11-04 14:44:34 -070064
Alex Klein1699fab2022-09-08 08:46:06 -060065 replication_util.main(["run", replication_config_path])
Andrew Lamb16f02bf2019-11-04 14:44:34 -070066
Alex Klein1699fab2022-09-08 08:46:06 -060067 expected_file_layout = (
68 D("src", ["audio_file"]),
69 D("dst", ["audio_file"]),
70 "replication_config.jsonpb",
71 )
Andrew Lamb16f02bf2019-11-04 14:44:34 -070072
Alex Klein1699fab2022-09-08 08:46:06 -060073 cros_test_lib.VerifyOnDiskHierarchy(self.tempdir, expected_file_layout)
Andrew Lamb16f02bf2019-11-04 14:44:34 -070074
Alex Klein1699fab2022-09-08 08:46:06 -060075 self.assertTempFileContents(audio_dst_path, "[Speaker A Settings]")
Andrew Lamb16f02bf2019-11-04 14:44:34 -070076
Alex Klein1699fab2022-09-08 08:46:06 -060077 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 Lamb16f02bf2019-11-04 14:44:34 -070080
Alex Klein1699fab2022-09-08 08:46:06 -060081 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 Lamb16f02bf2019-11-04 14:44:34 -070091
Alex Klein1699fab2022-09-08 08:46:06 -060092 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 Lamb16f02bf2019-11-04 14:44:34 -0700100
Alex Klein1699fab2022-09-08 08:46:06 -0600101 with self.assertRaises(json_format.ParseError):
102 replication_util.main(["run", replication_config_path])