Skip to content

Commit

Permalink
Normalize Duration to seconds. Add summary-line to json
Browse files Browse the repository at this point in the history
  • Loading branch information
aelsabbahy committed Oct 31, 2015
1 parent f4fbf20 commit 5756524
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion outputs/documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (r Documentation) Output(results <-chan []resource.TestResult, startTime ti
fmt.Print("\n")
}

fmt.Printf("Total Duration: %s\n", time.Now().Sub(startTime))
fmt.Printf("Total Duration: %.3fs\n", time.Since(startTime).Seconds())
if len(failed) > 0 {
color.Red("Count: %d, Failed: %d\n", testCount, len(failed))
return 1
Expand Down
19 changes: 16 additions & 3 deletions outputs/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,34 @@ import (
"time"

"github.com/aelsabbahy/goss/resource"
"github.com/fatih/color"
)

type Json struct{}

func (r Json) Output(results <-chan []resource.TestResult, startTime time.Time) (exitCode int) {
color.NoColor = true
testCount := 0
failed := 0
var resultsOut []resource.TestResult
var resultsOut []map[string]interface{}
for resultGroup := range results {
for _, testResult := range resultGroup {
if !testResult.Successful {
failed++
}
resultsOut = append(resultsOut, testResult)
m := struct2map(testResult)
m["summary-line"] = humanizeResult(testResult)
resultsOut = append(resultsOut, m)
testCount++
}
}

summary := make(map[string]interface{})
duration := time.Since(startTime)
summary["test-count"] = testCount
summary["failed-count"] = failed
summary["total-duration"] = time.Now().Sub(startTime)
summary["total-duration"] = duration
summary["summary-line"] = fmt.Sprintf("Count: %d, Failed: %d, Duration: %.3fs", testCount, failed, duration.Seconds())

out := make(map[string]interface{})
out["results"] = resultsOut
Expand All @@ -46,3 +52,10 @@ func (r Json) Output(results <-chan []resource.TestResult, startTime time.Time)
func init() {
RegisterOutputer("json", &Json{})
}

func struct2map(i interface{}) map[string]interface{} {
out := make(map[string]interface{})
j, _ := json.Marshal(i)
json.Unmarshal(j, &out)
return out
}
6 changes: 3 additions & 3 deletions outputs/nagios.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func (r Nagios) Output(results <-chan []resource.TestResult, startTime time.Time
}
}

duration := time.Now().Sub(startTime)
duration := time.Since(startTime)
if failed > 0 {
fmt.Printf("GOSS CRITICAL - Count: %d, Failed: %d, Duration: %s\n", testCount, failed, duration)
fmt.Printf("GOSS CRITICAL - Count: %d, Failed: %d, Duration: %.3fs\n", testCount, failed, duration.Seconds())
return 2
}
fmt.Printf("GOSS OK - Count: %d, Failed: %d, Duration: %s\n", testCount, failed, duration)
fmt.Printf("GOSS OK - Count: %d, Failed: %d, Duration: %.3fs\n", testCount, failed, duration.Seconds())
return 0
}

Expand Down
2 changes: 1 addition & 1 deletion outputs/rspecish.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (r Rspecish) Output(results <-chan []resource.TestResult, startTime time.Ti
fmt.Print("\n")
}

fmt.Printf("Total Duration: %s\n", time.Now().Sub(startTime))
fmt.Printf("Total Duration: %.3fs\n", time.Since(startTime).Seconds())
if len(failed) > 0 {
color.Red("Count: %d, Failed: %d\n", testCount, len(failed))
return 1
Expand Down

0 comments on commit 5756524

Please sign in to comment.