Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import json |
| 11 | import os |
| 12 | |
| 13 | from google.protobuf import json_format |
| 14 | |
| 15 | from chromite.api.gen.config.replication_config_pb2 import ( |
| 16 | ReplicationConfig, FileReplicationRule, FILE_TYPE_OTHER, |
| 17 | REPLICATION_TYPE_COPY) |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 18 | from chromite.lib import constants |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_test_lib |
| 20 | from chromite.lib import osutils |
| 21 | from chromite.scripts import replication_util |
| 22 | |
| 23 | D = cros_test_lib.Directory |
| 24 | |
| 25 | |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 26 | class RunTest(cros_test_lib.MockTempDirTestCase): |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 27 | """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 Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 37 | 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 Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 41 | |
| 42 | def testRun(self): |
Mike Frysinger | df1d0b0 | 2019-11-12 17:44:12 -0500 | [diff] [blame] | 43 | """Basic test of the 'run' command.""" |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 44 | audio_dst_path = os.path.join('dst', 'audio_file') |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 45 | |
| 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 Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 70 | self.assertTempFileContents(audio_dst_path, '[Speaker A Settings]') |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 71 | |
| 72 | def testUnknownFieldInConfig(self): |
| 73 | """Test that unknown fields in the ReplicationConfig cause an error.""" |
Andrew Lamb | ca1f35b | 2019-12-04 09:37:11 -0700 | [diff] [blame] | 74 | audio_dst_path = os.path.join('dst', 'audio_file') |
Andrew Lamb | 16f02bf | 2019-11-04 14:44:34 -0700 | [diff] [blame] | 75 | |
| 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]) |