Skip to content

Commit 2b50898

Browse files
authored
Merge pull request #58 from yangcao77/integrateValidation
validation consolidation
2 parents 59b15c7 + 0d751a2 commit 2b50898

9 files changed

+354
-318
lines changed

devfile.yaml

+12-10
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,23 @@ components:
2828
strategy: Dockerfile
2929
container:
3030
endpoints:
31-
- name: http-3000
32-
targetPort: 3000
31+
- name: http-8888
32+
targetPort: 8888
3333
image: registry.access.redhat.com/ubi8/nodejs-12:1-45
3434
memoryLimit: 1024Mi
3535
mountSources: true
3636
sourceMapping: /project
37+
command:
38+
- npm install
3739
- name: runtime3
3840
attributes:
3941
tool: odo
4042
cli:
4143
usage: deploy
4244
container:
4345
endpoints:
44-
- name: http-3000
45-
targetPort: 3000
46+
- name: http-8080
47+
targetPort: 8080
4648
image: registry.access.redhat.com/ubi8/nodejs-12:1-45
4749
memoryLimit: 1024Mi
4850
mountSources: true
@@ -52,8 +54,8 @@ components:
5254
tool: workspace-operator
5355
container:
5456
endpoints:
55-
- name: http-3000
56-
targetPort: 3000
57+
- name: http-9090
58+
targetPort: 9090
5759
image: registry.access.redhat.com/ubi8/nodejs-12:1-45
5860
memoryLimit: 1024Mi
5961
mountSources: true
@@ -63,7 +65,7 @@ commands:
6365
commandLine: npm install
6466
component: runtime2
6567
group:
66-
isDefault: true
68+
isDefault: false
6769
kind: build
6870
workingDir: /project
6971
id: install2
@@ -74,7 +76,7 @@ commands:
7476
commandLine: npm start
7577
component: runtime2
7678
group:
77-
isDefault: true
79+
isDefault: false
7880
kind: run
7981
workingDir: /project
8082
id: run2
@@ -85,15 +87,15 @@ commands:
8587
commandLine: npm run debug
8688
component: runtime2
8789
group:
88-
isDefault: true
90+
isDefault: false
8991
kind: debug
9092
workingDir: /project
9193
id: debug2
9294
- exec:
9395
commandLine: npm test
9496
component: runtime2
9597
group:
96-
isDefault: true
98+
isDefault: false
9799
kind: test
98100
workingDir: /project
99101
id: test2

pkg/devfile/parse.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package devfile
22

33
import (
44
"github.com/devfile/library/pkg/devfile/parser"
5+
"github.com/devfile/library/pkg/devfile/validate"
56
)
67

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

19-
// generic validation on devfile content - TODO
20+
// generic validation on devfile content
21+
err = validate.ValidateDevfileData(d.Data)
22+
if err != nil {
23+
return d, err
24+
}
2025

2126
return d, err
2227
}
@@ -31,8 +36,11 @@ func ParseFromDataAndValidate(data []byte) (d parser.DevfileObj, err error) {
3136
if err != nil {
3237
return d, err
3338
}
34-
35-
// generic validation on devfile content - TODO
39+
// generic validation on devfile content
40+
err = validate.ValidateDevfileData(d.Data)
41+
if err != nil {
42+
return d, err
43+
}
3644

3745
return d, err
3846
}
@@ -49,7 +57,11 @@ func ParseAndValidate(path string) (d parser.DevfileObj, err error) {
4957
return d, err
5058
}
5159

52-
// generic validation on devfile content - TODO
60+
// generic validation on devfile content
61+
err = validate.ValidateDevfileData(d.Data)
62+
if err != nil {
63+
return d, err
64+
}
5365

5466
return d, err
5567
}

pkg/devfile/validate/validate.go

+56-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
11
package validate
22

33
import (
4-
"k8s.io/klog"
4+
"fmt"
5+
"github.com/devfile/api/v2/pkg/validation"
6+
devfileData "github.com/devfile/library/pkg/devfile/parser/data"
7+
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
8+
"strings"
59
)
610

711
// ValidateDevfileData validates whether sections of devfile are compatible
8-
func ValidateDevfileData(data interface{}) error {
12+
func ValidateDevfileData(data devfileData.DevfileData) error {
913

10-
// Skipped
11-
klog.V(4).Info("No validation present. Skipped for the moment.")
12-
return nil
14+
commands, err := data.GetCommands(common.DevfileOptions{})
15+
if err != nil {
16+
return err
17+
}
18+
components, err := data.GetComponents(common.DevfileOptions{})
19+
if err != nil {
20+
return err
21+
}
22+
projects, err := data.GetProjects(common.DevfileOptions{})
23+
if err != nil {
24+
return err
25+
}
26+
//starterProjects, err := data.GetStarterProjects(common.DevfileOptions{})
27+
//if err != nil {
28+
// return err
29+
//}
30+
31+
var errstrings []string
32+
// validate components
33+
err = validation.ValidateComponents(components)
34+
if err != nil {
35+
errstrings = append(errstrings, err.Error())
36+
}
37+
38+
// validate commands
39+
err = validation.ValidateCommands(commands, components)
40+
if err != nil {
41+
errstrings = append(errstrings, err.Error())
42+
}
43+
44+
err = validation.ValidateEvents(data.GetEvents(), commands)
45+
if err != nil {
46+
errstrings = append(errstrings, err.Error())
47+
}
48+
49+
err = validation.ValidateProjects(projects)
50+
if err != nil {
51+
errstrings = append(errstrings, err.Error())
52+
}
53+
54+
//err = validation.ValidateStarterProjects(starterProjects)
55+
//if err != nil {
56+
// errstrings = append(errstrings, err.Error())
57+
//}
58+
59+
if len(errstrings) > 0 {
60+
return fmt.Errorf(strings.Join(errstrings, "\n"))
61+
} else {
62+
return nil
63+
}
1364

1465
}

tests/README.md

+15-21
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ Each test in ```parser_v200_verify_test.go``` sets values in a test structure wh
5151
CommandTypes []schema.CommandType
5252
ComponentTypes []schema.ComponentType
5353
FileName string
54-
CreateWithParser bool
5554
EditContent bool
5655
}
5756

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

