utils_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package utils
  13. import (
  14. "reflect"
  15. "testing"
  16. vault "github.com/oracle/oci-go-sdk/v56/vault"
  17. v1 "k8s.io/api/core/v1"
  18. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  19. )
  20. func TestObjectHash(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. input interface{}
  24. want string
  25. }{
  26. {
  27. name: "A nil should be still working",
  28. input: nil,
  29. want: "60046f14c917c18a9a0f923e191ba0dc",
  30. },
  31. {
  32. name: "We accept a simple scalar value, i.e. string",
  33. input: "hello there",
  34. want: "161bc25962da8fed6d2f59922fb642aa",
  35. },
  36. {
  37. name: "A complex object like a secret is not an issue",
  38. input: v1.Secret{Data: map[string][]byte{
  39. "xx": []byte("yyy"),
  40. }},
  41. want: "a9fe13fd43b20829b45f0a93372413dd",
  42. },
  43. {
  44. name: "map also works",
  45. input: map[string][]byte{
  46. "foo": []byte("value1"),
  47. "bar": []byte("value2"),
  48. },
  49. want: "caa0155759a6a9b3b6ada5a6883ee2bb",
  50. },
  51. }
  52. for _, tt := range tests {
  53. t.Run(tt.name, func(t *testing.T) {
  54. if got := ObjectHash(tt.input); got != tt.want {
  55. t.Errorf("ObjectHash() = %v, want %v", got, tt.want)
  56. }
  57. })
  58. }
  59. }
  60. func TestIsNil(t *testing.T) {
  61. tbl := []struct {
  62. name string
  63. val interface{}
  64. exp bool
  65. }{
  66. {
  67. name: "simple nil val",
  68. val: nil,
  69. exp: true,
  70. },
  71. {
  72. name: "nil slice",
  73. val: (*[]struct{})(nil),
  74. exp: true,
  75. },
  76. {
  77. name: "struct pointer",
  78. val: &testing.T{},
  79. exp: false,
  80. },
  81. {
  82. name: "struct",
  83. val: testing.T{},
  84. exp: false,
  85. },
  86. {
  87. name: "slice of struct",
  88. val: []struct{}{{}},
  89. exp: false,
  90. },
  91. {
  92. name: "slice of ptr",
  93. val: []*testing.T{nil},
  94. exp: false,
  95. },
  96. {
  97. name: "slice",
  98. val: []struct{}(nil),
  99. exp: false,
  100. },
  101. {
  102. name: "int default value",
  103. val: 0,
  104. exp: false,
  105. },
  106. {
  107. name: "empty str",
  108. val: "",
  109. exp: false,
  110. },
  111. {
  112. name: "oracle vault",
  113. val: vault.VaultsClient{},
  114. exp: false,
  115. },
  116. {
  117. name: "func",
  118. val: func() {
  119. // noop for testing and to make linter happy
  120. },
  121. exp: false,
  122. },
  123. {
  124. name: "channel",
  125. val: make(chan struct{}),
  126. exp: false,
  127. },
  128. {
  129. name: "map",
  130. val: map[string]string{},
  131. exp: false,
  132. },
  133. }
  134. for _, row := range tbl {
  135. t.Run(row.name, func(t *testing.T) {
  136. res := IsNil(row.val)
  137. if res != row.exp {
  138. t.Errorf("IsNil(%#v)=%t, expected %t", row.val, res, row.exp)
  139. }
  140. })
  141. }
  142. }
  143. func TestConvertKeys(t *testing.T) {
  144. type args struct {
  145. strategy esv1beta1.ExternalSecretConversionStrategy
  146. in map[string][]byte
  147. }
  148. tests := []struct {
  149. name string
  150. args args
  151. want map[string][]byte
  152. wantErr bool
  153. }{
  154. {
  155. name: "convert with special chars",
  156. args: args{
  157. strategy: esv1beta1.ExternalSecretConversionDefault,
  158. in: map[string][]byte{
  159. "foo$bar%baz*bing": []byte(`noop`),
  160. },
  161. },
  162. want: map[string][]byte{
  163. "foo_bar_baz_bing": []byte(`noop`),
  164. },
  165. },
  166. {
  167. name: "error on collision",
  168. args: args{
  169. strategy: esv1beta1.ExternalSecretConversionDefault,
  170. in: map[string][]byte{
  171. "foo$bar%baz*bing": []byte(`noop`),
  172. "foo_bar_baz$bing": []byte(`noop`),
  173. },
  174. },
  175. wantErr: true,
  176. },
  177. {
  178. name: "convert path",
  179. args: args{
  180. strategy: esv1beta1.ExternalSecretConversionDefault,
  181. in: map[string][]byte{
  182. "/foo/bar/baz/bing": []byte(`noop`),
  183. "foo/bar/baz/bing/": []byte(`noop`),
  184. },
  185. },
  186. want: map[string][]byte{
  187. "_foo_bar_baz_bing": []byte(`noop`),
  188. "foo_bar_baz_bing_": []byte(`noop`),
  189. },
  190. },
  191. {
  192. name: "convert unicode",
  193. args: args{
  194. strategy: esv1beta1.ExternalSecretConversionUnicode,
  195. in: map[string][]byte{
  196. "😀foo😁bar😂baz😈bing": []byte(`noop`),
  197. },
  198. },
  199. want: map[string][]byte{
  200. "_U1f600_foo_U1f601_bar_U1f602_baz_U1f608_bing": []byte(`noop`),
  201. },
  202. },
  203. }
  204. for _, tt := range tests {
  205. t.Run(tt.name, func(t *testing.T) {
  206. got, err := ConvertKeys(tt.args.strategy, tt.args.in)
  207. if (err != nil) != tt.wantErr {
  208. t.Errorf("ConvertKeys() error = %v, wantErr %v", err, tt.wantErr)
  209. return
  210. }
  211. if !reflect.DeepEqual(got, tt.want) {
  212. t.Errorf("ConvertKeys() = %v, want %v", got, tt.want)
  213. }
  214. })
  215. }
  216. }