@@ -22,9 +22,12 @@ import (
2222
2323 corev1 "k8s.io/api/core/v1"
2424
25+ "github.com/apache/cloudstack-go/v2/cloudstack"
26+ "github.com/golang/mock/gomock"
2527 . "github.com/onsi/ginkgo/v2"
2628 . "github.com/onsi/gomega"
2729 "sigs.k8s.io/cluster-api-provider-cloudstack/pkg/cloud"
30+ dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta1"
2831 "sigs.k8s.io/cluster-api-provider-cloudstack/test/helpers"
2932)
3033
@@ -36,9 +39,20 @@ type Global struct {
3639
3740var _ = Describe ("Client" , func () {
3841
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+ )
4049
4150 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 )
4256 })
4357
4458 AfterEach (func () {
@@ -100,10 +114,48 @@ var _ = Describe("Client", func() {
100114
101115 Context ("NewClientFromConf" , func () {
102116 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+ }
103123
104124 BeforeEach (func () {
105125 clientConfig .Data = map [string ]string {}
106126 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+
107159 })
108160
109161 It ("Returns a new client" , func () {
@@ -138,18 +190,5 @@ var _ = Describe("Client", func() {
138190 result2 , _ := cloud .NewClientFromConf (config2 , clientConfig )
139191 Ω (result1 ).Should (Equal (result2 ))
140192 })
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- })
154193 })
155194})
0 commit comments