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

validation consolidation #58

Merged
merged 6 commits into from
Feb 10, 2021
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
22 changes: 12 additions & 10 deletions devfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@ components:
strategy: Dockerfile
container:
endpoints:
- name: http-3000
targetPort: 3000
- name: http-8888
targetPort: 8888
image: registry.access.redhat.com/ubi8/nodejs-12:1-45
memoryLimit: 1024Mi
mountSources: true
sourceMapping: /project
command:
- npm install
- name: runtime3
attributes:
tool: odo
cli:
usage: deploy
container:
endpoints:
- name: http-3000
targetPort: 3000
- name: http-8080
targetPort: 8080
image: registry.access.redhat.com/ubi8/nodejs-12:1-45
memoryLimit: 1024Mi
mountSources: true
Expand All @@ -52,8 +54,8 @@ components:
tool: workspace-operator
container:
endpoints:
- name: http-3000
targetPort: 3000
- name: http-9090
targetPort: 9090
image: registry.access.redhat.com/ubi8/nodejs-12:1-45
memoryLimit: 1024Mi
mountSources: true
Expand All @@ -63,7 +65,7 @@ commands:
commandLine: npm install
component: runtime2
group:
isDefault: true
isDefault: false
kind: build
workingDir: /project
id: install2
Expand All @@ -74,7 +76,7 @@ commands:
commandLine: npm start
component: runtime2
group:
isDefault: true
isDefault: false
kind: run
workingDir: /project
id: run2
Expand All @@ -85,15 +87,15 @@ commands:
commandLine: npm run debug
component: runtime2
group:
isDefault: true
isDefault: false
kind: debug
workingDir: /project
id: debug2
- exec:
commandLine: npm test
component: runtime2
group:
isDefault: true
isDefault: false
kind: test
workingDir: /project
id: test2
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/devfile/library
go 1.13

require (
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7
github.com/fatih/color v1.7.0
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/gobwas/glob v0.2.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4 h1:jDzWFpF/BoyaemoPjAzw5SmlX172JsSsh+Frujm5Ww4=
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7 h1:bQGUVLEGQtVkvS94K4gQbu57Rk/npcZQmgORmCWYNy8=
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
Expand Down
20 changes: 16 additions & 4 deletions pkg/devfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package devfile

import (
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/validate"
)

// ParseFromURLAndValidate func parses the devfile data from the url
Expand All @@ -16,7 +17,11 @@ func ParseFromURLAndValidate(url string) (d parser.DevfileObj, err error) {
return d, err
}

// generic validation on devfile content - TODO
// generic validation on devfile content
err = validate.ValidateDevfileData(d.Data)
if err != nil {
return d, err
}

return d, err
}
Expand All @@ -31,8 +36,11 @@ func ParseFromDataAndValidate(data []byte) (d parser.DevfileObj, err error) {
if err != nil {
return d, err
}

// generic validation on devfile content - TODO
// generic validation on devfile content
err = validate.ValidateDevfileData(d.Data)
if err != nil {
return d, err
}

return d, err
}
Expand All @@ -49,7 +57,11 @@ func ParseAndValidate(path string) (d parser.DevfileObj, err error) {
return d, err
}

// generic validation on devfile content - TODO
// generic validation on devfile content
err = validate.ValidateDevfileData(d.Data)
if err != nil {
return d, err
}

return d, err
}
61 changes: 56 additions & 5 deletions pkg/devfile/validate/validate.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
package validate

import (
"k8s.io/klog"
"fmt"
"github.com/devfile/api/v2/pkg/validation"
devfileData "github.com/devfile/library/pkg/devfile/parser/data"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"strings"
)

// ValidateDevfileData validates whether sections of devfile are compatible
func ValidateDevfileData(data interface{}) error {
func ValidateDevfileData(data devfileData.DevfileData) error {

// Skipped
klog.V(4).Info("No validation present. Skipped for the moment.")
return nil
commands, err := data.GetCommands(common.DevfileOptions{})
if err != nil {
return err
}
components, err := data.GetComponents(common.DevfileOptions{})
if err != nil {
return err
}
projects, err := data.GetProjects(common.DevfileOptions{})
if err != nil {
return err
}
//starterProjects, err := data.GetStarterProjects(common.DevfileOptions{})
//if err != nil {
// return err
//}

var errstrings []string
// validate components
err = validation.ValidateComponents(components)
if err != nil {
errstrings = append(errstrings, err.Error())
}

// validate commands
err = validation.ValidateCommands(commands, components)
if err != nil {
errstrings = append(errstrings, err.Error())
}

err = validation.ValidateEvents(data.GetEvents(), commands)
if err != nil {
errstrings = append(errstrings, err.Error())
}

err = validation.ValidateProjects(projects)
if err != nil {
errstrings = append(errstrings, err.Error())
}

//err = validation.ValidateStarterProjects(starterProjects)
//if err != nil {
// errstrings = append(errstrings, err.Error())
//}

if len(errstrings) > 0 {
return fmt.Errorf(strings.Join(errstrings, "\n"))
} else {
return nil
}

}
36 changes: 15 additions & 21 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ Each test in ```parser_v200_verify_test.go``` sets values in a test structure wh
CommandTypes []schema.CommandType
ComponentTypes []schema.ComponentType
FileName string
CreateWithParser bool
EditContent bool
}

