@@ -18,6 +18,9 @@ package cloud_test
18
18
19
19
import (
20
20
"os"
21
+ "time"
22
+
23
+ corev1 "k8s.io/api/core/v1"
21
24
22
25
. "github.com/onsi/ginkgo/v2"
23
26
. "github.com/onsi/gomega"
@@ -36,17 +39,18 @@ var _ = Describe("Instance", func() {
36
39
var ()
37
40
38
41
BeforeEach (func () {
39
- // This test fixture is useful for development, but the actual method of parsing is confinded to the client's
40
- // new client method. The parsing used here is more of a schema, and we don't need to test another library's
41
- // abilities to parse said schema.
42
- Skip ("Dev test suite." )
43
42
})
44
43
45
44
AfterEach (func () {
46
45
})
47
46
48
47
Context ("When fetching a YAML config." , func () {
49
48
It ("Handles the positive case." , func () {
49
+ // This test fixture is useful for development, but the actual method of parsing is confinded to the client's
50
+ // new client method. The parsing used here is more of a schema, and we don't need to test another library's
51
+ // abilities to parse said schema.
52
+ Skip ("Dev test suite." )
53
+
50
54
// Create a real cloud client.
51
55
var connectionErr error
52
56
_ , connectionErr = helpers .NewCSClient ()
@@ -56,4 +60,97 @@ var _ = Describe("Instance", func() {
56
60
Ω (connectionErr ).ShouldNot (HaveOccurred ())
57
61
})
58
62
})
63
+
64
+ Context ("GetClientCacheTTL" , func () {
65
+ It ("Returns the default TTL when a nil is passed" , func () {
66
+ result := cloud .GetClientCacheTTL (nil )
67
+ Ω (result ).Should (Equal (cloud .DefaultClientCacheTTL ))
68
+ })
69
+
70
+ It ("Returns the default TTL when an empty config map is passed" , func () {
71
+ clientConfig := & corev1.ConfigMap {}
72
+ result := cloud .GetClientCacheTTL (clientConfig )
73
+ Ω (result ).Should (Equal (cloud .DefaultClientCacheTTL ))
74
+ })
75
+
76
+ It ("Returns the default TTL when the TTL key does not exist" , func () {
77
+ clientConfig := & corev1.ConfigMap {}
78
+ clientConfig .Data = map [string ]string {}
79
+ clientConfig .Data [cloud .ClientCacheTTLKey + "XXXX" ] = "1m5s"
80
+ result := cloud .GetClientCacheTTL (clientConfig )
81
+ Ω (result ).Should (Equal (cloud .DefaultClientCacheTTL ))
82
+ })
83
+
84
+ It ("Returns the default TTL when the TTL value is invalid" , func () {
85
+ clientConfig := & corev1.ConfigMap {}
86
+ clientConfig .Data = map [string ]string {}
87
+ clientConfig .Data [cloud .ClientCacheTTLKey ] = "5mXXX"
88
+ result := cloud .GetClientCacheTTL (clientConfig )
89
+ Ω (result ).Should (Equal (cloud .DefaultClientCacheTTL ))
90
+ })
91
+
92
+ It ("Returns the TTL from the input clientConfig map" , func () {
93
+ clientConfig := & corev1.ConfigMap {}
94
+ clientConfig .Data = map [string ]string {}
95
+ clientConfig .Data [cloud .ClientCacheTTLKey ] = "5m10s"
96
+ expected , _ := time .ParseDuration ("5m10s" )
97
+ result := cloud .GetClientCacheTTL (clientConfig )
98
+ Ω (result ).Should (Equal (expected ))
99
+ })
100
+ })
101
+
102
+ Context ("NewClientFromConf" , func () {
103
+ clientConfig := & corev1.ConfigMap {}
104
+
105
+ BeforeEach (func () {
106
+ clientConfig .Data = map [string ]string {}
107
+ clientConfig .Data [cloud .ClientCacheTTLKey ] = "100ms"
108
+ })
109
+
110
+ It ("Returns a new client" , func () {
111
+ config := cloud.Config {
112
+ APIUrl : "http://1.1.1.1" ,
113
+ }
114
+ result , err := cloud .NewClientFromConf (config , clientConfig )
115
+ Ω (err ).ShouldNot (HaveOccurred ())
116
+ Ω (result ).ShouldNot (BeNil ())
117
+ })
118
+
119
+ It ("Returns a new client for a different config" , func () {
120
+ config1 := cloud.Config {
121
+ APIUrl : "http://2.2.2.2" ,
122
+ }
123
+ config2 := cloud.Config {
124
+ APIUrl : "http://3.3.3.3" ,
125
+ }
126
+ result1 , _ := cloud .NewClientFromConf (config1 , clientConfig )
127
+ result2 , _ := cloud .NewClientFromConf (config2 , clientConfig )
128
+ Ω (result1 ).ShouldNot (Equal (result2 ))
129
+ })
130
+
131
+ It ("Returns a cached client for the same config" , func () {
132
+ config1 := cloud.Config {
133
+ APIUrl : "http://4.4.4.4" ,
134
+ }
135
+ config2 := cloud.Config {
136
+ APIUrl : "http://4.4.4.4" ,
137
+ }
138
+ result1 , _ := cloud .NewClientFromConf (config1 , clientConfig )
139
+ result2 , _ := cloud .NewClientFromConf (config2 , clientConfig )
140
+ Ω (result1 ).Should (Equal (result2 ))
141
+ })
142
+
143
+ It ("Returns a new client after cache expiration" , func () {
144
+ config1 := cloud.Config {
145
+ APIUrl : "http://5.5.5.5" ,
146
+ }
147
+ config2 := cloud.Config {
148
+ APIUrl : "http://5.5.5.5" ,
149
+ }
150
+ result1 , _ := cloud .NewClientFromConf (config1 , clientConfig )
151
+ time .Sleep (150 * time .Millisecond )
152
+ result2 , _ := cloud .NewClientFromConf (config2 , clientConfig )
153
+ Ω (result1 ).ShouldNot (Equal (result2 ))
154
+ })
155
+ })
59
156
})
0 commit comments