-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathdevfile.go
238 lines (212 loc) · 6.6 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// Copyright Red Hat
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testingutil
import (
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/attributes"
)
var (
isFalse = false
isTrue = true
)
// GetFakeContainerComponent returns a fake container component for testing.
// Deprecated: use GenerateDummyContainerComponent instead
func GetFakeContainerComponent(name string) v1.Component {
volumeName := "myvolume1"
volumePath := "/my/volume/mount/path1"
VolumeMounts := []v1.VolumeMount{
{
Name: volumeName,
Path: volumePath,
},
}
return GenerateDummyContainerComponent(name, VolumeMounts, nil, []v1.EnvVar{}, v1.Annotation{}, nil)
}
// GetFakeVolumeComponent returns a fake volume component for testing
func GetFakeVolumeComponent(name, size string) v1.Component {
return v1.Component{
Name: name,
ComponentUnion: v1.ComponentUnion{
Volume: &v1.VolumeComponent{
Volume: v1.Volume{
Size: size,
},
},
},
}
}
// GetFakeExecRunCommands returns fake commands for testing
func GetFakeExecRunCommands() []v1.ExecCommand {
return []v1.ExecCommand{
{
CommandLine: "ls -a",
Component: "alias1",
LabeledCommand: v1.LabeledCommand{
BaseCommand: v1.BaseCommand{
Group: &v1.CommandGroup{
Kind: v1.RunCommandGroupKind,
},
},
},
WorkingDir: "/root",
},
}
}
// GetFakeEnv returns a fake env for testing
func GetFakeEnv(name, value string) v1.EnvVar {
return v1.EnvVar{
Name: name,
Value: value,
}
}
// GetFakeEnvParentOverride returns a fake envParentOverride for testing
func GetFakeEnvParentOverride(name, value string) v1.EnvVarParentOverride {
return v1.EnvVarParentOverride{
Name: name,
Value: value,
}
}
// GetFakeVolumeMount returns a fake volume mount for testing
func GetFakeVolumeMount(name, path string) v1.VolumeMount {
return v1.VolumeMount{
Name: name,
Path: path,
}
}
// GetFakeVolumeMountParentOverride returns a fake volumeMountParentOverride for testing
func GetFakeVolumeMountParentOverride(name, path string) v1.VolumeMountParentOverride {
return v1.VolumeMountParentOverride{
Name: name,
Path: path,
}
}
// GenerateDummyContainerComponent returns a dummy container component for testing
func GenerateDummyContainerComponent(name string, volMounts []v1.VolumeMount, endpoints []v1.Endpoint, envs []v1.EnvVar, annotation v1.Annotation, dedicatedPod *bool) v1.Component {
image := "docker.io/maven:latest"
mountSources := true
return v1.Component{
Name: name,
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{
Container: v1.Container{
Image: image,
Annotation: &annotation,
Env: envs,
VolumeMounts: volMounts,
MountSources: &mountSources,
DedicatedPod: dedicatedPod,
},
Endpoints: endpoints,
}}}
}
// DockerImageValues struct can be used to set override or main component struct values
type DockerImageValues struct {
//maps to Image.ImageName
ImageName string
//maps to Image.Dockerfile.DockerfileSrc.Uri
Uri string
//maps to Image.Dockerfile.BuildContext
BuildContext string
//maps to Image.Dockerfile.RootRequired
RootRequired *bool
}
// GetDockerImageTestComponent returns a docker image component that is used for testing.
// The parameters allow customization of the content. If they are set to nil, then the properties will not be set
func GetDockerImageTestComponent(div DockerImageValues, autobuild *bool, attr attributes.Attributes) v1.Component {
comp := v1.Component{
Name: "image",
ComponentUnion: v1.ComponentUnion{
Image: &v1.ImageComponent{
Image: v1.Image{
ImageName: div.ImageName,
ImageUnion: v1.ImageUnion{
AutoBuild: autobuild,
Dockerfile: &v1.DockerfileImage{
DockerfileSrc: v1.DockerfileSrc{
Uri: div.Uri,
},
Dockerfile: v1.Dockerfile{
BuildContext: div.BuildContext,
},
},
},
},
},
},
}
if div.RootRequired != nil {
comp.Image.Dockerfile.RootRequired = div.RootRequired
}
if attr != nil {
comp.Attributes = attr
}
return comp
}
// GetDockerImageTestComponentParentOverride returns a docker image parent override component that is used for testing.
// The parameters allow customization of the content. If they are set to nil, then the properties will not be set
func GetDockerImageTestComponentParentOverride(div DockerImageValues) v1.ComponentParentOverride {
comp := v1.ComponentParentOverride{
Name: "image",
ComponentUnionParentOverride: v1.ComponentUnionParentOverride{
Image: &v1.ImageComponentParentOverride{
ImageParentOverride: v1.ImageParentOverride{
ImageName: div.ImageName,
ImageUnionParentOverride: v1.ImageUnionParentOverride{
Dockerfile: &v1.DockerfileImageParentOverride{
DockerfileSrcParentOverride: v1.DockerfileSrcParentOverride{
Uri: div.Uri,
},
DockerfileParentOverride: v1.DockerfileParentOverride{
BuildContext: div.BuildContext,
},
},
},
},
},
},
}
if div.RootRequired != nil {
comp.Image.Dockerfile.RootRequired = div.RootRequired
}
return comp
}
// GetDockerImageTestComponentPluginOverride returns a docker image parent override component that is used for testing.
// The parameters allow customization of the content. If they are set to nil, then the properties will not be set
func GetDockerImageTestComponentPluginOverride(div DockerImageValues) v1.ComponentPluginOverride {
comp := v1.ComponentPluginOverride{
Name: "image",
ComponentUnionPluginOverride: v1.ComponentUnionPluginOverride{
Image: &v1.ImageComponentPluginOverride{
ImagePluginOverride: v1.ImagePluginOverride{
ImageName: div.ImageName,
ImageUnionPluginOverride: v1.ImageUnionPluginOverride{
Dockerfile: &v1.DockerfileImagePluginOverride{
DockerfileSrcPluginOverride: v1.DockerfileSrcPluginOverride{
Uri: div.Uri,
},
DockerfilePluginOverride: v1.DockerfilePluginOverride{
BuildContext: div.BuildContext,
},
},
},
},
},
},
}
if div.RootRequired != nil {
comp.Image.Dockerfile.RootRequired = div.RootRequired
}
return comp
}