Skip to content

Commit 1f0a90e

Browse files
committed
Add and update tests
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent ed4bbee commit 1f0a90e

20 files changed

+484
-184
lines changed

devfile.yaml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ metadata:
33
name: nodejs
44
version: 1.0.0
55
attributes:
6-
alpha.build-dockerfile: https://raw.githubusercontent.com/odo-devfiles/registry/master/devfiles/nodejs/build/Dockerfile
7-
alpha.deployment-manifest: https://raw.githubusercontent.com/odo-devfiles/registry/master/devfiles/nodejs/deploy/deployment-manifest.yaml
6+
alpha.build-dockerfile: /relative/path/to/Dockerfile
87
starterProjects:
98
- name: nodejs-starter
109
git:
@@ -13,8 +12,9 @@ starterProjects:
1312
components:
1413
- name: runtime
1514
attributes:
16-
first: a
17-
last: c
15+
tool: console-import
16+
import:
17+
strategy: Dockerfile
1818
container:
1919
endpoints:
2020
- name: http-3000
@@ -25,9 +25,9 @@ components:
2525
sourceMapping: /project
2626
- name: runtime2
2727
attributes:
28-
name:
29-
first: a
30-
middle: b
28+
tool: odo
29+
cli:
30+
usage: deploy
3131
container:
3232
endpoints:
3333
- name: http-3000
@@ -38,10 +38,7 @@ components:
3838
sourceMapping: /project
3939
- name: runtime3
4040
attributes:
41-
first: maysun
42-
names:
43-
first: a
44-
middle: b
41+
tool: workspace-operator
4542
container:
4643
endpoints:
4744
- name: http-3000
@@ -60,8 +57,8 @@ commands:
6057
workingDir: /project
6158
id: install
6259
attributes:
63-
first: a
64-
last: c
60+
tool: odo
61+
mandatory: false
6562
- exec:
6663
commandLine: npm start
6764
component: runtime
@@ -71,7 +68,8 @@ commands:
7168
workingDir: /project
7269
id: run
7370
attributes:
74-
first: a
71+
tool: odo
72+
mandatory: true
7573
- exec:
7674
commandLine: npm run debug
7775
component: runtime

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ require (
1717
github.com/stretchr/testify v1.6.1 // indirect
1818
github.com/xeipuuv/gojsonschema v1.2.0
1919
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f // indirect
20-
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e // indirect
21-
gopkg.in/yaml.v2 v2.3.0 // indirect
2220
k8s.io/api v0.18.6
2321
k8s.io/apimachinery v0.18.6
2422
k8s.io/klog v1.0.0
25-
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06 // indirect
2623
sigs.k8s.io/yaml v1.2.0
2724
)

go.sum

