utils_test.go 4.8 KB

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