blob: c6fca4e466e28127cf93d6f0168bf56c776273a8 [file] [log] [blame]
Andrew Lamb16f02bf2019-11-04 14:44:34 -07001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for replication_util."""
7
8from __future__ import print_function
9
10import json
11import os
Mike Frysingerf8d9ba52020-02-10 23:48:22 -050012import sys
Andrew Lamb16f02bf2019-11-04 14:44:34 -070013
14from google.protobuf import json_format
15
16from chromite.api.gen.config.replication_config_pb2 import (
17 ReplicationConfig, FileReplicationRule, FILE_TYPE_OTHER,
18 REPLICATION_TYPE_COPY)
Andrew Lambca1f35b2019-12-04 09:37:11 -070019from chromite.lib import constants
Andrew Lamb16f02bf2019-11-04 14:44:34 -070020from chromite.lib import cros_test_lib
21from chromite.lib import osutils
22from chromite.scripts import replication_util
23
Mike Frysingerf8d9ba52020-02-10 23:48:22 -050024
25assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
26
27
Andrew Lamb16f02bf2019-11-04 14:44:34 -070028D = cros_test_lib.Directory
29
30
Andrew Lambca1f35b2019-12-04 09:37:11 -070031class RunTest(cros_test_lib.MockTempDirTestCase):
Andrew Lamb16f02bf2019-11-04 14:44:34 -070032 """Tests of the run command.
33
34 Note that detailed tests of replication behavior should be done in
35 replication_lib_unittest.
36 """
37
38 def setUp(self):
39 file_layout = (D('src', ['audio_file']),)
40 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
41
Andrew Lambca1f35b2019-12-04 09:37:11 -070042 self.audio_path = os.path.join('src', 'audio_file')
43 self.WriteTempFile(self.audio_path, '[Speaker A Settings]')
44
45 self.PatchObject(constants, 'SOURCE_ROOT', new=self.tempdir)
Andrew Lamb16f02bf2019-11-04 14:44:34 -070046
47 def testRun(self):
Mike Frysingerdf1d0b02019-11-12 17:44:12 -050048 """Basic test of the 'run' command."""
Andrew Lambca1f35b2019-12-04 09:37:11 -070049 audio_dst_path = os.path.join('dst', 'audio_file')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070050
51 replication_config = ReplicationConfig(file_replication_rules=[
52 FileReplicationRule(
53 source_path=self.audio_path,
54 destination_path=audio_dst_path,
55 file_type=FILE_TYPE_OTHER,
56 replication_type=REPLICATION_TYPE_COPY,
57 ),
58 ])
59
60 replication_config_path = os.path.join(self.tempdir,
61 'replication_config.jsonpb')
62 osutils.WriteFile(replication_config_path,
63 json_format.MessageToJson(replication_config))
64
65 replication_util.main(['run', replication_config_path])
66
67 expected_file_layout = (
68 D('src', ['audio_file']),
69 D('dst', ['audio_file']),
70 'replication_config.jsonpb',
71 )
72
73 cros_test_lib.VerifyOnDiskHierarchy(self.tempdir, expected_file_layout)
74
Andrew Lambca1f35b2019-12-04 09:37:11 -070075 self.assertTempFileContents(audio_dst_path, '[Speaker A Settings]')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070076
77 def testUnknownFieldInConfig(self):
78 """Test that unknown fields in the ReplicationConfig cause an error."""
Andrew Lambca1f35b2019-12-04 09:37:11 -070079 audio_dst_path = os.path.join('dst', 'audio_file')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070080
81 replication_config = ReplicationConfig(file_replication_rules=[
82 FileReplicationRule(
83 source_path=self.audio_path,
84 destination_path=audio_dst_path,
85 file_type=FILE_TYPE_OTHER,
86 replication_type=REPLICATION_TYPE_COPY,
87 ),
88 ])
89
90 replication_config_path = os.path.join(self.tempdir,
91 'replication_config.jsonpb')
92 replication_config_dict = json_format.MessageToDict(replication_config)
93 replication_config_dict['new_field'] = 1
94 osutils.WriteFile(replication_config_path,
95 json.dumps(replication_config_dict))
96
97 with self.assertRaises(json_format.ParseError):
98 replication_util.main(['run', replication_config_path])