Lines changed: 4 additions & 70 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,35 @@ func main() {
2323

2424
compOptions := common.DevfileOptions{
2525
Filter: map[string]interface{}{
26-
"first": "a",
27-
"last": "c",
28-
// "middle": "b",
29-
// "name": map[string]interface{}{
30-
// "first": "a",
31-
// },
26+
"tool": "console-import",
27+
"import": map[string]interface{}{
28+
"strategy": "Dockerfile",
29+
},
3230
},
3331
}
3432

35-
for _, component := range devfile.Data.GetComponents(compOptions) {
33+
components, e := devfile.Data.GetComponents(compOptions)
34+
if e != nil {
35+
fmt.Printf("err: %v\n", err)
36+
}
37+
38+
for _, component := range components {
3639
if component.Container != nil {
3740
fmt.Printf("component container: %s\n", component.Name)
3841
}
3942
}
4043

41-
for _, command := range devfile.Data.GetCommands(compOptions) {
44+
cmdOptions := common.DevfileOptions{
45+
Filter: map[string]interface{}{
46+
"tool": "odo",
47+
},
48+
}
49+
50+
commands, e := devfile.Data.GetCommands(cmdOptions)
51+
if e != nil {
52+
fmt.Printf("err: %v\n", err)
53+
}
54+
for _, command := range commands {
4255
if command.Exec != nil {
4356
fmt.Printf("exec command kind: %s\n", command.Exec.Group.Kind)
4457
}

pkg/devfile/generator/generators.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ func GetObjectMeta(name, namespace string, labels, annotations map[string]string
5252
// GetContainers iterates through the devfile components and returns a slice of the corresponding containers
5353
func GetContainers(devfileObj parser.DevfileObj, options common.DevfileOptions) ([]corev1.Container, error) {
5454
var containers []corev1.Container
55-
for _, comp := range devfileObj.Data.GetDevfileContainerComponents(options) {
55+
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
56+
if err != nil {
57+
return nil, err
58+
}
59+
for _, comp := range containerComponents {
5660
envVars := convertEnvs(comp.Container.Env)
5761
resourceReqs := getResourceReqs(comp)
5862
ports := convertPorts(comp.Container.Endpoints)
@@ -72,7 +76,11 @@ func GetContainers(devfileObj parser.DevfileObj, options common.DevfileOptions)
7276
if comp.Container.MountSources == nil || *comp.Container.MountSources {
7377
syncRootFolder := addSyncRootFolder(container, comp.Container.SourceMapping)
7478

75-
err := addSyncFolder(container, syncRootFolder, devfileObj.Data.GetProjects(common.DevfileOptions{}))
79+
projects, err := devfileObj.Data.GetProjects(common.DevfileOptions{})
80+
if err != nil {
81+
return nil, err
82+
}
83+
err = addSyncFolder(container, syncRootFolder, projects)
7684
if err != nil {
7785
return nil, err
7886
}

pkg/devfile/generator/utils.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ func getDeploymentSpec(deploySpecParams deploymentSpecParams) *appsv1.Deployment
197197
func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]string) (*corev1.ServiceSpec, error) {
198198

199199
var containerPorts []corev1.ContainerPort
200-
portExposureMap := getPortExposure(devfileObj)
200+
portExposureMap, err := getPortExposure(devfileObj)
201+
if err != nil {
202+
return nil, err
203+
}
201204
containers, err := GetContainers(devfileObj, common.DevfileOptions{})
202205
if err != nil {
203206
return nil, err
@@ -239,9 +242,12 @@ func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]stri
239242

240243
// getPortExposure iterates through all endpoints and returns the highest exposure level of all TargetPort.
241244
// exposure level: public > internal > none
242-
func getPortExposure(devfileObj parser.DevfileObj) map[int]v1.EndpointExposure {
245+
func getPortExposure(devfileObj parser.DevfileObj) (map[int]v1.EndpointExposure, error) {
243246
portExposureMap := make(map[int]v1.EndpointExposure)
244-
containerComponents := devfileObj.Data.GetDevfileContainerComponents(common.DevfileOptions{})
247+
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(common.DevfileOptions{})
248+
if err != nil {
249+
return portExposureMap, err
250+
}
245251
for _, comp := range containerComponents {
246252
for _, endpoint := range comp.Container.Endpoints {
247253
// if exposure=public, no need to check for existence
@@ -258,7 +264,7 @@ func getPortExposure(devfileObj parser.DevfileObj) map[int]v1.EndpointExposure {
258264
}
259265

260266
}
261-
return portExposureMap
267+
return portExposureMap, nil
262268
}
263269

264270
// IngressSpecParams struct for function GenerateIngressSpec

pkg/devfile/generator/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ func TestGetPortExposure(t *testing.T) {
926926
Components: tt.containerComponents,
927927
},
928928
}
929-
mapCreated := getPortExposure(devObj)
929+
mapCreated, _ := getPortExposure(devObj)
930930
if !reflect.DeepEqual(mapCreated, tt.wantMap) {
931931
t.Errorf("Expected: %v, got %v", tt.wantMap, mapCreated)
932932
}

pkg/devfile/parser/configurables.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ func (d DevfileObj) SetMetadataName(name string) error {
2828

2929
// AddEnvVars adds environment variables to all the components in a devfile
3030
func (d DevfileObj) AddEnvVars(otherList []v1.EnvVar) error {
31-
components := d.Data.GetComponents(common.DevfileOptions{})
31+
components, err := d.Data.GetComponents(common.DevfileOptions{})
32+
if err != nil {
33+
return err
34+
}
3235
for _, component := range components {
3336
if component.Container != nil {
3437
component.Container.Env = Merge(component.Container.Env, otherList)
@@ -40,7 +43,10 @@ func (d DevfileObj) AddEnvVars(otherList []v1.EnvVar) error {
4043

4144
// RemoveEnvVars removes the environment variables which have the keys from all the components in a devfile
4245
func (d DevfileObj) RemoveEnvVars(keys []string) (err error) {
43-
components := d.Data.GetComponents(common.DevfileOptions{})
46+
components, err := d.Data.GetComponents(common.DevfileOptions{})
47+
if err != nil {
48+
return err
49+
}
4450
for _, component := range components {
4551
if component.Container != nil {
4652
component.Container.Env, err = RemoveEnvVarsFromList(component.Container.Env, keys)
@@ -55,7 +61,10 @@ func (d DevfileObj) RemoveEnvVars(keys []string) (err error) {
5561

5662
// SetPorts converts ports to endpoints, adds to a devfile
5763
func (d DevfileObj) SetPorts(ports ...string) error {
58-
components := d.Data.GetComponents(common.DevfileOptions{})
64+
components, err := d.Data.GetComponents(common.DevfileOptions{})
65+
if err != nil {
66+
return err
67+
}
5968
endpoints, err := portsToEndpoints(ports...)
6069
if err != nil {
6170
return err
@@ -71,7 +80,10 @@ func (d DevfileObj) SetPorts(ports ...string) error {
7180

7281
// RemovePorts removes all container endpoints from a devfile
7382
func (d DevfileObj) RemovePorts() error {
74-
components := d.Data.GetComponents(common.DevfileOptions{})
83+
components, err := d.Data.GetComponents(common.DevfileOptions{})
84+
if err != nil {
85+
return err
86+
}
7587
for _, component := range components {
7688
if component.Container != nil {
7789
component.Container.Endpoints = []v1.Endpoint{}
@@ -83,7 +95,10 @@ func (d DevfileObj) RemovePorts() error {
8395

8496
// HasPorts checks if a devfile contains container endpoints
8597
func (d DevfileObj) HasPorts() bool {
86-
components := d.Data.GetComponents(common.DevfileOptions{})
98+
components, err := d.Data.GetComponents(common.DevfileOptions{})
99+
if err != nil {
100+
return false
101+
}
87102
for _, component := range components {
88103
if component.Container != nil {
89104
if len(component.Container.Endpoints) > 0 {
@@ -96,7 +111,10 @@ func (d DevfileObj) HasPorts() bool {
96111

97112
// SetMemory sets memoryLimit in devfile container
98113
func (d DevfileObj) SetMemory(memory string) error {
99-
components := d.Data.GetComponents(common.DevfileOptions{})
114+
components, err := d.Data.GetComponents(common.DevfileOptions{})
115+
if err != nil {
116+
return err
117+
}
100118
for _, component := range components {
101119
if component.Container != nil {
102120
component.Container.MemoryLimit = memory
@@ -108,7 +126,10 @@ func (d DevfileObj) SetMemory(memory string) error {
108126

109127
// GetMemory gets memoryLimit from devfile container
110128
func (d DevfileObj) GetMemory() string {
111-
components := d.Data.GetComponents(common.DevfileOptions{})
129+
components, err := d.Data.GetComponents(common.DevfileOptions{})
130+
if err != nil {
131+
return ""
132+
}
112133
for _, component := range components {
113134
if component.Container != nil {
114135
if component.Container.MemoryLimit != "" {

pkg/devfile/parser/data/interface.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ type DevfileData interface {
2323
UpdateEvents(postStart, postStop, preStart, preStop []string)
2424

2525
// component related methods
26-
GetComponents(common.DevfileOptions) []v1.Component
26+
GetComponents(common.DevfileOptions) ([]v1.Component, error)
2727
AddComponents(components []v1.Component) error
2828
UpdateComponent(component v1.Component)
2929

3030
// project related methods
31-
GetProjects(common.DevfileOptions) []v1.Project
31+
GetProjects(common.DevfileOptions) ([]v1.Project, error)
3232
AddProjects(projects []v1.Project) error
3333
UpdateProject(project v1.Project)
3434

3535
// starter projects related commands
36-
GetStarterProjects(common.DevfileOptions) []v1.StarterProject
36+
GetStarterProjects(common.DevfileOptions) ([]v1.StarterProject, error)
3737
AddStarterProjects(projects []v1.StarterProject) error
3838
UpdateStarterProject(project v1.StarterProject)
3939

4040
// command related methods
41-
GetCommands(common.DevfileOptions) []v1.Command
41+
GetCommands(common.DevfileOptions) ([]v1.Command, error)
4242
AddCommands(commands ...v1.Command) error
4343
UpdateCommand(command v1.Command)
4444

@@ -48,6 +48,6 @@ type DevfileData interface {
4848
GetVolumeMountPath(name string) (string, error)
4949

5050
//utils
51-
GetDevfileContainerComponents(common.DevfileOptions) []v1.Component
52-
GetDevfileVolumeComponents(common.DevfileOptions) []v1.Component
51+
GetDevfileContainerComponents(common.DevfileOptions) ([]v1.Component, error)
52+
GetDevfileVolumeComponents(common.DevfileOptions) ([]v1.Component, error)
5353
}

pkg/devfile/parser/data/v2/commands.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,34 @@ import (
88
)
99

1010
// GetCommands returns the slice of Command objects parsed from the Devfile
11-
func (d *DevfileV2) GetCommands(options common.DevfileOptions) []v1.Command {
11+
func (d *DevfileV2) GetCommands(options common.DevfileOptions) ([]v1.Command, error) {
1212
if len(options.Filter) == 0 {
13-
return d.Commands
13+
return d.Commands, nil
1414
}
1515

1616
var commands []v1.Command
1717
for _, command := range d.Commands {
18-
filterIn, _ := common.FilterDevfileObject(command.Attributes, options)
18+
filterIn, err := common.FilterDevfileObject(command.Attributes, options)
19+
if err != nil {
20+
return nil, err
21+
}
1922

2023
if filterIn {
2124
command.Id = strings.ToLower(command.Id)
2225
commands = append(commands, command)
2326
}
2427
}
2528

26-
return commands
29+
return commands, nil
2730
}
2831

2932
// AddCommands adds the slice of Command objects to the Devfile's commands
3033
// if a command is already defined, error out
3134
func (d *DevfileV2) AddCommands(commands ...v1.Command) error {
32-
devfileCommands := d.GetCommands(common.DevfileOptions{})
35+
devfileCommands, err := d.GetCommands(common.DevfileOptions{})
36+
if err != nil {
37+
return err
38+
}
3339

3440
for _, command := range commands {
3541
for _, devfileCommand := range devfileCommands {

0 commit comments

Comments
 (0)