def test_serialization(self): """Test binary format serialization/deserialization.""" serializer = message_util.BinarySerializer() # Build a message. msg = build_api_test_pb2.TestRequestMessage() msg.id = 'foo' # Round trip the message contents through the serializer. serialized = serializer.serialize(msg) deserialized = build_api_test_pb2.TestRequestMessage() serializer.deserialize(serialized, deserialized) # Make sure the round tripped data is unchanged. self.assertEqual(msg, deserialized)
def test_handle_nested_file(self): """Test the nested path handling.""" message = build_api_test_pb2.TestRequestMessage() message.nested_path.path.path = self.source_file1 message.nested_path.path.location = common_pb2.Path.OUTSIDE with field_handler.copy_paths_in(message, self.dest_dir): new_path = message.nested_path.path.path self._path_checks(self.source_file1, new_path, self.file1_contents)
def test_handle_empty_chroot_message(self): """Test handling of an empty chroot message.""" message = build_api_test_pb2.TestRequestMessage() empty_chroot = chroot_lib.Chroot(env={'FEATURES': 'separatedebug'}) chroot_handler = field_handler.ChrootHandler(clear_field=False) chroot = chroot_handler.handle(message) self.assertEqual(empty_chroot, chroot)
def testSuccess(self): """Test successful handling case.""" names = ['foo', 'bar', 'baz'] message = build_api_test_pb2.TestRequestMessage() for name in names: message.build_targets.add().name = name result = controller_util.ParseBuildTargets(message.build_targets) self.assertCountEqual([BuildTarget(name) for name in names], result)
def test_prefix_inside(self): """Test the transfer inside prefix handling.""" message = build_api_test_pb2.TestRequestMessage() message.path.path = self.source_dir message.path.location = common_pb2.Path.OUTSIDE with field_handler.copy_paths_in(message, self.dest_dir, prefix=self.tempdir): new_path = message.path.path # The prefix should be removed. self.assertFalse(new_path.startswith(self.tempdir))
def test_handle_directory(self): """Test handling of a directory.""" message = build_api_test_pb2.TestRequestMessage() message.path.path = self.source_dir message.path.location = common_pb2.Path.OUTSIDE with field_handler.copy_paths_in(message, self.dest_dir): new_path = message.path.path self._path_checks(self.source_dir, self.dest_dir) # Make sure both directories have the same files. self.assertCountEqual(os.listdir(self.source_dir), os.listdir(new_path))
def test_direction(self): """Test the direction argument preventing copies.""" message = build_api_test_pb2.TestRequestMessage() message.path.path = self.source_file1 message.path.location = common_pb2.Path.INSIDE with field_handler.copy_paths_in(message, self.dest_dir, delete=True): self.assertEqual(self.source_file1, message.path.path) # It should not be deleting the file when it doesn't need to copy it even # with delete=True. self.assertExists(self.source_file1)
def setUp(self): self.router = router.Router() self.router.Register(build_api_test_pb2) self.chroot_dir = os.path.join(self.tempdir, 'chroot') chroot_tmp = os.path.join(self.chroot_dir, 'tmp') # Make the tmp dir for the re-exec inside chroot input/output files. osutils.SafeMakedirs(chroot_tmp) # Build the input/output/config paths we'll be using in the tests. self.json_input_file = os.path.join(self.tempdir, 'input.json') self.json_output_file = os.path.join(self.tempdir, 'output.json') self.json_config_file = os.path.join(self.tempdir, 'config.json') self.binary_input_file = os.path.join(self.tempdir, 'input.bin') self.binary_output_file = os.path.join(self.tempdir, 'output.bin') self.binary_config_file = os.path.join(self.tempdir, 'config.bin') # The message handlers for the respective files. self.json_input_handler = message_util.get_message_handler( self.json_input_file, message_util.FORMAT_JSON) self.json_output_handler = message_util.get_message_handler( self.json_output_file, message_util.FORMAT_JSON) self.json_config_handler = message_util.get_message_handler( self.json_config_file, message_util.FORMAT_JSON) self.binary_input_handler = message_util.get_message_handler( self.binary_input_file, message_util.FORMAT_BINARY) self.binary_output_handler = message_util.get_message_handler( self.binary_output_file, message_util.FORMAT_BINARY) self.binary_config_handler = message_util.get_message_handler( self.binary_config_file, message_util.FORMAT_BINARY) # Build an input message to use. self.expected_id = 'input id' input_msg = build_api_test_pb2.TestRequestMessage() input_msg.id = self.expected_id input_msg.chroot.path = self.chroot_dir # Write out base input and config messages. osutils.WriteFile(self.json_input_file, json_format.MessageToJson(input_msg)) osutils.WriteFile(self.binary_input_file, input_msg.SerializeToString(), mode='wb') config_msg = self.api_config.get_proto() osutils.WriteFile(self.json_config_file, json_format.MessageToJson(config_msg)) osutils.WriteFile(self.binary_config_file, config_msg.SerializeToString(), mode='wb') self.subprocess_tempdir = os.path.join(self.chroot_dir, 'tempdir') osutils.SafeMakedirs(self.subprocess_tempdir)
def test_json_serialization(self): """Test json serialization/deserialization.""" msg_path = os.path.join(self.tempdir, 'proto') # Use the message handler configured in the module to avoid config drift. handler = message_util.get_message_handler(msg_path, message_util.FORMAT_JSON) msg = build_api_test_pb2.TestRequestMessage() msg.id = 'foo' # Round trip the data. self.assertNotExists(msg_path) handler.write_from(msg) self.assertExists(msg_path) deserialized = build_api_test_pb2.TestRequestMessage() handler.read_into(deserialized) # Make sure the data has not mutated. self.assertEqual(msg, deserialized)
def test_serialization(self): """Test json format serialization/deserialization.""" serializer = message_util.JsonSerializer() # Build a message. msg = build_api_test_pb2.TestRequestMessage() msg.id = 'foo' # Round trip the message contents through the serializer. serialized = serializer.serialize(msg) deserialized = build_api_test_pb2.TestRequestMessage() serializer.deserialize(serialized, deserialized) # Create an identical message manually. source = '{"id":"foo"}' deserialized2 = build_api_test_pb2.TestRequestMessage() serializer.deserialize(source, deserialized2) # Make sure the round tripped data is equal, and matches the manually # constructed version. self.assertEqual(deserialized, msg) self.assertEqual(deserialized, deserialized2)
def test_handle_file(self): """Test handling of a single file.""" message = build_api_test_pb2.TestRequestMessage() message.path.path = self.source_file1 message.path.location = common_pb2.Path.OUTSIDE with field_handler.copy_paths_in(message, self.dest_dir, delete=True): new_path = message.path.path self._path_checks(self.source_file1, new_path, self.file1_contents) # The file should have been deleted on exit with delete=True. self.assertNotExists(new_path) # The original should still exist. self.assertExists(self.source_file1) # The path should get reset. self.assertEqual(message.path.path, self.source_file1)
def test_handle_files(self): """Test handling of multiple files.""" message = build_api_test_pb2.TestRequestMessage() message.path.path = self.source_file1 message.path.location = common_pb2.Path.OUTSIDE message.another_path.path = self.source_file2 message.another_path.location = common_pb2.Path.OUTSIDE with field_handler.copy_paths_in(message, self.dest_dir, delete=False): new_path1 = message.path.path new_path2 = message.another_path.path self._path_checks(self.source_file1, new_path1, self.file1_contents) self._path_checks(self.source_file2, new_path2, self.file2_contents) # The files should still exist with delete=False. self.assertExists(new_path1) self.assertExists(new_path2)
def setUp(self): D = cros_test_lib.Directory filesystem = ( D('chroot', (D('tmp', (D('tempdir', ()), )), )), D('sources', ( D('single_file', ('single_file.txt', )), D('nested_directories', ( 'basedir_file.log', D('nested1', ( 'nested1.txt', D('nested2', ('nested2.txt', )), )), )), )), ) cros_test_lib.CreateOnDiskHierarchy(self.tempdir, filesystem) self.chroot = os.path.join(self.tempdir, 'chroot') self.chroot_tmp = os.path.join(self.chroot, 'tmp') self.destination = os.path.join(self.chroot_tmp, 'tempdir') self.inside_path = '/tmp/tempdir' self.single_file_src = os.path.join(self.tempdir, 'sources', 'single_file') self.sf_src_file = os.path.join(self.single_file_src, 'single_file.txt') self.sf_dest_file = os.path.join(self.destination, 'single_file.txt') self.nested_dirs_src = (os.path.join(self.tempdir, 'sources', 'nested_directories')) self.nested_src_files = ( os.path.join(self.nested_dirs_src, 'basedir_file.log'), os.path.join(self.nested_dirs_src, 'nested1', 'nested1.txt'), os.path.join(self.nested_dirs_src, 'nested1', 'nested2', 'nested2.txt'), ) self.nested_dest_files = ( os.path.join(self.destination, 'basedir_file.log'), os.path.join(self.destination, 'nested1', 'nested1.txt'), os.path.join(self.destination, 'nested1', 'nested2', 'nested2.txt'), ) self.message = build_api_test_pb2.TestRequestMessage()
def test_handle_success(self): """Test a successful Chroot message parse from a parent message.""" message = build_api_test_pb2.TestRequestMessage() message.chroot.path = self.path message.chroot.cache_dir = self.cache_dir message.chroot.chrome_dir = self.chrome_dir message.chroot.env.features.add().feature = 'thing' # First a no-clear parse. chroot_handler = field_handler.ChrootHandler(clear_field=False) chroot = chroot_handler.handle(message) self.assertEqual(self.expected_chroot, chroot) self.assertEqual(message.chroot.path, self.path) # A clear field parse. clear_chroot_handler = field_handler.ChrootHandler(clear_field=True) chroot = clear_chroot_handler.handle(message) self.assertEqual(self.expected_chroot, chroot) self.assertFalse(message.chroot.path)
def setUp(self): # Setup the directories. self.chroot_dir = os.path.join(self.tempdir, 'chroot') self.source_dir = '/source' self.chroot_source = os.path.join(self.chroot_dir, self.source_dir.lstrip(os.sep)) self.source_dir2 = '/source2' self.chroot_source2 = os.path.join(self.chroot_dir, self.source_dir2.lstrip(os.sep)) self.dest_dir = os.path.join(self.tempdir, 'destination') osutils.SafeMakedirs(self.chroot_source) osutils.SafeMakedirs(self.chroot_source2) osutils.SafeMakedirs(self.dest_dir) # Two files in the same directory inside the chroot. self.source_file1 = os.path.join(self.chroot_source, 'file1') self.source_file1_inside = os.path.join(self.source_dir, 'file1') self.file1_contents = 'file 1' osutils.WriteFile(self.source_file1, self.file1_contents) self.file2_contents = 'some data' self.source_file2 = os.path.join(self.chroot_source, 'file2') self.source_file2_inside = os.path.join(self.source_dir, 'file2') osutils.WriteFile(self.source_file2, self.file2_contents) # Third file in a different location. self.file3_contents = 'another file' self.source_file3 = os.path.join(self.chroot_source2, 'file3') self.source_file3_inside = os.path.join(self.source_dir2, 'file3') osutils.WriteFile(self.source_file3, self.file3_contents) self.request = build_api_test_pb2.TestRequestMessage() self.request.result_path.path.path = self.dest_dir self.request.result_path.path.location = common_pb2.Path.OUTSIDE self.response = build_api_test_pb2.TestResultMessage() self.chroot = chroot_lib.Chroot(path=self.chroot_dir)
def setUp(self): self.request = build_api_test_pb2.TestRequestMessage() self.response = build_api_test_pb2.TestResultMessage()
def testWrongMessage(self): """Test invalid message type given.""" with self.assertRaises(AssertionError): controller_util.ParseBuildTarget( build_api_test_pb2.TestRequestMessage())