blob: 351bdffcb83750bd802f1303809771485315b2c4 [file] [log] [blame]
Andrew Lamb16f02bf2019-11-04 14:44:34 -07001# 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 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
12from chromite.api.gen.config.replication_config_pb2 import (
Mike Frysinger807d8282022-04-28 22:45:17 -040013 FILE_TYPE_OTHER,
14 FileReplicationRule,
15 REPLICATION_TYPE_COPY,
16 ReplicationConfig,
17)
Andrew Lambca1f35b2019-12-04 09:37:11 -070018from chromite.lib import constants
Andrew Lamb16f02bf2019-11-04 14:44:34 -070019from chromite.lib import cros_test_lib
20from chromite.lib import osutils
21from chromite.scripts import replication_util
22
Mike Frysingerf8d9ba52020-02-10 23:48:22 -050023
Andrew Lamb16f02bf2019-11-04 14:44:34 -070024D = cros_test_lib.Directory
25
26
Andrew Lambca1f35b2019-12-04 09:37:11 -070027class RunTest(cros_test_lib.MockTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060028 """Tests of the run command.
Andrew Lamb16f02bf2019-11-04 14:44:34 -070029
Alex Klein1699fab2022-09-08 08:46:06 -060030 Note that detailed tests of replication behavior should be done in
31 replication_lib_unittest.
32 """
Andrew Lamb16f02bf2019-11-04 14:44:34 -070033
Alex Klein1699fab2022-09-08 08:46:06 -060034 def setUp(self):
35 file_layout = (D("src", ["audio_file"]),)
36 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Andrew Lamb16f02bf2019-11-04 14:44:34 -070037
Alex Klein1699fab2022-09-08 08:46:06 -060038 self.audio_path = os.path.join("src", "audio_file")
39 self.WriteTempFile(self.audio_path, "[Speaker A Settings]")
Andrew Lambca1f35b2019-12-04 09:37:11 -070040
Alex Klein1699fab2022-09-08 08:46:06 -060041 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Andrew Lamb16f02bf2019-11-04 14:44:34 -070042
Alex Klein1699fab2022-09-08 08:46:06 -060043 def testRun(self):
44 """Basic test of the 'run' command."""
45 audio_dst_path = os.path.join("dst", "audio_file")
Andrew Lamb16f02bf2019-11-04 14:44:34 -070046
Alex Klein1699fab2022-09-08 08:46:06 -060047 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 Lamb16f02bf2019-11-04 14:44:34 -070057
Alex Klein1699fab2022-09-08 08:46:06 -060058 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 Lamb16f02bf2019-11-04 14:44:34 -070065
Alex Klein1699fab2022-09-08 08:46:06 -060066 replication_util.main(["run", replication_config_path])
Andrew Lamb16f02bf2019-11-04 14:44:34 -070067
Alex Klein1699fab2022-09-08 08:46:06 -060068 expected_file_layout = (
69 D("src", ["audio_file"]),
70 D("dst", ["audio_file"]),
71 "replication_config.jsonpb",
72 )
Andrew Lamb16f02bf2019-11-04 14:44:34 -070073
Alex Klein1699fab2022-09-08 08:46:06 -060074 cros_test_lib.VerifyOnDiskHierarchy(self.tempdir, expected_file_layout)
Andrew Lamb16f02bf2019-11-04 14:44:34 -070075
Alex Klein1699fab2022-09-08 08:46:06 -060076 self.assertTempFileContents(audio_dst_path, "[Speaker A Settings]")
Andrew Lamb16f02bf2019-11-04 14:44:34 -070077
Alex Klein1699fab2022-09-08 08:46:06 -060078 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 Lamb16f02bf2019-11-04 14:44:34 -070081
Alex Klein1699fab2022-09-08 08:46:06 -060082 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 Lamb16f02bf2019-11-04 14:44:34 -070092
Alex Klein1699fab2022-09-08 08:46:06 -060093 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 Lamb16f02bf2019-11-04 14:44:34 -0700101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 with self.assertRaises(json_format.ParseError):
103 replication_util.main(["run", replication_config_path])