blob: a450ce366a8ab30b7f87bbc056379d4613939530 [file] [log] [blame]
Alex Kleinc05f3d12019-05-29 14:16:21 -06001# -*- 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"""field_handler module tests."""
7
8from __future__ import print_function
9
10import os
11
12from chromite.api import field_handler
13from chromite.api.gen.chromite.api import build_api_test_pb2
14from chromite.api.gen.chromiumos import common_pb2
15from chromite.lib import chroot_lib
16from chromite.lib import cros_test_lib
17from chromite.lib import osutils
18
19
20class ChrootHandlerTest(cros_test_lib.TestCase):
21 """ChrootHandler tests."""
22
23 def setUp(self):
24 self.path = '/chroot/dir'
25 self.cache_dir = '/cache/dir'
Alex Klein5e4b1bc2019-07-02 12:27:06 -060026 self.chrome_dir = '/chrome/dir'
Alex Kleinc05f3d12019-05-29 14:16:21 -060027 self.env = {'FEATURES': 'thing'}
Alex Klein5e4b1bc2019-07-02 12:27:06 -060028 self.expected_chroot = chroot_lib.Chroot(path=self.path,
29 cache_dir=self.cache_dir,
30 chrome_root=self.chrome_dir,
31 env=self.env)
Alex Kleinc05f3d12019-05-29 14:16:21 -060032
33 def test_parse_chroot_success(self):
34 """Test successful Chroot message parse."""
35 chroot_msg = common_pb2.Chroot()
36 chroot_msg.path = self.path
37 chroot_msg.cache_dir = self.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060038 chroot_msg.chrome_dir = self.chrome_dir
Alex Kleinc05f3d12019-05-29 14:16:21 -060039 chroot_msg.env.features.add().feature = 'thing'
40
41 chroot_handler = field_handler.ChrootHandler(clear_field=False)
42 parsed_chroot = chroot_handler.parse_chroot(chroot_msg)
43
44 self.assertEqual(self.expected_chroot, parsed_chroot)
45
46 def test_handle_success(self):
47 """Test a successful Chroot message parse from a parent message."""
48 message = build_api_test_pb2.TestRequestMessage()
49 message.chroot.path = self.path
50 message.chroot.cache_dir = self.cache_dir
Alex Klein5e4b1bc2019-07-02 12:27:06 -060051 message.chroot.chrome_dir = self.chrome_dir
Alex Kleinc05f3d12019-05-29 14:16:21 -060052 message.chroot.env.features.add().feature = 'thing'
53
54 # First a no-clear parse.
55 chroot_handler = field_handler.ChrootHandler(clear_field=False)
56 chroot = chroot_handler.handle(message)
57
58 self.assertEqual(self.expected_chroot, chroot)
59 self.assertEqual(message.chroot.path, self.path)
60
61 # A clear field parse.
62 clear_chroot_handler = field_handler.ChrootHandler(clear_field=True)
63 chroot = clear_chroot_handler.handle(message)
64
65 self.assertEqual(self.expected_chroot, chroot)
66 self.assertFalse(message.chroot.path)
67
68 def test_handle_empty_chroot_message(self):
69 """Test handling of an empty chroot message."""
70 message = build_api_test_pb2.TestRequestMessage()
71 empty_chroot = chroot_lib.Chroot(env={'FEATURES': 'separatedebug'})
72
73 chroot_handler = field_handler.ChrootHandler(clear_field=False)
74 chroot = chroot_handler.handle(message)
75
76 self.assertEqual(empty_chroot, chroot)
77
78
79class PathHandlerTest(cros_test_lib.TempDirTestCase):
80 """PathHandler tests."""
81
82 def setUp(self):
83 self.source_dir = os.path.join(self.tempdir, 'source')
84 self.dest_dir = os.path.join(self.tempdir, 'destination')
85 osutils.SafeMakedirs(self.source_dir)
86 osutils.SafeMakedirs(self.dest_dir)
87
88 self.source_file1 = os.path.join(self.source_dir, 'file1')
89 self.file1_contents = 'file 1'
90 osutils.WriteFile(self.source_file1, self.file1_contents)
91
92 self.file2_contents = 'some data'
93 self.source_file2 = os.path.join(self.source_dir, 'file2')
94 osutils.WriteFile(self.source_file2, self.file2_contents)
95
96 def _path_checks(self, source_file, dest_file, contents=None):
97 """Set of common checks for the copied files/directories."""
98 # Message should now reflect the new path.
99 self.assertNotEqual(source_file, dest_file)
100 # The new path should be in the destination directory.
101 self.assertStartsWith(dest_file, self.dest_dir)
102 # The new file should exist.
103 self.assertExists(dest_file)
104
105 if contents:
106 # The contents should be the same as the source file.
107 self.assertFileContents(dest_file, contents)
108
109 def test_handle_file(self):
110 """Test handling of a single file."""
111 message = build_api_test_pb2.TestRequestMessage()
112 message.path.path = self.source_file1
113 message.path.location = common_pb2.Path.OUTSIDE
114
115 with field_handler.handle_paths(message, self.dest_dir, delete=True):
116 new_path = message.path.path
117 self._path_checks(self.source_file1, new_path, self.file1_contents)
118
119 # The file should have been deleted on exit with delete=True.
120 self.assertNotExists(new_path)
Alex Kleinaa705412019-06-04 15:00:30 -0600121 # Make sure it gets reset.
122 self.assertEqual(message.path.path, self.source_file1)
Alex Kleinc05f3d12019-05-29 14:16:21 -0600123
124 def test_handle_files(self):
125 """Test handling of multiple files."""
126 message = build_api_test_pb2.TestRequestMessage()
127 message.path.path = self.source_file1
128 message.path.location = common_pb2.Path.OUTSIDE
129 message.another_path.path = self.source_file2
130 message.another_path.location = common_pb2.Path.OUTSIDE
131
132 with field_handler.handle_paths(message, self.dest_dir, delete=False):
133 new_path1 = message.path.path
134 new_path2 = message.another_path.path
135
136 self._path_checks(self.source_file1, new_path1, self.file1_contents)
137 self._path_checks(self.source_file2, new_path2, self.file2_contents)
138
139 # The files should still exist with delete=False.
140 self.assertExists(new_path1)
141 self.assertExists(new_path2)
142
Alex Kleinaa705412019-06-04 15:00:30 -0600143 def test_handle_nested_file(self):
144 """Test the nested path handling."""
145 message = build_api_test_pb2.TestRequestMessage()
146 message.nested_path.path.path = self.source_file1
147 message.nested_path.path.location = common_pb2.Path.OUTSIDE
148
149 with field_handler.handle_paths(message, self.dest_dir):
150 new_path = message.nested_path.path.path
151 self._path_checks(self.source_file1, new_path, self.file1_contents)
152
Alex Kleinc05f3d12019-05-29 14:16:21 -0600153 def test_handle_directory(self):
154 """Test handling of a directory."""
155 message = build_api_test_pb2.TestRequestMessage()
156 message.path.path = self.source_dir
157 message.path.location = common_pb2.Path.OUTSIDE
158
159 with field_handler.handle_paths(message, self.dest_dir):
160 new_path = message.path.path
161
162 self._path_checks(self.source_dir, self.dest_dir)
163 # Make sure both directories have the same files.
164 self.assertItemsEqual(os.listdir(self.source_dir), os.listdir(new_path))
165
166 def test_direction(self):
167 """Test the direction argument preventing copies."""
168 message = build_api_test_pb2.TestRequestMessage()
169 message.path.path = self.source_file1
170 message.path.location = common_pb2.Path.INSIDE
171
172 direction = field_handler.PathHandler.INSIDE
173 with field_handler.handle_paths(message, self.dest_dir, delete=True,
174 direction=direction):
175 self.assertEqual(self.source_file1, message.path.path)
176
177 # It should not be deleting the file when it doesn't need to copy it even
178 # with delete=True.
179 self.assertExists(self.source_file1)