forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testclient.go
61 lines (53 loc) · 1.67 KB
/
testclient.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
package testclient
import (
"reflect"
"strings"
"k8s.io/apimachinery/pkg/runtime"
clientgotesting "k8s.io/client-go/testing"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
"github.com/openshift/origin/pkg/client"
)
// NewFixtureClients returns mocks of the OpenShift and Kubernetes clients
// with data populated from provided path.
func NewFixtureClients(objs ...runtime.Object) (client.Interface, kclientset.Interface) {
oc := NewSimpleFake(OriginObjects(objs)...)
kc := fake.NewSimpleClientset(UpstreamObjects(objs)...)
return oc, kc
}
func NewErrorClients(err error) (client.Interface, kclientset.Interface) {
oc := &Fake{}
oc.PrependReactor("*", "*", func(action clientgotesting.Action) (bool, runtime.Object, error) {
return true, nil, err
})
kc := &fake.Clientset{}
kc.PrependReactor("*", "*", func(action clientgotesting.Action) (bool, runtime.Object, error) {
return true, nil, err
})
return oc, kc
}
// OriginObjects returns the origin types.
func OriginObjects(objs []runtime.Object) []runtime.Object {
ret := []runtime.Object{}
for _, obj := range objs {
if !upstreamType(obj) {
ret = append(ret, obj)
}
}
return ret
}
// UpstreamObjects returns the non-origin types.
func UpstreamObjects(objs []runtime.Object) []runtime.Object {
ret := []runtime.Object{}
for _, obj := range objs {
if upstreamType(obj) {
ret = append(ret, obj)
}
}
return ret
}
// upstreamType returns true for Kubernetes types.
func upstreamType(obj runtime.Object) bool {
t := reflect.TypeOf(obj).Elem()
return strings.Contains(t.PkgPath(), "k8s.io/kubernetes/")
}