blob: d572579b531c9916e69c51466a98d11f84b143c4 [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):
Andrew Lamb16f02bf2019-11-04 14:44:34 -070028 """Tests of the run command.
29
30 Note that detailed tests of replication behavior should be done in
31 replication_lib_unittest.
32 """
33
34 def setUp(self):
35 file_layout = (D('src', ['audio_file']),)
36 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
37
Andrew Lambca1f35b2019-12-04 09:37:11 -070038 self.audio_path = os.path.join('src', 'audio_file')
39 self.WriteTempFile(self.audio_path, '[Speaker A Settings]')
40
41 self.PatchObject(constants, 'SOURCE_ROOT', new=self.tempdir)
Andrew Lamb16f02bf2019-11-04 14:44:34 -070042
43 def testRun(self):
Mike Frysingerdf1d0b02019-11-12 17:44:12 -050044 """Basic test of the 'run' command."""
Andrew Lambca1f35b2019-12-04 09:37:11 -070045 audio_dst_path = os.path.join('dst', 'audio_file')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070046
47 replication_config = ReplicationConfig(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
56 replication_config_path = os.path.join(self.tempdir,
57 'replication_config.jsonpb')
58 osutils.WriteFile(replication_config_path,
59 json_format.MessageToJson(replication_config))
60
61 replication_util.main(['run', replication_config_path])
62
63 expected_file_layout = (
64 D('src', ['audio_file']),
65 D('dst', ['audio_file']),
66 'replication_config.jsonpb',
67 )
68
69 cros_test_lib.VerifyOnDiskHierarchy(self.tempdir, expected_file_layout)
70
Andrew Lambca1f35b2019-12-04 09:37:11 -070071 self.assertTempFileContents(audio_dst_path, '[Speaker A Settings]')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070072
73 def testUnknownFieldInConfig(self):
74 """Test that unknown fields in the ReplicationConfig cause an error."""
Andrew Lambca1f35b2019-12-04 09:37:11 -070075 audio_dst_path = os.path.join('dst', 'audio_file')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070076
77 replication_config = ReplicationConfig(file_replication_rules=[
78 FileReplicationRule(
79 source_path=self.audio_path,
80 destination_path=audio_dst_path,
81 file_type=FILE_TYPE_OTHER,
82 replication_type=REPLICATION_TYPE_COPY,
83 ),
84 ])
85
86 replication_config_path = os.path.join(self.tempdir,
87 'replication_config.jsonpb')
88 replication_config_dict = json_format.MessageToDict(replication_config)
89 replication_config_dict['new_field'] = 1
90 osutils.WriteFile(replication_config_path,
91 json.dumps(replication_config_dict))
92
93 with self.assertRaises(json_format.ParseError):
94 replication_util.main(['run', replication_config_path])