pulumi_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 pulumi
  13. import (
  14. "context"
  15. "encoding/json"
  16. "net/http"
  17. "net/http/httptest"
  18. "reflect"
  19. "testing"
  20. esc2 "github.com/pulumi/esc"
  21. esc "github.com/pulumi/esc/cmd/esc/cli/client"
  22. "github.com/stretchr/testify/assert"
  23. "github.com/stretchr/testify/require"
  24. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  25. )
  26. func newTestClient(t *testing.T, _, _ string, handler func(w http.ResponseWriter, r *http.Request)) *client {
  27. const userAgent = "test-user-agent"
  28. const token = "test-token"
  29. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  30. require.Equal(t, "token "+token, r.Header.Get("Authorization"))
  31. handler(w, r)
  32. }))
  33. t.Cleanup(server.Close)
  34. return &client{
  35. escClient: esc.New(userAgent, server.URL, token, true),
  36. organization: "foo",
  37. environment: "bar",
  38. }
  39. }
  40. func TestGetSecret(t *testing.T) {
  41. ctx := context.Background()
  42. expected := esc2.NewValue("world")
  43. client := newTestClient(t, http.MethodGet, "/api/preview/environments/foo/bar/open/session", func(w http.ResponseWriter, r *http.Request) {
  44. err := json.NewEncoder(w).Encode(expected)
  45. require.NoError(t, err)
  46. })
  47. testCases := map[string]struct {
  48. ref esv1beta1.ExternalSecretDataRemoteRef
  49. want []byte
  50. err error
  51. }{
  52. "querying for the key returns the value": {
  53. ref: esv1beta1.ExternalSecretDataRemoteRef{
  54. Key: "b",
  55. },
  56. want: []byte(`world`),
  57. },
  58. }
  59. for name, tc := range testCases {
  60. t.Run(name, func(t *testing.T) {
  61. got, err := client.GetSecret(ctx, tc.ref)
  62. if tc.err == nil {
  63. assert.NoError(t, err)
  64. assert.Equal(t, tc.want, got)
  65. } else {
  66. assert.Nil(t, got)
  67. assert.ErrorIs(t, err, tc.err)
  68. assert.Equal(t, tc.err, err)
  69. }
  70. })
  71. }
  72. }
  73. func TestGetSecretMap(t *testing.T) {
  74. tests := []struct {
  75. name string
  76. ref esv1beta1.ExternalSecretDataRemoteRef
  77. input string
  78. want map[string][]byte
  79. wantErr bool
  80. }{
  81. {
  82. name: "successful case (basic types)",
  83. ref: esv1beta1.ExternalSecretDataRemoteRef{
  84. Key: "mysec",
  85. },
  86. input: `{"foo": "bar", "foobar": 42, "bar": true}`,
  87. want: map[string][]byte{
  88. "foo": []byte("bar"),
  89. "foobar": []byte("42"),
  90. "bar": []byte(`true`),
  91. },
  92. wantErr: false,
  93. },
  94. {
  95. name: "successful case (nested)",
  96. ref: esv1beta1.ExternalSecretDataRemoteRef{
  97. Key: "mysec",
  98. },
  99. input: `{"foo": {"foobar": 42}, "bar": {"foo": "bar"}}`,
  100. want: map[string][]byte{
  101. "foo": []byte(`{"foobar":42}`),
  102. "bar": []byte(`{"foo":"bar"}`),
  103. },
  104. wantErr: false,
  105. },
  106. {
  107. name: "successful case (basic + nested)",
  108. ref: esv1beta1.ExternalSecretDataRemoteRef{
  109. Key: "mysec",
  110. },
  111. input: `{"foo": "bar", "bar": {"foo": {"bar": false}}}`,
  112. want: map[string][]byte{
  113. "foo": []byte(`bar`),
  114. "bar": []byte(`{"foo":{"bar":false}}`),
  115. },
  116. wantErr: false,
  117. },
  118. }
  119. for _, tt := range tests {
  120. t.Run(tt.name, func(t *testing.T) {
  121. p := newTestClient(t, http.MethodGet, "/api/preview/environments/foo/bar/open/session", func(w http.ResponseWriter, r *http.Request) {
  122. esc2Input, err1 := esc2.FromJSON(tt.input, false)
  123. require.NoError(t, err1)
  124. err2 := json.NewEncoder(w).Encode(esc2Input)
  125. require.NoError(t, err2)
  126. })
  127. got, err := p.GetSecretMap(context.Background(), tt.ref)
  128. if (err != nil) != tt.wantErr {
  129. t.Errorf("ProviderPulumi.GetSecretMap() error = %v, wantErr %v", err, tt.wantErr)
  130. return
  131. }
  132. if !reflect.DeepEqual(got, tt.want) {
  133. t.Errorf("ProviderPulumi.GetSecretMap() get = %v, want %v", got, tt.want)
  134. }
  135. })
  136. }
  137. }