Skip to content

Commit

Permalink
Merge pull request containers#12622 from flouthoc/return_exit_code_fo…
Browse files Browse the repository at this point in the history
…r_build

build: relay `exitcode` from imagebuildah to registry
  • Loading branch information
openshift-merge-robot authored Dec 17, 2021
2 parents c9a3f4e + 6423174 commit 820cf0e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
18 changes: 18 additions & 0 deletions cmd/podman/images/build.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package images

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -289,7 +291,23 @@ func build(cmd *cobra.Command, args []string) error {
}

report, err := registry.ImageEngine().Build(registry.GetContext(), containerFiles, *apiBuildOpts)

if err != nil {
exitCode := buildahCLI.ExecErrorCodeGeneric
if registry.IsRemote() {
// errors from server does not contain ExitCode
// so parse exit code from error message
remoteExitCode, parseErr := utils.ExitCodeFromBuildError(fmt.Sprint(errors.Cause(err)))
if parseErr == nil {
exitCode = remoteExitCode
}
}

if ee, ok := (errors.Cause(err)).(*exec.ExitError); ok {
exitCode = ee.ExitCode()
}

registry.SetExitCode(exitCode)
return err
}

Expand Down
26 changes: 26 additions & 0 deletions cmd/podman/utils/error.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package utils

import (
"errors"
"fmt"
"os"
"strconv"
"strings"

buildahCLI "github.com/containers/buildah/pkg/cli"
)

type OutputErrors []error
Expand All @@ -17,3 +22,24 @@ func (o OutputErrors) PrintErrors() (lastError error) {
}
return
}

/* For remote client, server does not returns error with exit code
instead returns a message and we cast it to a new error.
Following function performs parsing on build error and returns
exit status which was exepected for this current build
*/
func ExitCodeFromBuildError(errorMsg string) (int, error) {
if strings.Contains(errorMsg, "exit status") {
errorSplit := strings.Split(errorMsg, " ")
if errorSplit[len(errorSplit)-2] == "status" {
tmpSplit := strings.Split(errorSplit[len(errorSplit)-1], "\n")
exitCodeRemote, err := strconv.Atoi(tmpSplit[0])
if err == nil {
return exitCodeRemote, nil
}
return buildahCLI.ExecErrorCodeGeneric, err
}
}
return buildahCLI.ExecErrorCodeGeneric, errors.New("error message does not contains a valid exit code")
}
21 changes: 19 additions & 2 deletions test/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ RUN printenv http_proxy`, ALPINE)
os.Unsetenv("http_proxy")
})

It("podman build relay exit code to process", func() {
if IsRemote() {
podmanTest.StopRemoteService()
podmanTest.StartRemoteService()
}
podmanTest.AddImageToRWStore(ALPINE)
dockerfile := fmt.Sprintf(`FROM %s
RUN exit 5`, ALPINE)

dockerfilePath := filepath.Join(podmanTest.TempDir, "Dockerfile")
err := ioutil.WriteFile(dockerfilePath, []byte(dockerfile), 0755)
Expect(err).To(BeNil())
session := podmanTest.Podman([]string{"build", "-t", "error-test", "--file", dockerfilePath, podmanTest.TempDir})
session.Wait(120)
Expect(session).Should(Exit(5))
})

It("podman build and check identity", func() {
session := podmanTest.Podman([]string{"build", "--pull-never", "-f", "build/basicalpine/Containerfile.path", "--no-cache", "-t", "test", "build/basicalpine"})
session.WaitWithDefaultTimeout()
Expand Down Expand Up @@ -646,7 +663,7 @@ RUN ls /dev/fuse`, ALPINE)
Expect(err).To(BeNil())
session := podmanTest.Podman([]string{"build", "--pull-never", "-t", "test", "--file", containerfilePath, podmanTest.TempDir})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(125))
Expect(session).Should(Exit(1))

session = podmanTest.Podman([]string{"build", "--pull-never", "--device", "/dev/fuse", "-t", "test", "--file", containerfilePath, podmanTest.TempDir})
session.WaitWithDefaultTimeout()
Expand All @@ -662,7 +679,7 @@ RUN ls /dev/test1`, ALPINE)
Expect(err).To(BeNil())
session := podmanTest.Podman([]string{"build", "--pull-never", "-t", "test", "--file", containerfilePath, podmanTest.TempDir})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(125))
Expect(session).Should(Exit(1))

session = podmanTest.Podman([]string{"build", "--pull-never", "--device", "/dev/zero:/dev/test1", "-t", "test", "--file", containerfilePath, podmanTest.TempDir})
session.WaitWithDefaultTimeout()
Expand Down
2 changes: 1 addition & 1 deletion test/system/070-build.bats
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ FROM $IMAGE
RUN echo $rand_content
EOF

run_podman 125 --runtime-flag invalidflag build -t build_test $tmpdir
run_podman 1 --runtime-flag invalidflag build -t build_test $tmpdir
is "$output" ".*invalidflag" "failed when passing undefined flags to the runtime"
}

Expand Down

0 comments on commit 820cf0e

Please sign in to comment.