Skip to content

Commit

Permalink
Merge pull request #58 from yangcao77/integrateValidation
Browse files Browse the repository at this point in the history
validation consolidation
  • Loading branch information
mmulholla authored Feb 10, 2021
2 parents 59b15c7 + 0d751a2 commit 2b50898
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 318 deletions.
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
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

0 comments on commit 2b50898

Please sign in to comment.