From 1755e5d50eea4906b364c7de5d183d99ad6790b0 Mon Sep 17 00:00:00 2001 From: AdiAkhileshSingh15 Date: Sat, 12 Aug 2023 00:56:04 +0530 Subject: [PATCH] validation for yaml format Signed-off-by: AdiAkhileshSingh15 --- pkg/test/step.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/test/step.go b/pkg/test/step.go index 49bacf18..b4f7268d 100644 --- a/pkg/test/step.go +++ b/pkg/test/step.go @@ -11,6 +11,8 @@ import ( "testing" "time" + "gopkg.in/yaml.v2" + k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -738,5 +740,27 @@ func validateFieldFile(file string, dir string) error { return fmt.Errorf("the file %s specified in the field does not exist", completeFile) } + // Check if the file is in yaml format + err = validateYAMLFormat(completeFile) + if err != nil { + return fmt.Errorf("unable to validate yaml format and indentation for file %s", completeFile) + } + + return nil +} + +// validateYAMLFormat checks if the yaml file is in correct format and indentation +func validateYAMLFormat(file string) error { + data, err := os.ReadFile(file) + if err != nil { + return fmt.Errorf("failed to read file %s: %w", file, err) + } + + var temp []client.Object + err = yaml.Unmarshal(data, &temp) + if err != nil { + return fmt.Errorf("failed to parse yaml file %s: %w", file, err) + } + return nil }