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

Commit

Permalink
fix changes
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 13, 2023
1 parent 4225418 commit 85fc4c2
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 62 deletions.
61 changes: 0 additions & 61 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"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"
Expand Down Expand Up @@ -568,10 +566,6 @@ func (s *Step) LoadYAML(file string) error {
if err := validateTestStep(testStep, s.Dir); err != nil {
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); err != nil {
return fmt.Errorf("failed to check YAML in TestStep file %s: %v", file, err)
}
if s.Step != nil {
return fmt.Errorf("more than 1 TestStep not allowed in step %q", s.Name)
}
Expand Down Expand Up @@ -707,75 +701,20 @@ 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); err != nil {
return err
}
}
// Check if referenced files in Assert exist
for _, assert := range ts.Assert {
path := filepath.Join(baseDir, assert)
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("referenced file in Assert does not exist: %s", path)
}
if err := checkYAMLError(path); err != nil {
return err
}
}
// Check if referenced files in Error exist
for _, errorPath := range ts.Error {
path := filepath.Join(baseDir, errorPath)
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("referenced file in Error does not exist: %s", path)
}
if err := checkYAMLError(path); err != nil {
return err
}
}

return nil
}

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

decoder := yaml.NewDecoder(strings.NewReader(string(content)))
decoder.SetStrict(true)
for {
var data map[string]interface{}
if err := decoder.Decode(&data); err == io.EOF {
break
} else if 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
Expand Down
2 changes: 1 addition & 1 deletion pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func ConvertUnstructured(in client.Object) (client.Object, error) {
return in, nil
}

err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct, converted)
err = runtime.DefaultUnstructuredConverter.FromUnstructuredWithValidation(unstruct, converted, true)
if err != nil {
return nil, fmt.Errorf("error converting %s from unstructured error: %w", ResourceID(in), err)
}
Expand Down

0 comments on commit 85fc4c2

Please sign in to comment.