Skip to content

Commit

Permalink
[redhat-developer#5330] Make odo build-images return an error if no…
Browse files Browse the repository at this point in the history
… Image component found in Devfile (redhat-developer#5608)

* Add test highlighting the issue

* Make `odo build-images` return an error if no Image component found

As reported in redhat-developer#5330, this makes more sense rather than exiting silently.
  • Loading branch information
rm3l authored and cdrage committed Aug 31, 2022
1 parent 9afdd7e commit cb7ec96
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/devfile/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
devfile "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/redhat-developer/odo/pkg/libdevfile"
"github.com/redhat-developer/odo/pkg/log"
)

Expand Down Expand Up @@ -39,6 +40,9 @@ func BuildPushImages(devfileObj parser.DevfileObj, path string, push bool) error
if err != nil {
return err
}
if len(components) == 0 {
return libdevfile.NewComponentTypeNotFoundError(devfile.ImageComponentType)
}

for _, component := range components {
err = buildPushImage(backend, component.Image, path, push)
Expand Down
15 changes: 15 additions & 0 deletions pkg/libdevfile/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,18 @@ func NewNotAContainerError() NotAContainerError {
func (e NotAContainerError) Error() string {
return "component not a container"
}

// ComponentTypeNotFoundError is returned when no component with the specified type has been found in Devfile
type ComponentTypeNotFoundError struct {
componentType v1alpha2.ComponentType
}

func NewComponentTypeNotFoundError(componentType v1alpha2.ComponentType) ComponentTypeNotFoundError {
return ComponentTypeNotFoundError{
componentType: componentType,
}
}

func (e ComponentTypeNotFoundError) Error() string {
return fmt.Sprintf("no component with type %q found in Devfile", e.componentType)
}
18 changes: 18 additions & 0 deletions tests/integration/devfile/cmd_devfile_build_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ var _ = Describe("odo devfile build-images command tests", func() {
})
})

When("using a devfile.yaml with no Image component", func() {
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
helper.Cmd("odo", "init", "--name", "aname",
"--devfile-path",
helper.GetExamplePath("source", "devfiles", "nodejs", "devfile.yaml")).ShouldPass()
helper.CreateLocalEnv(commonVar.Context, "aname", commonVar.Project)
})
It("should not be able to run odo build-images", func() {
stdout, stderr := helper.Cmd("odo", "build-images").AddEnv("PODMAN_CMD=echo").ShouldFail().OutAndErr()
// Make sure no "{podman,docker} build -t ..." command gets executed
imageBuildCmd := "build -t "
Expect(stdout).ShouldNot(ContainSubstring(imageBuildCmd))
Expect(stderr).ShouldNot(ContainSubstring(imageBuildCmd))
Expect(stderr).To(ContainSubstring("no component with type \"Image\" found in Devfile"))
})
})

When("using a devfile.yaml containing an Image component with Dockerfile args", func() {
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
Expand Down

0 comments on commit cb7ec96

Please sign in to comment.