def _SetUpConfigFile(self): """Set up a configuration file to be read by guests. Returns: True if all goes well. """ logging.info('Setting up config file...') config = wheelbarrow_pb2.NfsAnalysisConfig() config.input_dir = os.path.join(self._guest_nfs_share, NfsAnalysisSetupAgent.INPUT_DIR) config.output_dir = os.path.join(self._guest_nfs_share, NfsAnalysisSetupAgent.OUTPUT_DIR) config.log_dir = os.path.join(self._guest_nfs_share, NfsAnalysisSetupAgent._LOG_DIR) config.text_output = self._text_output # We estimate that the VM startup and initial setup should take less than a # minute. config.timeout = self._timeout - 60 config_file_path = os.path.join(self._host_nfs_share, NfsAnalysisSetupAgent.CONFIG_FILE_NAME) if utils.WriteProtobufToFile(config, config_file_path, False): return True else: logging.error('Error while writing config file %s.', config_file_path) return False
def testWriteProtobufToFileWithNoExtension(self): package = self._CreateTestPackageProtobuf() path = os.path.join(self.tmp_dir, 'test_path') result = utils.WriteProtobufToFile(package, path, add_extension=True) extended_path = '%s%s' % (path, '.txt') CheckResultProtobufFromFile(self, package, extended_path, wheelbarrow_pb2.Package()) self.assertTrue(result)
def testWriteProtobufToFileWithBadFilePath(self): protobuf = self._CreateTestPackageProtobuf() path = os.path.join(self.tmp_dir, 'nonexistent_dir', 'path_test') logging.error('Could not write protobuf to file %s: %s', path, mox.IgnoreArg()) self.mox.ReplayAll() result = utils.WriteProtobufToFile(protobuf, path, False, False) self.assertFalse(result) self.mox.ReplayAll()
def testWriteProtobufToFileWithEncodeError(self): self.mox.StubOutWithMock(text_format, 'MessageToString') protobuf = self._CreateTestPackageProtobuf() text_format.MessageToString(protobuf).AndRaise(EncodeError) path = 'path_test' logging.error('Could not encode protobuf when writing to file %s: %s', 'path_test', mox.IgnoreArg()) self.mox.ReplayAll() result = utils.WriteProtobufToFile(protobuf, path, True, False) self.assertFalse(result) self.mox.VerifyAll()
def _WritePackageDescriptorToFile(self, package_name, version): """Write out the packages to be analysed.""" package_pb = wheelbarrow_pb2.Package() package_pb.name = package_name package_pb.architecture = version.architecture package_pb.version = version.version package_pb.status = wheelbarrow_pb2.Package.AVAILABLE file_name = '%s-%s-%s' % (package_name, version.version, version.architecture) path = os.path.join(self._host_nfs_share, NfsAnalysisSetupAgent.INPUT_DIR, file_name) if utils.WriteProtobufToFile(package_pb, path, self._text_output, True): return True else: logging.error('Could not write package descriptor to file %s.', path) return False