Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use full filename and path to check for operatorFile and paramsFile #1344

Merged
merged 3 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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