|
|
|
|
@ -16,6 +16,7 @@ limitations under the License.
|
|
|
|
|
package rules
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
@ -134,6 +135,114 @@ func TestValidateDependenciesUnique(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestValidateDependencyConditions(t *testing.T) {
|
|
|
|
|
newChart := func(deps []*chart.Dependency, values map[string]any, subcharts ...*chart.Chart) *chart.Chart {
|
|
|
|
|
c := &chart.Chart{
|
|
|
|
|
Metadata: &chart.Metadata{
|
|
|
|
|
Name: "parentchart",
|
|
|
|
|
Version: "0.1.0",
|
|
|
|
|
APIVersion: "v3",
|
|
|
|
|
Dependencies: deps,
|
|
|
|
|
},
|
|
|
|
|
Values: values,
|
|
|
|
|
}
|
|
|
|
|
c.SetDependencies(subcharts...)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
subchart := func(values map[string]any) *chart.Chart {
|
|
|
|
|
return &chart.Chart{
|
|
|
|
|
Metadata: &chart.Metadata{
|
|
|
|
|
Name: "sub",
|
|
|
|
|
Version: "0.1.0",
|
|
|
|
|
APIVersion: "v3",
|
|
|
|
|
},
|
|
|
|
|
Values: values,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
chart *chart.Chart
|
|
|
|
|
valueOverrides map[string]any
|
|
|
|
|
wantErr string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "dependency without condition",
|
|
|
|
|
chart: newChart([]*chart.Dependency{{Name: "sub"}}, nil, subchart(nil)),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "condition resolving to a parent chart value",
|
|
|
|
|
chart: newChart(
|
|
|
|
|
[]*chart.Dependency{{Name: "sub", Condition: "sub.enabled"}},
|
|
|
|
|
map[string]any{"sub": map[string]any{"enabled": false}},
|
|
|
|
|
subchart(nil),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "condition resolving to a dependency default value",
|
|
|
|
|
chart: newChart(
|
|
|
|
|
[]*chart.Dependency{{Name: "sub", Condition: "sub.enabled"}},
|
|
|
|
|
nil,
|
|
|
|
|
subchart(map[string]any{"enabled": true}),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "condition resolving to a value override",
|
|
|
|
|
chart: newChart(
|
|
|
|
|
[]*chart.Dependency{{Name: "sub", Condition: "sub.enabled"}},
|
|
|
|
|
nil,
|
|
|
|
|
subchart(nil),
|
|
|
|
|
),
|
|
|
|
|
valueOverrides: map[string]any{"sub": map[string]any{"enabled": false}},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "second condition path resolving to a value",
|
|
|
|
|
chart: newChart(
|
|
|
|
|
[]*chart.Dependency{{Name: "sub", Condition: "sub.enabled,global.sub.enabled"}},
|
|
|
|
|
map[string]any{"global": map[string]any{"sub": map[string]any{"enabled": false}}},
|
|
|
|
|
subchart(nil),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "condition not resolving to a value",
|
|
|
|
|
chart: newChart(
|
|
|
|
|
[]*chart.Dependency{{Name: "sub", Condition: "sub.enabled"}},
|
|
|
|
|
map[string]any{"sub": map[string]any{"nested": false}},
|
|
|
|
|
subchart(nil),
|
|
|
|
|
),
|
|
|
|
|
wantErr: `sub (condition "sub.enabled")`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "condition of aliased dependency resolving to a dependency default value",
|
|
|
|
|
chart: newChart(
|
|
|
|
|
[]*chart.Dependency{{Name: "sub", Alias: "other", Condition: "other.enabled"}},
|
|
|
|
|
nil,
|
|
|
|
|
subchart(map[string]any{"enabled": false}),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "condition of aliased dependency not resolving to a value",
|
|
|
|
|
chart: newChart(
|
|
|
|
|
[]*chart.Dependency{{Name: "sub", Alias: "other", Condition: "other.enabled"}},
|
|
|
|
|
nil,
|
|
|
|
|
subchart(nil),
|
|
|
|
|
),
|
|
|
|
|
wantErr: `sub (condition "other.enabled")`,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
err := validateDependencyConditions(tt.chart, tt.valueOverrides)
|
|
|
|
|
if tt.wantErr == "" {
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
} else {
|
|
|
|
|
assert.ErrorContains(t, err, tt.wantErr)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDependencies(t *testing.T) {
|
|
|
|
|
tmp := t.TempDir()
|
|
|
|
|
|
|
|
|
|
@ -148,3 +257,47 @@ func TestDependencies(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDependenciesWithValues(t *testing.T) {
|
|
|
|
|
tmp := t.TempDir()
|
|
|
|
|
|
|
|
|
|
c := chart.Chart{
|
|
|
|
|
Metadata: &chart.Metadata{
|
|
|
|
|
Name: "condchart",
|
|
|
|
|
Version: "0.1.0",
|
|
|
|
|
APIVersion: "v3",
|
|
|
|
|
Dependencies: []*chart.Dependency{
|
|
|
|
|
{Name: "sub", Version: "0.1.0", Condition: "sub.enabled"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.SetDependencies(&chart.Chart{
|
|
|
|
|
Metadata: &chart.Metadata{
|
|
|
|
|
Name: "sub",
|
|
|
|
|
Version: "0.1.0",
|
|
|
|
|
APIVersion: "v3",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, chartutil.SaveDir(&c, tmp))
|
|
|
|
|
chartDir := filepath.Join(tmp, c.Metadata.Name)
|
|
|
|
|
|
|
|
|
|
// The condition does not resolve to any value, warn about it
|
|
|
|
|
linter := support.Linter{ChartDir: chartDir}
|
|
|
|
|
Dependencies(&linter)
|
|
|
|
|
require.Len(t, linter.Messages, 1)
|
|
|
|
|
assert.Equal(t, support.WarningSev, linter.Messages[0].Severity)
|
|
|
|
|
require.ErrorContains(t, linter.Messages[0].Err, `sub (condition "sub.enabled")`)
|
|
|
|
|
|
|
|
|
|
// The condition resolves to a value override, no warning
|
|
|
|
|
linter = support.Linter{ChartDir: chartDir}
|
|
|
|
|
DependenciesWithValues(&linter, map[string]any{"sub": map[string]any{"enabled": true}})
|
|
|
|
|
assert.Empty(t, linter.Messages)
|
|
|
|
|
|
|
|
|
|
// Conditions are not checked when dependencies are missing from the
|
|
|
|
|
// charts directory, as their default values cannot be resolved
|
|
|
|
|
require.NoError(t, os.RemoveAll(filepath.Join(chartDir, "charts")))
|
|
|
|
|
linter = support.Linter{ChartDir: chartDir}
|
|
|
|
|
Dependencies(&linter)
|
|
|
|
|
require.Len(t, linter.Messages, 1)
|
|
|
|
|
assert.ErrorContains(t, linter.Messages[0].Err, "chart directory is missing these dependencies")
|
|
|
|
|
}
|
|
|
|
|
|