Skip to content

Commit c36a9fa

Browse files
committed
Update mock to test devfile with private parent
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent 20a0c91 commit c36a9fa

File tree

6 files changed

+177
-27
lines changed

6 files changed

+177
-27
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ main
2424
# File created running tests
2525
tests/**/tmp/
2626
tests/v2/lib-test-coverage.*
27+
*resource.file*
2728

2829

2930
# Mac related files

pkg/devfile/parser/parse_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4448,7 +4448,7 @@ func Test_parseFromURI_GitProviders(t *testing.T) {
44484448
tt.devfileUtilsClient.GitTestToken = tt.token
44494449
tt.devfileUtilsClient.MockGitURL = util.MockGitUrl(*tt.gitUrl)
44504450

4451-
got, err := parseFromURI(tt.importReference, curDevfileContext, &resolutionContextTree{}, resolverTools{downloadGitResources: tt.downloadGitResources, devfileUtilsClient: tt.devfileUtilsClient})
4451+
got, err := parseFromURI(tt.importReference, curDevfileContext, &resolutionContextTree{}, resolverTools{downloadGitResources: tt.downloadGitResources, devfileUtilsClient: &tt.devfileUtilsClient})
44524452

44534453
// validate even if we want an error; check that no files are copied to destDir
44544454
validateGitResourceFunctions(t, tt.wantResources, tt.wantResourceContent, destDir)

pkg/devfile/parser/reader_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
198198
URL: "http://" + serverIP,
199199
},
200200
fs: nil,
201-
devfileUtilsClient: parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}},
201+
devfileUtilsClient: &parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}},
202202
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
203203
wantServiceNames: []string{"service-sample", "service-sample-2"},
204204
wantRouteNames: []string{"route-sample", "route-sample-2"},
@@ -212,7 +212,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
212212
Token: "valid-token",
213213
},
214214
fs: nil,
215-
devfileUtilsClient: parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}, GitTestToken: "valid-token"},
215+
devfileUtilsClient: &parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}, GitTestToken: "valid-token"},
216216
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
217217
wantServiceNames: []string{"service-sample", "service-sample-2"},
218218
wantRouteNames: []string{"route-sample", "route-sample-2"},
@@ -226,7 +226,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
226226
Token: "invalid-token",
227227
},
228228
fs: nil,
229-
devfileUtilsClient: parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}, GitTestToken: "invalid-token"},
229+
devfileUtilsClient: &parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}, GitTestToken: "invalid-token"},
230230
wantErr: true,
231231
},
232232
}

pkg/devfile/parser/util/mock.go

+40-8
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,66 @@ func NewMockDevfileUtilsClient() MockDevfileUtilsClient {
4242
return MockDevfileUtilsClient{}
4343
}
4444

