gcr_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package gcr
  14. import (
  15. "context"
  16. "reflect"
  17. "testing"
  18. "time"
  19. "golang.org/x/oauth2"
  20. v1 "k8s.io/api/core/v1"
  21. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "sigs.k8s.io/controller-runtime/pkg/client"
  24. clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
  25. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  26. )
  27. func TestGenerate(t *testing.T) {
  28. type args struct {
  29. ctx context.Context
  30. jsonSpec *apiextensions.JSON
  31. kube client.Client
  32. namespace string
  33. fakeTokenSource tokenSourceFunc
  34. }
  35. tests := []struct {
  36. name string
  37. g *Generator
  38. args args
  39. want map[string][]byte
  40. wantErr bool
  41. }{
  42. {
  43. name: "nil spec",
  44. args: args{
  45. jsonSpec: nil,
  46. },
  47. wantErr: true,
  48. },
  49. {
  50. name: "full spec",
  51. args: args{
  52. namespace: "foobar",
  53. kube: clientfake.NewClientBuilder().WithObjects(&v1.Secret{
  54. ObjectMeta: metav1.ObjectMeta{
  55. Name: "example",
  56. Namespace: "foobar",
  57. },
  58. Data: map[string][]byte{
  59. "foo": []byte("bar"),
  60. },
  61. }).Build(),
  62. fakeTokenSource: func(ctx context.Context, auth esv1.GCPSMAuth, projectID string, storeKind string, kube client.Client, namespace string) (oauth2.TokenSource, error) {
  63. return oauth2.StaticTokenSource(&oauth2.Token{
  64. AccessToken: "1234",
  65. Expiry: time.Unix(5555, 0),
  66. }), nil
  67. },
  68. jsonSpec: &apiextensions.JSON{
  69. Raw: []byte(`apiVersion: generators.external-secrets.io/v1alpha1
  70. kind: GCRAccessToken
  71. spec:
  72. projectID: "foobar"
  73. auth:
  74. secretRef:
  75. secretAccessKeySecretRef:
  76. name: "example"
  77. key: "foo"
  78. `),
  79. },
  80. },
  81. want: map[string][]byte{
  82. "username": []byte(defaultLoginUsername),
  83. "password": []byte("1234"),
  84. "expiry": []byte(`5555`),
  85. },
  86. },
  87. }
  88. for _, tt := range tests {
  89. t.Run(tt.name, func(t *testing.T) {
  90. g := &Generator{}
  91. got, _, err := g.generate(
  92. tt.args.ctx,
  93. tt.args.jsonSpec,
  94. tt.args.kube,
  95. tt.args.namespace,
  96. tt.args.fakeTokenSource)
  97. if (err != nil) != tt.wantErr {
  98. t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
  99. return
  100. }
  101. if !reflect.DeepEqual(got, tt.want) {
  102. t.Errorf("Generator.Generate() = %v, want %v", got, tt.want)
  103. }
  104. })
  105. }
  106. }