Skip to content

Commit

Permalink
Merge pull request #192 from maysunfaisal/update-mock-2
Browse files Browse the repository at this point in the history
Update mock to test devfile with private parent
  • Loading branch information
maysunfaisal authored Dec 6, 2023
2 parents 20a0c91 + 10a737a commit 705f00d
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ main
# File created running tests
tests/**/tmp/
tests/v2/lib-test-coverage.*
*resource.file*


# Mac related files
Expand Down
33 changes: 29 additions & 4 deletions pkg/devfile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4260,6 +4260,7 @@ func Test_parseFromURI_GitProviders(t *testing.T) {
wantResources []string
wantResourceContent []byte
downloadGitResources bool
noMockData bool
}{
{
name: "private parent devfile",
Expand All @@ -4281,6 +4282,26 @@ func Test_parseFromURI_GitProviders(t *testing.T) {
wantResourceContent: []byte("private repo\ngit switched"),
downloadGitResources: true,
},
{
name: "private parent devfile without mock data",
url: validUrl,
devfileUtilsClient: parserUtil.MockDevfileUtilsClient{
DownloadOptions: util.MockDownloadOptions{
MockFile: minimalDevfileContent,
},
},
token: validToken,
importReference: v1.ImportReference{
ImportReferenceUnion: v1.ImportReferenceUnion{
Uri: "https://github.com/private-url-devfile",
},
},
wantDevFile: minimalDevfile,
wantResources: []string{"resource.file"},
wantResourceContent: []byte("private repo\ngit switched"),
downloadGitResources: true,
noMockData: true,
},
{
name: "public parent devfile",
url: validUrl,
Expand Down Expand Up @@ -4444,11 +4465,15 @@ func Test_parseFromURI_GitProviders(t *testing.T) {
t.Errorf("Unexpected err: %+v", err)
}

tt.devfileUtilsClient.ParentURLAlias = tt.url
tt.devfileUtilsClient.GitTestToken = tt.token
tt.devfileUtilsClient.MockGitURL = util.MockGitUrl(*tt.gitUrl)
if tt.noMockData {
curDevfileContext.SetToken(tt.token)
} else {
tt.devfileUtilsClient.ParentURLAlias = tt.url
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
}

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"
}

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

//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
42 changes: 31 additions & 11 deletions pkg/devfile/parser/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ func TestDownloadInMemoryClient(t *testing.T) {
devfileUtilsClient := NewDevfileUtilsClient()

tests := []struct {
name string
url string
token string
client DevfileUtils
want []byte
wantErr string
name string
url string
token string
client DevfileUtils
want []byte
wantParent []byte
wantErr string
}{
{
name: "Case 1: Input url is valid",
Expand Down Expand Up @@ -90,28 +91,36 @@ 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},
},
{
name: "Case 10: Test devfile with private parent",
client: &MockDevfileUtilsClient{},
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
token: "parent-devfile",
want: []byte(util.MockDevfileWithParentRef),
wantParent: []byte(util.MockParentDevfile),
},
}

for _, tt := range tests {
Expand All @@ -120,10 +129,21 @@ func TestDownloadInMemoryClient(t *testing.T) {
if (err != nil) != (tt.wantErr != "") {
t.Errorf("Failed to download file with error: %s", err)
} else if err == nil && !reflect.DeepEqual(data, tt.want) {
t.Errorf("Expected: %v, received: %v, difference at %v", tt.want, string(data[:]), pretty.Compare(tt.want, data))
t.Errorf("Expected: %v, received: %v, difference at %v", string(tt.want), string(data[:]), pretty.Compare(tt.want, data))
} else if err != nil {
assert.Regexp(t, tt.wantErr, err.Error(), "Error message should match")
}

if len(tt.wantParent) > 0 {
data, err := tt.client.DownloadInMemory(util.HTTPRequestParams{URL: tt.url, Token: tt.token})
if (err != nil) != (tt.wantErr != "") {
t.Errorf("Failed to download file with error: %s", err)
} else if err == nil && !reflect.DeepEqual(data, tt.wantParent) {
t.Errorf("Expected: %v, received: %v, difference at %v", string(tt.wantParent), string(data[:]), pretty.Compare(tt.wantParent, data))
} else if err != nil {
assert.Regexp(t, tt.wantErr, err.Error(), "Error message should match")
}
}
})
}
}
Loading

0 comments on commit 705f00d

Please sign in to comment.