helm_dependency_ensure_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 hack
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. "github.com/external-secrets/external-secrets/pkg/executil"
  20. )
  21. const (
  22. buildMarkerFile = "build-called"
  23. helperScriptPath = "helm.dependency.ensure.sh"
  24. chartPath = "deploy/charts/external-secrets"
  25. helmBinEnvPrefix = "HELM_BIN="
  26. helperFailedMsg = "helper failed: %v\n%s"
  27. scriptPreamble = "#!/usr/bin/env bash\nset -euo pipefail\n\n"
  28. scriptFailure = "echo \"unexpected helm invocation: $*\" >&2\nexit 1\n"
  29. )
  30. func TestHelmDependencyEnsureSkipsBuildWhenDependenciesAreReady(t *testing.T) {
  31. t.Parallel()
  32. assertHelperBuildBehavior(t, "v0.5.2", "ok", false, "expected helper to skip helm dependency build when dependencies are already available")
  33. }
  34. func TestHelmDependencyEnsureSkipsBuildWhenDependenciesAreUnpacked(t *testing.T) {
  35. t.Parallel()
  36. assertHelperBuildBehavior(t, "v0.6.0", "unpacked", false, "expected helper to skip helm dependency build when dependencies are unpacked locally")
  37. }
  38. func TestHelmDependencyEnsureBuildsWhenDependenciesAreMissing(t *testing.T) {
  39. t.Parallel()
  40. assertHelperBuildBehavior(t, "v0.5.2", "missing", true, "expected helper to invoke helm dependency build when dependencies are missing")
  41. }
  42. func assertHelperBuildBehavior(t *testing.T, version, status string, expectBuild bool, failureMsg string) {
  43. t.Helper()
  44. workdir := t.TempDir()
  45. marker := filepath.Join(workdir, buildMarkerFile)
  46. fakeHelm := writeFakeHelm(t, workdir, fakeHelmScript(version, status, marker))
  47. output, err := runHelper(t, fakeHelm)
  48. if err != nil {
  49. t.Fatalf(helperFailedMsg, err, string(output))
  50. }
  51. if expectBuild {
  52. if _, err := os.Stat(marker); err != nil {
  53. t.Fatalf("%s: %v", failureMsg, err)
  54. }
  55. return
  56. }
  57. if _, err := os.Stat(marker); !os.IsNotExist(err) {
  58. t.Fatal(failureMsg)
  59. }
  60. }
  61. func writeFakeHelm(t *testing.T, dir, content string) string {
  62. t.Helper()
  63. path := filepath.Join(dir, "helm")
  64. if err := os.WriteFile(path, []byte(content), 0o755); err != nil {
  65. t.Fatalf("write fake helm: %v", err)
  66. }
  67. return path
  68. }
  69. func runHelper(t *testing.T, fakeHelm string) ([]byte, error) {
  70. t.Helper()
  71. cmd, err := executil.Command("bash", helperScriptPath, chartPath)
  72. if err != nil {
  73. return nil, err
  74. }
  75. cmd.Dir = "."
  76. cmd.Env = append(os.Environ(), helmBinEnvPrefix+fakeHelm)
  77. return cmd.CombinedOutput()
  78. }
  79. func fakeHelmScript(version, status, marker string) string {
  80. return fmt.Sprintf(
  81. "%s%s%s",
  82. scriptPreamble,
  83. fmt.Sprintf(
  84. `if [[ "$1" == "dependency" && "$2" == "list" ]]; then
  85. cat <<'EOF'
  86. NAME VERSION REPOSITORY STATUS
  87. bitwarden-sdk-server %s oci://ghcr.io/external-secrets/charts %s
  88. EOF
  89. exit 0
  90. fi
  91. if [[ "$1" == "dependency" && "$2" == "build" ]]; then
  92. touch %q
  93. exit 0
  94. fi
  95. `,
  96. version,
  97. status,
  98. marker,
  99. ),
  100. scriptFailure,
  101. )
  102. }