@@ -17,6 +17,7 @@ limitations under the License.
1717package internal
1818
1919import (
20+ "errors"
2021 "testing"
2122
2223 "github.com/onsi/gomega"
@@ -87,3 +88,139 @@ kind: ClusterStatus`,
8788 )),
8889 ))
8990}
91+
92+ func TestUpdateEtcdMeta (t * testing.T ) {
93+
94+ tests := []struct {
95+ name string
96+ clusterConfigurationValue string
97+ imageRepository string
98+ imageTag string
99+ expectChanged bool
100+ expectErr error
101+ }{
102+ {
103+ name : "it should set the values, if they were empty" ,
104+ clusterConfigurationValue : `
105+ apiVersion: kubeadm.k8s.io/v1beta2
106+ kind: ClusterConfiguration
107+ etcd:
108+ local:
109+ dataDir: /var/lib/etcd
110+ ` ,
111+ imageRepository : "gcr.io/k8s/etcd" ,
112+ imageTag : "0.10.9" ,
113+ expectChanged : true ,
114+ },
115+ {
116+ name : "it should return false with no error, if there are no changes" ,
117+ clusterConfigurationValue : `
118+ apiVersion: kubeadm.k8s.io/v1beta2
119+ kind: ClusterConfiguration
120+ etcd:
121+ local:
122+ dataDir: /var/lib/etcd
123+ imageRepository: "gcr.io/k8s/etcd"
124+ imageTag: "0.10.9"
125+ ` ,
126+ imageRepository : "gcr.io/k8s/etcd" ,
127+ imageTag : "0.10.9" ,
128+ expectChanged : false ,
129+ },
130+ {
131+ name : "it shouldn't write empty strings" ,
132+ clusterConfigurationValue : `
133+ apiVersion: kubeadm.k8s.io/v1beta2
134+ kind: ClusterConfiguration
135+ etcd:
136+ local:
137+ dataDir: /var/lib/etcd
138+ ` ,
139+ imageRepository : "" ,
140+ imageTag : "" ,
141+ expectChanged : false ,
142+ },
143+ {
144+ name : "it should overwrite imageTag" ,
145+ clusterConfigurationValue : `
146+ apiVersion: kubeadm.k8s.io/v1beta2
147+ kind: ClusterConfiguration
148+ etcd:
149+ local:
150+ imageTag: 0.10.8
151+ dataDir: /var/lib/etcd
152+ ` ,
153+ imageTag : "0.10.9" ,
154+ expectChanged : true ,
155+ },
156+ {
157+ name : "it should overwrite imageRepository" ,
158+ clusterConfigurationValue : `
159+ apiVersion: kubeadm.k8s.io/v1beta2
160+ kind: ClusterConfiguration
161+ etcd:
162+ local:
163+ imageRepository: another-custom-repo
164+ dataDir: /var/lib/etcd
165+ ` ,
166+ imageRepository : "gcr.io/k8s/etcd" ,
167+ expectChanged : true ,
168+ },
169+ {
170+ name : "it should error if it's not a valid k8s object" ,
171+ clusterConfigurationValue : `
172+ etcd:
173+ local:
174+ imageRepository: another-custom-repo
175+ dataDir: /var/lib/etcd
176+ ` ,
177+ expectErr : errors .New ("Object 'Kind' is missing" ),
178+ },
179+ {
180+ name : "it should error if the current value is a type we don't expect" ,
181+ clusterConfigurationValue : `
182+ apiVersion: kubeadm.k8s.io/v1beta2
183+ kind: ClusterConfiguration
184+ etcd:
185+ local:
186+ imageRepository: true
187+ dataDir: /var/lib/etcd
188+ ` ,
189+ expectErr : errors .New (".etcd.local.imageRepository accessor error" ),
190+ },
191+ }
192+
193+ for _ , test := range tests {
194+ t .Run (test .name , func (t * testing.T ) {
195+ g := gomega .NewWithT (t )
196+
197+ kconfig := & kubeadmConfig {
198+ ConfigMap : & corev1.ConfigMap {
199+ Data : map [string ]string {
200+ clusterConfigurationKey : test .clusterConfigurationValue ,
201+ },
202+ },
203+ }
204+
205+ changed , err := kconfig .UpdateEtcdMeta (test .imageRepository , test .imageTag )
206+ if test .expectErr == nil {
207+ g .Expect (err ).ToNot (HaveOccurred ())
208+ } else {
209+ g .Expect (err ).To (HaveOccurred ())
210+ g .Expect (err .Error ()).To (gomega .ContainSubstring (test .expectErr .Error ()))
211+ }
212+
213+ g .Expect (changed ).To (gomega .Equal (test .expectChanged ))
214+ if changed {
215+ if test .imageRepository != "" {
216+ g .Expect (kconfig .ConfigMap .Data [clusterConfigurationKey ]).To (gomega .ContainSubstring (test .imageRepository ))
217+ }
218+ if test .imageTag != "" {
219+ g .Expect (kconfig .ConfigMap .Data [clusterConfigurationKey ]).To (gomega .ContainSubstring (test .imageTag ))
220+ }
221+ }
222+
223+ })
224+ }
225+
226+ }
0 commit comments