key_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Copyright © The ESO Authors
  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 crd
  14. import (
  15. "strings"
  16. "testing"
  17. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  18. )
  19. func TestParseRemoteRefKey(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. storeKind string
  23. key string
  24. wantObj string
  25. wantNS *string
  26. wantErrSubs string
  27. }{
  28. {name: "SecretStore bare name", storeKind: esv1.SecretStoreKind, key: "myobj", wantObj: "myobj"},
  29. {name: "SecretStore rejects slash", storeKind: esv1.SecretStoreKind, key: "ns/obj", wantErrSubs: "must not contain '/'"},
  30. {name: "SecretStore empty key", storeKind: esv1.SecretStoreKind, key: "", wantErrSubs: "must not be empty"},
  31. {name: "ClusterSecretStore bare name", storeKind: esv1.ClusterSecretStoreKind, key: "cluster-only", wantObj: "cluster-only"},
  32. {name: "ClusterSecretStore namespace/name", storeKind: esv1.ClusterSecretStoreKind, key: "default/myobj", wantObj: "myobj", wantNS: new("default")},
  33. {name: "ClusterSecretStore empty namespace segment", storeKind: esv1.ClusterSecretStoreKind, key: "/obj", wantErrSubs: "namespace segment"},
  34. {name: "ClusterSecretStore empty name segment", storeKind: esv1.ClusterSecretStoreKind, key: "ns/", wantErrSubs: "object name after '/'"},
  35. {name: "ClusterSecretStore extra slash", storeKind: esv1.ClusterSecretStoreKind, key: "ns/foo/bar", wantErrSubs: "exactly one '/'"},
  36. }
  37. for _, tt := range tests {
  38. t.Run(tt.name, func(t *testing.T) {
  39. obj, ns, err := parseRemoteRefKey(tt.storeKind, tt.key)
  40. if tt.wantErrSubs != "" {
  41. if err == nil || !strings.Contains(err.Error(), tt.wantErrSubs) {
  42. t.Fatalf("parseRemoteRefKey() error = %v, want substring %q", err, tt.wantErrSubs)
  43. }
  44. return
  45. }
  46. if err != nil {
  47. t.Fatalf("parseRemoteRefKey() unexpected error: %v", err)
  48. }
  49. if obj != tt.wantObj {
  50. t.Fatalf("objectName = %q, want %q", obj, tt.wantObj)
  51. }
  52. if tt.wantNS == nil {
  53. if ns != nil {
  54. t.Fatalf("keyNamespace = %v, want nil", ns)
  55. }
  56. return
  57. }
  58. if ns == nil || *ns != *tt.wantNS {
  59. t.Fatalf("keyNamespace = %v, want %q", ns, *tt.wantNS)
  60. }
  61. })
  62. }
  63. }