-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathoutputs.go
222 lines (197 loc) · 5.58 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package outputs
import (
"fmt"
"io"
"sort"
"strings"
"sync"
"time"
"github.com/aelsabbahy/goss/resource"
"github.com/aelsabbahy/goss/util"
"github.com/fatih/color"
)
type Outputer interface {
Output(io.Writer, <-chan []resource.TestResult, time.Time, util.OutputConfig) int
}
var green = color.New(color.FgGreen).SprintfFunc()
var red = color.New(color.FgRed).SprintfFunc()
var yellow = color.New(color.FgYellow).SprintfFunc()
func humanizeResult(r resource.TestResult) string {
if r.Err != nil {
return red("%s: %s: Error: %s", r.ResourceId, r.Property, r.Err)
}
switch r.Result {
case resource.SUCCESS:
return green("%s: %s: %s: matches expectation: %s", r.ResourceType, r.ResourceId, r.Property, r.Expected)
case resource.SKIP:
return yellow("%s: %s: %s: skipped", r.ResourceType, r.ResourceId, r.Property)
case resource.FAIL:
if r.Human != "" {
return red("%s: %s: %s:\n%s", r.ResourceType, r.ResourceId, r.Property, r.Human)
}
return humanizeResult2(r)
default:
panic(fmt.Sprintf("Unexpected Result Code: %v\n", r.Result))
}
}
func humanizeResult2(r resource.TestResult) string {
if r.Err != nil {
return red("%s: %s: Error: %s", r.ResourceId, r.Property, r.Err)
}
switch r.Result {
case resource.SUCCESS:
switch r.TestType {
case resource.Value:
return green("%s: %s: %s: matches expectation: %s", r.ResourceType, r.ResourceId, r.Property, r.Expected)
case resource.Values:
return green("%s: %s: %s: all expectations found: [%s]", r.ResourceType, r.ResourceId, r.Property, strings.Join(r.Expected, ", "))
case resource.Contains:
return green("%s: %s: %s: all expectations found: [%s]", r.ResourceType, r.ResourceId, r.Property, strings.Join(r.Expected, ", "))
default:
return red("Unexpected type %d", r.TestType)
}
case resource.FAIL:
switch r.TestType {
case resource.Value:
return red("%s: %s: %s: doesn't match, expect: %s found: %s", r.ResourceType, r.ResourceId, r.Property, r.Expected, r.Found)
case resource.Values:
return red("%s: %s: %s: expectations not found [%s]", r.ResourceType, r.ResourceId, r.Property, strings.Join(subtractSlice(r.Expected, r.Found), ", "))
case resource.Contains:
return red("%s: %s: %s: patterns not found: [%s]", r.ResourceType, r.ResourceId, r.Property, strings.Join(subtractSlice(r.Expected, r.Found), ", "))
default:
return red("Unexpected type %d", r.TestType)
}
case resource.SKIP:
return yellow("%s: %s: %s: skipped", r.ResourceType, r.ResourceId, r.Property)
default:
panic(fmt.Sprintf("Unexpected Result Code: %v\n", r.Result))
}
}
// Copied from database/sql
var (
outputersMu sync.Mutex
outputers = make(map[string]Outputer)
outputerFormatOptions = make(map[string][]string)
)
func RegisterOutputer(name string, outputer Outputer, formatOptions []string) {
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
outputerFormatOptions[name] = formatOptions
}
// 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
}
// FormatOptions are all the valid options formatters accept
func FormatOptions() []string {
outputersMu.Lock()
defer outputersMu.Unlock()
var list []string
for _, formatOptions := range outputerFormatOptions {
for _, opt := range formatOptions {
if !(util.IsValueInList(opt, list)) {
list = append(list, opt)
}
}
}
sort.Strings(list)
return list
}
// IsValidFormat determines if f is a valid format name based on Outputers()
func IsValidFormat(f string) bool {
for _, o := range Outputers() {
if o == f {
return true
}
}
return false
}
// IsValidFormatOption determines if o is a valid format option based on FormatOptions()
func IsValidFormatOption(o string) bool {
for _, p := range FormatOptions() {
if p == o {
return true
}
}
return false
}
func GetOutputer(name string) (Outputer, error) {
if _, ok := outputers[name]; !ok {
return nil, fmt.Errorf("bad output format: " + name)
}
return outputers[name], nil
}
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
}
func header(t resource.TestResult) string {
var out string
if t.Title != "" {
out += fmt.Sprintf("Title: %s\n", t.Title)
}
if t.Meta != nil {
var keys []string
for k := range t.Meta {
keys = append(keys, k)
}
sort.Strings(keys)
out += "Meta:\n"
for _, k := range keys {
out += fmt.Sprintf(" %v: %v\n", k, t.Meta[k])
}
}
return out
}
func summary(startTime time.Time, count, failed, skipped int) string {
var s string
s += fmt.Sprintf("Total Duration: %.3fs\n", time.Since(startTime).Seconds())
f := green
if failed > 0 {
f = red
}
s += f("Count: %d, Failed: %d, Skipped: %d\n", count, failed, skipped)
return s
}
func failedOrSkippedSummary(failedOrSkipped [][]resource.TestResult) string {
var s string
if len(failedOrSkipped) > 0 {
s += fmt.Sprint("Failures/Skipped:\n\n")
for _, failedGroup := range failedOrSkipped {
first := failedGroup[0]
header := header(first)
if header != "" {
s += fmt.Sprint(header)
}
for _, testResult := range failedGroup {
s += fmt.Sprintln(humanizeResult(testResult))
}
s += fmt.Sprint("\n")
}
}
return s
}