Created
March 21, 2024 16:32
-
-
Save naveensrinivasan/8f347e3265f81bd4a15e3a6787d57ba1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package packager | |
import ( | |
"reflect" | |
"testing" | |
"github.com/defenseunicorns/zarf/src/types" | |
) | |
type generateValuesOverridesTestCase struct { | |
name string | |
args struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
} | |
want map[string]any | |
} | |
func TestGenerateValuesOverrides(t *testing.T) { | |
t.Parallel() | |
// Grouping test cases by categories for better organization and readability | |
// Category: Basic Functionality | |
basicFunctionalityTestCases := []generateValuesOverridesTestCase{ | |
{ | |
name: "Empty inputs", | |
args: struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
}{ | |
chartVariables: []types.ZarfChartVariable{}, | |
setVariableMap: map[string]*types.ZarfSetVariable{}, | |
deployOpts: types.ZarfDeployOptions{}, | |
componentName: "", | |
chartName: "", | |
}, | |
want: map[string]any{}, | |
}, | |
{ | |
name: "Single variable", | |
args: struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
}{ | |
chartVariables: []types.ZarfChartVariable{{Name: "testVar", Path: "testVar"}}, | |
setVariableMap: map[string]*types.ZarfSetVariable{"testVar": {Value: "testValue"}}, | |
deployOpts: types.ZarfDeployOptions{}, | |
componentName: "testComponent", | |
chartName: "testChart", | |
}, | |
want: map[string]any{"testVar": "testValue"}, | |
}, | |
{ | |
name: "Non-matching setVariable", | |
args: struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
}{ | |
chartVariables: []types.ZarfChartVariable{{Name: "expectedVar", Path: "path.to.expectedVar"}}, | |
setVariableMap: map[string]*types.ZarfSetVariable{"unexpectedVar": {Value: "unexpectedValue"}}, | |
deployOpts: types.ZarfDeployOptions{}, | |
componentName: "testComponent", | |
chartName: "testChart", | |
}, | |
want: map[string]any{}, | |
}, | |
} | |
// Category: Nested Variables | |
nestedVariablesTestCases := []generateValuesOverridesTestCase{ | |
{ | |
name: "Nested 3 level setVariableMap", | |
args: struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
}{ | |
chartVariables: []types.ZarfChartVariable{ | |
{Name: "level1.level2.level3Var", Path: "level1.level2.level3Var"}, | |
}, | |
setVariableMap: map[string]*types.ZarfSetVariable{ | |
"level1.level2.level3Var": {Value: "nestedValue"}, | |
}, | |
deployOpts: types.ZarfDeployOptions{}, | |
componentName: "nestedComponent", | |
chartName: "nestedChart", | |
}, | |
want: map[string]any{ | |
"level1": map[string]any{ | |
"level2": map[string]any{ | |
"level3Var": "nestedValue", | |
}, | |
}, | |
}, | |
}, | |
{ | |
name: "Multiple variables with nested and non-nested paths", | |
args: struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
}{ | |
chartVariables: []types.ZarfChartVariable{ | |
{Name: "nestedVar.level2", Path: "nestedVar.level2"}, | |
{Name: "simpleVar", Path: "simpleVar"}, | |
}, | |
setVariableMap: map[string]*types.ZarfSetVariable{ | |
"nestedVar.level2": {Value: "nestedValue"}, | |
"simpleVar": {Value: "simpleValue"}, | |
}, | |
deployOpts: types.ZarfDeployOptions{}, | |
componentName: "mixedComponent", | |
chartName: "mixedChart", | |
}, | |
want: map[string]any{ | |
"nestedVar": map[string]any{ | |
"level2": "nestedValue", | |
}, | |
"simpleVar": "simpleValue", | |
}, | |
}, | |
} | |
// Category: Overrides | |
overridesTestCases := []generateValuesOverridesTestCase{ | |
{ | |
name: "Values override test", | |
args: struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
}{ | |
chartVariables: []types.ZarfChartVariable{ | |
{Name: "overrideVar", Path: "path"}, | |
}, | |
setVariableMap: map[string]*types.ZarfSetVariable{ | |
"path": {Value: "overrideValue"}, | |
}, | |
deployOpts: types.ZarfDeployOptions{ | |
ValuesOverridesMap: map[string]map[string]map[string]any{ | |
"testComponent": { | |
"testChart": { | |
"path": "deployOverrideValue", | |
}, | |
}, | |
}, | |
}, | |
componentName: "testComponent", | |
chartName: "testChart", | |
}, | |
want: map[string]any{ | |
"path": "deployOverrideValue", | |
}, | |
}, | |
{ | |
name: "Missing variable in setVariableMap but present in ValuesOverridesMap", | |
args: struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
}{ | |
chartVariables: []types.ZarfChartVariable{ | |
{Name: "missingVar", Path: "missingVarPath"}, | |
}, | |
setVariableMap: map[string]*types.ZarfSetVariable{}, // Empty setVariableMap | |
deployOpts: types.ZarfDeployOptions{ | |
ValuesOverridesMap: map[string]map[string]map[string]any{ | |
"testComponent": { | |
"testChart": { | |
"missingVarPath": "overrideValue", | |
}, | |
}, | |
}, | |
}, | |
componentName: "testComponent", | |
chartName: "testChart", | |
}, | |
want: map[string]any{ | |
"missingVarPath": "overrideValue", | |
}, | |
}, | |
} | |
// Category: Special Cases | |
specialCasesTestCases := []generateValuesOverridesTestCase{ | |
{ | |
name: "Non-existent component or chart", | |
args: struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
}{ | |
chartVariables: []types.ZarfChartVariable{{Name: "someVar", Path: "someVar"}}, | |
setVariableMap: map[string]*types.ZarfSetVariable{"someVar": {Value: "value"}}, | |
deployOpts: types.ZarfDeployOptions{ | |
ValuesOverridesMap: map[string]map[string]map[string]any{ | |
"nonExistentComponent": { | |
"nonExistentChart": { | |
"someVar": "overrideValue", | |
}, | |
}, | |
}, | |
}, | |
componentName: "actualComponent", | |
chartName: "actualChart", | |
}, | |
want: map[string]any{"someVar": "value"}, | |
}, | |
{ | |
name: "Variable in setVariableMap but not in chartVariables", | |
args: struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
}{ | |
chartVariables: []types.ZarfChartVariable{}, // Empty chartVariables | |
setVariableMap: map[string]*types.ZarfSetVariable{ | |
"orphanVar": {Value: "orphanValue"}, | |
}, | |
deployOpts: types.ZarfDeployOptions{}, | |
componentName: "orphanComponent", | |
chartName: "orphanChart", | |
}, | |
want: map[string]any{}, // Expecting it to be ignored or handled differently | |
}, | |
{ | |
name: "Empty ValuesOverridesMap with non-empty setVariableMap and chartVariables", | |
args: struct { | |
chartVariables []types.ZarfChartVariable | |
setVariableMap map[string]*types.ZarfSetVariable | |
deployOpts types.ZarfDeployOptions | |
componentName string | |
chartName string | |
}{ | |
chartVariables: []types.ZarfChartVariable{ | |
{Name: "var1", Path: "path.to.var1"}, | |
{Name: "var2", Path: "path.to.var2"}, | |
{Name: "var3", Path: "path.to3.var3"}, | |
}, | |
setVariableMap: map[string]*types.ZarfSetVariable{ | |
"var1": {Value: "value1"}, | |
"var2": {Value: "value2"}, | |
"var3": {Value: "value3"}, | |
}, | |
deployOpts: types.ZarfDeployOptions{ | |
ValuesOverridesMap: map[string]map[string]map[string]any{}, // Empty ValuesOverridesMap | |
}, | |
componentName: "componentWithVars", | |
chartName: "chartWithVars", | |
}, | |
want: map[string]any{ | |
"path": map[string]any{ | |
"to": map[string]any{ | |
"var1": "value1", | |
"var2": "value2", | |
}, | |
"to3": map[string]any{ | |
"var3": "value3", | |
}, | |
}, | |
}, | |
}, | |
} | |
// Consolidating all categorized test cases into a single slice for iteration | |
allTestCases := append(basicFunctionalityTestCases, nestedVariablesTestCases...) | |
allTestCases = append(allTestCases, overridesTestCases...) | |
allTestCases = append(allTestCases, specialCasesTestCases...) | |
for _, tc := range allTestCases { | |
tc := tc | |
t.Run(tc.name, func(t *testing.T) { | |
got, _ := generateValuesOverrides(tc.args.chartVariables, | |
tc.args.setVariableMap, tc.args.deployOpts, tc.args.componentName, tc.args.chartName) | |
if !reflect.DeepEqual(got, tc.want) { | |
t.Errorf("%s: generateValuesOverrides() got = %v, want %v", tc.name, got, tc.want) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment