Skip to content

Commit

Permalink
Merge pull request #81 from nocturnalastro/fix_parseTemplate_metadata…
Browse files Browse the repository at this point in the history
…_nil

Fix parse template metadata nil
  • Loading branch information
openshift-merge-bot[bot] authored Aug 15, 2024
2 parents c8c0920 + 6c924ad commit b72ed4f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
23 changes: 21 additions & 2 deletions pkg/compare/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ func (c Check) getPath(test Test, mode Mode) string {
return path.Join(test.getTestDir(), string(mode.crSource)+c.suffix)
}

func (c Check) hasErrorFile(test Test, mode Mode) bool {
if _, err := os.Stat(c.getPath(test, mode)); errors.Is(err, os.ErrNotExist) {
return false
}
return true
}

func (c Check) check(t *testing.T, test Test, mode Mode, value string) {
switch c.checkType {
case matchFile:
Expand Down Expand Up @@ -249,6 +256,8 @@ func matchErrorRegexCheck(msg string) Check {
}
}

const ExpectedPanic = "Expected Error Test Case"

// TestCompareRun ensures that Run command calls the right actions
// and returns the expected error.
func TestCompareRun(t *testing.T) {
Expand Down Expand Up @@ -342,13 +351,23 @@ func TestCompareRun(t *testing.T) {
IOStream, _, out, _ := genericiooptions.NewTestIOStreams()
klog.SetOutputBySeverity("INFO", out)
cmd := getCommand(t, &test, i, tf, &IOStream) // nolint:gosec

hasCheckedError := false
cmdutil.BehaviorOnFatal(func(str string, code int) {
errorStr := fmt.Sprintf("%s\nerror code:%d\n", testutils.RemoveInconsistentInfo(t, str), code)
test.checks.Err.check(t, test, mode, errorStr)
panic("Expected Error Test Case")
hasCheckedError = true
panic(ExpectedPanic)
})

defer func() {
_ = recover()
r := recover()
if s, ok := r.(string); r != nil && (!ok || s != ExpectedPanic) {
t.Fatalf("test paniced: %v", r)
}
if !hasCheckedError && test.checks.Err.hasErrorFile(test, mode) {
t.Fatalf("Unchecked error file %s", test.checks.Err.getPath(test, mode))
}
test.checks.Out.check(t, test, mode, testutils.RemoveInconsistentInfo(t, out.String()))
}()
cmd.Run(cmd, []string{})
Expand Down
9 changes: 6 additions & 3 deletions pkg/compare/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ func (rf ReferenceTemplate) Exec(params map[string]any) (*unstructured.Unstructu
content := buf.Bytes()
err = yaml.Unmarshal(bytes.ReplaceAll(content, []byte(noValue), []byte("")), &data)
if err != nil {
return nil, fmt.Errorf("template: %s isn't a yaml file after injection. yaml unmarshal error: %w. The Template After Execution: %s", rf.Name(), err, string(content))
return nil, fmt.Errorf(
"template: %s isn't a yaml file after injection. yaml unmarshal error: %w. The Template After Execution: %s",
rf.Name(), err, string(content),
)
}
return &unstructured.Unstructured{Object: data}, nil
}
Expand Down Expand Up @@ -272,13 +275,13 @@ func parseTemplates(templateReference []*ReferenceTemplate, functionTemplates []
temp.Template = parsedTemp
temp.metadata, err = temp.Exec(map[string]any{}) // Extract Metadata
if err != nil {
errs = append(errs, err)
errs = append(errs, fmt.Errorf("failed to parse template %s with empty data: %w", temp.Path, err))
}
err = temp.ValidateFieldsToOmit(ref.FieldsToOmit)
if err != nil {
errs = append(errs, err)
}
if temp.metadata.GetKind() == "" {
if temp.metadata != nil && temp.metadata.GetKind() == "" {
errs = append(errs, fmt.Errorf("template missing kind: %s", temp.Path))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: failed to constuct template: template: apps.v1.DaemonSet.kube-system.kindnet.yaml:5:61: executing "apps.v1.DaemonSet.kube-system.kindnet.yaml" at <len .spec.annotations>: error calling len: reflect: call of reflect.Value.Type on zero Value
error: failed to parse template apps.v1.DaemonSet.kube-system.kindnet.yaml with empty data: failed to constuct template: template: apps.v1.DaemonSet.kube-system.kindnet.yaml:5:61: executing "apps.v1.DaemonSet.kube-system.kindnet.yaml" at <len .spec.annotations>: error calling len: reflect: call of reflect.Value.Type on zero Value
error code:2
6 changes: 3 additions & 3 deletions pkg/compare/testdata/YAMLOutput/localout.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Diffs:
CorrelatedTemplate: deploymentDashboard.yaml
DiffOutput: "diff -u -N TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard
TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\n---
TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n+++ TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n@@ -14,7 +14,7 @@\n template:\n metadata:\n labels:\n- k8s-app:
kubernetes-dashboard\n+ k8s-app: kubernetes-dashboard-diff\n spec:\n
\ containers:\n - args:\n"
TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n+++ TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n@@ -14,7 +14,7 @@\n template:\n metadata:\n labels:\n-
\ k8s-app: kubernetes-dashboard\n+ k8s-app: kubernetes-dashboard-diff\n
\ spec:\n containers:\n - args:\n"
Summary:
NumDiffCRs: 1
NumMissing: 1
Expand Down

0 comments on commit b72ed4f

Please sign in to comment.