utils_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. "encoding/json"
  15. "reflect"
  16. "testing"
  17. "time"
  18. "github.com/aws/aws-sdk-go/aws"
  19. "github.com/oracle/oci-go-sdk/v65/vault"
  20. v1 "k8s.io/api/core/v1"
  21. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  22. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  23. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  24. )
  25. const (
  26. base64DecodedValue string = "foo%_?bar"
  27. base64EncodedValue string = "Zm9vJV8/YmFy"
  28. base64URLEncodedValue string = "Zm9vJV8_YmFy"
  29. keyWithEmojis string = "😀foo😁bar😂baz😈bing"
  30. keyWithInvalidChars string = "some-array[0].entity"
  31. keyWithEncodedInvalidChars string = "some-array_U005b_0_U005d_.entity"
  32. )
  33. func TestObjectHash(t *testing.T) {
  34. tests := []struct {
  35. name string
  36. input any
  37. want string
  38. }{
  39. {
  40. name: "A nil should be still working",
  41. input: nil,
  42. want: "60046f14c917c18a9a0f923e191ba0dc",
  43. },
  44. {
  45. name: "We accept a simple scalar value, i.e. string",
  46. input: "hello there",
  47. want: "161bc25962da8fed6d2f59922fb642aa",
  48. },
  49. {
  50. name: "A complex object like a secret is not an issue",
  51. input: v1.Secret{Data: map[string][]byte{
  52. "xx": []byte("yyy"),
  53. }},
  54. want: "85eabdeb376371ffc5a658d7a162eba8",
  55. },
  56. {
  57. name: "map also works",
  58. input: map[string][]byte{
  59. "foo": []byte("value1"),
  60. "bar": []byte("value2"),
  61. },
  62. want: "caa0155759a6a9b3b6ada5a6883ee2bb",
  63. },
  64. }
  65. for _, tt := range tests {
  66. t.Run(tt.name, func(t *testing.T) {
  67. if got := ObjectHash(tt.input); got != tt.want {
  68. t.Errorf("ObjectHash() = %v, want %v", got, tt.want)
  69. }
  70. })
  71. }
  72. }
  73. func TestIsNil(t *testing.T) {
  74. tbl := []struct {
  75. name string
  76. val any
  77. exp bool
  78. }{
  79. {
  80. name: "simple nil val",
  81. val: nil,
  82. exp: true,
  83. },
  84. {
  85. name: "nil slice",
  86. val: (*[]struct{})(nil),
  87. exp: true,
  88. },
  89. {
  90. name: "struct pointer",
  91. val: &testing.T{},
  92. exp: false,
  93. },
  94. {
  95. name: "struct",
  96. val: testing.T{},
  97. exp: false,
  98. },
  99. {
  100. name: "slice of struct",
  101. val: []struct{}{{}},
  102. exp: false,
  103. },
  104. {
  105. name: "slice of ptr",
  106. val: []*testing.T{nil},
  107. exp: false,
  108. },
  109. {
  110. name: "slice",
  111. val: []struct{}(nil),
  112. exp: false,
  113. },
  114. {
  115. name: "int default value",
  116. val: 0,
  117. exp: false,
  118. },
  119. {
  120. name: "empty str",
  121. val: "",
  122. exp: false,
  123. },
  124. {
  125. name: "oracle vault",
  126. val: vault.VaultsClient{},
  127. exp: false,
  128. },
  129. {
  130. name: "func",
  131. val: func() {
  132. // noop for testing and to make linter happy
  133. },
  134. exp: false,
  135. },
  136. {
  137. name: "channel",
  138. val: make(chan struct{}),
  139. exp: false,
  140. },
  141. {
  142. name: "map",
  143. val: map[string]string{},
  144. exp: false,
  145. },
  146. }
  147. for _, row := range tbl {
  148. t.Run(row.name, func(t *testing.T) {
  149. res := IsNil(row.val)
  150. if res != row.exp {
  151. t.Errorf("IsNil(%#v)=%t, expected %t", row.val, res, row.exp)
  152. }
  153. })
  154. }
  155. }
  156. func TestConvertKeys(t *testing.T) {
  157. type args struct {
  158. strategy esv1beta1.ExternalSecretConversionStrategy
  159. in map[string][]byte
  160. }
  161. tests := []struct {
  162. name string
  163. args args
  164. want map[string][]byte
  165. wantErr bool
  166. }{
  167. {
  168. name: "convert with special chars",
  169. args: args{
  170. strategy: esv1beta1.ExternalSecretConversionDefault,
  171. in: map[string][]byte{
  172. "foo$bar%baz*bing": []byte(`noop`),
  173. },
  174. },
  175. want: map[string][]byte{
  176. "foo_bar_baz_bing": []byte(`noop`),
  177. },
  178. },
  179. {
  180. name: "error on collision",
  181. args: args{
  182. strategy: esv1beta1.ExternalSecretConversionDefault,
  183. in: map[string][]byte{
  184. "foo$bar%baz*bing": []byte(`noop`),
  185. "foo_bar_baz$bing": []byte(`noop`),
  186. },
  187. },
  188. wantErr: true,
  189. },
  190. {
  191. name: "convert path",
  192. args: args{
  193. strategy: esv1beta1.ExternalSecretConversionDefault,
  194. in: map[string][]byte{
  195. "/foo/bar/baz/bing": []byte(`noop`),
  196. "foo/bar/baz/bing/": []byte(`noop`),
  197. },
  198. },
  199. want: map[string][]byte{
  200. "_foo_bar_baz_bing": []byte(`noop`),
  201. "foo_bar_baz_bing_": []byte(`noop`),
  202. },
  203. },
  204. {
  205. name: "convert unicode",
  206. args: args{
  207. strategy: esv1beta1.ExternalSecretConversionUnicode,
  208. in: map[string][]byte{
  209. keyWithEmojis: []byte(`noop`),
  210. },
  211. },
  212. want: map[string][]byte{
  213. "_U1f600_foo_U1f601_bar_U1f602_baz_U1f608_bing": []byte(`noop`),
  214. },
  215. },
  216. }
  217. for _, tt := range tests {
  218. t.Run(tt.name, func(t *testing.T) {
  219. got, err := ConvertKeys(tt.args.strategy, tt.args.in)
  220. if (err != nil) != tt.wantErr {
  221. t.Errorf("ConvertKeys() error = %v, wantErr %v", err, tt.wantErr)
  222. return
  223. }
  224. if !reflect.DeepEqual(got, tt.want) {
  225. t.Errorf("ConvertKeys() = %v, want %v", got, tt.want)
  226. }
  227. })
  228. }
  229. }
  230. func TestReverseKeys(t *testing.T) {
  231. type args struct {
  232. encodingStrategy esv1beta1.ExternalSecretConversionStrategy
  233. decodingStrategy esv1alpha1.PushSecretConversionStrategy
  234. in map[string][]byte
  235. }
  236. tests := []struct {
  237. name string
  238. args args
  239. want map[string][]byte
  240. wantErr bool
  241. }{
  242. {
  243. name: "encoding and decoding strategy are selecting Unicode conversion and reverse unicode, so the in and want should match, this test covers Unicode characters beyond the Basic Multilingual Plane (BMP)",
  244. args: args{
  245. encodingStrategy: esv1beta1.ExternalSecretConversionUnicode,
  246. decodingStrategy: esv1alpha1.PushSecretConversionReverseUnicode,
  247. in: map[string][]byte{
  248. keyWithEmojis: []byte(`noop`),
  249. },
  250. },
  251. want: map[string][]byte{
  252. keyWithEmojis: []byte(`noop`),
  253. },
  254. },
  255. {
  256. name: "encoding and decoding strategy are selecting Unicode conversion and reverse unicode, so the in and want should match, this test covers Unicode characters in the Basic Multilingual Plane (BMP)",
  257. args: args{
  258. encodingStrategy: esv1beta1.ExternalSecretConversionUnicode,
  259. decodingStrategy: esv1alpha1.PushSecretConversionReverseUnicode,
  260. in: map[string][]byte{
  261. keyWithInvalidChars: []byte(`noop`),
  262. },
  263. },
  264. want: map[string][]byte{
  265. keyWithInvalidChars: []byte(`noop`),
  266. },
  267. },
  268. {
  269. name: "the encoding strategy is selecting Unicode conversion, but the decoding strategy is none, so we want an encoded representation of the content",
  270. args: args{
  271. encodingStrategy: esv1beta1.ExternalSecretConversionUnicode,
  272. decodingStrategy: esv1alpha1.PushSecretConversionNone,
  273. in: map[string][]byte{
  274. keyWithInvalidChars: []byte(`noop`),
  275. },
  276. },
  277. want: map[string][]byte{
  278. keyWithEncodedInvalidChars: []byte(`noop`),
  279. },
  280. },
  281. }
  282. for _, tt := range tests {
  283. t.Run(tt.name, func(t *testing.T) {
  284. got, err := ConvertKeys(tt.args.encodingStrategy, tt.args.in)
  285. if (err != nil) != tt.wantErr {
  286. t.Errorf("ConvertKeys() error = %v, wantErr %v", err, tt.wantErr)
  287. return
  288. }
  289. got, err = ReverseKeys(tt.args.decodingStrategy, got)
  290. if (err != nil) != tt.wantErr {
  291. t.Errorf("ReverseKeys() error = %v, wantErr %v", err, tt.wantErr)
  292. return
  293. }
  294. if !reflect.DeepEqual(got, tt.want) {
  295. t.Errorf("ReverseKeys() = %v, want %v", got, tt.want)
  296. }
  297. })
  298. }
  299. }
  300. func TestDecode(t *testing.T) {
  301. type args struct {
  302. strategy esv1beta1.ExternalSecretDecodingStrategy
  303. in map[string][]byte
  304. }
  305. tests := []struct {
  306. name string
  307. args args
  308. want map[string][]byte
  309. wantErr bool
  310. }{
  311. {
  312. name: "base64 decoded",
  313. args: args{
  314. strategy: esv1beta1.ExternalSecretDecodeBase64,
  315. in: map[string][]byte{
  316. "foo": []byte("YmFy"),
  317. },
  318. },
  319. want: map[string][]byte{
  320. "foo": []byte("bar"),
  321. },
  322. },
  323. {
  324. name: "invalid base64",
  325. args: args{
  326. strategy: esv1beta1.ExternalSecretDecodeBase64,
  327. in: map[string][]byte{
  328. "foo": []byte("foo"),
  329. },
  330. },
  331. wantErr: true,
  332. },
  333. {
  334. name: "base64url decoded",
  335. args: args{
  336. strategy: esv1beta1.ExternalSecretDecodeBase64URL,
  337. in: map[string][]byte{
  338. "foo": []byte(base64URLEncodedValue),
  339. },
  340. },
  341. want: map[string][]byte{
  342. "foo": []byte(base64DecodedValue),
  343. },
  344. },
  345. {
  346. name: "invalid base64url",
  347. args: args{
  348. strategy: esv1beta1.ExternalSecretDecodeBase64URL,
  349. in: map[string][]byte{
  350. "foo": []byte("foo"),
  351. },
  352. },
  353. wantErr: true,
  354. },
  355. {
  356. name: "none",
  357. args: args{
  358. strategy: esv1beta1.ExternalSecretDecodeNone,
  359. in: map[string][]byte{
  360. "foo": []byte(base64URLEncodedValue),
  361. },
  362. },
  363. want: map[string][]byte{
  364. "foo": []byte(base64URLEncodedValue),
  365. },
  366. },
  367. {
  368. name: "auto",
  369. args: args{
  370. strategy: esv1beta1.ExternalSecretDecodeAuto,
  371. in: map[string][]byte{
  372. "b64": []byte(base64EncodedValue),
  373. "invalidb64": []byte("foo"),
  374. "b64url": []byte(base64URLEncodedValue),
  375. },
  376. },
  377. want: map[string][]byte{
  378. "b64": []byte(base64DecodedValue),
  379. "invalidb64": []byte("foo"),
  380. "b64url": []byte(base64DecodedValue),
  381. },
  382. },
  383. }
  384. for _, tt := range tests {
  385. t.Run(tt.name, func(t *testing.T) {
  386. got, err := DecodeMap(tt.args.strategy, tt.args.in)
  387. if (err != nil) != tt.wantErr {
  388. t.Errorf("DecodeMap() error = %v, wantErr %v", err, tt.wantErr)
  389. return
  390. }
  391. if !reflect.DeepEqual(got, tt.want) {
  392. t.Errorf("DecodeMap() = %v, want %v", got, tt.want)
  393. }
  394. })
  395. }
  396. }
  397. func TestValidate(t *testing.T) {
  398. err := NetworkValidate("http://google.com", 10*time.Second)
  399. if err != nil {
  400. t.Errorf("Connection problem: %v", err)
  401. }
  402. }
  403. func TestRewrite(t *testing.T) {
  404. type args struct {
  405. operations []esv1beta1.ExternalSecretRewrite
  406. in map[string][]byte
  407. }
  408. tests := []struct {
  409. name string
  410. args args
  411. want map[string][]byte
  412. wantErr bool
  413. }{
  414. {
  415. name: "replace of a single key",
  416. args: args{
  417. operations: []esv1beta1.ExternalSecretRewrite{
  418. {
  419. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  420. Source: "-",
  421. Target: "_",
  422. },
  423. },
  424. },
  425. in: map[string][]byte{
  426. "foo-bar": []byte("bar"),
  427. },
  428. },
  429. want: map[string][]byte{
  430. "foo_bar": []byte("bar"),
  431. },
  432. },
  433. {
  434. name: "no operation",
  435. args: args{
  436. operations: []esv1beta1.ExternalSecretRewrite{
  437. {
  438. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  439. Source: "hello",
  440. Target: "world",
  441. },
  442. },
  443. },
  444. in: map[string][]byte{
  445. "foo": []byte("bar"),
  446. },
  447. },
  448. want: map[string][]byte{
  449. "foo": []byte("bar"),
  450. },
  451. },
  452. {
  453. name: "removing prefix from keys",
  454. args: args{
  455. operations: []esv1beta1.ExternalSecretRewrite{
  456. {
  457. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  458. Source: "^my/initial/path/",
  459. Target: "",
  460. },
  461. },
  462. },
  463. in: map[string][]byte{
  464. "my/initial/path/foo": []byte("bar"),
  465. },
  466. },
  467. want: map[string][]byte{
  468. "foo": []byte("bar"),
  469. },
  470. },
  471. {
  472. name: "using un-named capture groups",
  473. args: args{
  474. operations: []esv1beta1.ExternalSecretRewrite{
  475. {
  476. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  477. Source: "f(.*)o",
  478. Target: "a_new_path_$1",
  479. },
  480. },
  481. },
  482. in: map[string][]byte{
  483. "foo": []byte("bar"),
  484. "foodaloo": []byte("barr"),
  485. },
  486. },
  487. want: map[string][]byte{
  488. "a_new_path_o": []byte("bar"),
  489. "a_new_path_oodalo": []byte("barr"),
  490. },
  491. },
  492. {
  493. name: "using named and numbered capture groups",
  494. args: args{
  495. operations: []esv1beta1.ExternalSecretRewrite{
  496. {
  497. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  498. Source: "f(?P<content>.*)o",
  499. Target: "a_new_path_${content}_${1}",
  500. },
  501. },
  502. },
  503. in: map[string][]byte{
  504. "foo": []byte("bar"),
  505. "floo": []byte("barr"),
  506. },
  507. },
  508. want: map[string][]byte{
  509. "a_new_path_o_o": []byte("bar"),
  510. "a_new_path_lo_lo": []byte("barr"),
  511. },
  512. },
  513. {
  514. name: "using sequenced rewrite operations",
  515. args: args{
  516. operations: []esv1beta1.ExternalSecretRewrite{
  517. {
  518. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  519. Source: "my/(.*?)/bar/(.*)",
  520. Target: "$1-$2",
  521. },
  522. },
  523. {
  524. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  525. Source: "-",
  526. Target: "_",
  527. },
  528. },
  529. {
  530. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  531. Source: "ass",
  532. Target: "***",
  533. },
  534. },
  535. },
  536. in: map[string][]byte{
  537. "my/app/bar/key": []byte("bar"),
  538. "my/app/bar/password": []byte("barr"),
  539. },
  540. },
  541. want: map[string][]byte{
  542. "app_key": []byte("bar"),
  543. "app_p***word": []byte("barr"),
  544. },
  545. },
  546. {
  547. name: "using transform rewrite operation to create env var format keys",
  548. args: args{
  549. operations: []esv1beta1.ExternalSecretRewrite{
  550. {
  551. Regexp: &esv1beta1.ExternalSecretRewriteRegexp{
  552. Source: "my/(.*?)/bar/(.*)",
  553. Target: "$1-$2",
  554. },
  555. },
  556. {
  557. Transform: &esv1beta1.ExternalSecretRewriteTransform{
  558. Template: `{{ .value | upper | replace "-" "_" }}`,
  559. },
  560. },
  561. },
  562. in: map[string][]byte{
  563. "my/app/bar/api-key": []byte("bar"),
  564. "my/app/bar/api-password": []byte("barr"),
  565. },
  566. },
  567. want: map[string][]byte{
  568. "APP_API_KEY": []byte("bar"),
  569. "APP_API_PASSWORD": []byte("barr"),
  570. },
  571. },
  572. {
  573. name: "using transform rewrite operation to lower case",
  574. args: args{
  575. operations: []esv1beta1.ExternalSecretRewrite{
  576. {
  577. Transform: &esv1beta1.ExternalSecretRewriteTransform{
  578. Template: `{{ .value | lower }}`,
  579. },
  580. },
  581. },
  582. in: map[string][]byte{
  583. "API_FOO": []byte("bar"),
  584. "KEY_FOO": []byte("barr"),
  585. },
  586. },
  587. want: map[string][]byte{
  588. "api_foo": []byte("bar"),
  589. "key_foo": []byte("barr"),
  590. },
  591. },
  592. }
  593. for _, tt := range tests {
  594. t.Run(tt.name, func(t *testing.T) {
  595. got, err := RewriteMap(tt.args.operations, tt.args.in)
  596. if (err != nil) != tt.wantErr {
  597. t.Errorf("RewriteMap() error = %v, wantErr %v", err, tt.wantErr)
  598. return
  599. }
  600. if !reflect.DeepEqual(got, tt.want) {
  601. t.Errorf("RewriteMap() = %v, want %v", got, tt.want)
  602. }
  603. })
  604. }
  605. }
  606. func TestReverse(t *testing.T) {
  607. type args struct {
  608. strategy esv1alpha1.PushSecretConversionStrategy
  609. in string
  610. }
  611. tests := []struct {
  612. name string
  613. args args
  614. want string
  615. }{
  616. {
  617. name: "do not change the key when using the None strategy",
  618. args: args{
  619. strategy: esv1alpha1.PushSecretConversionNone,
  620. in: keyWithEncodedInvalidChars,
  621. },
  622. want: keyWithEncodedInvalidChars,
  623. },
  624. {
  625. name: "reverse an unicode encoded key",
  626. args: args{
  627. strategy: esv1alpha1.PushSecretConversionReverseUnicode,
  628. in: keyWithEncodedInvalidChars,
  629. },
  630. want: keyWithInvalidChars,
  631. },
  632. {
  633. name: "do not attempt to decode an invalid unicode representation",
  634. args: args{
  635. strategy: esv1alpha1.PushSecretConversionReverseUnicode,
  636. in: "_U0xxx_x_U005b_",
  637. },
  638. want: "_U0xxx_x[",
  639. },
  640. }
  641. for _, tt := range tests {
  642. t.Run(tt.name, func(t *testing.T) {
  643. if got := reverse(tt.args.strategy, tt.args.in); got != tt.want {
  644. t.Errorf("reverse() = %v, want %v", got, tt.want)
  645. }
  646. })
  647. }
  648. }
  649. func TestFetchValueFromMetadata(t *testing.T) {
  650. type args struct {
  651. key string
  652. data *apiextensionsv1.JSON
  653. def any
  654. }
  655. type testCase struct {
  656. name string
  657. args args
  658. wantT any
  659. wantErr bool
  660. }
  661. tests := []testCase{
  662. {
  663. name: "plain dig for an existing key",
  664. args: args{
  665. key: "key",
  666. data: &apiextensionsv1.JSON{
  667. Raw: []byte(
  668. `{"key": "value"}`,
  669. ),
  670. },
  671. def: "def",
  672. },
  673. wantT: "value",
  674. wantErr: false,
  675. },
  676. {
  677. name: "return default if key not found",
  678. args: args{
  679. key: "key2",
  680. data: &apiextensionsv1.JSON{
  681. Raw: []byte(
  682. `{"key": "value"}`,
  683. ),
  684. },
  685. def: "def",
  686. },
  687. wantT: "def",
  688. wantErr: false,
  689. },
  690. {
  691. name: "use a different type",
  692. args: args{
  693. key: "key",
  694. data: &apiextensionsv1.JSON{
  695. Raw: []byte(
  696. `{"key": 123}`,
  697. ),
  698. },
  699. def: 1234,
  700. },
  701. wantT: float64(123), // unmarshal is always float64
  702. wantErr: false,
  703. },
  704. {
  705. name: "digging deeper",
  706. args: args{
  707. key: "key2",
  708. data: &apiextensionsv1.JSON{
  709. Raw: []byte(
  710. `{"key": {"key2": "value"}}`,
  711. ),
  712. },
  713. def: "",
  714. },
  715. wantT: "value",
  716. wantErr: false,
  717. },
  718. }
  719. for _, tt := range tests {
  720. t.Run(tt.name, func(t *testing.T) {
  721. gotT, err := FetchValueFromMetadata(tt.args.key, tt.args.data, tt.args.def)
  722. if (err != nil) != tt.wantErr {
  723. t.Errorf("FetchValueFromMetadata() error = %v, wantErr %v", err, tt.wantErr)
  724. return
  725. }
  726. if !reflect.DeepEqual(gotT, tt.wantT) {
  727. t.Errorf("FetchValueFromMetadata() gotT = %v, want %v", gotT, tt.wantT)
  728. }
  729. })
  730. }
  731. }
  732. func TestGetByteValue(t *testing.T) {
  733. type args struct {
  734. data any
  735. }
  736. type testCase struct {
  737. name string
  738. args args
  739. want []byte
  740. wantErr bool
  741. }
  742. tests := []testCase{
  743. {
  744. name: "string",
  745. args: args{
  746. data: "value",
  747. },
  748. want: []byte("value"),
  749. wantErr: false,
  750. },
  751. {
  752. name: "map of any",
  753. args: args{
  754. data: map[string]any{
  755. "key": "value",
  756. },
  757. },
  758. want: []byte(`{"key":"value"}`),
  759. wantErr: false,
  760. },
  761. {
  762. name: "slice of string",
  763. args: args{
  764. data: []string{"value1", "value2"},
  765. },
  766. want: []byte("value1\nvalue2"),
  767. wantErr: false,
  768. },
  769. {
  770. name: "json.RawMessage",
  771. args: args{
  772. data: json.RawMessage(`{"key":"value"}`),
  773. },
  774. want: []byte(`{"key":"value"}`),
  775. wantErr: false,
  776. },
  777. {
  778. name: "float64",
  779. args: args{
  780. data: 123.45,
  781. },
  782. want: []byte("123.45"),
  783. wantErr: false,
  784. },
  785. {
  786. name: "json.Number",
  787. args: args{
  788. data: json.Number("123.45"),
  789. },
  790. want: []byte("123.45"),
  791. wantErr: false,
  792. },
  793. {
  794. name: "slice of any",
  795. args: args{
  796. data: []any{"value1", "value2"},
  797. },
  798. want: []byte(`["value1","value2"]`),
  799. wantErr: false,
  800. },
  801. {
  802. name: "boolean",
  803. args: args{
  804. data: true,
  805. },
  806. want: []byte("true"),
  807. wantErr: false,
  808. },
  809. {
  810. name: "nil",
  811. args: args{
  812. data: nil,
  813. },
  814. want: []byte(nil),
  815. wantErr: false,
  816. },
  817. }
  818. for _, tt := range tests {
  819. t.Run(tt.name, func(t *testing.T) {
  820. got, err := GetByteValue(tt.args.data)
  821. if (err != nil) != tt.wantErr {
  822. t.Errorf("GetByteValue() error = %v, wantErr %v", err, tt.wantErr)
  823. return
  824. }
  825. if !reflect.DeepEqual(got, tt.want) {
  826. t.Errorf("GetByteValue() = %v, want %v", got, tt.want)
  827. }
  828. })
  829. }
  830. }
  831. func TestCompareStringAndByteSlices(t *testing.T) {
  832. type args struct {
  833. stringValue *string
  834. byteValueSlice []byte
  835. }
  836. type testCase struct {
  837. name string
  838. args args
  839. want bool
  840. wantErr bool
  841. }
  842. tests := []testCase{
  843. {
  844. name: "same contents",
  845. args: args{
  846. stringValue: aws.String("value"),
  847. byteValueSlice: []byte("value"),
  848. },
  849. want: true,
  850. wantErr: true,
  851. }, {
  852. name: "different contents",
  853. args: args{
  854. stringValue: aws.String("value89"),
  855. byteValueSlice: []byte("value"),
  856. },
  857. want: true,
  858. wantErr: false,
  859. }, {
  860. name: "same contents with random",
  861. args: args{
  862. stringValue: aws.String("value89!3#@212"),
  863. byteValueSlice: []byte("value89!3#@212"),
  864. },
  865. want: true,
  866. wantErr: true,
  867. }, {
  868. name: "check Nil",
  869. args: args{
  870. stringValue: nil,
  871. byteValueSlice: []byte("value89!3#@212"),
  872. },
  873. want: false,
  874. wantErr: false,
  875. },
  876. }
  877. for _, tt := range tests {
  878. t.Run(tt.name, func(t *testing.T) {
  879. got := CompareStringAndByteSlices(tt.args.stringValue, tt.args.byteValueSlice)
  880. if got != tt.wantErr {
  881. t.Errorf("CompareStringAndByteSlices() got = %v, want = %v", got, tt.wantErr)
  882. return
  883. }
  884. })
  885. }
  886. }