download_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package checkpoint
  14. import (
  15. "fmt"
  16. "testing"
  17. "github.com/davecgh/go-spew/spew"
  18. apiv1 "k8s.io/api/core/v1"
  19. apiequality "k8s.io/apimachinery/pkg/api/equality"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. clientset "k8s.io/client-go/kubernetes"
  22. fakeclient "k8s.io/client-go/kubernetes/fake"
  23. "k8s.io/client-go/tools/cache"
  24. utiltest "k8s.io/kubernetes/pkg/kubelet/kubeletconfig/util/test"
  25. )
  26. func TestNewRemoteConfigSource(t *testing.T) {
  27. cases := []struct {
  28. desc string
  29. source *apiv1.NodeConfigSource
  30. expect RemoteConfigSource
  31. err string
  32. }{
  33. {
  34. desc: "all NodeConfigSource subfields nil",
  35. source: &apiv1.NodeConfigSource{},
  36. expect: nil,
  37. err: "exactly one subfield must be non-nil",
  38. },
  39. {
  40. desc: "ConfigMap: valid reference",
  41. source: &apiv1.NodeConfigSource{
  42. ConfigMap: &apiv1.ConfigMapNodeConfigSource{
  43. Name: "name",
  44. Namespace: "namespace",
  45. UID: "uid",
  46. KubeletConfigKey: "kubelet",
  47. }},
  48. expect: &remoteConfigMap{&apiv1.NodeConfigSource{
  49. ConfigMap: &apiv1.ConfigMapNodeConfigSource{
  50. Name: "name",
  51. Namespace: "namespace",
  52. UID: "uid",
  53. KubeletConfigKey: "kubelet",
  54. }}},
  55. err: "",
  56. },
  57. }
  58. for _, c := range cases {
  59. t.Run(c.desc, func(t *testing.T) {
  60. source, _, err := NewRemoteConfigSource(c.source)
  61. utiltest.ExpectError(t, err, c.err)
  62. if err != nil {
  63. return
  64. }
  65. // underlying object should match the object passed in
  66. if !apiequality.Semantic.DeepEqual(c.expect.NodeConfigSource(), source.NodeConfigSource()) {
  67. t.Errorf("case %q, expect RemoteConfigSource %s but got %s", c.desc, spew.Sdump(c.expect), spew.Sdump(source))
  68. }
  69. })
  70. }
  71. }
  72. func TestRemoteConfigMapUID(t *testing.T) {
  73. const expect = "uid"
  74. source, _, err := NewRemoteConfigSource(&apiv1.NodeConfigSource{ConfigMap: &apiv1.ConfigMapNodeConfigSource{
  75. Name: "name",
  76. Namespace: "namespace",
  77. UID: expect,
  78. KubeletConfigKey: "kubelet",
  79. }})
  80. if err != nil {
  81. t.Fatalf("error constructing remote config source: %v", err)
  82. }
  83. uid := source.UID()
  84. if expect != uid {
  85. t.Errorf("expect %q, but got %q", expect, uid)
  86. }
  87. }
  88. func TestRemoteConfigMapAPIPath(t *testing.T) {
  89. const (
  90. name = "name"
  91. namespace = "namespace"
  92. )
  93. source, _, err := NewRemoteConfigSource(&apiv1.NodeConfigSource{ConfigMap: &apiv1.ConfigMapNodeConfigSource{
  94. Name: name,
  95. Namespace: namespace,
  96. UID: "uid",
  97. KubeletConfigKey: "kubelet",
  98. }})
  99. if err != nil {
  100. t.Fatalf("error constructing remote config source: %v", err)
  101. }
  102. expect := fmt.Sprintf(configMapAPIPathFmt, namespace, name)
  103. path := source.APIPath()
  104. if expect != path {
  105. t.Errorf("expect %q, but got %q", expect, path)
  106. }
  107. }
  108. func TestRemoteConfigMapDownload(t *testing.T) {
  109. cm := &apiv1.ConfigMap{
  110. ObjectMeta: metav1.ObjectMeta{
  111. Name: "name",
  112. Namespace: "namespace",
  113. UID: "uid",
  114. ResourceVersion: "1",
  115. }}
  116. source := &apiv1.NodeConfigSource{ConfigMap: &apiv1.ConfigMapNodeConfigSource{
  117. Name: "name",
  118. Namespace: "namespace",
  119. KubeletConfigKey: "kubelet",
  120. }}
  121. expectPayload, err := NewConfigMapPayload(cm)
  122. if err != nil {
  123. t.Fatalf("error constructing payload: %v", err)
  124. }
  125. missingStore := cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
  126. hasStore := cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
  127. if err := hasStore.Add(cm); err != nil {
  128. t.Fatalf("unexpected error constructing hasStore")
  129. }
  130. missingClient := fakeclient.NewSimpleClientset()
  131. hasClient := fakeclient.NewSimpleClientset(cm)
  132. cases := []struct {
  133. desc string
  134. client clientset.Interface
  135. store cache.Store
  136. err string
  137. }{
  138. {
  139. desc: "nil store, object does not exist in API server",
  140. client: missingClient,
  141. err: "not found",
  142. },
  143. {
  144. desc: "nil store, object exists in API server",
  145. client: hasClient,
  146. },
  147. {
  148. desc: "object exists in store and API server",
  149. store: hasStore,
  150. client: hasClient,
  151. },
  152. {
  153. desc: "object exists in store, but does not exist in API server",
  154. store: hasStore,
  155. client: missingClient,
  156. },
  157. {
  158. desc: "object does not exist in store, but exists in API server",
  159. store: missingStore,
  160. client: hasClient,
  161. },
  162. {
  163. desc: "object does not exist in store or API server",
  164. client: missingClient,
  165. store: missingStore,
  166. err: "not found",
  167. },
  168. }
  169. for _, c := range cases {
  170. t.Run(c.desc, func(t *testing.T) {
  171. // deep copy so we can always check the UID/ResourceVersion are set after Download
  172. s, _, err := NewRemoteConfigSource(source.DeepCopy())
  173. if err != nil {
  174. t.Fatalf("error constructing remote config source %v", err)
  175. }
  176. // attempt download
  177. p, _, err := s.Download(c.client, c.store)
  178. utiltest.ExpectError(t, err, c.err)
  179. if err != nil {
  180. return
  181. }
  182. // downloaded object should match the expected
  183. if !apiequality.Semantic.DeepEqual(expectPayload.object(), p.object()) {
  184. t.Errorf("expect Checkpoint %s but got %s", spew.Sdump(expectPayload), spew.Sdump(p))
  185. }
  186. // source UID and ResourceVersion should be updated by Download
  187. if p.UID() != s.UID() {
  188. t.Errorf("expect UID to be updated by Download to match payload: %s, but got source UID: %s", p.UID(), s.UID())
  189. }
  190. if p.ResourceVersion() != s.ResourceVersion() {
  191. t.Errorf("expect ResourceVersion to be updated by Download to match payload: %s, but got source ResourceVersion: %s", p.ResourceVersion(), s.ResourceVersion())
  192. }
  193. })
  194. }
  195. }
  196. func TestEqualRemoteConfigSources(t *testing.T) {
  197. cases := []struct {
  198. desc string
  199. a RemoteConfigSource
  200. b RemoteConfigSource
  201. expect bool
  202. }{
  203. {"both nil", nil, nil, true},
  204. {"a nil", nil, &remoteConfigMap{}, false},
  205. {"b nil", &remoteConfigMap{}, nil, false},
  206. {"neither nil, equal", &remoteConfigMap{}, &remoteConfigMap{}, true},
  207. {
  208. desc: "neither nil, not equal",
  209. a: &remoteConfigMap{&apiv1.NodeConfigSource{ConfigMap: &apiv1.ConfigMapNodeConfigSource{Name: "a"}}},
  210. b: &remoteConfigMap{&apiv1.NodeConfigSource{ConfigMap: &apiv1.ConfigMapNodeConfigSource{KubeletConfigKey: "kubelet"}}},
  211. expect: false,
  212. },
  213. }
  214. for _, c := range cases {
  215. t.Run(c.desc, func(t *testing.T) {
  216. if EqualRemoteConfigSources(c.a, c.b) != c.expect {
  217. t.Errorf("expected EqualRemoteConfigSources to return %t, but got %t", c.expect, !c.expect)
  218. }
  219. })
  220. }
  221. }