Files
helmfile/pkg/state/issue923_test.go
T
34ead21107 feat: allow helmfile to continue on failed releases (#2616)
* feat: allow helmfile to continue on failed releases

Co-authored-by: Peter Honeder <peter.honeder@unwired.at>
Signed-off-by: Niklas Ott <niklas.ott@unwired.at>

* fix: skip failed-prep releases, complete flag wiring, add tests and docs (#64)

Review follow-ups for --allow-failed-releases (#2616):

- Track per-release chart preparation failures in PrepareCharts (returned
  as a map keyed by release) and remove those releases from the state in
  Run.WithPreparedCharts when --allow-failed-releases is set, so a failed
  release is never executed against its original, un-prepared chart
  reference (which could either fail again with a duplicate error or, for
  charts requiring chartify, bypass patches/dependency modifications and
  produce an unintended result). All failures are still reported at the
  end via the aggregated MultiError.
- Complete the release identity on error results from
  prepareChartForRelease so failures are attributed to the correct
  release.
- With --allow-failed-releases, continue building dependencies of the
  remaining charts when 'helm dep build' fails for one release, and skip
  the affected releases during execution.
- Wire --allow-failed-releases into 'helmfile unittest' and 'helmfile
  status'; remove the dead flag wiring for write-values and list (both
  never prepare charts, see commandsSkipChartPrep).
- Simplify control flow (guard clauses, errors.As, drop dead code and
  redundant else branches).
- Add end-to-end coverage in pkg/app/issue_2616_test.go and extend the
  state-level tests; document the flag in docs/cli.md and CHANGELOG.md.

Signed-off-by: yxxhero <11087727+yxxhero@users.noreply.github.com>

---------

Signed-off-by: Niklas Ott <niklas.ott@unwired.at>
Signed-off-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
Co-authored-by: Peter Honeder <peter.honeder@unwired.at>
Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
2026-09-03 06:26:46 +08:00

219 lines
6.5 KiB
Go

package state
import (
"os"
"path/filepath"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"github.com/helmfile/helmfile/pkg/exectest"
)
// cleanupLeakedChartDirs removes chart directories and lock files that
// PrepareCharts may leak into the CWD when OutputDirTemplate does not include
// {{ .OutputDir }}. This is a safety net; the proper fix is to always
// include {{ .OutputDir }}.
func cleanupLeakedChartDirs(t *testing.T, dirs ...string) {
t.Helper()
t.Cleanup(func() {
for _, d := range dirs {
_ = os.RemoveAll(d)
_ = os.Remove(d + ".lock")
}
})
}
// TestIssue923_OCIChartPreparedForNeededRelease verifies that OCI charts are
// prepared (pulled) for releases that are included via --include-needs.
// See https://github.com/helmfile/helmfile/issues/923
func TestIssue923_OCIChartPreparedForNeededRelease(t *testing.T) {
resetChartCacheForTest()
cleanupLeakedChartDirs(t, "argocd", "argocd-secrets")
helmfileContent := []byte(`
repositories:
- name: huma
url: ghcr.io/huma-engineering/helm-charts
oci: true
- name: argo
url: https://argoproj.github.io/argo-helm
releases:
- name: argocd
namespace: argocd
chart: argo/argo-cd
labels:
app: argocd
needs:
- argocd/argocd-secrets
version: ~5.37.1
- name: argocd-secrets
namespace: argocd
chart: huma/external-secret-resources
labels:
app: secrets
version: ~0.1.0
`)
logger := zap.NewExample().Sugar()
st, err := createFromYaml(helmfileContent, "example/path/to/helmfile.yaml", DefaultEnv, logger)
require.NoError(t, err)
// Simulate: helmfile -l app=argocd --include-needs
st.Selectors = []string{"app=argocd"}
tempDir := t.TempDir()
helm := &exectest.Helm{
Helm3: true,
ChartsMutex: &sync.Mutex{},
}
// PrepareCharts uses opts.IncludeTransitiveNeeds to determine which releases
// to prepare charts for. When --include-needs is set, this should be true,
// matching c.IncludeNeeds() in the app layer.
// OutputDirTemplate includes .OutputDir so charts go into tempDir (auto-cleaned
// by t.TempDir), not the global cache or CWD.
opts := ChartPrepareOptions{
SkipResolve: true,
IncludeTransitiveNeeds: true,
Concurrency: 1,
OutputDirTemplate: "{{ .OutputDir }}/{{ .Release.Name }}",
}
releaseToChart, _, errs := st.PrepareCharts(helm, tempDir, 1, "apply", opts)
require.Empty(t, errs, "PrepareCharts should not return errors")
// Verify both releases have prepared charts
assert.Contains(t, releaseToChart, PrepareChartKey{Name: "argocd", Namespace: "argocd"},
"argocd chart should be prepared")
assert.Contains(t, releaseToChart, PrepareChartKey{Name: "argocd-secrets", Namespace: "argocd"},
"argocd-secrets (needed OCI chart) should be prepared")
// The OCI chart path should be a local path (pulled by ChartPull), not the
// remote chart name. This confirms the OCI chart was actually prepared.
chartPath := releaseToChart[PrepareChartKey{Name: "argocd-secrets", Namespace: "argocd"}]
assert.NotEqual(t, "huma/external-secret-resources", chartPath,
"argocd-secrets chart path should be a local path (pulled), not the remote chart name")
}
// TestIssue923_OCIChartNotPreparedWithoutIncludeNeeds verifies that without
// --include-needs, the OCI chart for the needed release is NOT prepared.
func TestIssue923_OCIChartNotPreparedWithoutIncludeNeeds(t *testing.T) {
resetChartCacheForTest()
cleanupLeakedChartDirs(t, "argocd", "argocd-secrets")
helmfileContent := []byte(`
repositories:
- name: huma
url: ghcr.io/huma-engineering/helm-charts
oci: true
releases:
- name: argocd
namespace: argocd
chart: argo/argo-cd
labels:
app: argocd
needs:
- argocd/argocd-secrets
- name: argocd-secrets
namespace: argocd
chart: huma/external-secret-resources
labels:
app: secrets
version: ~0.1.0
`)
logger := zap.NewExample().Sugar()
st, err := createFromYaml(helmfileContent, "example/path/to/helmfile.yaml", DefaultEnv, logger)
require.NoError(t, err)
// Simulate: helmfile -l app=argocd (without --include-needs)
st.Selectors = []string{"app=argocd"}
tempDir := t.TempDir()
helm := &exectest.Helm{
Helm3: true,
ChartsMutex: &sync.Mutex{},
}
opts := ChartPrepareOptions{
SkipResolve: true,
IncludeTransitiveNeeds: false,
Concurrency: 1,
OutputDirTemplate: "{{ .OutputDir }}/{{ .Release.Name }}",
}
releaseToChart, _, errs := st.PrepareCharts(helm, tempDir, 1, "apply", opts)
require.Empty(t, errs)
// argocd-secrets should NOT have a prepared chart
assert.NotContains(t, releaseToChart, PrepareChartKey{Name: "argocd-secrets", Namespace: "argocd"},
"argocd-secrets chart should NOT be prepared without --include-needs")
assert.Empty(t, helm.PulledCharts,
"ChartPull should NOT have been called without --include-needs")
}
// TestIssue923_GetSelectedReleasesWithNeeds verifies the selection logic
// that PrepareCharts depends on.
func TestIssue923_GetSelectedReleasesWithNeeds(t *testing.T) {
helmfileContent := []byte(`
repositories:
- name: huma
url: ghcr.io/huma-engineering/helm-charts
oci: true
releases:
- name: argocd
namespace: argocd
chart: argo/argo-cd
labels:
app: argocd
needs:
- argocd/argocd-secrets
- name: argocd-secrets
namespace: argocd
chart: huma/external-secret-resources
labels:
app: secrets
`)
logger := zap.NewExample().Sugar()
st, err := createFromYaml(helmfileContent, filepath.Join("example", "path", "to", "helmfile.yaml"), DefaultEnv, logger)
require.NoError(t, err)
st.Selectors = []string{"app=argocd"}
// With includeTransitiveNeeds=true (as PrepareCharts does when --include-needs is set)
selected, err := st.GetSelectedReleases(true)
require.NoError(t, err)
names := make([]string, len(selected))
for i, r := range selected {
names[i] = r.Name
}
assert.Contains(t, names, "argocd", "selected releases should include argocd")
assert.Contains(t, names, "argocd-secrets", "selected releases should include the needed release argocd-secrets")
// With includeTransitiveNeeds=false (without --include-needs)
selectedFalse, err := st.GetSelectedReleases(false)
require.NoError(t, err)
namesFalse := make([]string, len(selectedFalse))
for i, r := range selectedFalse {
namesFalse[i] = r.Name
}
assert.Contains(t, namesFalse, "argocd")
assert.NotContains(t, namesFalse, "argocd-secrets",
"needed release should NOT be selected without --include-needs")
}