Skip to content

Commit

Permalink
Use full filename and path to check for operatorFile and paramsFile (#…
Browse files Browse the repository at this point in the history
…1344)

* Use full filename and path to check for operatorFile and paramsFile instead of using only suffix
* Use only relative paths in package parser

Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
  • Loading branch information
ANeumann82 committed Feb 21, 2020
1 parent 9aa79df commit bbb21cd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pkg/kudoctl/packages/reader/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ func newPackageFiles() packages.Files {
}
}

// parsePackageFile parses the passed file into the correct type and adds it to the currentPackage
// The filePath needs to be relative to the package root
func parsePackageFile(filePath string, fileBytes []byte, currentPackage *packages.Files) error {
isOperatorFile := func(name string) bool {
return strings.HasSuffix(name, OperatorFileName)
dir, file := filepath.Split(name)
base := filepath.Base(dir)

return base == "." && file == OperatorFileName
}

isTemplateFile := func(name string) bool {
Expand All @@ -46,14 +51,20 @@ func parsePackageFile(filePath string, fileBytes []byte, currentPackage *package
}

isParametersFile := func(name string) bool {
return strings.HasSuffix(name, ParamsFileName)
dir, file := filepath.Split(name)
base := filepath.Base(dir)

return base == "." && file == ParamsFileName
}

switch {
case isOperatorFile(filePath):
if err := yaml.Unmarshal(fileBytes, &currentPackage.Operator); err != nil {
return fmt.Errorf("failed to unmarshal operator file: %w", err)
}
if currentPackage.Operator == nil {
return fmt.Errorf("failed to parse yaml into valid operator %s", filePath)
}
if currentPackage.Operator.APIVersion == "" {
currentPackage.Operator.APIVersion = APIVersion
}
Expand Down
72 changes: 72 additions & 0 deletions pkg/kudoctl/packages/reader/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package reader

import (
"errors"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

const (
validOperator = `
apiVersion: kudo.dev/v1beta1
name: "first-operator"
operatorVersion: "0.1.0"
`
validParams = `
apiVersion: kudo.dev/v1beta1
parameters:
- name: param
default: "value"
`
)

func TestParsePackageFile(t *testing.T) {
tests := []struct {
filePath string
fileContent string

isOperator bool
isParam bool
isTemplate bool

expectedError error
}{
{filePath: "operator.yaml", fileContent: validOperator, isOperator: true},
{filePath: "params.yaml", fileContent: validParams, isParam: true},
{filePath: "templates/pod-params.yaml", isTemplate: true},
{filePath: "templates/pod-operator.yaml", isTemplate: true},
{filePath: "templates/some-template.yaml", isTemplate: true},
{filePath: "operator.yaml", isOperator: true, expectedError: errors.New("failed to parse yaml into valid operator operator.yaml")},
}

for _, tt := range tests {
tt := tt

pf := newPackageFiles()

err := parsePackageFile(tt.filePath, []byte(tt.fileContent), &pf)

if tt.expectedError != nil {
assert.Equal(t, tt.expectedError.Error(), err.Error())
continue
} else {
assert.Nil(t, err)
}

if tt.isOperator {
assert.NotNil(t, pf.Operator, "%v was not parsed as an operator file", tt.filePath)
}
if tt.isParam {
assert.NotNil(t, pf.Params, "%v was not parsed as a param file", tt.filePath)
}
if tt.isTemplate {
assert.Equal(t, 1, len(pf.Templates), "%v was not parsed as a template file", tt.filePath)

fileName := strings.TrimPrefix(tt.filePath, "templates/")
assert.NotNil(t, pf.Templates[fileName], "%v was not stored in template map", tt.filePath)
}

}
}
6 changes: 6 additions & 0 deletions pkg/kudoctl/packages/reader/read_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ func FromDir(fs afero.Fs, packagePath string) (*packages.Files, error) {
return err
}

// Trim package path to use only package relative paths in parser
path, err = filepath.Rel(packagePath, path)
if err != nil {
return err
}

return parsePackageFile(path, buf, &result)
})
if err != nil {
Expand Down

0 comments on commit bbb21cd

Please sign in to comment.