-
Notifications
You must be signed in to change notification settings - Fork 444
/
input_resources.go
90 lines (70 loc) · 3.03 KB
/
input_resources.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
package helpers
import (
"time"
"github.com/solo-io/gloo/pkg/utils/statusutils"
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gstruct"
errors "github.com/rotisserie/eris"
"github.com/solo-io/solo-kit/pkg/api/v1/resources"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
skerrors "github.com/solo-io/solo-kit/pkg/errors"
)
const (
defaultEventuallyTimeout = 30 * time.Second
defaultEventuallyPollingInterval = 1 * time.Second
)
type InputResourceGetter func() (resources.InputResource, error)
type InputResourceListGetter func() (resources.InputResourceList, error)
func EventuallyResourceAccepted(getter InputResourceGetter, intervals ...interface{}) {
EventuallyResourceStatusMatchesState(1, getter, core.Status_Accepted, intervals...)
}
func EventuallyResourceWarning(getter InputResourceGetter, intervals ...interface{}) {
EventuallyResourceStatusMatchesState(1, getter, core.Status_Warning, intervals...)
}
func EventuallyResourceRejected(getter InputResourceGetter, intervals ...interface{}) {
EventuallyResourceStatusMatchesState(1, getter, core.Status_Rejected, intervals...)
}
func EventuallyResourceStatusMatchesState(offset int, getter InputResourceGetter, desiredStatusState core.Status_State, intervals ...interface{}) {
statusStateMatcher := gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
"State": gomega.Equal(desiredStatusState),
})
statusClient := statusutils.GetStatusClientFromEnvOrDefault(defaults.GlooSystem)
timeoutInterval, pollingInterval := getTimeoutAndPollingIntervalsOrDefault(intervals...)
gomega.EventuallyWithOffset(offset+1, func() (core.Status, error) {
resource, err := getter()
if err != nil {
return core.Status{}, errors.Wrapf(err, "failed to get resource")
}
status := statusClient.GetStatus(resource)
if status == nil {
return core.Status{}, errors.Wrapf(err, "waiting for %v status to be non-nil", resource.GetMetadata().GetName())
}
return *status, nil
}, timeoutInterval, pollingInterval).Should(statusStateMatcher)
}
func EventuallyResourceDeleted(getter InputResourceGetter, intervals ...interface{}) {
EventuallyResourceDeletedWithOffset(1, getter, intervals...)
}
func EventuallyResourceDeletedWithOffset(offset int, getter InputResourceGetter, intervals ...interface{}) {
timeoutInterval, pollingInterval := getTimeoutAndPollingIntervalsOrDefault(intervals...)
gomega.EventuallyWithOffset(offset+1, func() (bool, error) {
_, err := getter()
if err != nil && skerrors.IsNotExist(err) {
return true, nil
}
return false, err
}, timeoutInterval, pollingInterval).Should(gomega.BeTrue())
}
func getTimeoutAndPollingIntervalsOrDefault(intervals ...interface{}) (interface{}, interface{}) {
var timeoutInterval, pollingInterval interface{}
timeoutInterval = defaultEventuallyTimeout
pollingInterval = defaultEventuallyPollingInterval
if len(intervals) > 0 {
timeoutInterval = intervals[0]
}
if len(intervals) > 1 {
pollingInterval = intervals[1]
}
return timeoutInterval, pollingInterval
}