Skip to content

Commit

Permalink
FIX gosec errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kispaljr committed Apr 27, 2023
1 parent 776f866 commit 82d5b1d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 58 deletions.
6 changes: 3 additions & 3 deletions krm-functions/dnn-fn/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
var theScheme *runtime.Scheme = runtime.NewScheme()

func init() {
nephioreqv1alpha1.AddToScheme(theScheme)
infrav1alpha1.AddToScheme(theScheme)
ipamv1alpha1.AddToScheme(theScheme)
_ = nephioreqv1alpha1.AddToScheme(theScheme)
_ = infrav1alpha1.AddToScheme(theScheme)
_ = ipamv1alpha1.AddToScheme(theScheme)
}

// Type constraint for checking if *T is runtime.Object
Expand Down
166 changes: 111 additions & 55 deletions krm-functions/dnn-fn/testlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"errors"
"os"
"path/filepath"
"sort"
Expand All @@ -28,73 +29,88 @@ import (
"sigs.k8s.io/yaml"
)

func ParseResourceListFromDir(t *testing.T, dir string) *fn.ResourceList {
files, err := os.ReadDir(dir)
// NOTE: functions in this file are candidates to be eventually merged to
// github.com/GoogleContainerTools/kpt-functions-sdk/go/fn/testhelpers

// RunFailureCases provides the test infra to run test similar to testhelpers.RunGoldenTests,
// but instead of checking the whole output of the KRM function, RunFailureCases is able to
// check:
// - if the main method of the KRM function (`Run`) returned with an error
// - if the `Results` field of the output contained the expected Results
//
// - "basedir" should be the parent directory, under where the sub-directories contains test data.
// For example, the "testdata" is the basedir. It contains two cases "test1" and "test2"
//
// └── testdata
// └── test1
// ├── _expected_error.txt
// ├── _fnconfig.yaml
// └── resources.yaml
// └── test2
// ├── _expected_results.yaml
// ├── _fnconfig.yaml
// └── resources.yaml
//
// if `_expected_error.txt` is present in the testdir, then the KRM function is expected to return with
// an error whose message contains the string in the file
// if `_expected_results.yaml` is present in the testdir: it should contain a list of fn.Result objects (serialized in YAML),
// that are checked against the actual Results list of the KRM functions output
//
// - "krmFunction" should be your ResourceListProcessor implementation.
func RunFailureCases(t *testing.T, basedir string, krmFunction fn.ResourceListProcessor) {
dirEntries, err := os.ReadDir(basedir)
if err != nil {
t.Fatalf("failed to read directory %q: %v", dir, err)
t.Fatalf("ReadDir(%q) failed: %v", basedir, err)
}
sort.Slice(files, func(i, j int) bool { return files[i].Name() < files[j].Name() })
var items fn.KubeObjects
for _, f := range files {
if strings.HasPrefix(f.Name(), "_") {
continue
}
// Users can put other types of files to the test dir, but they won't be read.
// A default kpt package contains README file.
if !testhelpers.IsValidYAMLOrKptfile(f.Name()) {

for _, dirEntry := range dirEntries {
dir := filepath.Join(basedir, dirEntry.Name())
if !dirEntry.IsDir() {
t.Errorf("expected directory, found %s", dir)
continue
}
content := testhelpers.MustReadFile(t, filepath.Join(dir, f.Name()))
fileItems, err := fn.ParseKubeObjects(content)
if err != nil {
t.Fatalf("failed to parse objects from file %q: %v", filepath.Join(dir, f.Name()), err)
}
items = append(items, fileItems...)
}

var functionConfig *fn.KubeObject = fn.NewEmptyKubeObject()
// config := mustParseFile(t, filepath.Join(dir, "_fnconfig.yaml"))
// if len(config) == 0 {
// functionConfig = fn.NewEmptyKubeObject()
// } else if len(config) == 1 {
// functionConfig = config[0]
// } else {
// t.Fatalf("found multiple config objects in %s", filepath.Join(dir, "_fnconfig.yaml"))
// }
return &fn.ResourceList{Items: items, FunctionConfig: functionConfig}
t.Run(dir, func(t *testing.T) {
rl := ParseResourceListFromDir(t, dir)
_, processError := krmFunction.Process(rl)

CheckRunError(t, dir, processError)
CheckResults(t, dir, rl)
})
}
}

func CheckRunError(t *testing.T, expectedErrorFile string, actualError error) {
content, err := os.ReadFile(expectedErrorFile)
func CheckRunError(t *testing.T, dir string, actualError error) {
content, err := os.ReadFile(filepath.Clean(filepath.Join(dir, "_expected_error.txt")))
if err != nil {
if os.IsNotExist(err) {
// if no expected errors are set: handle KRM function errors normally
if actualError != nil {
t.Errorf("the KRM function failed unexpectedly: %v", actualError)
t.Fatalf("the KRM function failed unexpectedly: %v", actualError)
}
return
} else {
t.Fatalf("couldn't read file %q: %v", expectedErrorFile, err)
t.Fatalf("couldn't read file %v/_expected_error.txt: %v", dir, err)
}
}

expectedError := strings.TrimSpace(string(content))
if actualError == nil {
t.Errorf("the KRM function hasn't returned any errors, but it should have failed with this error message: %v", expectedError)
t.Fatalf("the KRM function hasn't returned any errors, but it should have failed with this error message: %v", expectedError)
}
if !strings.Contains(actualError.Error(), expectedError) {
t.Errorf("the KRM function returned with the wrong error message.\n expected error: %v\n actual error: %v", expectedError, actualError)
t.Fatalf("the KRM function returned with the wrong error message.\n expected error: %v\n actual error: %v", expectedError, actualError)
}
}

func CheckResults(t *testing.T, expectedResultsFile string, rl *fn.ResourceList) {
resultsBytes, err := os.ReadFile(expectedResultsFile)
func CheckResults(t *testing.T, dir string, rl *fn.ResourceList) {
resultsBytes, err := os.ReadFile(filepath.Clean(filepath.Join(dir, "_expected_results.yaml")))
if err != nil {
if os.IsNotExist(err) {
// if no expected results are set: then pass the test
return
} else {
t.Fatalf("couldn't read file %q: %v", expectedResultsFile, err)
t.Fatalf("couldn't read file %v/_expected_results.yaml: %v", dir, err)
}
}
var expectedResults fn.Results
Expand All @@ -112,33 +128,73 @@ func CheckResults(t *testing.T, expectedResultsFile string, rl *fn.ResourceList)

}
if !found {
t.Errorf("missing %q result with this message: %v", er.Severity, er.Message)
t.Fatalf("missing %q result with this message: %v", er.Severity, er.Message)
}
}
}

func RunFailureCases(t *testing.T, basedir string, krmFunction fn.ResourceListProcessor) {
dirEntries, err := os.ReadDir(basedir)
// MustParseKubeObjects reads a list of KubeObjects from the given YAML file or fails the test
func MustParseKubeObjects(t *testing.T, path string) fn.KubeObjects {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return nil
}
b := testhelpers.MustReadFile(t, path)

objects, err := fn.ParseKubeObjects(b)
if err != nil {
t.Fatalf("ReadDir(%q) failed: %v", basedir, err)
t.Fatalf("failed to parse objects from file %q: %v", path, err)
}
return objects
}

for _, dirEntry := range dirEntries {
dir := filepath.Join(basedir, dirEntry.Name())
if !dirEntry.IsDir() {
t.Fatalf("expected directory, found %s", dir)
// MustParseKubeObject reads one KubeObject from the given YAML file or fails the test
func MustParseKubeObject(t *testing.T, path string) *fn.KubeObject {
b := testhelpers.MustReadFile(t, path)
object, err := fn.ParseKubeObject(b)
if err != nil {
t.Fatalf("failed to parse object from file %q: %v", path, err)
}
return object
}

// ParseResourceListFromDir parses all YAML files from the given `dir`,
// and gives back the parsed objects in a ResourceList, or fails the test
func ParseResourceListFromDir(t *testing.T, dir string) *fn.ResourceList {
files, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("failed to read directory %q: %v", dir, err)
}
sort.Slice(files, func(i, j int) bool { return files[i].Name() < files[j].Name() })
var items fn.KubeObjects
for _, f := range files {
if strings.HasPrefix(f.Name(), "_") {
continue
}
// Users can put other types of files to the test dir, but they won't be read.
// A default kpt package contains README file.
if !testhelpers.IsValidYAMLOrKptfile(f.Name()) {
continue
}
fileItems := MustParseKubeObjects(t, filepath.Join(dir, f.Name()))
items = append(items, fileItems...)
}

t.Run(dir, func(t *testing.T) {
rl := ParseResourceListFromDir(t, dir)
_, processError := krmFunction.Process(rl)
var functionConfig *fn.KubeObject
config := MustParseKubeObjects(t, filepath.Join(dir, "_fnconfig.yaml"))
if len(config) == 0 {
functionConfig = fn.NewEmptyKubeObject()
} else if len(config) == 1 {
functionConfig = config[0]
} else {
t.Fatalf("found multiple config objects in %s", filepath.Join(dir, "_fnconfig.yaml"))
}
return &fn.ResourceList{Items: items, FunctionConfig: functionConfig}
}

p := filepath.Join(dir, "_expected_error.txt")
CheckRunError(t, p, processError)
// InsertBeforeExtension inserts the string `toInsert` into a filepath right before the extension
func InsertBeforeExtension(origPath string, toInsert string) string {
ext := filepath.Ext(origPath)
base, _ := strings.CutSuffix(origPath, ext)
return base + toInsert + ext

p = filepath.Join(dir, "_expected_results.yaml")
CheckResults(t, p, rl)
})
}
}

0 comments on commit 82d5b1d

Please sign in to comment.