// uninstallLog unregisters the event logger from the registry and closes the // log's handle if it is open. func uninstallLog(provider, source string, log *elog.Log) error { var errs multierror.Errors if log != nil { err := eventlogging.ClearEventLog(eventlogging.Handle(log.Handle), "") if err != nil { errs = append(errs, err) } err = log.Close() if err != nil { errs = append(errs, err) } } err := elog.RemoveSource(providerName, sourceName) if err != nil { errs = append(errs, err) } err = elog.RemoveProvider(providerName) if err != nil { errs = append(errs, err) } return errs.Err() }
// Verify that all messages are read from the event log. func TestRead(t *testing.T) { configureLogp() log, err := initLog(providerName, sourceName, eventCreateMsgFile) if err != nil { t.Fatal(err) } defer func() { err := uninstallLog(providerName, sourceName, log) if err != nil { t.Fatal(err) } }() // Publish test messages: for k, m := range messages { err = log.Report(m.eventType, k, []string{m.message}) if err != nil { t.Fatal(err) } } // Read messages: eventlog, err := newEventLogging(map[string]interface{}{"name": providerName}) if err != nil { t.Fatal(err) } err = eventlog.Open(0) if err != nil { t.Fatal(err) } defer func() { err := eventlog.Close() if err != nil { t.Fatal(err) } }() records, err := eventlog.Read() if err != nil { t.Fatal(err) } // Validate messages: assert.Len(t, records, len(messages)) for _, record := range records { t.Log(record) m, exists := messages[record.EventIdentifier.ID] if !exists { t.Errorf("Unknown EventId %d Read() from event log. %v", record.EventIdentifier.ID, record) continue } assert.Equal(t, eventlogging.EventType(m.eventType).String(), record.Level) assert.Equal(t, m.message, strings.TrimRight(record.Message, "\r\n")) } // Validate getNumberOfEventLogRecords returns the correct number of messages. numMessages, err := eventlogging.GetNumberOfEventLogRecords(eventlogging.Handle(log.Handle)) assert.NoError(t, err) assert.Equal(t, len(messages), int(numMessages)) }
// TestReadWhileCleared tests that the Read method recovers from the event log // being cleared or reset while reading. func TestReadWhileCleared(t *testing.T) { if testing.Short() { t.Skip("Skipping integration test in short mode.") } configureLogp() log, err := initLog(providerName, sourceName, eventCreateMsgFile) if err != nil { t.Fatal(err) } defer func() { err := uninstallLog(providerName, sourceName, log) if err != nil { t.Fatal(err) } }() eventlog, err := newEventLogging(Config{Name: providerName}) if err != nil { t.Fatal(err) } err = eventlog.Open(0) if err != nil { t.Fatal(err) } defer func() { err := eventlog.Close() if err != nil { t.Fatal(err) } }() log.Info(1, "Message 1") log.Info(2, "Message 2") lr, err := eventlog.Read() assert.NoError(t, err, "Expected 2 messages but received error") assert.Len(t, lr, 2, "Expected 2 messages") assert.NoError(t, eventlogging.ClearEventLog(eventlogging.Handle(log.Handle), "")) lr, err = eventlog.Read() assert.NoError(t, err, "Expected 0 messages but received error") assert.Len(t, lr, 0, "Expected 0 message") log.Info(3, "Message 3") lr, err = eventlog.Read() assert.NoError(t, err, "Expected 1 message but received error") assert.Len(t, lr, 1, "Expected 1 message") if len(lr) > 0 { assert.Equal(t, uint32(3), lr[0].EventID) } }
// Initializes logp if the verbose flag was set. func configureLogp() { oneTimeLogpInit.Do(func() { if testing.Verbose() { logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"eventlog"}) logp.Info("DEBUG enabled for eventlog.") } else { logp.LogInit(logp.LOG_WARNING, "", false, true, []string{}) } // Clear the event log before starting. log, _ := elog.Open(sourceName) eventlogging.ClearEventLog(eventlogging.Handle(log.Handle), "") log.Close() }) }
// TestReadWhileCleared tests that the Read method recovers from the event log // being cleared or reset while reading. func TestReadWhileCleared(t *testing.T) { configureLogp() log, err := initLog(providerName, sourceName, eventCreateMsgFile) if err != nil { t.Fatal(err) } defer func() { err := uninstallLog(providerName, sourceName, log) if err != nil { t.Fatal(err) } }() eventlog, err := newEventLogging(map[string]interface{}{"name": providerName}) if err != nil { t.Fatal(err) } err = eventlog.Open(0) if err != nil { t.Fatal(err) } defer func() { err := eventlog.Close() if err != nil { t.Fatal(err) } }() log.Info(1, "Message 1") log.Info(2, "Message 2") lr, err := eventlog.Read() assert.NoError(t, err, "Expected 2 messages but received error") assert.Len(t, lr, 2, "Expected 2 messages") assert.NoError(t, eventlogging.ClearEventLog(eventlogging.Handle(log.Handle), "")) lr, err = eventlog.Read() assert.NoError(t, err, "Expected 0 messages but received error") assert.Len(t, lr, 0, "Expected 0 message") log.Info(3, "Message 3") lr, err = eventlog.Read() assert.NoError(t, err, "Expected 1 message but received error") assert.Len(t, lr, 1, "Expected 1 message") if len(lr) > 0 { assert.Equal(t, uint32(3), lr[0].EventIdentifier.ID) } }