Skip to content

Commit

Permalink
add multiple name conventions for devfile check
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Dubrick <[email protected]>
  • Loading branch information
Jdubrick committed Feb 21, 2024
1 parent 2a1d045 commit 22c637b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
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)) {

Check warning on line 116 in pkg/devfile/parser/util/mock.go

View check run for this annotation

Codecov / codecov/patch

pkg/devfile/parser/util/mock.go#L116

Added line #L116 was not covered by tests
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.yml", ".devfile.yaml", ".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)) {

Check warning on line 53 in pkg/devfile/parser/util/utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/devfile/parser/util/utils.go#L53

Added line #L53 was not covered by tests
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)
})
}
}

0 comments on commit 22c637b

Please sign in to comment.