Skip to content

Commit

Permalink
Update mock to test devfile with private parent
Browse files Browse the repository at this point in the history
Signed-off-by: Maysun J Faisal <maysunaneek@gmail.com>
  • Loading branch information
maysunfaisal committed Dec 5, 2023
1 parent 20a0c91 commit 782abcd
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pkg/devfile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4448,7 +4448,7 @@ func Test_parseFromURI_GitProviders(t *testing.T) {
tt.devfileUtilsClient.GitTestToken = tt.token
tt.devfileUtilsClient.MockGitURL = util.MockGitUrl(*tt.gitUrl)

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

// validate even if we want an error; check that no files are copied to destDir
validateGitResourceFunctions(t, tt.wantResources, tt.wantResourceContent, destDir)
Expand Down
6 changes: 3 additions & 3 deletions pkg/devfile/parser/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
URL: "http://" + serverIP,
},
fs: nil,
devfileUtilsClient: parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}},
devfileUtilsClient: &parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}},
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
wantServiceNames: []string{"service-sample", "service-sample-2"},
wantRouteNames: []string{"route-sample", "route-sample-2"},
Expand All @@ -212,7 +212,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
Token: "valid-token",
},
fs: nil,
devfileUtilsClient: parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}, GitTestToken: "valid-token"},
devfileUtilsClient: &parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}, GitTestToken: "valid-token"},
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
wantServiceNames: []string{"service-sample", "service-sample-2"},
wantRouteNames: []string{"route-sample", "route-sample-2"},
Expand All @@ -226,7 +226,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
Token: "invalid-token",
},
fs: nil,
devfileUtilsClient: parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}, GitTestToken: "invalid-token"},
devfileUtilsClient: &parserUtil.MockDevfileUtilsClient{DownloadOptions: util.MockDownloadOptions{MockFile: string(data)}, MockGitURL: util.MockGitUrl{Host: "http://github.com"}, GitTestToken: "invalid-token"},
wantErr: true,
},
}
Expand Down
48 changes: 40 additions & 8 deletions pkg/devfile/parser/util/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,66 @@ func NewMockDevfileUtilsClient() MockDevfileUtilsClient {
return MockDevfileUtilsClient{}
}

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

var mockGitUrl util.MockGitUrl

if gc.MockGitURL.Host != "" {
if util.IsGitProviderRepo(gc.MockGitURL.Host) {
mockGitUrl = gc.MockGitURL
mockGitUrl.Token = gc.GitTestToken
gc.MockGitURL.Token = gc.GitTestToken
}
} else if params.URL != "" {
// Not all clients have the ability to pass in mock data
// So we should be adaptable and use the function params
// and mock the output
if util.IsGitProviderRepo(params.URL) {
gc.MockGitURL.Host = params.URL
mockGitUrl = gc.MockGitURL
mockGitUrl.Token = params.Token
gc.MockGitURL.Token = params.Token
}
}

return mockGitUrl.DownloadInMemoryWithClient(params, httpClient, gc.DownloadOptions)
if gc.DownloadOptions.MockParent == nil {
gc.DownloadOptions.MockParent = &util.MockParent{}
}

file, err := gc.MockGitURL.DownloadInMemoryWithClient(params, httpClient, gc.DownloadOptions)

if gc.DownloadOptions.MockParent != nil && gc.DownloadOptions.MockParent.IsMainDevfileDownloaded && gc.DownloadOptions.MockParent.IsParentDevfileDownloaded {
// Since gc is a pointer, if both the main and parent devfiles are downloaded, reset the flag.
// So that other tests can use the Mock Parent Devfile download if required.
gc.DownloadOptions.MockParent.IsMainDevfileDownloaded = false
gc.DownloadOptions.MockParent.IsParentDevfileDownloaded = false
}

Check warning on line 75 in pkg/devfile/parser/util/mock.go

View check run for this annotation

Codecov / codecov/patch

pkg/devfile/parser/util/mock.go#L71-L75

Added lines #L71 - L75 were not covered by tests

if gc.MockGitURL.Host != "" && params.URL != "" {
// Since gc is a pointer, reset the mock data if both the URL and Host are present
gc.MockGitURL.Host = ""
gc.MockGitURL.Token = ""
}

return file, err
}

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

// if mock data is unavailable as certain clients cant provide mock data
// then adapt and create mock data from actual params
if gc.ParentURLAlias == "" {
gc.ParentURLAlias = url
gc.MockGitURL.IsFile = true
gc.MockGitURL.Revision = "main"
gc.MockGitURL.Path = OutputDevfileYamlPath
gc.MockGitURL.Host = "github.com"
gc.MockGitURL.Protocol = "https"
gc.MockGitURL.Owner = "devfile"
gc.MockGitURL.Repo = "library"
}

Check warning on line 99 in pkg/devfile/parser/util/mock.go

View check run for this annotation

Codecov / codecov/patch

pkg/devfile/parser/util/mock.go#L88-L99

Added lines #L88 - L99 were not covered by tests

if gc.GitTestToken == "" {
gc.GitTestToken = token
}

Check warning on line 103 in pkg/devfile/parser/util/mock.go

View check run for this annotation

Codecov / codecov/patch

pkg/devfile/parser/util/mock.go#L101-L103

Added lines #L101 - L103 were not covered by tests

