ecr_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 ecr
  13. import (
  14. "context"
  15. "encoding/base64"
  16. "errors"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/aws/aws-sdk-go/aws/session"
  21. "github.com/aws/aws-sdk-go/service/ecr"
  22. "github.com/aws/aws-sdk-go/service/ecr/ecriface"
  23. v1 "k8s.io/api/core/v1"
  24. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. utilpointer "k8s.io/utils/pointer"
  27. "sigs.k8s.io/controller-runtime/pkg/client"
  28. clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
  29. )
  30. func TestGenerate(t *testing.T) {
  31. type args struct {
  32. ctx context.Context
  33. jsonSpec *apiextensions.JSON
  34. kube client.Client
  35. namespace string
  36. authTokenFunc func(*ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error)
  37. }
  38. tests := []struct {
  39. name string
  40. g *Generator
  41. args args
  42. want map[string][]byte
  43. wantErr bool
  44. }{
  45. {
  46. name: "nil spec",
  47. args: args{
  48. jsonSpec: nil,
  49. },
  50. wantErr: true,
  51. },
  52. {
  53. name: "invalid json",
  54. args: args{
  55. authTokenFunc: func(gati *ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error) {
  56. return nil, errors.New("boom")
  57. },
  58. jsonSpec: &apiextensions.JSON{
  59. Raw: []byte(``),
  60. },
  61. },
  62. wantErr: true,
  63. },
  64. {
  65. name: "full spec",
  66. args: args{
  67. namespace: "foobar",
  68. kube: clientfake.NewClientBuilder().WithObjects(&v1.Secret{
  69. ObjectMeta: metav1.ObjectMeta{
  70. Name: "my-aws-creds",
  71. Namespace: "foobar",
  72. },
  73. Data: map[string][]byte{
  74. "key-id": []byte("foo"),
  75. "access-secret": []byte("bar"),
  76. },
  77. }).Build(),
  78. authTokenFunc: func(in *ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error) {
  79. t := time.Unix(1234, 0)
  80. return &ecr.GetAuthorizationTokenOutput{
  81. AuthorizationData: []*ecr.AuthorizationData{
  82. {
  83. AuthorizationToken: utilpointer.String(base64.StdEncoding.EncodeToString([]byte("uuser:pass"))),
  84. ProxyEndpoint: utilpointer.String("foo"),
  85. ExpiresAt: &t,
  86. },
  87. },
  88. }, nil
  89. },
  90. jsonSpec: &apiextensions.JSON{
  91. Raw: []byte(`apiVersion: generators.external-secrets.io/v1alpha1
  92. kind: ECRAuthorizationToken
  93. spec:
  94. region: eu-west-1
  95. role: "my-role"
  96. auth:
  97. secretRef:
  98. accessKeyIDSecretRef:
  99. name: "my-aws-creds"
  100. key: "key-id"
  101. secretAccessKeySecretRef:
  102. name: "my-aws-creds"
  103. key: "access-secret"`),
  104. },
  105. },
  106. want: map[string][]byte{
  107. "username": []byte("uuser"),
  108. "password": []byte("pass"),
  109. "proxy_endpoint": []byte("foo"),
  110. "expires_at": []byte("1234"),
  111. },
  112. },
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.name, func(t *testing.T) {
  116. g := &Generator{}
  117. got, err := g.generate(
  118. tt.args.ctx,
  119. tt.args.jsonSpec,
  120. tt.args.kube,
  121. tt.args.namespace,
  122. func(aws *session.Session) ecriface.ECRAPI {
  123. return &FakeECR{
  124. authTokenFunc: tt.args.authTokenFunc,
  125. }
  126. },
  127. )
  128. if (err != nil) != tt.wantErr {
  129. t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
  130. return
  131. }
  132. if !reflect.DeepEqual(got, tt.want) {
  133. t.Errorf("Generator.Generate() = %v, want %v", got, tt.want)
  134. }
  135. })
  136. }
  137. }
  138. type FakeECR struct {
  139. ecriface.ECRAPI
  140. authTokenFunc func(*ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error)
  141. }
  142. func (e *FakeECR) GetAuthorizationToken(in *ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error) {
  143. return e.authTokenFunc(in)
  144. }