Expand All @@ -68,7 +67,6 @@ An example test:
func Test_MultiCommand(t *testing.T) {
testContent := TestContent{}
testContent.CommandTypes = []schema.CommandType{schema.ExecCommandType, schema.CompositeCommandType}
testContent.CreateWithParser = true
testContent.EditContent = true
testContent.FileName = GetDevFileName()
runTest(testContent, t)
Expand Down Expand Up @@ -128,14 +126,14 @@ For example add support for apply command to existing command support:

1. In ```command-test-utils.go```
* add functions:
* ```func setApplyCommandValues(applyCommand *schema.ApplyCommand)```
* randomly set attribute values in the provided apply command object
* ```func createApplyCommand() *schema.ApplyCommand```
* creates the apply command object and calls setApplyCommandValues to add attribute values
* ```func (devfile *TestDevfile) setApplyCommandValues(applyCommand *schema.Command)```
* randomly sets attribute values in the provided apply command object
* ```func (devfile *TestDevfile) createApplyCommand() *schema.ApplyCommand```
* creates an empty apply command object and adds it to parser and test schema data
* follow the implementation of other similar functions.
* modify:
* ```func generateCommand(command *schema.Command, genericCommand *GenericCommand)```
* add logic to call createApplyCommand if commandType indicates such.
* ```func (devfile *TestDevfile) AddCommand(commandType schema.CommandType) schema.Command```
* add logic to call createApplyCommand if commandType indicates such and call setApplyCommandValues
* ```func (devfile *TestDevfile) UpdateCommand(command *schema.Command) error```
* add logic to call setApplyCommandValues if commandType indicates such.
1. In ```parser_v200_verify_test.go```
Expand All @@ -156,24 +154,21 @@ Using existing support for commands as an illustration, any new property support
* Specific to commands
* Commands require support for 5 different command types:
* Exec
* Appy (to be implemented)
* Apply (to be implemented)
* Composite
* VSCodeLaunch (to be implemented)
* VSCodeTask (to be implemented)
* Each of these command-types have equivalent functions:
* ```func create<command-type>Command() *schema.<command-type>```
* ```func (devfile *TestDevfile) create<command-type>Command() *schema.Command```
* creates the command object and calls ```set<command-type>CommandValues``` to add attribute values
* for example see: ```func createExecCommand(execCommand *schema.ExecCommand)```
* ```func set<command-type>CommandValues(project-sourceProject *schema.<project-source>)```
* for example see: ```func (devfile *TestDevfile) createExecCommand() *schema.Command```
* ```func (devfile *TestDevfile) set<command-type>CommandValues(command *schema.Command)```
* sets random attributes into the provided object
* for example see: ```func setExecCommandValues(execCommand *schema.ExecCommand)```
* for example see: ```func (devfile *TestDevfile) setExecCommandValues(ommand *schema.Command)```
* Functions general to all commands
* ```func generateCommand(command *schema.Command, genericCommand *GenericCommand)```
* ```func addCommand(genericCommand *GenericCommand) schema.Command```
* includes logic to call the ```create<Command-Type>Command``` function for the command-Type of the supplied command object.
* ```func (devfile *TestDevfile) addCommand(commandType schema.CommandType) string```
* main entry point for a test to add a command
* maintains the array of commands in the schema structure
* calls generateCommand()
* ```func (devfile *TestDevfile) UpdateCommand(command *schema.Command) error```
* includes logic to call set<commad-type>CommandValues for each commandType.
* ```func (devfile TestDevfile) VerifyCommands(parserCommands []schema.Command) error```
Expand Down Expand Up @@ -218,10 +213,9 @@ Create, modify and verify an exec command:
1. parser_v200_verify_test.Test_ExecCommand
1. parser-v200-test.runTest
1. command-test-utils.AddCommand
1. command-test-utils.GenerateCommand
1. command-test-utils.createExecCommand
1. command-test-utils.setExecCommandValues
1. test-utils.CreateDevfile
1. command-test-utils.createExecCommand
1. command-test-utils.setExecCommandValues
1. test-utils.WriteDevfile
1. test-utils.EditCommands
1. command-test-utils.UpdateCommand
1. command-test-utils.setExecCommandValues
Expand Down
Loading