client_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/stretchr/testify/assert"
  19. corev1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  22. )
  23. const (
  24. errSomethingWentWrong = "Something went wrong"
  25. )
  26. type fakeClient struct {
  27. t *testing.T
  28. secretMap map[string]corev1.Secret
  29. expectedListOptions metav1.ListOptions
  30. }
  31. func (fk fakeClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*corev1.Secret, error) {
  32. secret, ok := fk.secretMap[name]
  33. if !ok {
  34. return nil, errors.New(errSomethingWentWrong)
  35. }
  36. return &secret, nil
  37. }
  38. func (fk fakeClient) List(ctx context.Context, opts metav1.ListOptions) (*corev1.SecretList, error) {
  39. assert.Equal(fk.t, fk.expectedListOptions, opts)
  40. list := &corev1.SecretList{}
  41. for _, v := range fk.secretMap {
  42. list.Items = append(list.Items, v)
  43. }
  44. return list, nil
  45. }
  46. func TestGetSecret(t *testing.T) {
  47. type fields struct {
  48. Client KClient
  49. ReviewClient RClient
  50. Namespace string
  51. }
  52. tests := []struct {
  53. name string
  54. fields fields
  55. ref esv1beta1.ExternalSecretDataRemoteRef
  56. want []byte
  57. wantErr bool
  58. }{
  59. {
  60. name: "err GetSecretMap",
  61. fields: fields{
  62. Client: fakeClient{
  63. t: t,
  64. secretMap: map[string]corev1.Secret{},
  65. },
  66. Namespace: "default",
  67. },
  68. ref: esv1beta1.ExternalSecretDataRemoteRef{
  69. Key: "mysec",
  70. Property: "token",
  71. },
  72. wantErr: true,
  73. },
  74. {
  75. name: "wrong property",
  76. fields: fields{
  77. Client: fakeClient{
  78. t: t,
  79. secretMap: map[string]corev1.Secret{
  80. "mysec": {
  81. Data: map[string][]byte{
  82. "token": []byte(`foobar`),
  83. },
  84. },
  85. },
  86. },
  87. Namespace: "default",
  88. },
  89. ref: esv1beta1.ExternalSecretDataRemoteRef{
  90. Key: "mysec",
  91. Property: "not-the-token",
  92. },
  93. wantErr: true,
  94. },
  95. {
  96. name: "successful case",
  97. fields: fields{
  98. Client: fakeClient{
  99. t: t,
  100. secretMap: map[string]corev1.Secret{
  101. "mysec": {
  102. Data: map[string][]byte{
  103. "token": []byte(`foobar`),
  104. },
  105. },
  106. },
  107. },
  108. Namespace: "default",
  109. },
  110. ref: esv1beta1.ExternalSecretDataRemoteRef{
  111. Key: "mysec",
  112. Property: "token",
  113. },
  114. want: []byte(`foobar`),
  115. },
  116. {
  117. name: "successful case without property",
  118. fields: fields{
  119. Client: fakeClient{
  120. t: t,
  121. secretMap: map[string]corev1.Secret{
  122. "mysec": {
  123. Data: map[string][]byte{
  124. "token": []byte(`foobar`),
  125. },
  126. },
  127. },
  128. },
  129. Namespace: "default",
  130. },
  131. ref: esv1beta1.ExternalSecretDataRemoteRef{
  132. Key: "mysec",
  133. },
  134. want: []byte(`{"token":"foobar"}`),
  135. },
  136. }
  137. for _, tt := range tests {
  138. t.Run(tt.name, func(t *testing.T) {
  139. p := &Client{
  140. userSecretClient: tt.fields.Client,
  141. userReviewClient: tt.fields.ReviewClient,
  142. namespace: tt.fields.Namespace,
  143. }
  144. got, err := p.GetSecret(context.Background(), tt.ref)
  145. if (err != nil) != tt.wantErr {
  146. t.Errorf("ProviderKubernetes.GetSecret() error = %v, wantErr %v", err, tt.wantErr)
  147. return
  148. }
  149. if !reflect.DeepEqual(got, tt.want) {
  150. t.Errorf("ProviderKubernetes.GetSecret() = %v, want %v", got, tt.want)
  151. }
  152. })
  153. }
  154. }
  155. func TestGetAllSecrets(t *testing.T) {
  156. type fields struct {
  157. Client KClient
  158. ReviewClient RClient
  159. Namespace string
  160. }
  161. type args struct {
  162. ctx context.Context
  163. ref esv1beta1.ExternalSecretFind
  164. }
  165. tests := []struct {
  166. name string
  167. fields fields
  168. args args
  169. want map[string][]byte
  170. wantErr bool
  171. }{
  172. {
  173. name: "use regex",
  174. fields: fields{
  175. Client: fakeClient{
  176. t: t,
  177. secretMap: map[string]corev1.Secret{
  178. "mysec": {
  179. ObjectMeta: metav1.ObjectMeta{
  180. Name: "mysec",
  181. },
  182. Data: map[string][]byte{
  183. "token": []byte(`foo`),
  184. },
  185. },
  186. "other": {
  187. ObjectMeta: metav1.ObjectMeta{
  188. Name: "other",
  189. },
  190. Data: map[string][]byte{
  191. "token": []byte(`bar`),
  192. },
  193. },
  194. },
  195. },
  196. },
  197. args: args{
  198. ref: esv1beta1.ExternalSecretFind{
  199. Name: &esv1beta1.FindName{
  200. RegExp: "other",
  201. },
  202. },
  203. },
  204. want: map[string][]byte{
  205. "other": []byte(`{"token":"bar"}`),
  206. },
  207. },
  208. {
  209. name: "use tags/labels",
  210. fields: fields{
  211. Client: fakeClient{
  212. t: t,
  213. expectedListOptions: metav1.ListOptions{
  214. LabelSelector: "app=foobar",
  215. },
  216. secretMap: map[string]corev1.Secret{
  217. "mysec": {
  218. ObjectMeta: metav1.ObjectMeta{
  219. Name: "mysec",
  220. },
  221. Data: map[string][]byte{
  222. "token": []byte(`foo`),
  223. },
  224. },
  225. "other": {
  226. ObjectMeta: metav1.ObjectMeta{
  227. Name: "other",
  228. },
  229. Data: map[string][]byte{
  230. "token": []byte(`bar`),
  231. },
  232. },
  233. },
  234. },
  235. },
  236. args: args{
  237. ref: esv1beta1.ExternalSecretFind{
  238. Tags: map[string]string{
  239. "app": "foobar",
  240. },
  241. },
  242. },
  243. want: map[string][]byte{
  244. "mysec": []byte(`{"token":"foo"}`),
  245. "other": []byte(`{"token":"bar"}`),
  246. },
  247. },
  248. }
  249. for _, tt := range tests {
  250. t.Run(tt.name, func(t *testing.T) {
  251. p := &Client{
  252. userSecretClient: tt.fields.Client,
  253. userReviewClient: tt.fields.ReviewClient,
  254. namespace: tt.fields.Namespace,
  255. }
  256. got, err := p.GetAllSecrets(tt.args.ctx, tt.args.ref)
  257. if (err != nil) != tt.wantErr {
  258. t.Errorf("ProviderKubernetes.GetAllSecrets() error = %v, wantErr %v", err, tt.wantErr)
  259. return
  260. }
  261. if !reflect.DeepEqual(got, tt.want) {
  262. t.Errorf("ProviderKubernetes.GetAllSecrets() = %v, want %v", got, tt.want)
  263. }
  264. })
  265. }
  266. }