45-
func (gc MockDevfileUtilsClient) DownloadInMemory(params util.HTTPRequestParams) ([]byte, error) {
45+
func (gc *MockDevfileUtilsClient) DownloadInMemory(params util.HTTPRequestParams) ([]byte, error) {
4646
var httpClient = &http.Client{Transport: &http.Transport{
4747
ResponseHeaderTimeout: util.HTTPRequestResponseTimeout,
4848
}, Timeout: util.HTTPRequestResponseTimeout}
4949

50-
var mockGitUrl util.MockGitUrl
51-
5250
if gc.MockGitURL.Host != "" {
5351
if util.IsGitProviderRepo(gc.MockGitURL.Host) {
54-
mockGitUrl = gc.MockGitURL
55-
mockGitUrl.Token = gc.GitTestToken
52+
gc.MockGitURL.Token = gc.GitTestToken
5653
}
5754
} else if params.URL != "" {
5855
// Not all clients have the ability to pass in mock data
5956
// So we should be adaptable and use the function params
6057
// and mock the output
6158
if util.IsGitProviderRepo(params.URL) {
6259
gc.MockGitURL.Host = params.URL
63-
mockGitUrl = gc.MockGitURL
64-
mockGitUrl.Token = params.Token
60+
gc.MockGitURL.Token = params.Token
6561
}
6662
}
6763

68-
return mockGitUrl.DownloadInMemoryWithClient(params, httpClient, gc.DownloadOptions)
64+
if gc.DownloadOptions.MockParent == nil {
65+
gc.DownloadOptions.MockParent = &util.MockParent{}
66+
}
67+
68+
file, err := gc.MockGitURL.DownloadInMemoryWithClient(params, httpClient, gc.DownloadOptions)
69+
70+
if gc.DownloadOptions.MockParent != nil && gc.DownloadOptions.MockParent.IsMainDevfileDownloaded && gc.DownloadOptions.MockParent.IsParentDevfileDownloaded {
71+
// Since gc is a pointer, if both the main and parent devfiles are downloaded, reset the flag.
72+
// So that other tests can use the Mock Parent Devfile download if required.
73+
gc.DownloadOptions.MockParent.IsMainDevfileDownloaded = false
74+
gc.DownloadOptions.MockParent.IsParentDevfileDownloaded = false
75+
}
76+
77+
if gc.MockGitURL.Host != "" && params.URL != "" {
78+
// Since gc is a pointer, reset the mock data if both the URL and Host are present
79+
gc.MockGitURL.Host = ""
80+
gc.MockGitURL.Token = ""
81+
}
82+
83+
return file, err
6984
}
7085

7186
func (gc MockDevfileUtilsClient) DownloadGitRepoResources(url string, destDir string, token string) error {
7287

88+
// if mock data is unavailable as certain clients cant provide mock data
89+
// then adapt and create mock data from actual params
90+
if gc.ParentURLAlias == "" {
91+
gc.ParentURLAlias = url
92+
gc.MockGitURL.IsFile = true
93+
gc.MockGitURL.Revision = "main"
94+
gc.MockGitURL.Path = OutputDevfileYamlPath
95+
gc.MockGitURL.Host = "github.com"
96+
gc.MockGitURL.Protocol = "https"
97+
gc.MockGitURL.Owner = "devfile"
98+
gc.MockGitURL.Repo = "library"
99+
}
100+
101+
if gc.GitTestToken == "" {
102+
gc.GitTestToken = token
103+
}
104+
73105
//the url parameter that gets passed in will be the localhost IP of the test server, so it will fail all the validation checks. We will use the global testURL variable instead
74106
//skip the Git Provider check since it'll fail
75107
if util.IsGitProviderRepo(gc.ParentURLAlias) {

pkg/devfile/parser/util/utils_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,25 @@ func TestDownloadInMemoryClient(t *testing.T) {
9090
},
9191
{
9292
name: "Case 6: Input url is valid with a mock client, dont use mock data during invocation",
93-
client: MockDevfileUtilsClient{},
93+
client: &MockDevfileUtilsClient{},
9494
url: server.URL,
9595
want: []byte{79, 75},
9696
},
9797
{
9898
name: "Case 7: Input url is valid with a mock client and mock token",
99-
client: MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, GitTestToken: "valid-token", DownloadOptions: util.MockDownloadOptions{MockFile: "OK"}},
99+
client: &MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, GitTestToken: "valid-token", DownloadOptions: util.MockDownloadOptions{MockFile: "OK"}},
100100
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
101101
want: []byte{79, 75},
102102
},
103103
{
104104
name: "Case 8: Public Github repo, with invalid token ",
105-
client: MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, GitTestToken: "invalid-token"},
105+
client: &MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, GitTestToken: "invalid-token"},
106106
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
107107
wantErr: "failed to retrieve https://github.com/devfile/library/blob/main/devfile.yaml",
108108
},
109109
{
110110
name: "Case 9: Input github url is valid with a mock client, dont use mock data during invocation",
111-
client: MockDevfileUtilsClient{},
111+
client: &MockDevfileUtilsClient{},
112112
url: "https://raw.githubusercontent.com/maysunfaisal/OK/main/OK.txt",
113113
want: []byte{79, 75},
114114
},

pkg/util/mock.go

+128-11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ type MockDownloadOptions struct {
3737
MockDevfile bool
3838
MockDockerfile bool
3939
MockFile string
40+
MockParent *MockParent
41+
}
42+
43+
type MockParent struct {
44+
IsMainDevfileDownloaded bool
45+
IsParentDevfileDownloaded bool
4046
}
4147

