@@ -22,9 +22,12 @@ import (
22
22
23
23
corev1 "k8s.io/api/core/v1"
24
24
25
+ "github.com/apache/cloudstack-go/v2/cloudstack"
26
+ "github.com/golang/mock/gomock"
25
27
. "github.com/onsi/ginkgo/v2"
26
28
. "github.com/onsi/gomega"
27
29
"sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
30
+ dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta1"
28
31
"sigs.k8s.io/cluster-api-provider-cloudstack/test/helpers"
29
32
)
30
33
@@ -36,9 +39,20 @@ type Global struct {
36
39
37
40
var _ = Describe ("Client" , func () {
38
41
39
- var ()
42
+ var (
43
+ mockCtrl * gomock.Controller
44
+ mockClient * cloudstack.CloudStackClient
45
+ us * cloudstack.MockUserServiceIface
46
+ ds * cloudstack.MockDomainServiceIface
47
+ as * cloudstack.MockAccountServiceIface
48
+ )
40
49
41
50
BeforeEach (func () {
51
+ mockCtrl = gomock .NewController (GinkgoT ())
52
+ mockClient = cloudstack .NewMockClient (mockCtrl )
53
+ us = mockClient .User .(* cloudstack.MockUserServiceIface )
54
+ ds = mockClient .Domain .(* cloudstack.MockDomainServiceIface )
55
+ as = mockClient .Account .(* cloudstack.MockAccountServiceIface )
42
56
})
43
57
44
58
AfterEach (func () {
@@ -100,10 +114,48 @@ var _ = Describe("Client", func() {
100
114
101
115
Context ("NewClientFromConf" , func () {
102
116
clientConfig := & corev1.ConfigMap {}
117
+ cloud .NewAsyncClient = func (apiurl , apikey , secret string , verifyssl bool , options ... cloudstack.ClientOption ) * cloudstack.CloudStackClient {
118
+ return mockClient
119
+ }
120
+ cloud .NewClient = func (apiurl , apikey , secret string , verifyssl bool , options ... cloudstack.ClientOption ) * cloudstack.CloudStackClient {
121
+ return mockClient
122
+ }
103
123
104
124
BeforeEach (func () {
105
125
clientConfig .Data = map [string ]string {}
106
126
clientConfig .Data [cloud .ClientCacheTTLKey ] = "100ms"
127
+ fakeListParams := & cloudstack.ListUsersParams {}
128
+ fakeUser := & cloudstack.User {
129
+ Id : dummies .UserID ,
130
+ Account : dummies .AccountName ,
131
+ Domain : dummies .DomainName ,
132
+ }
133
+ us .EXPECT ().NewListUsersParams ().Return (fakeListParams ).AnyTimes ()
134
+ us .EXPECT ().ListUsers (fakeListParams ).Return (& cloudstack.ListUsersResponse {
135
+ Count : 1 , Users : []* cloudstack.User {fakeUser },
136
+ }, nil ).AnyTimes ()
137
+
138
+ dsp := & cloudstack.ListDomainsParams {}
139
+ ds .EXPECT ().NewListDomainsParams ().Return (dsp ).AnyTimes ()
140
+ ds .EXPECT ().ListDomains (dsp ).Return (& cloudstack.ListDomainsResponse {Count : 1 , Domains : []* cloudstack.Domain {{
141
+ Id : dummies .DomainID ,
142
+ Name : dummies .DomainName ,
143
+ Path : dummies .DomainPath ,
144
+ }}}, nil ).AnyTimes ()
145
+
146
+ asp := & cloudstack.ListAccountsParams {}
147
+ as .EXPECT ().NewListAccountsParams ().Return (asp ).AnyTimes ()
148
+ as .EXPECT ().ListAccounts (asp ).Return (& cloudstack.ListAccountsResponse {Count : 1 , Accounts : []* cloudstack.Account {{
149
+ Id : dummies .AccountID ,
150
+ Name : dummies .AccountName ,
151
+ }}}, nil ).AnyTimes ()
152
+ ukp := & cloudstack.GetUserKeysParams {}
153
+ us .EXPECT ().NewGetUserKeysParams (gomock .Any ()).Return (ukp ).AnyTimes ()
154
+ us .EXPECT ().GetUserKeys (ukp ).Return (& cloudstack.GetUserKeysResponse {
155
+ Apikey : dummies .Apikey ,
156
+ Secretkey : dummies .SecretKey ,
157
+ }, nil ).AnyTimes ()
158
+
107
159
})
108
160
109
161
It ("Returns a new client" , func () {
@@ -138,18 +190,5 @@ var _ = Describe("Client", func() {
138
190
result2 , _ := cloud .NewClientFromConf (config2 , clientConfig )
139
191
Ω (result1 ).Should (Equal (result2 ))
140
192
})
141
-
142
- It ("Returns a new client after cache expiration" , func () {
143
- config1 := cloud.Config {
144
- APIUrl : "http://5.5.5.5" ,
145
- }
146
- config2 := cloud.Config {
147
- APIUrl : "http://5.5.5.5" ,
148
- }
149
- result1 , _ := cloud .NewClientFromConf (config1 , clientConfig )
150
- time .Sleep (150 * time .Millisecond )
151
- result2 , _ := cloud .NewClientFromConf (config2 , clientConfig )
152
- Ω (result1 ).ShouldNot (Equal (result2 ))
153
- })
154
193
})
155
194
})
0 commit comments