def test_compute(self): file_name = 'data/correct.pbs' expected_cost = 685.99 parser = PbsScriptParser(self._config, self._event_defs) with open(file_name, 'r') as pbs_file: parser.parse_file(pbs_file) quote_calc = QuoteCalculator(self._config) credits = quote_calc.compute(parser.job) self.assertAlmostEqual(expected_cost, credits, delta=0.1)
def check_credit_account(self, job): '''Check whether a project is available to debit credits from, and whether the balance is sufficient''' if 'mock_balance' in self._config: with open(self._config['mock_balance'], 'r') as balance_file: balance_sheet = ''.join(balance_file.readlines()) else: balance_cmd = self._config['balance_cmd'] try: balance_sheet = check_output([balance_cmd], stderr=STDOUT) except OSError: return except CalledProcessError: # TODO: decide on user feedback return accounts = GbalanceParser().parse(balance_sheet) if len(accounts) == 0: self.reg_event('no_credit_account') return elif job.project is None: credit_account = None for account_id in accounts: account_name = accounts[account_id].name if (account_name == '' or account_name == self._config['default_project']): credit_account = accounts[account_id] break if not credit_account: self.reg_event('no_default_credit_account') return else: credit_account = None for account_id in accounts: if accounts[account_id].name == job.project: credit_account = accounts[account_id] break if not credit_account: self.reg_event('unknown_credit_account', {'account': job.project}) return quoteCalculator = QuoteCalculator(self._config) credits = quoteCalculator.compute(job) if credits > credit_account.available_credits: self.reg_event('insufficient_credits', {'account': credit_account.name})