client_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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 kubernetes
  13. import (
  14. "context"
  15. "errors"
  16. "reflect"
  17. "testing"
  18. "github.com/google/go-cmp/cmp"
  19. "github.com/stretchr/testify/assert"
  20. v1 "k8s.io/api/core/v1"
  21. apierrors "k8s.io/apimachinery/pkg/api/errors"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  25. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  26. )
  27. const (
  28. errSomethingWentWrong = "Something went wrong"
  29. )
  30. type fakeClient struct {
  31. t *testing.T
  32. secretMap map[string]*v1.Secret
  33. expectedListOptions metav1.ListOptions
  34. err error
  35. }
  36. func (fk *fakeClient) Get(_ context.Context, name string, _ metav1.GetOptions) (*v1.Secret, error) {
  37. if fk.err != nil {
  38. return nil, fk.err
  39. }
  40. secret, ok := fk.secretMap[name]
  41. if !ok {
  42. return nil, apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "Secret"}, "secret")
  43. }
  44. // return inmutable to simulate external system and avoid accidental side effects
  45. sCopy := secret.DeepCopy()
  46. // update operation requires to relate names
  47. sCopy.Name = name
  48. return sCopy, nil
  49. }
  50. func (fk *fakeClient) List(_ context.Context, opts metav1.ListOptions) (*v1.SecretList, error) {
  51. assert.Equal(fk.t, fk.expectedListOptions, opts)
  52. list := &v1.SecretList{}
  53. for _, v := range fk.secretMap {
  54. list.Items = append(list.Items, *v)
  55. }
  56. return list, nil
  57. }
  58. func (fk *fakeClient) Delete(_ context.Context, name string, _ metav1.DeleteOptions) error {
  59. if fk.err != nil {
  60. return fk.err
  61. }
  62. _, ok := fk.secretMap[name]
  63. if !ok {
  64. return apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "Secret"}, "secret")
  65. }
  66. delete(fk.secretMap, name)
  67. return nil
  68. }
  69. func (fk *fakeClient) Create(_ context.Context, secret *v1.Secret, _ metav1.CreateOptions) (*v1.Secret, error) {
  70. s := &v1.Secret{
  71. Data: secret.Data,
  72. }
  73. fk.secretMap[secret.Name] = s
  74. return s, nil
  75. }
  76. func (fk *fakeClient) Update(_ context.Context, secret *v1.Secret, _ metav1.UpdateOptions) (*v1.Secret, error) {
  77. s, ok := fk.secretMap[secret.Name]
  78. if !ok {
  79. return nil, errors.New("error while updating secret")
  80. }
  81. s.Data = secret.Data
  82. return s, nil
  83. }
  84. func TestGetSecret(t *testing.T) {
  85. type fields struct {
  86. Client KClient
  87. ReviewClient RClient
  88. Namespace string
  89. }
  90. tests := []struct {
  91. name string
  92. fields fields
  93. ref esv1beta1.ExternalSecretDataRemoteRef
  94. want []byte
  95. wantErr bool
  96. }{
  97. {
  98. name: "secretNotFound",
  99. fields: fields{
  100. Client: &fakeClient{
  101. t: t,
  102. secretMap: map[string]*v1.Secret{
  103. "mysec": {
  104. Data: map[string][]byte{
  105. "token": []byte(`foobar`),
  106. },
  107. },
  108. },
  109. err: apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "Secret"}, "secret"),
  110. },
  111. Namespace: "default",
  112. },
  113. ref: esv1beta1.ExternalSecretDataRemoteRef{
  114. Key: "mysec",
  115. Property: "token",
  116. },
  117. wantErr: true,
  118. },
  119. {
  120. name: "err GetSecretMap",
  121. fields: fields{
  122. Client: &fakeClient{
  123. t: t,
  124. secretMap: map[string]*v1.Secret{},
  125. },
  126. Namespace: "default",
  127. },
  128. ref: esv1beta1.ExternalSecretDataRemoteRef{
  129. Key: "mysec",
  130. Property: "token",
  131. },
  132. wantErr: true,
  133. },
  134. {
  135. name: "wrong property",
  136. fields: fields{
  137. Client: &fakeClient{
  138. t: t,
  139. secretMap: map[string]*v1.Secret{
  140. "mysec": {
  141. Data: map[string][]byte{
  142. "token": []byte(`foobar`),
  143. },
  144. },
  145. },
  146. },
  147. Namespace: "default",
  148. },
  149. ref: esv1beta1.ExternalSecretDataRemoteRef{
  150. Key: "mysec",
  151. Property: "not-the-token",
  152. },
  153. wantErr: true,
  154. },
  155. {
  156. name: "successful case",
  157. fields: fields{
  158. Client: &fakeClient{
  159. t: t,
  160. secretMap: map[string]*v1.Secret{
  161. "mysec": {
  162. Data: map[string][]byte{
  163. "token": []byte(`foobar`),
  164. },
  165. },
  166. },
  167. },
  168. Namespace: "default",
  169. },
  170. ref: esv1beta1.ExternalSecretDataRemoteRef{
  171. Key: "mysec",
  172. Property: "token",
  173. },
  174. want: []byte(`foobar`),
  175. },
  176. {
  177. name: "successful case without property",
  178. fields: fields{
  179. Client: &fakeClient{
  180. t: t,
  181. secretMap: map[string]*v1.Secret{
  182. "mysec": {
  183. Data: map[string][]byte{
  184. "token": []byte(`foobar`),
  185. },
  186. },
  187. },
  188. },
  189. Namespace: "default",
  190. },
  191. ref: esv1beta1.ExternalSecretDataRemoteRef{
  192. Key: "mysec",
  193. },
  194. want: []byte(`{"token":"foobar"}`),
  195. },
  196. {
  197. name: "successful case metadata without property",
  198. fields: fields{
  199. Client: &fakeClient{
  200. t: t,
  201. secretMap: map[string]*v1.Secret{
  202. "mysec": {
  203. ObjectMeta: metav1.ObjectMeta{
  204. Annotations: map[string]string{"date": "today"},
  205. Labels: map[string]string{"dev": "seb"},
  206. },
  207. },
  208. },
  209. },
  210. Namespace: "default",
  211. },
  212. ref: esv1beta1.ExternalSecretDataRemoteRef{
  213. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  214. Key: "mysec",
  215. },
  216. want: []byte(`{"annotations":{"date":"today"},"labels":{"dev":"seb"}}`),
  217. },
  218. {
  219. name: "successful case metadata with single property",
  220. fields: fields{
  221. Client: &fakeClient{
  222. t: t,
  223. secretMap: map[string]*v1.Secret{
  224. "mysec": {
  225. ObjectMeta: metav1.ObjectMeta{
  226. Annotations: map[string]string{"date": "today"},
  227. Labels: map[string]string{"dev": "seb"},
  228. },
  229. },
  230. },
  231. },
  232. Namespace: "default",
  233. },
  234. ref: esv1beta1.ExternalSecretDataRemoteRef{
  235. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  236. Key: "mysec",
  237. Property: "labels",
  238. },
  239. want: []byte(`{"dev":"seb"}`),
  240. },
  241. {
  242. name: "successful case metadata with multiple properties",
  243. fields: fields{
  244. Client: &fakeClient{
  245. t: t,
  246. secretMap: map[string]*v1.Secret{
  247. "mysec": {
  248. ObjectMeta: metav1.ObjectMeta{
  249. Annotations: map[string]string{"date": "today"},
  250. Labels: map[string]string{"dev": "seb"},
  251. },
  252. },
  253. },
  254. },
  255. Namespace: "default",
  256. },
  257. ref: esv1beta1.ExternalSecretDataRemoteRef{
  258. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  259. Key: "mysec",
  260. Property: "labels.dev",
  261. },
  262. want: []byte(`seb`),
  263. },
  264. {
  265. name: "error case metadata with wrong property",
  266. fields: fields{
  267. Client: &fakeClient{
  268. t: t,
  269. secretMap: map[string]*v1.Secret{
  270. "mysec": {
  271. ObjectMeta: metav1.ObjectMeta{
  272. Annotations: map[string]string{"date": "today"},
  273. Labels: map[string]string{"dev": "seb"},
  274. },
  275. },
  276. },
  277. },
  278. Namespace: "default",
  279. },
  280. ref: esv1beta1.ExternalSecretDataRemoteRef{
  281. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  282. Key: "mysec",
  283. Property: "foo",
  284. },
  285. wantErr: true,
  286. },
  287. }
  288. for _, tt := range tests {
  289. t.Run(tt.name, func(t *testing.T) {
  290. p := &Client{
  291. userSecretClient: tt.fields.Client,
  292. userReviewClient: tt.fields.ReviewClient,
  293. namespace: tt.fields.Namespace,
  294. }
  295. got, err := p.GetSecret(context.Background(), tt.ref)
  296. if (err != nil) != tt.wantErr {
  297. t.Errorf("ProviderKubernetes.GetSecret() error = %v, wantErr %v", err, tt.wantErr)
  298. return
  299. }
  300. if !reflect.DeepEqual(got, tt.want) {
  301. t.Errorf("ProviderKubernetes.GetSecret() = %v, want %v", got, tt.want)
  302. }
  303. })
  304. }
  305. }
  306. func TestGetSecretMap(t *testing.T) {
  307. type fields struct {
  308. Client KClient
  309. ReviewClient RClient
  310. Namespace string
  311. }
  312. tests := []struct {
  313. name string
  314. fields fields
  315. ref esv1beta1.ExternalSecretDataRemoteRef
  316. want map[string][]byte
  317. wantErr bool
  318. }{
  319. {
  320. name: "successful case metadata without property",
  321. fields: fields{
  322. Client: &fakeClient{
  323. t: t,
  324. secretMap: map[string]*v1.Secret{
  325. "mysec": {
  326. ObjectMeta: metav1.ObjectMeta{
  327. Annotations: map[string]string{"date": "today"},
  328. Labels: map[string]string{"dev": "seb"},
  329. },
  330. },
  331. },
  332. },
  333. Namespace: "default",
  334. },
  335. ref: esv1beta1.ExternalSecretDataRemoteRef{
  336. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  337. Key: "mysec",
  338. },
  339. want: map[string][]byte{"annotations": []byte("{\"date\":\"today\"}"), "labels": []byte("{\"dev\":\"seb\"}")},
  340. },
  341. {
  342. name: "successful case metadata with single property",
  343. fields: fields{
  344. Client: &fakeClient{
  345. t: t,
  346. secretMap: map[string]*v1.Secret{
  347. "mysec": {
  348. ObjectMeta: metav1.ObjectMeta{
  349. Annotations: map[string]string{"date": "today"},
  350. Labels: map[string]string{"dev": "seb"},
  351. },
  352. },
  353. },
  354. },
  355. Namespace: "default",
  356. },
  357. ref: esv1beta1.ExternalSecretDataRemoteRef{
  358. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  359. Key: "mysec",
  360. Property: "labels",
  361. },
  362. want: map[string][]byte{"dev": []byte("\"seb\"")},
  363. },
  364. {
  365. name: "error case metadata with wrong property",
  366. fields: fields{
  367. Client: &fakeClient{
  368. t: t,
  369. secretMap: map[string]*v1.Secret{
  370. "mysec": {
  371. ObjectMeta: metav1.ObjectMeta{
  372. Annotations: map[string]string{"date": "today"},
  373. Labels: map[string]string{"dev": "seb"},
  374. },
  375. },
  376. },
  377. },
  378. Namespace: "default",
  379. },
  380. ref: esv1beta1.ExternalSecretDataRemoteRef{
  381. MetadataPolicy: esv1beta1.ExternalSecretMetadataPolicyFetch,
  382. Key: "mysec",
  383. Property: "foo",
  384. },
  385. wantErr: true,
  386. },
  387. }
  388. for _, tt := range tests {
  389. t.Run(tt.name, func(t *testing.T) {
  390. p := &Client{
  391. userSecretClient: tt.fields.Client,
  392. userReviewClient: tt.fields.ReviewClient,
  393. namespace: tt.fields.Namespace,
  394. }
  395. got, err := p.GetSecretMap(context.Background(), tt.ref)
  396. if (err != nil) != tt.wantErr {
  397. t.Errorf("ProviderKubernetes.GetSecretMap() error = %v, wantErr %v", err, tt.wantErr)
  398. return
  399. }
  400. if !reflect.DeepEqual(got, tt.want) {
  401. t.Errorf("ProviderKubernetes.GetSecretMap() = %v, want %v", got, tt.want)
  402. }
  403. })
  404. }
  405. }
  406. func TestGetAllSecrets(t *testing.T) {
  407. type fields struct {
  408. Client KClient
  409. ReviewClient RClient
  410. Namespace string
  411. }
  412. type args struct {
  413. ctx context.Context
  414. ref esv1beta1.ExternalSecretFind
  415. }
  416. tests := []struct {
  417. name string
  418. fields fields
  419. args args
  420. want map[string][]byte
  421. wantErr bool
  422. }{
  423. {
  424. name: "use regex",
  425. fields: fields{
  426. Client: &fakeClient{
  427. t: t,
  428. secretMap: map[string]*v1.Secret{
  429. "mysec": {
  430. ObjectMeta: metav1.ObjectMeta{
  431. Name: "mysec",
  432. },
  433. Data: map[string][]byte{
  434. "token": []byte(`foo`),
  435. },
  436. },
  437. "other": {
  438. ObjectMeta: metav1.ObjectMeta{
  439. Name: "other",
  440. },
  441. Data: map[string][]byte{
  442. "token": []byte(`bar`),
  443. },
  444. },
  445. },
  446. },
  447. },
  448. args: args{
  449. ref: esv1beta1.ExternalSecretFind{
  450. Name: &esv1beta1.FindName{
  451. RegExp: "other",
  452. },
  453. },
  454. },
  455. want: map[string][]byte{
  456. "other": []byte(`{"token":"bar"}`),
  457. },
  458. },
  459. {
  460. name: "use tags/labels",
  461. fields: fields{
  462. Client: &fakeClient{
  463. t: t,
  464. expectedListOptions: metav1.ListOptions{
  465. LabelSelector: "app=foobar",
  466. },
  467. secretMap: map[string]*v1.Secret{
  468. "mysec": {
  469. ObjectMeta: metav1.ObjectMeta{
  470. Name: "mysec",
  471. },
  472. Data: map[string][]byte{
  473. "token": []byte(`foo`),
  474. },
  475. },
  476. "other": {
  477. ObjectMeta: metav1.ObjectMeta{
  478. Name: "other",
  479. },
  480. Data: map[string][]byte{
  481. "token": []byte(`bar`),
  482. },
  483. },
  484. },
  485. },
  486. },
  487. args: args{
  488. ref: esv1beta1.ExternalSecretFind{
  489. Tags: map[string]string{
  490. "app": "foobar",
  491. },
  492. },
  493. },
  494. want: map[string][]byte{
  495. "mysec": []byte(`{"token":"foo"}`),
  496. "other": []byte(`{"token":"bar"}`),
  497. },
  498. },
  499. }
  500. for _, tt := range tests {
  501. t.Run(tt.name, func(t *testing.T) {
  502. p := &Client{
  503. userSecretClient: tt.fields.Client,
  504. userReviewClient: tt.fields.ReviewClient,
  505. namespace: tt.fields.Namespace,
  506. }
  507. got, err := p.GetAllSecrets(tt.args.ctx, tt.args.ref)
  508. if (err != nil) != tt.wantErr {
  509. t.Errorf("ProviderKubernetes.GetAllSecrets() error = %v, wantErr %v", err, tt.wantErr)
  510. return
  511. }
  512. if !reflect.DeepEqual(got, tt.want) {
  513. t.Errorf("ProviderKubernetes.GetAllSecrets() = %v, want %v", got, tt.want)
  514. }
  515. })
  516. }
  517. }
  518. func TestDeleteSecret(t *testing.T) {
  519. type fields struct {
  520. Client KClient
  521. }
  522. tests := []struct {
  523. name string
  524. fields fields
  525. ref esv1beta1.PushRemoteRef
  526. wantSecretMap map[string]*v1.Secret
  527. wantErr bool
  528. }{
  529. {
  530. name: "refuse to delete without property",
  531. fields: fields{
  532. Client: &fakeClient{
  533. t: t,
  534. secretMap: map[string]*v1.Secret{
  535. "mysec": {
  536. Data: map[string][]byte{
  537. "token": []byte(`foobar`),
  538. },
  539. },
  540. },
  541. },
  542. },
  543. ref: v1alpha1.PushSecretRemoteRef{
  544. RemoteKey: "mysec",
  545. },
  546. wantErr: true,
  547. wantSecretMap: map[string]*v1.Secret{
  548. "mysec": {
  549. Data: map[string][]byte{
  550. "token": []byte(`foobar`),
  551. },
  552. },
  553. },
  554. },
  555. {
  556. name: "gracefully ignore not found secret",
  557. fields: fields{
  558. Client: &fakeClient{
  559. t: t,
  560. secretMap: map[string]*v1.Secret{},
  561. },
  562. },
  563. ref: v1alpha1.PushSecretRemoteRef{
  564. RemoteKey: "mysec",
  565. Property: "token",
  566. },
  567. wantErr: false,
  568. wantSecretMap: map[string]*v1.Secret{},
  569. },
  570. {
  571. name: "gracefully ignore not found property",
  572. fields: fields{
  573. Client: &fakeClient{
  574. t: t,
  575. secretMap: map[string]*v1.Secret{
  576. "mysec": {
  577. Data: map[string][]byte{
  578. "token": []byte(`foobar`),
  579. },
  580. },
  581. },
  582. },
  583. },
  584. ref: v1alpha1.PushSecretRemoteRef{
  585. RemoteKey: "mysec",
  586. Property: "secret",
  587. },
  588. wantErr: false,
  589. wantSecretMap: map[string]*v1.Secret{
  590. "mysec": {
  591. Data: map[string][]byte{
  592. "token": []byte(`foobar`),
  593. },
  594. },
  595. },
  596. },
  597. {
  598. name: "unexpected lookup error",
  599. fields: fields{
  600. Client: &fakeClient{
  601. t: t,
  602. secretMap: map[string]*v1.Secret{
  603. "mysec": {
  604. Data: map[string][]byte{
  605. "token": []byte(`foobar`),
  606. },
  607. },
  608. },
  609. err: errors.New(errSomethingWentWrong),
  610. },
  611. },
  612. ref: v1alpha1.PushSecretRemoteRef{
  613. RemoteKey: "mysec",
  614. },
  615. wantErr: true,
  616. wantSecretMap: map[string]*v1.Secret{
  617. "mysec": {
  618. Data: map[string][]byte{
  619. "token": []byte(`foobar`),
  620. },
  621. },
  622. },
  623. },
  624. {
  625. name: "delete whole secret if only property should be removed",
  626. fields: fields{
  627. Client: &fakeClient{
  628. t: t,
  629. secretMap: map[string]*v1.Secret{
  630. "mysec": {
  631. Data: map[string][]byte{
  632. "token": []byte(`foobar`),
  633. },
  634. },
  635. },
  636. },
  637. },
  638. ref: v1alpha1.PushSecretRemoteRef{
  639. RemoteKey: "mysec",
  640. Property: "token",
  641. },
  642. wantErr: false,
  643. wantSecretMap: map[string]*v1.Secret{},
  644. },
  645. {
  646. name: "multiple properties, just remove that one",
  647. fields: fields{
  648. Client: &fakeClient{
  649. t: t,
  650. secretMap: map[string]*v1.Secret{
  651. "mysec": {
  652. Data: map[string][]byte{
  653. "token": []byte(`foo`),
  654. "secret": []byte(`bar`),
  655. },
  656. },
  657. },
  658. },
  659. },
  660. ref: v1alpha1.PushSecretRemoteRef{
  661. RemoteKey: "mysec",
  662. Property: "token",
  663. },
  664. wantErr: false,
  665. wantSecretMap: map[string]*v1.Secret{
  666. "mysec": {
  667. Data: map[string][]byte{
  668. "secret": []byte(`bar`),
  669. },
  670. },
  671. },
  672. },
  673. }
  674. for _, tt := range tests {
  675. t.Run(tt.name, func(t *testing.T) {
  676. p := &Client{
  677. userSecretClient: tt.fields.Client,
  678. }
  679. err := p.DeleteSecret(context.Background(), tt.ref)
  680. if (err != nil) != tt.wantErr {
  681. t.Errorf("ProviderKubernetes.DeleteSecret() error = %v, wantErr %v", err, tt.wantErr)
  682. return
  683. }
  684. fClient := tt.fields.Client.(*fakeClient)
  685. if diff := cmp.Diff(tt.wantSecretMap, fClient.secretMap); diff != "" {
  686. t.Errorf("Unexpected resulting secrets map: -want, +got :\n%s\n", diff)
  687. }
  688. })
  689. }
  690. }
  691. func TestPushSecret(t *testing.T) {
  692. type fields struct {
  693. Client KClient
  694. PushValue string
  695. }
  696. tests := []struct {
  697. name string
  698. fields fields
  699. ref esv1beta1.PushRemoteRef
  700. wantSecretMap map[string]*v1.Secret
  701. wantErr bool
  702. }{
  703. {
  704. name: "refuse to work without property",
  705. fields: fields{
  706. Client: &fakeClient{
  707. t: t,
  708. secretMap: map[string]*v1.Secret{
  709. "mysec": {
  710. Data: map[string][]byte{
  711. "token": []byte(`foo`),
  712. },
  713. },
  714. },
  715. },
  716. PushValue: "bar",
  717. },
  718. ref: v1alpha1.PushSecretRemoteRef{
  719. RemoteKey: "mysec",
  720. },
  721. wantErr: true,
  722. wantSecretMap: map[string]*v1.Secret{
  723. "mysec": {
  724. Data: map[string][]byte{
  725. "token": []byte(`foo`),
  726. },
  727. },
  728. },
  729. },
  730. {
  731. name: "add missing property to existing secret",
  732. fields: fields{
  733. Client: &fakeClient{
  734. t: t,
  735. secretMap: map[string]*v1.Secret{
  736. "mysec": {
  737. Data: map[string][]byte{
  738. "token": []byte(`foo`),
  739. },
  740. },
  741. },
  742. },
  743. PushValue: "bar",
  744. },
  745. ref: v1alpha1.PushSecretRemoteRef{
  746. RemoteKey: "mysec",
  747. Property: "secret",
  748. },
  749. wantErr: false,
  750. wantSecretMap: map[string]*v1.Secret{
  751. "mysec": {
  752. Data: map[string][]byte{
  753. "token": []byte(`foo`),
  754. "secret": []byte(`bar`),
  755. },
  756. },
  757. },
  758. },
  759. {
  760. name: "replace existing property in existing secret",
  761. fields: fields{
  762. Client: &fakeClient{
  763. t: t,
  764. secretMap: map[string]*v1.Secret{
  765. "mysec": {
  766. Data: map[string][]byte{
  767. "token": []byte(`foo`),
  768. },
  769. },
  770. },
  771. },
  772. PushValue: "bar",
  773. },
  774. ref: v1alpha1.PushSecretRemoteRef{
  775. RemoteKey: "mysec",
  776. Property: "token",
  777. },
  778. wantErr: false,
  779. wantSecretMap: map[string]*v1.Secret{
  780. "mysec": {
  781. Data: map[string][]byte{
  782. "token": []byte(`bar`),
  783. },
  784. },
  785. },
  786. },
  787. {
  788. name: "create new secret",
  789. fields: fields{
  790. Client: &fakeClient{
  791. t: t,
  792. secretMap: map[string]*v1.Secret{
  793. "yoursec": {
  794. Data: map[string][]byte{
  795. "token": []byte(`foo`),
  796. },
  797. },
  798. },
  799. },
  800. PushValue: "bar",
  801. },
  802. ref: v1alpha1.PushSecretRemoteRef{
  803. RemoteKey: "mysec",
  804. Property: "secret",
  805. },
  806. wantErr: false,
  807. wantSecretMap: map[string]*v1.Secret{
  808. "yoursec": {
  809. Data: map[string][]byte{
  810. "token": []byte(`foo`),
  811. },
  812. },
  813. "mysec": {
  814. Data: map[string][]byte{
  815. "secret": []byte(`bar`),
  816. },
  817. },
  818. },
  819. },
  820. }
  821. for _, tt := range tests {
  822. t.Run(tt.name, func(t *testing.T) {
  823. p := &Client{
  824. userSecretClient: tt.fields.Client,
  825. store: &esv1beta1.KubernetesProvider{},
  826. }
  827. err := p.PushSecret(context.Background(), []byte(tt.fields.PushValue), tt.ref)
  828. if (err != nil) != tt.wantErr {
  829. t.Errorf("ProviderKubernetes.DeleteSecret() error = %v, wantErr %v", err, tt.wantErr)
  830. return
  831. }
  832. fClient := tt.fields.Client.(*fakeClient)
  833. if diff := cmp.Diff(tt.wantSecretMap, fClient.secretMap); diff != "" {
  834. t.Errorf("Unexpected resulting secrets map: -want, +got :\n%s\n", diff)
  835. }
  836. })
  837. }
  838. }