From eefdbd323c456a6fad860aba9c9be9889765c9ef Mon Sep 17 00:00:00 2001 From: Jordan Dubrick Date: Thu, 22 Feb 2024 12:33:03 -0500 Subject: [PATCH] Add Functionality For Multiple Devfile Name Variations (#198) * add multiple name conventions for devfile check Signed-off-by: Jordan Dubrick * change order of sorting Signed-off-by: Jordan Dubrick --------- Signed-off-by: Jordan Dubrick --- pkg/devfile/parser/util/mock.go | 8 +++-- pkg/devfile/parser/util/utils.go | 18 +++++++--- pkg/devfile/parser/util/utils_test.go | 48 +++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 7 deletions(-) diff --git a/pkg/devfile/parser/util/mock.go b/pkg/devfile/parser/util/mock.go index ec6bd24f..3c1f042c 100644 --- a/pkg/devfile/parser/util/mock.go +++ b/pkg/devfile/parser/util/mock.go @@ -19,11 +19,15 @@ import ( "fmt" "net/http" "os" - "strings" "github.com/devfile/library/v2/pkg/util" ) +// Default filenames for create devfile to be used in mocks +const ( + OutputDevfileYamlPath = "devfile.yaml" +) + type MockDevfileUtilsClient struct { // Specify a valid git URL as an alias if using a localhost HTTP server in order to pass validation. ParentURLAlias string @@ -109,7 +113,7 @@ func (gc MockDevfileUtilsClient) DownloadGitRepoResources(url string, destDir st mockGitUrl := gc.MockGitURL mockGitUrl.Token = gc.GitTestToken - if !mockGitUrl.IsFile || mockGitUrl.Revision == "" || !strings.Contains(mockGitUrl.Path, OutputDevfileYamlPath) { + if !mockGitUrl.IsFile || mockGitUrl.Revision == "" || !ValidateDevfileExistence((mockGitUrl.Path)) { return fmt.Errorf("error getting devfile from url: failed to retrieve %s", url+"/"+mockGitUrl.Path) } diff --git a/pkg/devfile/parser/util/utils.go b/pkg/devfile/parser/util/utils.go index 4f1ada5e..cbd2769b 100644 --- a/pkg/devfile/parser/util/utils.go +++ b/pkg/devfile/parser/util/utils.go @@ -25,10 +25,8 @@ import ( "github.com/hashicorp/go-multierror" ) -// Default filenames for create devfile -const ( - OutputDevfileYamlPath = "devfile.yaml" -) +// Contains common naming conventions for devfiles to look for when downloading resources +var DevfilePossibilities = [...]string{"devfile.yaml", ".devfile.yaml", "devfile.yml", ".devfile.yml"} type DevfileUtilsClient struct { } @@ -52,7 +50,7 @@ func (c DevfileUtilsClient) DownloadGitRepoResources(url string, destDir string, return err } - if !gitUrl.IsFile || gitUrl.Revision == "" || !strings.Contains(gitUrl.Path, OutputDevfileYamlPath) { + if !gitUrl.IsFile || gitUrl.Revision == "" || !ValidateDevfileExistence((gitUrl.Path)) { return fmt.Errorf("error getting devfile from url: failed to retrieve %s", url) } @@ -88,3 +86,13 @@ func (c DevfileUtilsClient) DownloadGitRepoResources(url string, destDir string, return nil } + +// ValidateDevfileExistence verifies if any of the naming possibilities for devfile are present in the url path +func ValidateDevfileExistence(path string) bool { + for _, devfile := range DevfilePossibilities { + if strings.Contains(path, devfile) { + return true + } + } + return false +} diff --git a/pkg/devfile/parser/util/utils_test.go b/pkg/devfile/parser/util/utils_test.go index b69f0f3b..873f9d46 100644 --- a/pkg/devfile/parser/util/utils_test.go +++ b/pkg/devfile/parser/util/utils_test.go @@ -147,3 +147,51 @@ func TestDownloadInMemoryClient(t *testing.T) { }) } } + +func TestValidateDevfileExistence(t *testing.T) { + + tests := []struct { + name string + url string + wantErr bool + expectedValue bool + }{ + { + name: "recognizes devfile.yaml", + url: "https://dummyurlpath/devfile/registry/main/stacks/python/3.0.0/devfile.yaml", + wantErr: false, + expectedValue: true, + }, + { + name: "recognizes devfile.yml", + url: "https://dummyurlpath/devfile/registry/main/stacks/python/3.0.0/devfile.yml", + wantErr: false, + expectedValue: true, + }, + { + name: "recognizes .devfile.yaml", + url: "https://dummyurlpath/devfile/registry/main/stacks/python/3.0.0/.devfile.yaml", + wantErr: false, + expectedValue: true, + }, + { + name: "recognizes .devfile.yml", + url: "https://dummyurlpath/devfile/registry/main/stacks/python/3.0.0/.devfile.yml", + wantErr: false, + expectedValue: true, + }, + { + name: "no valid devfile in path", + url: "https://dummyurlpath/devfile/registry/main/stacks/python/3.0.0/deploy.yaml", + wantErr: true, + expectedValue: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + res := ValidateDevfileExistence(tt.url) + assert.EqualValues(t, tt.expectedValue, res, "expected res = %t, got %t", tt.expectedValue, res) + }) + } +}