Skip to content

Commit bc315ea

Browse files
committed
Add unit tests to client cache implementation
1 parent 6a684f6 commit bc315ea

File tree

2 files changed

+108
-7
lines changed

2 files changed

+108
-7
lines changed

pkg/cloud/client.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ func generateClientCacheKey(conf Config) string {
205205

206206
func newClientCache(clientConfig *corev1.ConfigMap) *ttlcache.Cache {
207207
clientCache := ttlcache.NewCache()
208+
clientCache.SetTTL(GetClientCacheTTL(clientConfig))
209+
clientCache.SkipTtlExtensionOnHit(false)
210+
return clientCache
211+
}
212+
213+
func GetClientCacheTTL(clientConfig *corev1.ConfigMap) time.Duration {
208214
var cacheTTL time.Duration
209215
if clientConfig != nil {
210216
if ttl, exists := clientConfig.Data[ClientCacheTTLKey]; exists {
@@ -214,7 +220,5 @@ func newClientCache(clientConfig *corev1.ConfigMap) *ttlcache.Cache {
214220
if cacheTTL == 0 {
215221
cacheTTL = DefaultClientCacheTTL
216222
}
217-
clientCache.SetTTL(cacheTTL)
218-
clientCache.SkipTtlExtensionOnHit(false)
219-
return clientCache
223+
return cacheTTL
220224
}

pkg/cloud/client_test.go

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package cloud_test
1818

1919
import (
2020
"os"
21+
"time"
22+
23+
corev1 "k8s.io/api/core/v1"
2124

2225
. "github.com/onsi/ginkgo/v2"
2326
. "github.com/onsi/gomega"
@@ -36,17 +39,18 @@ var _ = Describe("Instance", func() {
3639
var ()
3740

3841
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.")
4342
})
4443

4544
AfterEach(func() {
4645
})
4746

4847
Context("When fetching a YAML config.", func() {
4948
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+
5054
// Create a real cloud client.
5155
var connectionErr error
5256
_, connectionErr = helpers.NewCSClient()
@@ -56,4 +60,97 @@ var _ = Describe("Instance", func() {
5660
Ω(connectionErr).ShouldNot(HaveOccurred())
5761
})
5862
})
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+
})
59156
})

0 commit comments

Comments
 (0)