//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
//skip the Git Provider check since it'll fail
if util.IsGitProviderRepo(gc.ParentURLAlias) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/devfile/parser/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,25 @@ func TestDownloadInMemoryClient(t *testing.T) {
},
{
name: "Case 6: Input url is valid with a mock client, dont use mock data during invocation",
client: MockDevfileUtilsClient{},
client: &MockDevfileUtilsClient{},
url: server.URL,
want: []byte{79, 75},
},
{
name: "Case 7: Input url is valid with a mock client and mock token",
client: MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, GitTestToken: "valid-token", DownloadOptions: util.MockDownloadOptions{MockFile: "OK"}},
client: &MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, GitTestToken: "valid-token", DownloadOptions: util.MockDownloadOptions{MockFile: "OK"}},
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
want: []byte{79, 75},
},
{
name: "Case 8: Public Github repo, with invalid token ",
client: MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, GitTestToken: "invalid-token"},
client: &MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, GitTestToken: "invalid-token"},
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
wantErr: "failed to retrieve https://github.com/devfile/library/blob/main/devfile.yaml",
},
{
name: "Case 9: Input github url is valid with a mock client, dont use mock data during invocation",
client: MockDevfileUtilsClient{},
client: &MockDevfileUtilsClient{},
url: "https://raw.githubusercontent.com/maysunfaisal/OK/main/OK.txt",
want: []byte{79, 75},
},
Expand Down
139 changes: 128 additions & 11 deletions pkg/util/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ type MockDownloadOptions struct {
MockDevfile bool
MockDockerfile bool
MockFile string
MockParent *MockParent
}

type MockParent struct {
IsMainDevfileDownloaded bool
IsParentDevfileDownloaded bool
}

func (m *MockGitUrl) GetToken() string {
Expand All @@ -57,6 +63,8 @@ var mockExecute = func(baseDir string, cmd CommandType, args ...string) ([]byte,
// private repository
if hasPassword {
switch password {
case "parent-devfile":
fallthrough

Check warning on line 67 in pkg/util/mock.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/mock.go#L66-L67

Added lines #L66 - L67 were not covered by tests
case "valid-token":
_, err := resourceFile.WriteString("private repo\n")
if err != nil {
Expand Down Expand Up @@ -147,18 +155,8 @@ metadata:
tags:
- Go
version: 1.0.0
starterProjects:
- name: go-starter
git:
checkoutFrom:
revision: main
remotes:
origin: https://github.com/devfile-samples/devfile-stack-go.git
components:
- container:
endpoints:
- name: http
targetPort: 8080
image: golang:latest
memoryLimit: 1024Mi
mountSources: true
Expand Down Expand Up @@ -190,15 +188,85 @@ commands:
commandLine: GOCACHE=/project/.cache go build main.go
component: runtime
group:
isDefault: true
kind: build
workingDir: /project
id: build
- exec:
commandLine: ./main
component: runtime
group:
kind: run
workingDir: /project
id: run
- id: build-image
apply:
component: image-build
- id: deployk8s
apply:
component: kubernetes-deploy
- id: deploy
composite:
commands:
- build-image
- deployk8s
group:
kind: deploy
isDefault: true
`

var mockDevfileWithParentRef = `
schemaVersion: 2.2.0
metadata:
displayName: Go Mock Runtime
icon: https://raw.githubusercontent.com/devfile-samples/devfile-stack-icons/main/golang.svg
language: go
name: go
projectType: go
tags:
- Go
version: 1.0.0
parent:
uri: https://github.com/private-url-devfile
components:
- container:
image: golang:latest
memoryLimit: 1024Mi
mountSources: true
sourceMapping: /project
name: runtime
- name: image-build
image:
imageName: go-image:latest
dockerfile:
uri: docker/Dockerfile
buildContext: .
rootRequired: false
- name: kubernetes-deploy
kubernetes:
inlined: |-
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
test: test
name: deploy-sample
endpoints:
- name: http-8081
targetPort: 8081
path: /
commands:
- exec:
commandLine: GOCACHE=/project/.cache go build main.go
component: runtime
group:
kind: build
workingDir: /project
id: build
- exec:
commandLine: ./main
component: runtime
group:
kind: run
workingDir: /project
id: run
Expand All @@ -218,6 +286,45 @@ commands:
isDefault: true
`

var mockParentDevfile = `
schemaVersion: 2.2.0
metadata:
displayName: Go Mock Parent
language: go
name: goparent
projectType: go
tags:
- Go
version: 1.0.0
components:
- container:
endpoints:
- name: http
targetPort: 8080
image: golang:latest
memoryLimit: 1024Mi
mountSources: true
sourceMapping: /project
name: runtime2
commands:
- exec:
commandLine: GOCACHE=/project/.cache go build main.go
component: runtime2
group:
isDefault: true
kind: build
workingDir: /project
id: build2
- exec:
commandLine: ./main
component: runtime2
group:
isDefault: true
kind: run
workingDir: /project
id: run2
`

var mockDockerfile = `
FROM python:slim
Expand Down Expand Up @@ -249,6 +356,16 @@ func (m MockGitUrl) DownloadInMemoryWithClient(params HTTPRequestParams, httpCli
default:
return []byte(mockDevfile), nil
}
} else if m.GetToken() == "parent-devfile" {
if options.MockParent != nil && !options.MockParent.IsMainDevfileDownloaded {
options.MockParent.IsMainDevfileDownloaded = true
return []byte(mockDevfileWithParentRef), nil
}

Check warning on line 363 in pkg/util/mock.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/mock.go#L359-L363

Added lines #L359 - L363 were not covered by tests

if options.MockParent != nil && !options.MockParent.IsParentDevfileDownloaded {
options.MockParent.IsParentDevfileDownloaded = true
return []byte(mockParentDevfile), nil
}

Check warning on line 368 in pkg/util/mock.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/mock.go#L365-L368

Added lines #L365 - L368 were not covered by tests
} else if m.GetToken() == "" {
// if no token is provided, assume normal operation
return DownloadInMemory(params)
Expand Down
2 changes: 2 additions & 0 deletions resource.file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
private repo
git switched

0 comments on commit 782abcd

Please sign in to comment.