def test_get_log_events(self):
        date1 = datetime.datetime(1901, 1, 1, 1, 1, 1, 1)
        date2 = datetime.datetime(2010, 1, 1, 1, 1, 1, 1)
        date3 = datetime.datetime(2000, 1, 1, 1, 1, 1, 1)
        host2 = ClassLServer("host2")
        event_type2 = LogEventType("test2 event")

        log1 = LogEvent(source_entity = self.host,
                        user="******",
                        event_type=clusto.get_by_name("test event"),
                        timestamp=date1,
                        description="test description",
                        test_param = "test2")
        log2 = LogEvent(source_entity = self.host,
                        user="******",
                        event_type=event_type2,
                        timestamp=date2,
                        description="test description",
                        test_param = "test2")        
        log3 = LogEvent(source_entity = host2,
                        user="******",
                        event_type=clusto.get_by_name("test event"),
                        timestamp=date3,
                        description="test description",
                        test_param = "test3")

        self.assertEquals(sorted(LogEvent.get_log_events(user="******")), sorted([log1, log3]))
        self.assertEquals(sorted(LogEvent.get_log_events(event_type=event_type2)), [log2])
        self.assertEquals(sorted(LogEvent.get_log_events(source_entity=self.host)), sorted([log1, log2]))
        self.assertEquals(sorted(LogEvent.get_log_events(start_timestamp=datetime.datetime(1902, 1, 1, 1, 1, 1, 1))), sorted([log2, log3]))
        self.assertEquals(sorted(LogEvent.get_log_events(end_timestamp=datetime.datetime(2012, 1, 1, 1, 1, 1, 1))), sorted([log1, log2, log3]))
        self.assertEquals(sorted(LogEvent.get_log_events(start_timestamp=datetime.datetime(2000, 1, 1, 1, 1, 1, 1),
                                                         end_timestamp=datetime.datetime(2011, 1, 1, 1, 1, 1, 1))), sorted([log2, log3]))
        self.assertEquals(sorted(LogEvent.get_log_events(start_timestamp=datetime.datetime(1902, 1, 1, 1, 1, 1, 1),
                                                         end_timestamp=datetime.datetime(2011, 1, 1, 1, 1, 1, 1),
                                                         user="******")), [log3])
        self.assertEquals(sorted(LogEvent.get_log_events(start_timestamp=datetime.datetime(2001, 1, 1, 1, 1, 1, 1),
                                                         end_timestamp=datetime.datetime(2011, 1, 1, 1, 1, 1, 1),
                                                         event_type=event_type2)), [log2])
        self.assertEquals(sorted(LogEvent.get_log_events(event_type=clusto.get_by_name("test event"), user="******", 
                                                         source_entity=self.host)), sorted([log1]))
Esempio n. 2
0
        if len(host) < 1:
            return HttpResponseNotFound("No entity found for '%s'" % hostname)
        elif len(host) > 1:
            return HttpResponseInvalidState("Multiple entities found for  '%s'" % hostname)
        else:
            host = host[0]

    if event_type:
        try:
            event_type = clusto.get_by_name(event_type)
        except LookupError, e:
            return HttpResponseNotFound("Event type '%s' does not exist" % event_type)

    levents = LogEvent.get_log_events(source_entity = host,
                                      user=user,
                                      event_type=event_type,
                                      start_timestamp=start_timestamp,
                                      end_timestamp=end_timestamp)
    if len(levents) < 1:
        return []

    for levent in levents:
        if not levent.source_entity:
            event_hostname = None
        else:
            event_hostname = levent.source_entity.hostname

        log_event = {"name" : levent.name,
                     "hostname" : event_hostname,
                     "event_type" : levent.event_type.name,
                     "user" : levent.user,