129127
1. In ```command-test-utils.go```
130128
* add functions:
131-
* ```func setApplyCommandValues(applyCommand *schema.ApplyCommand)```
132-
* randomly set attribute values in the provided apply command object
133-
* ```func createApplyCommand() *schema.ApplyCommand```
134-
* creates the apply command object and calls setApplyCommandValues to add attribute values
129+
* ```func (devfile *TestDevfile) setApplyCommandValues(applyCommand *schema.Command)```
130+
* randomly sets attribute values in the provided apply command object
131+
* ```func (devfile *TestDevfile) createApplyCommand() *schema.ApplyCommand```
132+
* creates an empty apply command object and adds it to parser and test schema data
135133
* follow the implementation of other similar functions.
136134
* modify:
137-
* ```func generateCommand(command *schema.Command, genericCommand *GenericCommand)```
138-
* add logic to call createApplyCommand if commandType indicates such.
135+
* ```func (devfile *TestDevfile) AddCommand(commandType schema.CommandType) schema.Command```
136+
* add logic to call createApplyCommand if commandType indicates such and call setApplyCommandValues
139137
* ```func (devfile *TestDevfile) UpdateCommand(command *schema.Command) error```
140138
* add logic to call setApplyCommandValues if commandType indicates such.
141139
1. In ```parser_v200_verify_test.go```
@@ -156,24 +154,21 @@ Using existing support for commands as an illustration, any new property support
156154
* Specific to commands
157155
* Commands require support for 5 different command types:
158156
* Exec
159-
* Appy (to be implemented)
157+
* Apply (to be implemented)
160158
* Composite
161159
* VSCodeLaunch (to be implemented)
162160
* VSCodeTask (to be implemented)
163161
* Each of these command-types have equivalent functions:
164-
* ```func create<command-type>Command() *schema.<command-type>```
162+
* ```func (devfile *TestDevfile) create<command-type>Command() *schema.Command```
165163
* creates the command object and calls ```set<command-type>CommandValues``` to add attribute values
166-
* for example see: ```func createExecCommand(execCommand *schema.ExecCommand)```
167-
* ```func set<command-type>CommandValues(project-sourceProject *schema.<project-source>)```
164+
* for example see: ```func (devfile *TestDevfile) createExecCommand() *schema.Command```
165+
* ```func (devfile *TestDevfile) set<command-type>CommandValues(command *schema.Command)```
168166
* sets random attributes into the provided object
169-
* for example see: ```func setExecCommandValues(execCommand *schema.ExecCommand)```
167+
* for example see: ```func (devfile *TestDevfile) setExecCommandValues(ommand *schema.Command)```
170168
* Functions general to all commands
171-
* ```func generateCommand(command *schema.Command, genericCommand *GenericCommand)```
169+
* ```func addCommand(genericCommand *GenericCommand) schema.Command```
172170
* includes logic to call the ```create<Command-Type>Command``` function for the command-Type of the supplied command object.
173-
* ```func (devfile *TestDevfile) addCommand(commandType schema.CommandType) string```
174171
* main entry point for a test to add a command
175-
* maintains the array of commands in the schema structure
176-
* calls generateCommand()
177172
* ```func (devfile *TestDevfile) UpdateCommand(command *schema.Command) error```
178173
* includes logic to call set<commad-type>CommandValues for each commandType.
179174
* ```func (devfile TestDevfile) VerifyCommands(parserCommands []schema.Command) error```
@@ -218,10 +213,9 @@ Create, modify and verify an exec command:
218213
1. parser_v200_verify_test.Test_ExecCommand
219214
1. parser-v200-test.runTest
220215
1. command-test-utils.AddCommand
221-
1. command-test-utils.GenerateCommand
222-
1. command-test-utils.createExecCommand
223-
1. command-test-utils.setExecCommandValues
224-
1. test-utils.CreateDevfile
216+
1. command-test-utils.createExecCommand
217+
1. command-test-utils.setExecCommandValues
218+
1. test-utils.WriteDevfile
225219
1. test-utils.EditCommands
226220
1. command-test-utils.UpdateCommand
227221
1. command-test-utils.setExecCommandValues

0 commit comments

Comments
 (0)