blob: eea31f158013b78e0dceaac654499f4506945eeb [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
10from google.protobuf import json_format
11
12from chromite.api.gen.config.replication_config_pb2 import (
13 ReplicationConfig, FileReplicationRule, FILE_TYPE_OTHER,
14 REPLICATION_TYPE_COPY)
Andrew Lambca1f35b2019-12-04 09:37:11 -070015from chromite.lib import constants
Andrew Lamb16f02bf2019-11-04 14:44:34 -070016from chromite.lib import cros_test_lib
17from chromite.lib import osutils
18from chromite.scripts import replication_util
19
Mike Frysingerf8d9ba52020-02-10 23:48:22 -050020
Andrew Lamb16f02bf2019-11-04 14:44:34 -070021D = cros_test_lib.Directory
22
23
Andrew Lambca1f35b2019-12-04 09:37:11 -070024class RunTest(cros_test_lib.MockTempDirTestCase):
Andrew Lamb16f02bf2019-11-04 14:44:34 -070025 """Tests of the run command.
26
27 Note that detailed tests of replication behavior should be done in
28 replication_lib_unittest.
29 """
30
31 def setUp(self):
32 file_layout = (D('src', ['audio_file']),)
33 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
34
Andrew Lambca1f35b2019-12-04 09:37:11 -070035 self.audio_path = os.path.join('src', 'audio_file')
36 self.WriteTempFile(self.audio_path, '[Speaker A Settings]')
37
38 self.PatchObject(constants, 'SOURCE_ROOT', new=self.tempdir)
Andrew Lamb16f02bf2019-11-04 14:44:34 -070039
40 def testRun(self):
Mike Frysingerdf1d0b02019-11-12 17:44:12 -050041 """Basic test of the 'run' command."""
Andrew Lambca1f35b2019-12-04 09:37:11 -070042 audio_dst_path = os.path.join('dst', 'audio_file')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070043
44 replication_config = ReplicationConfig(file_replication_rules=[
45 FileReplicationRule(
46 source_path=self.audio_path,
47 destination_path=audio_dst_path,
48 file_type=FILE_TYPE_OTHER,
49 replication_type=REPLICATION_TYPE_COPY,
50 ),
51 ])
52
53 replication_config_path = os.path.join(self.tempdir,
54 'replication_config.jsonpb')
55 osutils.WriteFile(replication_config_path,
56 json_format.MessageToJson(replication_config))
57
58 replication_util.main(['run', replication_config_path])
59
60 expected_file_layout = (
61 D('src', ['audio_file']),
62 D('dst', ['audio_file']),
63 'replication_config.jsonpb',
64 )
65
66 cros_test_lib.VerifyOnDiskHierarchy(self.tempdir, expected_file_layout)
67
Andrew Lambca1f35b2019-12-04 09:37:11 -070068 self.assertTempFileContents(audio_dst_path, '[Speaker A Settings]')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070069
70 def testUnknownFieldInConfig(self):
71 """Test that unknown fields in the ReplicationConfig cause an error."""
Andrew Lambca1f35b2019-12-04 09:37:11 -070072 audio_dst_path = os.path.join('dst', 'audio_file')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070073
74 replication_config = ReplicationConfig(file_replication_rules=[
75 FileReplicationRule(
76 source_path=self.audio_path,
77 destination_path=audio_dst_path,
78 file_type=FILE_TYPE_OTHER,
79 replication_type=REPLICATION_TYPE_COPY,
80 ),
81 ])
82
83 replication_config_path = os.path.join(self.tempdir,
84 'replication_config.jsonpb')
85 replication_config_dict = json_format.MessageToDict(replication_config)
86 replication_config_dict['new_field'] = 1
87 osutils.WriteFile(replication_config_path,
88 json.dumps(replication_config_dict))
89
90 with self.assertRaises(json_format.ParseError):
91 replication_util.main(['run', replication_config_path])