Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#5330] Make odo build-images return an error if no Image component found in Devfile #5608

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
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