Skip to content

Commit

Permalink
[KOGITO-1206] Bump events after scenario run (apache#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
radtriste authored Feb 21, 2020
1 parent 20e83ea commit 6dc0b51
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
82 changes: 82 additions & 0 deletions bddframework/framework/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/kiegroup/kogito-cloud-operator/pkg/client/kubernetes"
"github.com/kiegroup/kogito-cloud-operator/pkg/logger"
"go.uber.org/zap"
"k8s.io/api/events/v1beta1"

"io/ioutil"
)
Expand Down Expand Up @@ -278,3 +279,84 @@ type monitoredPod struct {
type monitoredContainer struct {
loggingFinished bool
}

/////////////////////////////////////////////////////////////////////////
// Events logging
/////////////////////////////////////////////////////////////////////////

const (
eventLastSeenKey = "LAST_SEEN"
eventFirstSeenKey = "FIRST_SEEN"
eventCountKey = "COUNT"
eventNameKey = "NAME"
eventKindKey = "KIND"
eventSubObjectKey = "SUBOBJECT"
eventTypeKey = "TYPE"
eventReasonKey = "REASON"
eventActionKey = "ACTION"
eventControllerKey = "CONTROLLER"
eventInstanceKey = "INSTANCE"
eventMessageKey = "MESSAGE"
)

var eventKeys = []string{
eventLastSeenKey,
eventFirstSeenKey,
eventCountKey,
eventNameKey,
eventKindKey,
eventSubObjectKey,
eventTypeKey,
eventReasonKey,
eventActionKey,
eventControllerKey,
eventInstanceKey,
eventMessageKey,
}

// BumpEvents will bump all events into events.log file
func BumpEvents(namespace string) {
eventList, err := kubernetes.EventC(kubeClient).GetEvents(namespace)
if err != nil {
GetMainLogger().Errorf("Error retrieving events from namespace %s: %v", namespace, err)
}
fileWriter, err := os.Create(getLogFile(namespace, "events"))
if err != nil {
GetMainLogger().Errorf("Error while creating filewriter: %v", err)
}

PrintDataMap(eventKeys, mapEvents(eventList), fileWriter)

if err := fileWriter.Close(); err != nil {
GetMainLogger().Errorf("Error while closing filewriter: %v", err)
}
}

func mapEvents(eventList *v1beta1.EventList) []map[string]string {
eventMaps := []map[string]string{}

for _, event := range eventList.Items {
eventMap := make(map[string]string)
eventMap[eventLastSeenKey] = getDefaultIfNull(event.DeprecatedLastTimestamp.Format("2006-01-02 15:04:05"))
eventMap[eventFirstSeenKey] = getDefaultIfNull(event.DeprecatedFirstTimestamp.Format("2006-01-02 15:04:05"))
eventMap[eventNameKey] = getDefaultIfNull(event.GetName())
eventMap[eventKindKey] = getDefaultIfNull(event.TypeMeta.Kind)
eventMap[eventSubObjectKey] = getDefaultIfNull(event.Regarding.FieldPath)
eventMap[eventTypeKey] = getDefaultIfNull(event.Type)
eventMap[eventReasonKey] = getDefaultIfNull(event.Reason)
eventMap[eventActionKey] = getDefaultIfNull(event.Action)
eventMap[eventControllerKey] = getDefaultIfNull(event.ReportingController)
eventMap[eventInstanceKey] = getDefaultIfNull(event.ReportingInstance)
eventMap[eventMessageKey] = getDefaultIfNull(event.Note)

eventMaps = append(eventMaps, eventMap)
}
return eventMaps
}

func getDefaultIfNull(value string) string {
if len(value) <= 0 {
return "-"
}
return value
}
42 changes: 42 additions & 0 deletions bddframework/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package framework

import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
Expand Down Expand Up @@ -102,3 +103,44 @@ func GetScenarioName(s interface{}) string {
}
return s.(*gherkin.ScenarioOutline).Name
}

// PrintDataMap prints a formatted dataMap using the given writer
func PrintDataMap(keys []string, dataMaps []map[string]string, writer io.StringWriter) {
// Get size of strings to be written, to be able to format correctly
maxStringSizeMap := make(map[string]int)
for _, key := range keys {
maxSize := len(key)
for _, dataMap := range dataMaps {
if len(dataMap[key]) > maxSize {
maxSize = len(dataMap[key])
}
}
maxStringSizeMap[key] = maxSize
}

// Write headers
for _, header := range keys {
writer.WriteString(header)
writer.WriteString(getWhitespaceStr(maxStringSizeMap[header] - len(header) + 1))
writer.WriteString(" | ")
}
writer.WriteString("\n")

// Write events
for _, dataMap := range dataMaps {
for _, key := range eventKeys {
writer.WriteString(dataMap[key])
writer.WriteString(getWhitespaceStr(maxStringSizeMap[key] - len(dataMap[key]) + 1))
writer.WriteString(" | ")
}
writer.WriteString("\n")
}
}

func getWhitespaceStr(size int) string {
whiteSpaceStr := ""
for i := 0; i < size; i++ {
whiteSpaceStr += " "
}
return whiteSpaceStr
}
1 change: 1 addition & 0 deletions bddframework/steps/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (data *Data) BeforeScenario(s interface{}) {
func (data *Data) AfterScenario(s interface{}, err error) {
framework.StopPodLogCollector(data.Namespace)
framework.FlushLogger(data.Namespace)
framework.BumpEvents(data.Namespace)

logScenarioDuration(data, s)
handleScenarioResult(data, s, err)
Expand Down

0 comments on commit 6dc0b51

Please sign in to comment.