'Tests that we can use integer matching rules on the foreman.'

def testIntegerComparisons(self):

    fd = aff4.FACTORY.Create('C.0000000000000011', aff4_grr.VFSGRRClient, token=self.token)
    fd.Set(fd.Schema.SYSTEM, rdfvalue.RDFString('Windows    XP'))
    fd.Set(fd.Schema.INSTALL_DATE(1336480583077736))
    fd.Close()
    fd = aff4.FACTORY.Create('C.0000000000000012', aff4_grr.VFSGRRClient, token=self.token)
    fd.Set(fd.Schema.SYSTEM, rdfvalue.RDFString('Windows    7'))
    fd.Set(fd.Schema.INSTALL_DATE(1336480583077736))
    fd.Close()
    fd = aff4.FACTORY.Create('C.0000000000000013', aff4_grr.VFSGRRClient, token=self.token)
    fd.Set(fd.Schema.SYSTEM, rdfvalue.RDFString('Windows    7'))
    fd.Set(fd.Schema.INSTALL_DATE((1336480583077736 - (((7 * 24) * 3600) * 1000000.0))))
    fd.Close()
    fd = aff4.FACTORY.Create('C.0000000000000014', aff4_grr.VFSGRRClient, token=self.token)
    fd.Set(fd.Schema.SYSTEM, rdfvalue.RDFString('Windows    7'))
    fd.Set(fd.Schema.LAST_BOOT_TIME(1336300000000000))
    fd.Close()
    with utils.Stubber(flow.GRRFlow, 'StartFlow', self.StartFlow):
        now = (time.time() * 1000000.0)
        expires = ((time.time() + 3600) * 1000000.0)
        foreman = aff4.FACTORY.Open('aff4:/foreman', mode='rw', token=self.token)
        rule = rdf_foreman.ForemanRule(created=int(now), expires=int(expires), description='Test    rule(old)')
        rule.client_rule_set = rdf_foreman.ForemanClientRuleSet(rules=[rdf_foreman.ForemanClientRule(rule_type=rdf_foreman.ForemanClientRule.Type.INTEGER, integer=rdf_foreman.ForemanIntegerClientRule(attribute_name=fd.Schema.INSTALL_DATE.name, operator=rdf_foreman.ForemanIntegerClientRule.Operator.LESS_THAN, value=int((1336480583077736 - (3600 * 1000000.0)))))])
        old_flow = 'Test    flow    for    old    clients'
        rule.actions.Append(flow_name=old_flow, argv=rdf_protodict.Dict(dict(foo='bar')))
        rule_set = foreman.Schema.RULES()
        rule_set.Append(rule)
        rule = rdf_foreman.ForemanRule(created=int(now), expires=int(expires), description='Test    rule(new)')
        rule.client_rule_set = rdf_foreman.ForemanClientRuleSet(rules=[rdf_foreman.ForemanClientRule(rule_type=rdf_foreman.ForemanClientRule.Type.INTEGER, integer=rdf_foreman.ForemanIntegerClientRule(attribute_name=fd.Schema.INSTALL_DATE.name, operator=rdf_foreman.ForemanIntegerClientRule.Operator.GREATER_THAN, value=int((1336480583077736 - (3600 * 1000000.0)))))])
        new_flow = 'Test    flow    for    newer    clients'
        rule.actions.Append(flow_name=new_flow, argv=rdf_protodict.Dict(dict(foo='bar')))
        rule_set.Append(rule)
        rule = rdf_foreman.ForemanRule(created=int(now), expires=int(expires), description='Test    rule(eq)')
        rule.client_rule_set = rdf_foreman.ForemanClientRuleSet(rules=[rdf_foreman.ForemanClientRule(rule_type=rdf_foreman.ForemanClientRule.Type.INTEGER, integer=rdf_foreman.ForemanIntegerClientRule(attribute_name=fd.Schema.LAST_BOOT_TIME.name, operator=rdf_foreman.ForemanIntegerClientRule.Operator.EQUAL, value=1336300000000000))])
        eq_flow = 'Test    flow    for    LAST_BOOT_TIME'
        rule.actions.Append(flow_name=eq_flow, argv=rdf_protodict.Dict(dict(foo='bar')))
        rule_set.Append(rule)
        foreman.Set(foreman.Schema.RULES, rule_set)
        foreman.Close()
        self.clients_launched = []
        foreman.AssignTasksToClient('C.0000000000000011')
        foreman.AssignTasksToClient('C.0000000000000012')
        foreman.AssignTasksToClient('C.0000000000000013')
        foreman.AssignTasksToClient('C.0000000000000014')
        self.assertEqual(len(self.clients_launched), 4)
        self.assertEqual(self.clients_launched[0][0], rdf_client.ClientURN('C.0000000000000011'))
        self.assertEqual(self.clients_launched[0][1], new_flow)
        self.assertEqual(self.clients_launched[1][0], rdf_client.ClientURN('C.0000000000000012'))
        self.assertEqual(self.clients_launched[1][1], new_flow)
        self.assertEqual(self.clients_launched[2][0], rdf_client.ClientURN('C.0000000000000013'))
        self.assertEqual(self.clients_launched[2][1], old_flow)
        self.assertEqual(self.clients_launched[3][0], rdf_client.ClientURN('C.0000000000000014'))
        self.assertEqual(self.clients_launched[3][1], eq_flow)
