forked from redhat-developer/odo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devfile.go
189 lines (150 loc) · 5.59 KB
/
devfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package testingutil
import (
"github.com/openshift/odo/pkg/devfile/parser/data/common"
versionsCommon "github.com/openshift/odo/pkg/devfile/parser/data/common"
)
// TestDevfileData is a convenience data type used to mock up a devfile configuration
type TestDevfileData struct {
Components []versionsCommon.DevfileComponent
ExecCommands []versionsCommon.Exec
CompositeCommands []versionsCommon.Composite
Commands []versionsCommon.DevfileCommand
Events common.DevfileEvents
}
// GetComponents is a mock function to get the components from a devfile
func (d TestDevfileData) GetComponents() []versionsCommon.DevfileComponent {
return d.Components
}
// GetMetadata is a mock function to get metadata from devfile
func (d TestDevfileData) GetMetadata() versionsCommon.DevfileMetadata {
return versionsCommon.DevfileMetadata{}
}
// GetEvents is a mock function to get events from devfile
func (d TestDevfileData) GetEvents() versionsCommon.DevfileEvents {
return d.Events
}
// GetParent is a mock function to get parent from devfile
func (d TestDevfileData) GetParent() versionsCommon.DevfileParent {
return versionsCommon.DevfileParent{}
}
// GetAliasedComponents is a mock function to get the components that have an alias from a devfile
func (d TestDevfileData) GetAliasedComponents() []versionsCommon.DevfileComponent {
var aliasedComponents = []common.DevfileComponent{}
for _, comp := range d.Components {
if comp.Container != nil {
if comp.Container.Name != "" {
aliasedComponents = append(aliasedComponents, comp)
}
}
}
return aliasedComponents
}
// GetProjects is a mock function to get the components that have an alias from a devfile
func (d TestDevfileData) GetProjects() []versionsCommon.DevfileProject {
projectName := [...]string{"test-project", "anotherproject"}
clonePath := [...]string{"/test-project", "/anotherproject"}
sourceLocation := [...]string{"https://github.com/someproject/test-project.git", "https://github.com/another/project.git"}
project1 := versionsCommon.DevfileProject{
ClonePath: clonePath[0],
Name: projectName[0],
Git: &versionsCommon.Git{
Location: sourceLocation[0],
},
}
project2 := versionsCommon.DevfileProject{
ClonePath: clonePath[1],
Name: projectName[1],
Git: &versionsCommon.Git{
Location: sourceLocation[1],
},
}
return []versionsCommon.DevfileProject{project1, project2}
}
// GetCommands is a mock function to get the commands from a devfile
func (d TestDevfileData) GetCommands() []versionsCommon.DevfileCommand {
if d.Commands == nil {
var commands []versionsCommon.DevfileCommand
for i := range d.ExecCommands {
commands = append(commands, versionsCommon.DevfileCommand{Exec: &d.ExecCommands[i]})
}
for i := range d.CompositeCommands {
commands = append(commands, versionsCommon.DevfileCommand{Composite: &d.CompositeCommands[i]})
}
return commands
} else {
return d.Commands
}
}
// Validate is a mock validation that always validates without error
func (d TestDevfileData) Validate() error {
return nil
}
// SetMetadata sets metadata for devfile
func (d TestDevfileData) SetMetadata(name, version string) {}
// SetSchemaVersion sets schema version for devfile
func (d TestDevfileData) SetSchemaVersion(version string) {}
func (d TestDevfileData) AddComponents(components []common.DevfileComponent) error { return nil }
func (d TestDevfileData) UpdateComponent(component common.DevfileComponent) {}
func (d TestDevfileData) AddCommands(commands []common.DevfileCommand) error { return nil }
func (d TestDevfileData) UpdateCommand(command common.DevfileCommand) {}
func (d TestDevfileData) SetEvents(events common.DevfileEvents) {}
func (d TestDevfileData) AddProjects(projects []common.DevfileProject) error { return nil }
func (d TestDevfileData) UpdateProject(project common.DevfileProject) {}
func (d TestDevfileData) AddEvents(events common.DevfileEvents) error { return nil }
func (d TestDevfileData) UpdateEvents(postStart, postStop, preStart, preStop []string) {}
func (d TestDevfileData) SetParent(parent common.DevfileParent) {}
// GetFakeContainerComponent returns a fake container component for testing
func GetFakeContainerComponent(name string) versionsCommon.DevfileComponent {
image := "docker.io/maven:latest"
memoryLimit := "128Mi"
volumeName := "myvolume1"
volumePath := "/my/volume/mount/path1"
return versionsCommon.DevfileComponent{
Container: &versionsCommon.Container{
Name: name,
Image: image,
Env: []versionsCommon.Env{},
MemoryLimit: memoryLimit,
VolumeMounts: []versionsCommon.VolumeMount{{
Name: volumeName,
Path: volumePath,
}},
MountSources: true,
}}
}
// GetFakeVolumeComponent returns a fake volume component for testing
func GetFakeVolumeComponent(name string) versionsCommon.DevfileComponent {
size := "4Gi"
return versionsCommon.DevfileComponent{
Volume: &versionsCommon.Volume{
Name: name,
Size: size,
}}
}
// GetFakeExecRunCommands returns fake commands for testing
func GetFakeExecRunCommands() []versionsCommon.Exec {
return []versionsCommon.Exec{
{
CommandLine: "ls -a",
Component: "alias1",
Group: &versionsCommon.Group{
Kind: versionsCommon.RunCommandGroupType,
},
WorkingDir: "/root",
},
}
}
// GetFakeExecRunCommands returns a fake env for testing
func GetFakeEnv(name, value string) versionsCommon.Env {
return versionsCommon.Env{
Name: name,
Value: value,
}
}
// GetFakeVolumeMount returns a fake volume mount for testing
func GetFakeVolumeMount(name, path string) versionsCommon.VolumeMount {
return versionsCommon.VolumeMount{
Name: name,
Path: path,
}
}