forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputs.go
106 lines (91 loc) · 2.51 KB
/
outputs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package outputs
import (
"fmt"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/aelsabbahy/goss/resource"
"github.com/fatih/color"
)
type Outputer interface {
Output(<-chan []resource.TestResult, time.Time) int
}
var green = color.New(color.FgGreen).SprintfFunc()
var red = color.New(color.FgRed).SprintfFunc()
func humanizeResult(r resource.TestResult) string {
if r.Err != nil {
return red("%s: %s: Error: %s", r.Title, r.Property, r.Err)
}
switch r.TestType {
case resource.Value:
if r.Successful {
return green("%s: %s: %s: matches expectation: %s", r.ResourceType, r.Title, r.Property, r.Expected)
} else {
return red("%s: %s: %s: doesn't match, expect: %s found: %s", r.ResourceType, r.Title, r.Property, r.Expected, r.Found)
}
case resource.Values:
if r.Successful {
return green("%s: %s: %s: all expectations found: [%s]", r.ResourceType, r.Title, r.Property, strings.Join(r.Expected, ", "))
} else {
return red("%s: %s: %s: expectations not found [%s]", r.ResourceType, r.Title, r.Property, strings.Join(subtractSlice(r.Expected, r.Found), ", "))
}
case resource.Contains:
if r.Successful {
return green("%s: %s: %s: all patterns found: [%s]", r.ResourceType, r.Title, r.Property, strings.Join(r.Expected, ", "))
} else {
return red("%s: %s: %s: patterns not found: [%s]", r.ResourceType, r.Title, r.Property, strings.Join(subtractSlice(r.Expected, r.Found), ", "))
}
default:
return red("Unexpected type %d", r.TestType)
}
}
// Copied from database/sql
var (
outputersMu sync.Mutex
outputers = make(map[string]Outputer)
)
func RegisterOutputer(name string, outputer Outputer) {
outputersMu.Lock()
defer outputersMu.Unlock()
if outputer == nil {
panic("goss: Register outputer is nil")
}
if _, dup := outputers[name]; dup {
panic("goss: Register called twice for ouputer " + name)
}
outputers[name] = outputer
}
// Outputers returns a sorted list of the names of the registered outputers.
func Outputers() []string {
outputersMu.Lock()
defer outputersMu.Unlock()
var list []string
for name := range outputers {
list = append(list, name)
}
sort.Strings(list)
return list
}
func GetOutputer(name string) Outputer {
if _, ok := outputers[name]; !ok {
fmt.Println("goss: Bad output format: " + name)
os.Exit(1)
}
return outputers[name]
}
func subtractSlice(x, y []string) []string {
m := make(map[string]bool)
for _, y := range y {
m[y] = true
}
var ret []string
for _, x := range x {
if m[x] {
continue
}
ret = append(ret, x)
}
return ret
}