forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchers.go
62 lines (54 loc) · 1.53 KB
/
matchers.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
// Copyright 2017 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package integration
import (
"fmt"
"gopkg.in/check.v1"
)
type baseMatcher struct {
name string
params []string
check func(result *Result, params []interface{}) (bool, string)
}
func (m *baseMatcher) Info() *check.CheckerInfo {
return &check.CheckerInfo{
Name: m.name,
Params: m.params,
}
}
func (m *baseMatcher) Check(params []interface{}, names []string) (bool, string) {
result, ok := params[0].(*Result)
if !ok {
return false, fmt.Sprintf("first param must be a *Result, got %T", params[0])
}
return m.check(result, params)
}
var ResultOk = &baseMatcher{
name: "ResultOK",
params: []string{"result"},
check: func(result *Result, params []interface{}) (bool, string) {
if result.Error != nil || result.ExitCode != 0 {
return false, fmt.Sprintf("result error: %v", result)
}
if result.Timeout {
return false, fmt.Sprintf("result timeout after %v: %v", result.Command.Timeout, result)
}
return true, ""
},
}
var ResultMatches = &baseMatcher{
name: "ResultMatches",
params: []string{"result", "expected"},
check: func(result *Result, params []interface{}) (bool, string) {
expected, ok := params[1].(Expected)
if !ok {
return false, fmt.Sprintf("second param must be a Expected, got %T", params[0])
}
err := result.Compare(expected)
if err != nil {
return false, fmt.Sprintf("%v\n%v", err.Error(), result)
}
return true, ""
},
}