blob: c09f45cd9a28db6b5987f60ece907d35473db1a5 [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
12
13from google.protobuf import json_format
14
15from chromite.api.gen.config.replication_config_pb2 import (
16 ReplicationConfig, FileReplicationRule, FILE_TYPE_OTHER,
17 REPLICATION_TYPE_COPY)
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
23D = cros_test_lib.Directory
24
25
Andrew Lambca1f35b2019-12-04 09:37:11 -070026class RunTest(cros_test_lib.MockTempDirTestCase):
Andrew Lamb16f02bf2019-11-04 14:44:34 -070027 """Tests of the run command.
28
29 Note that detailed tests of replication behavior should be done in
30 replication_lib_unittest.
31 """
32
33 def setUp(self):
34 file_layout = (D('src', ['audio_file']),)
35 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
36
Andrew Lambca1f35b2019-12-04 09:37:11 -070037 self.audio_path = os.path.join('src', 'audio_file')
38 self.WriteTempFile(self.audio_path, '[Speaker A Settings]')
39
40 self.PatchObject(constants, 'SOURCE_ROOT', new=self.tempdir)
Andrew Lamb16f02bf2019-11-04 14:44:34 -070041
42 def testRun(self):
Mike Frysingerdf1d0b02019-11-12 17:44:12 -050043 """Basic test of the 'run' command."""
Andrew Lambca1f35b2019-12-04 09:37:11 -070044 audio_dst_path = os.path.join('dst', 'audio_file')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070045
46 replication_config = ReplicationConfig(file_replication_rules=[
47 FileReplicationRule(
48 source_path=self.audio_path,
49 destination_path=audio_dst_path,
50 file_type=FILE_TYPE_OTHER,
51 replication_type=REPLICATION_TYPE_COPY,
52 ),
53 ])
54
55 replication_config_path = os.path.join(self.tempdir,
56 'replication_config.jsonpb')
57 osutils.WriteFile(replication_config_path,
58 json_format.MessageToJson(replication_config))
59
60 replication_util.main(['run', replication_config_path])
61
62 expected_file_layout = (
63 D('src', ['audio_file']),
64 D('dst', ['audio_file']),
65 'replication_config.jsonpb',
66 )
67
68 cros_test_lib.VerifyOnDiskHierarchy(self.tempdir, expected_file_layout)
69
Andrew Lambca1f35b2019-12-04 09:37:11 -070070 self.assertTempFileContents(audio_dst_path, '[Speaker A Settings]')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070071
72 def testUnknownFieldInConfig(self):
73 """Test that unknown fields in the ReplicationConfig cause an error."""
Andrew Lambca1f35b2019-12-04 09:37:11 -070074 audio_dst_path = os.path.join('dst', 'audio_file')
Andrew Lamb16f02bf2019-11-04 14:44:34 -070075
76 replication_config = ReplicationConfig(file_replication_rules=[
77 FileReplicationRule(
78 source_path=self.audio_path,
79 destination_path=audio_dst_path,
80 file_type=FILE_TYPE_OTHER,
81 replication_type=REPLICATION_TYPE_COPY,
82 ),
83 ])
84
85 replication_config_path = os.path.join(self.tempdir,
86 'replication_config.jsonpb')
87 replication_config_dict = json_format.MessageToDict(replication_config)
88 replication_config_dict['new_field'] = 1
89 osutils.WriteFile(replication_config_path,
90 json.dumps(replication_config_dict))
91
92 with self.assertRaises(json_format.ParseError):
93 replication_util.main(['run', replication_config_path])