Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions pkg/devfile/parser/util/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
18 changes: 13 additions & 5 deletions pkg/devfile/parser/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}
48 changes: 48 additions & 0 deletions pkg/devfile/parser/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}