def test_operator(self): expr = classad.Literal(1) + 2 self.assertTrue(isinstance(expr, classad.ExprTree)) self.assertTrue(expr) self.assertTrue(expr.sameAs(classad.ExprTree('1 + 2'))) expr = classad.Literal(1) & 2 self.assertTrue(isinstance(expr, classad.ExprTree)) self.assertEquals(expr.eval(), 0) self.assertTrue(expr.sameAs(classad.ExprTree('1 & 2'))) expr = classad.Attribute("foo").is_(classad.Value.Undefined) self.assertTrue(expr.eval()) ad = classad.ClassAd("[foo = 1]") expr = classad.Attribute("foo").isnt_(classad.Value.Undefined) self.assertTrue(expr.eval(ad)) expr = classad.Literal(1).and_(classad.Literal(2)) self.assertRaises(RuntimeError, bool, expr)
def updateJobInformation(self, workflow, task, **kwargs): """ _updateJobInformation_ Update job information for all jobs in the workflow and task, the change will take effect if the job is Idle or becomes idle. The currently supported changes are only priority for which both the task (taskPriority) and workflow priority (requestPriority) must be provided. """ schedd = htcondor.Schedd() if 'taskPriority' in kwargs and 'requestPriority' in kwargs: newPriority = int(kwargs['requestPriority']) + int( kwargs['taskPriority'] * self.maxTaskPriority) try: constraint = "WMAgent_SubTaskName =?= %s" % classad.quote( str(task)) constraint += " && WMAgent_RequestName =?= %s" % classad.quote( str(workflow)) constraint += " && JobPrio =!= %d" % newPriority schedd.edit(constraint, 'JobPrio', classad.Literal(newPriority)) except Exception as ex: logging.error( "Failed to update JobPrio for WMAgent_SubTaskName=%s", task) logging.exception(ex) return
def updateJobInformation(self, workflow, task, **kwargs): """ _updateJobInformation_ Update job information for all jobs in the workflow and task, the change will take effect if the job is Idle or becomes idle. The currently supported changes are only priority for which both the task (taskPriority) and workflow priority (requestPriority) must be provided. """ sd = condor.Schedd() if 'taskPriority' in kwargs and 'requestPriority' in kwargs: # Do a priority update priority = (int(kwargs['requestPriority']) + int(kwargs['taskPriority'] * self.maxTaskPriority)) try: sd.edit( 'WMAgent_SubTaskName == %s && WMAgent_RequestName == %s && JobPrio != %d' % (classad.quote(str(task)), classad.quote( str(workflow)), priority), "JobPrio", classad.Literal(int(priority))) except: msg = "Failed to update JobPrio for WMAgent_SubTaskName=%s" % task logging.warn(msg) return
def testTransaction(self): self.launch_daemons(["SCHEDD", "COLLECTOR", "STARTD", "NEGOTIATOR"]) output_file = os.path.join(testdir, "test.out") log_file = os.path.join(testdir, "test.log") if os.path.exists(output_file): os.unlink(output_file) if os.path.exists(log_file): os.unlink(log_file) schedd = htcondor.Schedd() ad = classad.parse(open("tests/submit_sleep.ad")) result_ads = [] cluster = schedd.submit(ad, 1, True, result_ads) with schedd.transaction() as txn: schedd.edit(["%d.0" % cluster], 'foo', classad.Literal(1)) schedd.edit(["%d.0" % cluster], 'bar', classad.Literal(2)) ads = schedd.query("ClusterId == %d" % cluster, ["JobStatus", 'foo', 'bar']) self.assertEquals(len(ads), 1) self.assertEquals(ads[0]['foo'], 1) self.assertEquals(ads[0]['bar'], 2) with schedd.transaction() as txn: schedd.edit(["%d.0" % cluster], 'baz', classad.Literal(3)) with schedd.transaction( htcondor.TransactionFlags.NonDurable | htcondor.TransactionFlags.ShouldLog, True) as txn: schedd.edit(["%d.0" % cluster], 'foo', classad.Literal(4)) schedd.edit(["%d.0" % cluster], 'bar', classad.Literal(5)) ads = schedd.query("ClusterId == %d" % cluster, ["JobStatus", 'foo', 'bar', 'baz']) self.assertEquals(len(ads), 1) self.assertEquals(ads[0]['foo'], 4) self.assertEquals(ads[0]['bar'], 5) self.assertEquals(ads[0]['baz'], 3) try: with schedd.transaction() as txn: schedd.edit(["%d.0" % cluster], 'foo', classad.Literal(6)) schedd.edit(["%d.0" % cluster], 'bar', classad.Literal(7)) raise Exception("force abort") except: exctype, e = sys.exc_info()[:2] if not issubclass(exctype, Exception): raise self.assertEquals(str(e), "force abort") ads = schedd.query("ClusterId == %d" % cluster, ["JobStatus", 'foo', 'bar']) self.assertEquals(len(ads), 1) self.assertEquals(ads[0]['foo'], 4) self.assertEquals(ads[0]['bar'], 5) try: with schedd.transaction() as txn: schedd.edit(["%d.0" % cluster], 'baz', classad.Literal(8)) with schedd.transaction( htcondor.TransactionFlags.NonDurable | htcondor.TransactionFlags.ShouldLog, True) as txn: schedd.edit(["%d.0" % cluster], 'foo', classad.Literal(9)) schedd.edit(["%d.0" % cluster], 'bar', classad.Literal(10)) raise Exception("force abort") except: exctype, e = sys.exc_info()[:2] if not issubclass(exctype, Exception): raise self.assertEquals(str(e), "force abort") ads = schedd.query("ClusterId == %d" % cluster, ["JobStatus", 'foo', 'bar', 'baz']) self.assertEquals(len(ads), 1) self.assertEquals(ads[0]['foo'], 4) self.assertEquals(ads[0]['bar'], 5) self.assertEquals(ads[0]['baz'], 3) schedd.act(htcondor.JobAction.Remove, ["%d.0" % cluster]) ads = schedd.query("ClusterId == %d" % cluster, ["JobStatus"]) self.assertEquals(len(ads), 0)
def test_bool(self): self.assertTrue(bool(classad.ExprTree('true || false'))) self.assertTrue(bool(classad.Literal(True).or_(False))) self.assertFalse(bool(classad.ExprTree('true && false'))) self.assertFalse(bool(classad.Literal(True).and_(False)))
def test_literal(self): self.assertEquals(classad.ExprTree('"foo"'), classad.Literal("foo")) self.assertEquals(classad.Literal(1).eval(), 1)
ad["foo"] = "bar" assert expr.eval(ad) == "bar" def test_evaluate_in_context_of_ad_without_matching_key(ad): expr = classad.ExprTree("foo") ad["no"] = "bar" assert expr.eval(ad) == classad.Value.Undefined @pytest.mark.parametrize( "expr, expected", [ (classad.Literal(1) + 2, 3), (classad.Literal(1) + classad.Literal(2), 3), (classad.Literal(1) - 2, -1), (classad.Literal(1) * 2, 2), (classad.Literal(2) / 2, 1), (classad.Literal(1) & 2, 0), (classad.Literal(1) | 2, 3), (classad.Literal(1).and_(2), True), (classad.Literal(1).and_(classad.Literal(2)), True), (classad.Attribute("foo").is_(classad.Value.Undefined), True), ], ) def test_operators_produce_exprtrees_and_eval_as_expected(expr, expected): assert isinstance(expr, classad.ExprTree) assert expr.eval() == expected