4248
func (m *MockGitUrl) GetToken() string {
@@ -57,6 +63,8 @@ var mockExecute = func(baseDir string, cmd CommandType, args ...string) ([]byte,
5763
// private repository
5864
if hasPassword {
5965
switch password {
66+
case "parent-devfile":
67+
fallthrough
6068
case "valid-token":
6169
_, err := resourceFile.WriteString("private repo\n")
6270
if err != nil {
@@ -147,18 +155,8 @@ metadata:
147155
tags:
148156
- Go
149157
version: 1.0.0
150-
starterProjects:
151-
- name: go-starter
152-
git:
153-
checkoutFrom:
154-
revision: main
155-
remotes:
156-
origin: https://github.com/devfile-samples/devfile-stack-go.git
157158
components:
158159
- container:
159-
endpoints:
160-
- name: http
161-
targetPort: 8080
162160
image: golang:latest
163161
memoryLimit: 1024Mi
164162
mountSources: true
@@ -190,15 +188,85 @@ commands:
190188
commandLine: GOCACHE=/project/.cache go build main.go
191189
component: runtime
192190
group:
193-
isDefault: true
194191
kind: build
195192
workingDir: /project
196193
id: build
197194
- exec:
198195
commandLine: ./main
199196
component: runtime
200197
group:
198+
kind: run
199+
workingDir: /project
200+
id: run
201+
- id: build-image
202+
apply:
203+
component: image-build
204+
- id: deployk8s
205+
apply:
206+
component: kubernetes-deploy
207+
- id: deploy
208+
composite:
209+
commands:
210+
- build-image
211+
- deployk8s
212+
group:
213+
kind: deploy
201214
isDefault: true
215+
`
216+
217+
var mockDevfileWithParentRef = `
218+
schemaVersion: 2.2.0
219+
metadata:
220+
displayName: Go Mock Runtime
221+
icon: https://raw.githubusercontent.com/devfile-samples/devfile-stack-icons/main/golang.svg
222+
language: go
223+
name: go
224+
projectType: go
225+
tags:
226+
- Go
227+
version: 1.0.0
228+
parent:
229+
uri: https://github.com/private-url-devfile
230+
components:
231+
- container:
232+
image: golang:latest
233+
memoryLimit: 1024Mi
234+
mountSources: true
235+
sourceMapping: /project
236+
name: runtime
237+
- name: image-build
238+
image:
239+
imageName: go-image:latest
240+
dockerfile:
241+
uri: docker/Dockerfile
242+
buildContext: .
243+
rootRequired: false
244+
- name: kubernetes-deploy
245+
kubernetes:
246+
inlined: |-
247+
apiVersion: apps/v1
248+
kind: Deployment
249+
metadata:
250+
creationTimestamp: null
251+
labels:
252+
test: test
253+
name: deploy-sample
254+
endpoints:
255+
- name: http-8081
256+
targetPort: 8081
257+
path: /
258+
commands:
259+
- exec:
260+
commandLine: GOCACHE=/project/.cache go build main.go
261+
component: runtime
262+
group:
263+
kind: build
264+
workingDir: /project
265+
id: build
266+
- exec:
267+
commandLine: ./main
268+
component: runtime
269+
group:
202270
kind: run
203271
workingDir: /project
204272
id: run
@@ -218,6 +286,45 @@ commands:
218286
isDefault: true
219287
`
220288

289+
var mockParentDevfile = `
290+
schemaVersion: 2.2.0
291+
metadata:
292+
displayName: Go Mock Parent
293+
language: go
294+
name: goparent
295+
projectType: go
296+
tags:
297+
- Go
298+
version: 1.0.0
299+
components:
300+
- container:
301+
endpoints:
302+
- name: http
303+
targetPort: 8080
304+
image: golang:latest
305+
memoryLimit: 1024Mi
306+
mountSources: true
307+
sourceMapping: /project
308+
name: runtime2
309+
commands:
310+
- exec:
311+
commandLine: GOCACHE=/project/.cache go build main.go
312+
component: runtime2
313+
group:
314+
isDefault: true
315+
kind: build
316+
workingDir: /project
317+
id: build2
318+
- exec:
319+
commandLine: ./main
320+
component: runtime2
321+
group:
322+
isDefault: true
323+
kind: run
324+
workingDir: /project
325+
id: run2
326+
`
327+
221328
var mockDockerfile = `
222329
FROM python:slim
223330
@@ -249,6 +356,16 @@ func (m MockGitUrl) DownloadInMemoryWithClient(params HTTPRequestParams, httpCli
249356
default:
250357
return []byte(mockDevfile), nil
251358
}
359+
} else if m.GetToken() == "parent-devfile" {
360+
if options.MockParent != nil && !options.MockParent.IsMainDevfileDownloaded {
361+
options.MockParent.IsMainDevfileDownloaded = true
362+
return []byte(mockDevfileWithParentRef), nil
363+
}
364+
365+
if options.MockParent != nil && !options.MockParent.IsParentDevfileDownloaded {
366+
options.MockParent.IsParentDevfileDownloaded = true
367+
return []byte(mockParentDevfile), nil
368+
}
252369
} else if m.GetToken() == "" {
253370
// if no token is provided, assume normal operation
254371
return DownloadInMemory(params)

0 commit comments

Comments
 (0)