generated from cloud-gov/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf_test.go
194 lines (180 loc) · 5.01 KB
/
cf_test.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
package brokertags
import (
"context"
"errors"
"fmt"
"testing"
"github.com/cloudfoundry/go-cfclient/v3/resource"
"github.com/google/go-cmp/cmp"
)
type mockOrganizations struct {
getOrganizationErr error
organizationName string
organizationGuid string
}
func (o *mockOrganizations) Get(ctx context.Context, guid string) (*resource.Organization, error) {
if o.getOrganizationErr != nil {
return nil, o.getOrganizationErr
}
if guid != o.organizationGuid {
return nil, fmt.Errorf("guid argument: %s does not match expected guid: %s", guid, o.organizationGuid)
}
return &resource.Organization{
Name: o.organizationName,
}, nil
}
type mockSpaces struct {
getSpaceErr error
spaceName string
spaceGuid string
}
func (s *mockSpaces) Get(ctx context.Context, guid string) (*resource.Space, error) {
if s.getSpaceErr != nil {
return nil, s.getSpaceErr
}
if guid != s.spaceGuid {
return nil, fmt.Errorf("guid argument: %s does not match expected guid: %s", guid, s.spaceGuid)
}
return &resource.Space{
Name: s.spaceName,
}, nil
}
type mockServiceInstances struct {
getServiceInstanceErr error
instanceName string
instanceGUID string
}
func (si *mockServiceInstances) Get(ctx context.Context, guid string) (*resource.ServiceInstance, error) {
if si.getServiceInstanceErr != nil {
return nil, si.getServiceInstanceErr
}
if guid != si.instanceGUID {
return nil, fmt.Errorf("guid argument: %s does not match expected guid: %s", guid, si.instanceGUID)
}
return &resource.ServiceInstance{
Name: si.instanceName,
}, nil
}
func TestGetOrganization(t *testing.T) {
testCases := map[string]struct {
cfResourceGetter *cfResourceGetter
expectedOrganization *resource.Organization
expectedErr error
organizationGuid string
}{
"success": {
cfResourceGetter: &cfResourceGetter{
Organizations: &mockOrganizations{
organizationName: "org-1",
organizationGuid: "guid-1",
},
},
organizationGuid: "guid-1",
expectedOrganization: &resource.Organization{
Name: "org-1",
},
},
"error": {
cfResourceGetter: &cfResourceGetter{
Organizations: &mockOrganizations{
getOrganizationErr: errors.New("error getting organization"),
},
},
expectedErr: errors.New("error getting organization"),
},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
organization, err := test.cfResourceGetter.getOrganization(test.organizationGuid)
if !cmp.Equal(organization, test.expectedOrganization) {
t.Errorf(cmp.Diff(organization, test.expectedOrganization))
}
if (test.expectedErr != nil && err == nil) ||
(err != nil && err.Error() != test.expectedErr.Error()) {
t.Fatalf("expected error: %s, got: %s", test.expectedErr, err)
}
})
}
}
func TestGetSpace(t *testing.T) {
testCases := map[string]struct {
cfResourceGetter *cfResourceGetter
expectedSpace *resource.Space
expectedErr error
spaceGuid string
}{
"success": {
cfResourceGetter: &cfResourceGetter{
Spaces: &mockSpaces{
spaceName: "space-1",
spaceGuid: "guid-1",
},
},
spaceGuid: "guid-1",
expectedSpace: &resource.Space{
Name: "space-1",
},
},
"error": {
cfResourceGetter: &cfResourceGetter{
Spaces: &mockSpaces{
getSpaceErr: errors.New("error getting space"),
},
},
expectedErr: errors.New("error getting space"),
},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
space, err := test.cfResourceGetter.getSpace(test.spaceGuid)
if !cmp.Equal(space, test.expectedSpace) {
t.Errorf(cmp.Diff(space, test.expectedSpace))
}
if (test.expectedErr != nil && err == nil) ||
(err != nil && err.Error() != test.expectedErr.Error()) {
t.Fatalf("expected error: %s, got: %s", test.expectedErr, err)
}
})
}
}
func TestGetServiceInstance(t *testing.T) {
testCases := map[string]struct {
cfResourceGetter *cfResourceGetter
expectedServiceInstance *resource.ServiceInstance
expectedErr error
instanceGUID string
}{
"success": {
cfResourceGetter: &cfResourceGetter{
ServiceInstances: &mockServiceInstances{
instanceName: "instance-1",
instanceGUID: "guid-1",
},
},
instanceGUID: "guid-1",
expectedServiceInstance: &resource.ServiceInstance{
Name: "instance-1",
},
},
"error": {
cfResourceGetter: &cfResourceGetter{
ServiceInstances: &mockServiceInstances{
getServiceInstanceErr: errors.New("error getting instance"),
},
},
expectedErr: errors.New("error getting instance"),
},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
instance, err := test.cfResourceGetter.getServiceInstance(test.instanceGUID)
if !cmp.Equal(instance, test.expectedServiceInstance) {
t.Errorf(cmp.Diff(instance, test.expectedServiceInstance))
}
if (test.expectedErr != nil && err == nil) ||
(err != nil && err.Error() != test.expectedErr.Error()) {
t.Fatalf("expected error: %s, got: %s", test.expectedErr, err)
}
})
}
}