diff --git a/pkg/devfile/parser/parse.go b/pkg/devfile/parser/parse.go index 470d4717..eb73d5b8 100644 --- a/pkg/devfile/parser/parse.go +++ b/pkg/devfile/parser/parse.go @@ -126,7 +126,8 @@ type ParserArgs struct { // all container and Kubernetes/OpenShift components matching a relative image name (say "my-image-name") of an Image component // will be replaced in the resulting Devfile by: "//-my-image-name:some-dynamic-unique-tag". type ImageSelectorArgs struct { - // Registry is the registry base path under which images matching selectors will be built and pushed to. + // Registry is the registry base path under which images matching selectors will be built and pushed to. Required. + // // Example: / Registry string // Tag represents a tag identifier under which images matching selectors will be built and pushed to. @@ -137,6 +138,10 @@ type ImageSelectorArgs struct { // ParseDevfile func populates the devfile data, parses and validates the devfile integrity. // Creates devfile context and runtime objects func ParseDevfile(args ParserArgs) (d DevfileObj, err error) { + if args.ImageNamesAsSelector != nil && strings.TrimSpace(args.ImageNamesAsSelector.Registry) == "" { + return DevfileObj{}, errors.New("registry is mandatory when setting ImageNamesAsSelector in the parser args") + } + if args.Data != nil { d.Ctx, err = devfileCtx.NewByteContentDevfileCtx(args.Data) if err != nil { diff --git a/pkg/devfile/parser/parse_test.go b/pkg/devfile/parser/parse_test.go index bdc5edae..a574bf48 100644 --- a/pkg/devfile/parser/parse_test.go +++ b/pkg/devfile/parser/parse_test.go @@ -18,7 +18,6 @@ package parser import ( "context" "fmt" - v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" "io/ioutil" "net" "net/http" @@ -29,6 +28,8 @@ import ( "strings" "testing" + v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" + "github.com/devfile/api/v2/pkg/attributes" devfilepkg "github.com/devfile/api/v2/pkg/devfile" devfileCtx "github.com/devfile/library/v2/pkg/devfile/parser/context" @@ -3329,6 +3330,7 @@ commands: tests := []struct { name string parserArgs ParserArgs + wantErr bool wantBoolPropsSet []setFields }{ { @@ -3371,12 +3373,32 @@ commands: {compProp: compBooleanProp{prop: "Secure", name: "kubernetes-deploy", value: nil, compType: v1.KubernetesComponentType}}, //unset property should be nil }, }, + { + name: "error if ImageNamesAsSelector is non-nil but ImageNamesAsSelector.Registry is empty", + parserArgs: ParserArgs{ + Data: []byte(mainDevfile), + ConvertKubernetesContentInUri: &isFalse, //this is a workaround for a known parsing issue that requires support for downloading the deploy.yaml https://github.com/devfile/api/issues/1073 + ImageNamesAsSelector: &ImageSelectorArgs{}, + }, + wantErr: true, + }, + { + name: "error if ImageNamesAsSelector is non-nil but ImageNamesAsSelector.Registry is blank", + parserArgs: ParserArgs{ + Data: []byte(mainDevfile), + ConvertKubernetesContentInUri: &isFalse, //this is a workaround for a known parsing issue that requires support for downloading the deploy.yaml https://github.com/devfile/api/issues/1073 + ImageNamesAsSelector: &ImageSelectorArgs{ + Registry: " \t \n", + }, + }, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { d, err := ParseDevfile(tt.parserArgs) - if err != nil { - t.Errorf("Problems parsing devfile") + if (err != nil) != tt.wantErr { + t.Errorf("unexpected err when parsing devfile: error=%v, wantErr =%v", err, tt.wantErr) } for i, _ := range tt.wantBoolPropsSet { wantProps := tt.wantBoolPropsSet[i]