client_test.go 6.7 KB

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