Skip to content

Commit 705f00d

Browse files
authored
Merge pull request #192 from maysunfaisal/update-mock-2
Update mock to test devfile with private parent
2 parents 20a0c91 + 10a737a commit 705f00d

File tree

6 files changed

+232
-37
lines changed

6 files changed

+232
-37
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

+29-4
Original file line numberDiff line numberDiff line change
@@ -4260,6 +4260,7 @@ func Test_parseFromURI_GitProviders(t *testing.T) {
42604260
wantResources []string
42614261
wantResourceContent []byte
42624262
downloadGitResources bool
4263+
noMockData bool
42634264
}{
42644265
{
42654266
name: "private parent devfile",
@@ -4281,6 +4282,26 @@ func Test_parseFromURI_GitProviders(t *testing.T) {
42814282
wantResourceContent: []byte("private repo\ngit switched"),
42824283
downloadGitResources: true,
42834284
},
4285+
{
4286+
name: "private parent devfile without mock data",
4287+
url: validUrl,
4288+
devfileUtilsClient: parserUtil.MockDevfileUtilsClient{
4289+
DownloadOptions: util.MockDownloadOptions{
4290+
MockFile: minimalDevfileContent,
4291+
},
4292+
},
4293+
token: validToken,
4294+
importReference: v1.ImportReference{
4295+
ImportReferenceUnion: v1.ImportReferenceUnion{
4296+
Uri: "https://github.com/private-url-devfile",
4297+
},
4298+
},
4299+
wantDevFile: minimalDevfile,
4300+
wantResources: []string{"resource.file"},
4301+
wantResourceContent: []byte("private repo\ngit switched"),
4302+
downloadGitResources: true,
4303+
noMockData: true,
4304+
},
42844305
{
42854306
name: "public parent devfile",
42864307
url: validUrl,
@@ -4444,11 +4465,15 @@ func Test_parseFromURI_GitProviders(t *testing.T) {
44444465
t.Errorf("Unexpected err: %+v", err)
44454466
}
44464467

4447-
tt.devfileUtilsClient.ParentURLAlias = tt.url
4448-
tt.devfileUtilsClient.GitTestToken = tt.token
4449-
tt.devfileUtilsClient.MockGitURL = util.MockGitUrl(*tt.gitUrl)
4468+
if tt.noMockData {
4469+
curDevfileContext.SetToken(tt.token)
4470+
} else {
4471+
tt.devfileUtilsClient.ParentURLAlias = tt.url
4472+
tt.devfileUtilsClient.GitTestToken = tt.token
4473+
tt.devfileUtilsClient.MockGitURL = util.MockGitUrl(*tt.gitUrl)
4474+
}
44504475

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

44534478
// validate even if we want an error; check that no files are copied to destDir
44544479
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

+31-11
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ func TestDownloadInMemoryClient(t *testing.T) {
4848
devfileUtilsClient := NewDevfileUtilsClient()
4949

5050
tests := []struct {
51-
name string
52-
url string
53-
token string
54-
client DevfileUtils
55-
want []byte
56-
wantErr string
51+
name string
52+
url string
53+
token string
54+
client DevfileUtils
55+
want []byte
56+
wantParent []byte
57+
wantErr string
5758
}{
5859
{
5960
name: "Case 1: Input url is valid",
@@ -90,28 +91,36 @@ func TestDownloadInMemoryClient(t *testing.T) {
9091
},
9192
{
9293
name: "Case 6: Input url is valid with a mock client, dont use mock data during invocation",
93-
client: MockDevfileUtilsClient{},
94+
client: &MockDevfileUtilsClient{},
9495
url: server.URL,
9596
want: []byte{79, 75},
9697
},
9798
{
9899
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"}},
100+
client: &MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, GitTestToken: "valid-token", DownloadOptions: util.MockDownloadOptions{MockFile: "OK"}},
100101
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
101102
want: []byte{79, 75},
102103
},
103104
{
104105
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"},
106+
client: &MockDevfileUtilsClient{MockGitURL: util.MockGitUrl{Host: "https://github.com/devfile/library/blob/main/devfile.yaml"}, GitTestToken: "invalid-token"},
106107
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
107108
wantErr: "failed to retrieve https://github.com/devfile/library/blob/main/devfile.yaml",
108109
},
109110
{
110111
name: "Case 9: Input github url is valid with a mock client, dont use mock data during invocation",
111-
client: MockDevfileUtilsClient{},
112+
client: &MockDevfileUtilsClient{},
112113
url: "https://raw.githubusercontent.com/maysunfaisal/OK/main/OK.txt",
113114
want: []byte{79, 75},
114115
},
116+
{
117+
name: "Case 10: Test devfile with private parent",
118+
client: &MockDevfileUtilsClient{},
119+
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
120+
token: "parent-devfile",
121+
want: []byte(util.MockDevfileWithParentRef),
122+
wantParent: []byte(util.MockParentDevfile),
123+
},
115124
}
116125

117126
for _, tt := range tests {
@@ -120,10 +129,21 @@ func TestDownloadInMemoryClient(t *testing.T) {
120129
if (err != nil) != (tt.wantErr != "") {
121130
t.Errorf("Failed to download file with error: %s", err)
122131
} else if err == nil && !reflect.DeepEqual(data, tt.want) {
123-
t.Errorf("Expected: %v, received: %v, difference at %v", tt.want, string(data[:]), pretty.Compare(tt.want, data))
132+
t.Errorf("Expected: %v, received: %v, difference at %v", string(tt.want), string(data[:]), pretty.Compare(tt.want, data))
124133
} else if err != nil {
125134
assert.Regexp(t, tt.wantErr, err.Error(), "Error message should match")
126135
}
136+
137+
if len(tt.wantParent) > 0 {
138+
data, err := tt.client.DownloadInMemory(util.HTTPRequestParams{URL: tt.url, Token: tt.token})
139+
if (err != nil) != (tt.wantErr != "") {
140+
t.Errorf("Failed to download file with error: %s", err)
141+
} else if err == nil && !reflect.DeepEqual(data, tt.wantParent) {
142+
t.Errorf("Expected: %v, received: %v, difference at %v", string(tt.wantParent), string(data[:]), pretty.Compare(tt.wantParent, data))
143+
} else if err != nil {
144+
assert.Regexp(t, tt.wantErr, err.Error(), "Error message should match")
145+
}
146+
}
127147
})
128148
}
129149
}

0 commit comments

Comments
 (0)