configmap_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "reflect"
  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. utiltest "k8s.io/kubernetes/pkg/kubelet/kubeletconfig/util/test"
  22. )
  23. func TestNewConfigMapPayload(t *testing.T) {
  24. cases := []struct {
  25. desc string
  26. cm *apiv1.ConfigMap
  27. err string
  28. }{
  29. {
  30. desc: "nil",
  31. cm: nil,
  32. err: "ConfigMap must be non-nil",
  33. },
  34. {
  35. desc: "missing uid",
  36. cm: &apiv1.ConfigMap{
  37. ObjectMeta: metav1.ObjectMeta{
  38. Name: "name",
  39. ResourceVersion: "rv",
  40. },
  41. },
  42. err: "ConfigMap must have a non-empty UID",
  43. },
  44. {
  45. desc: "missing resourceVersion",
  46. cm: &apiv1.ConfigMap{
  47. ObjectMeta: metav1.ObjectMeta{
  48. Name: "name",
  49. UID: "uid",
  50. },
  51. },
  52. err: "ConfigMap must have a non-empty ResourceVersion",
  53. },
  54. {
  55. desc: "populated v1/ConfigMap",
  56. cm: &apiv1.ConfigMap{
  57. ObjectMeta: metav1.ObjectMeta{
  58. Name: "name",
  59. UID: "uid",
  60. ResourceVersion: "rv",
  61. },
  62. Data: map[string]string{
  63. "key1": "value1",
  64. "key2": "value2",
  65. },
  66. },
  67. err: "",
  68. },
  69. }
  70. for _, c := range cases {
  71. t.Run(c.desc, func(t *testing.T) {
  72. payload, err := NewConfigMapPayload(c.cm)
  73. utiltest.ExpectError(t, err, c.err)
  74. if err != nil {
  75. return
  76. }
  77. // underlying object should match the object passed in
  78. if !apiequality.Semantic.DeepEqual(c.cm, payload.object()) {
  79. t.Errorf("expect %s but got %s", spew.Sdump(c.cm), spew.Sdump(payload))
  80. }
  81. })
  82. }
  83. }
  84. func TestConfigMapPayloadUID(t *testing.T) {
  85. const expect = "uid"
  86. payload, err := NewConfigMapPayload(&apiv1.ConfigMap{ObjectMeta: metav1.ObjectMeta{UID: expect, ResourceVersion: "rv"}})
  87. if err != nil {
  88. t.Fatalf("error constructing payload: %v", err)
  89. }
  90. uid := payload.UID()
  91. if expect != uid {
  92. t.Errorf("expect %q, but got %q", expect, uid)
  93. }
  94. }
  95. func TestConfigMapPayloadResourceVersion(t *testing.T) {
  96. const expect = "rv"
  97. payload, err := NewConfigMapPayload(&apiv1.ConfigMap{ObjectMeta: metav1.ObjectMeta{UID: "uid", ResourceVersion: expect}})
  98. if err != nil {
  99. t.Fatalf("error constructing payload: %v", err)
  100. }
  101. resourceVersion := payload.ResourceVersion()
  102. if expect != resourceVersion {
  103. t.Errorf("expect %q, but got %q", expect, resourceVersion)
  104. }
  105. }
  106. func TestConfigMapPayloadFiles(t *testing.T) {
  107. cases := []struct {
  108. desc string
  109. data map[string]string
  110. expect map[string]string
  111. }{
  112. {"nil", nil, nil},
  113. {"empty", map[string]string{}, map[string]string{}},
  114. {"populated",
  115. map[string]string{
  116. "foo": "1",
  117. "bar": "2",
  118. },
  119. map[string]string{
  120. "foo": "1",
  121. "bar": "2",
  122. }},
  123. }
  124. for _, c := range cases {
  125. t.Run(c.desc, func(t *testing.T) {
  126. payload, err := NewConfigMapPayload(&apiv1.ConfigMap{ObjectMeta: metav1.ObjectMeta{UID: "uid", ResourceVersion: "rv"}, Data: c.data})
  127. if err != nil {
  128. t.Fatalf("error constructing payload: %v", err)
  129. }
  130. files := payload.Files()
  131. if !reflect.DeepEqual(c.expect, files) {
  132. t.Errorf("expected %v, but got %v", c.expect, files)
  133. }
  134. })
  135. }
  136. }