Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mock interface and avoid validating tokens #178

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 12 additions & 56 deletions pkg/devfile/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
"reflect"
"strings"

"github.com/devfile/library/v2/pkg/git"
"github.com/hashicorp/go-multierror"

"github.com/devfile/api/v2/pkg/attributes"
devfileCtx "github.com/devfile/library/v2/pkg/devfile/parser/context"
"github.com/devfile/library/v2/pkg/devfile/parser/data"
Expand All @@ -47,57 +44,6 @@
"github.com/pkg/errors"
)

// downloadGitRepoResources is exposed as a global variable for the purpose of running mock tests
var downloadGitRepoResources = func(url string, destDir string, httpTimeout *int, token string) error {
var returnedErr error

if util.IsGitProviderRepo(url) {
gitUrl, err := git.NewGitUrlWithURL(url)
if err != nil {
return err
}

if !gitUrl.IsFile || gitUrl.Revision == "" || !strings.Contains(gitUrl.Path, OutputDevfileYamlPath) {
return fmt.Errorf("error getting devfile from url: failed to retrieve %s", url)
}

stackDir, err := os.MkdirTemp("", fmt.Sprintf("git-resources"))
if err != nil {
return fmt.Errorf("failed to create dir: %s, error: %v", stackDir, err)
}

defer func(path string) {
err := os.RemoveAll(path)
if err != nil {
returnedErr = multierror.Append(returnedErr, err)
}
}(stackDir)

if !gitUrl.IsPublic(httpTimeout) {
err = gitUrl.SetToken(token, httpTimeout)
if err != nil {
returnedErr = multierror.Append(returnedErr, err)
return returnedErr
}
}

err = gitUrl.CloneGitRepo(stackDir)
if err != nil {
returnedErr = multierror.Append(returnedErr, err)
return returnedErr
}

dir := path.Dir(path.Join(stackDir, gitUrl.Path))
err = git.CopyAllDirFiles(dir, destDir)
if err != nil {
returnedErr = multierror.Append(returnedErr, err)
return returnedErr
}
}

return nil
}

// ParseDevfile func validates the devfile integrity.
// Creates devfile context and runtime objects
func parseDevfile(d DevfileObj, resolveCtx *resolutionContextTree, tool resolverTools, flattenedDevfile bool) (DevfileObj, error) {
Expand Down Expand Up @@ -134,7 +80,7 @@
// ParserArgs is the struct to pass into parser functions which contains required info for parsing devfile.
// It accepts devfile path, devfile URL or devfile content in []byte format.
type ParserArgs struct {
// Path is a relative or absolute devfile path.
// Path is a relative or absolute devfile path on disk
Path string
// URL is the URL address of the specific devfile.
URL string
Expand Down Expand Up @@ -170,6 +116,8 @@
ImageNamesAsSelector *ImageSelectorArgs
// DownloadGitResources downloads the resources from Git repository if true
DownloadGitResources *bool
// DevfileUtilsClient exposes the interface for mock implementation.
DevfileUtilsClient DevfileUtils
}

// ImageSelectorArgs defines the structure to leverage for using image names as selectors after parsing the Devfile.
Expand Down Expand Up @@ -217,6 +165,11 @@
d.Ctx.SetToken(args.Token)
}

args.DevfileUtilsClient = NewDevfileUtilsClient()
kim-tsao marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return d, fmt.Errorf("Error %v, URL cannot be parsed %s ", err, args.URL)
}

Check warning on line 171 in pkg/devfile/parser/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/devfile/parser/parse.go#L170-L171

Added lines #L170 - L171 were not covered by tests
kim-tsao marked this conversation as resolved.
Show resolved Hide resolved

downloadGitResources := true
if args.DownloadGitResources != nil {
downloadGitResources = *args.DownloadGitResources
Expand All @@ -229,6 +182,7 @@
k8sClient: args.K8sClient,
httpTimeout: args.HTTPTimeout,
downloadGitResources: downloadGitResources,
devfileUtilsClient: args.DevfileUtilsClient,
}

flattenedDevfile := true
Expand Down Expand Up @@ -284,6 +238,8 @@
httpTimeout *int
// downloadGitResources downloads the resources from Git repository if true
downloadGitResources bool
// devfileUtilsClient exposes the Git Interface to be able to use mock implementation.
devfileUtilsClient DevfileUtils
}

func populateAndParseDevfile(d DevfileObj, resolveCtx *resolutionContextTree, tool resolverTools, flattenedDevfile bool) (DevfileObj, error) {
Expand Down Expand Up @@ -535,7 +491,7 @@

if tool.downloadGitResources {
destDir := path.Dir(curDevfileCtx.GetAbsPath())
err = downloadGitRepoResources(newUri, destDir, tool.httpTimeout, token)
err = tool.devfileUtilsClient.DownloadGitRepoResources(newUri, destDir, token)
if err != nil {
return DevfileObj{}, err
}
Expand Down
Loading
Loading