Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
fix multiple file check
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <iamshubhamgupta2001@gmail.com>
  • Loading branch information
shubham-cmyk committed Sep 12, 2023
1 parent 4eaeffa commit aec40b3
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func (s *Step) LoadYAML(file string) error {
return fmt.Errorf("failed to validate TestStep object from %s: %v", file, err)
}
// Check for YAML errors in the TestStep file
if err := checkYAMLError(file, true); err != nil {
if err := checkYAMLError(file); err != nil {
return fmt.Errorf("failed to check YAML in TestStep file %s: %v", file, err)
}
if s.Step != nil {
Expand Down Expand Up @@ -706,7 +706,7 @@ func validateTestStep(ts *harness.TestStep, baseDir string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("referenced file in Apply does not exist: %s", path)
}
if err := checkYAMLError(path, false); err != nil {
if err := checkYAMLError(path); err != nil {
return err
}
}
Expand All @@ -716,7 +716,7 @@ func validateTestStep(ts *harness.TestStep, baseDir string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("referenced file in Assert does not exist: %s", path)
}
if err := checkYAMLError(path, false); err != nil {
if err := checkYAMLError(path); err != nil {
return err
}
}
Expand All @@ -726,45 +726,58 @@ func validateTestStep(ts *harness.TestStep, baseDir string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("referenced file in Error does not exist: %s", path)
}
if err := checkYAMLError(path, false); err != nil {
if err := checkYAMLError(path); err != nil {
return err
}
}

return nil
}

func checkYAMLError(path string, isTestStepFile bool) error {
func checkYAMLError(path string) error {
content, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading file %s: %v", path, err)
}

var data map[string]interface{}
if err := yaml.UnmarshalStrict(content, &data); err != nil {
return fmt.Errorf("YAML indentation or structural error in file %s: %v", path, err)
}

// check for unknown field in TestStep file
if isTestStepFile {
allowedFields := map[string]bool{
"apply": true,
"assert": true,
"error": true,
"delete": true,
"index": true,
"commands": true,
"kubeconfig": true,
"unitTest": true,
"metadata": true,
"kind": true,
"apiVersion": true,
}
for key := range data {
if _, ok := allowedFields[key]; !ok {
return fmt.Errorf("unexpected field %s in TestStep file %s", key, path)
manifests := strings.Split(string(content), "---")
for _, manifest := range manifests {
manifest = strings.TrimSpace(manifest)
if manifest == "" {
continue
}

var data map[string]interface{}
if err := yaml.UnmarshalStrict([]byte(manifest), &data); err != nil {
return fmt.Errorf("YAML indentation or structural error in file %s: %v", path, err)
}

kind, ok := data["kind"].(string)
if !ok || kind == "" {
continue
}

if kind == "TestStep" {
allowedFields := map[string]bool{
"apply": true,
"assert": true,
"error": true,
"delete": true,
"index": true,
"commands": true,
"kubeconfig": true,
"unitTest": true,
"metadata": true,
"kind": true,
"apiVersion": true,
}
for key := range data {
if _, ok := allowedFields[key]; !ok {
return fmt.Errorf("unexpected field %s in TestStep manifest in file %s", key, path)
}
}
}
}

return nil
}

0 comments on commit aec40b3

Please sign in to comment.