-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
phase.go
126 lines (110 loc) · 2.97 KB
/
phase.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
package matcher
import (
"fmt"
"reflect"
"github.com/onsi/gomega/types"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"kubevirt.io/kubevirt/tests/framework/matcher/helper"
)
func BeRunning() types.GomegaMatcher {
return phaseMatcher{
expectedPhase: "Running",
}
}
func HaveSucceeded() types.GomegaMatcher {
return phaseMatcher{
expectedPhase: "Succeeded",
}
}
func BeInPhase(phase interface{}) types.GomegaMatcher {
return phaseMatcher{
expectedPhase: phase,
}
}
func HavePhase(phase interface{}) types.GomegaMatcher {
return phaseMatcher{
expectedPhase: phase,
}
}
type phaseMatcher struct {
expectedPhase interface{}
}
func (p phaseMatcher) Match(actual interface{}) (success bool, err error) {
if helper.IsNil(actual) {
return false, nil
}
if helper.IsSlice(actual) {
return helper.MatchElementsInSlice(actual, p.match)
}
return p.match(actual)
}
func (p phaseMatcher) match(actual interface{}) (success bool, err error) {
if helper.IsNil(actual) {
return false, nil
}
phase, err := getCurrentPhase(actual)
if err != nil {
return false, err
}
expectedPhase := getExpectedPhase(p.expectedPhase)
if phase == expectedPhase {
return true, nil
}
return false, nil
}
func (p phaseMatcher) FailureMessage(actual interface{}) (message string) {
if helper.IsNil(actual) {
return fmt.Sprintf("object does not exist")
}
expectedPhase := getExpectedPhase(p.expectedPhase)
if helper.IsSlice(actual) {
return fmt.Sprintf("expected phases to be in %v but got %v", expectedPhase, collectPhasesForPrinting(actual))
}
phase, err := getCurrentPhase(actual)
if err != nil {
return err.Error()
}
return fmt.Sprintf("expected phase is '%v' but got '%v'", expectedPhase, phase)
}
func (p phaseMatcher) NegatedFailureMessage(actual interface{}) (message string) {
if helper.IsNil(actual) {
return fmt.Sprintf("object does not exist")
}
expectedPhase := getExpectedPhase(p.expectedPhase)
if helper.IsSlice(actual) {
return fmt.Sprintf("expected phases to not be in %v but got %v", expectedPhase, collectPhasesForPrinting(actual))
}
phase, err := getCurrentPhase(actual)
if err != nil {
return err.Error()
}
return fmt.Sprintf("expected phase '%v' to not match '%v'", expectedPhase, phase)
}
func getCurrentPhase(actual interface{}) (string, error) {
actual = helper.ToPointer(actual)
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(actual)
if err != nil {
return "", err
}
str, _, err := unstructured.NestedString(obj, "status", "phase")
return str, err
}
func collectPhasesForPrinting(actual interface{}) (phases []string) {
helper.IterateOverSlice(actual, func(value interface{}) bool {
if helper.IsNil(value) {
phases = append(phases, "nil")
return true
}
phase, err := getCurrentPhase(value)
if err != nil {
phase = err.Error()
}
phases = append(phases, phase)
return true
})
return
}
func getExpectedPhase(actual interface{}) string {
return reflect.ValueOf(actual).String()
}