mirror of
https://github.com/containers/podman.git
synced 2025-06-20 17:13:43 +08:00
Fix podman build --logfile
Currently this command blows up because it closes the file descriptor before doing the build. Add tests to make sure we don't regress. Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1877188 Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
@ -211,7 +211,16 @@ func build(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
apiBuildOpts, err := buildFlagsWrapperToOptions(cmd, contextDir, &buildOpts)
|
var logfile *os.File
|
||||||
|
if cmd.Flag("logfile").Changed {
|
||||||
|
logfile, err = os.OpenFile(buildOpts.Logfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Errorf("error opening logfile %q: %v", buildOpts.Logfile, err)
|
||||||
|
}
|
||||||
|
defer logfile.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
apiBuildOpts, err := buildFlagsWrapperToOptions(cmd, contextDir, &buildOpts, logfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -225,7 +234,7 @@ func build(cmd *cobra.Command, args []string) error {
|
|||||||
// conversion here prevents the API from doing that (redundantly).
|
// conversion here prevents the API from doing that (redundantly).
|
||||||
//
|
//
|
||||||
// TODO: this code should really be in Buildah.
|
// TODO: this code should really be in Buildah.
|
||||||
func buildFlagsWrapperToOptions(c *cobra.Command, contextDir string, flags *buildFlagsWrapper) (*entities.BuildOptions, error) {
|
func buildFlagsWrapperToOptions(c *cobra.Command, contextDir string, flags *buildFlagsWrapper, logfile *os.File) (*entities.BuildOptions, error) {
|
||||||
output := ""
|
output := ""
|
||||||
tags := []string{}
|
tags := []string{}
|
||||||
if c.Flag("tag").Changed {
|
if c.Flag("tag").Changed {
|
||||||
@ -284,16 +293,11 @@ func buildFlagsWrapperToOptions(c *cobra.Command, contextDir string, flags *buil
|
|||||||
stderr = os.Stderr
|
stderr = os.Stderr
|
||||||
reporter = os.Stderr
|
reporter = os.Stderr
|
||||||
|
|
||||||
if c.Flag("logfile").Changed {
|
if logfile != nil {
|
||||||
f, err := os.OpenFile(flags.Logfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
|
logrus.SetOutput(logfile)
|
||||||
if err != nil {
|
stdout = logfile
|
||||||
return nil, errors.Errorf("error opening logfile %q: %v", flags.Logfile, err)
|
stderr = logfile
|
||||||
}
|
reporter = logfile
|
||||||
defer f.Close()
|
|
||||||
logrus.SetOutput(f)
|
|
||||||
stdout = f
|
|
||||||
stderr = f
|
|
||||||
reporter = f
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var memoryLimit, memorySwap int64
|
var memoryLimit, memorySwap int64
|
||||||
|
@ -57,6 +57,29 @@ var _ = Describe("Podman build", func() {
|
|||||||
Expect(session.ExitCode()).To(Equal(0))
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman build with logfile", func() {
|
||||||
|
SkipIfRemote()
|
||||||
|
logfile := filepath.Join(podmanTest.TempDir, "logfile")
|
||||||
|
session := podmanTest.PodmanNoCache([]string{"build", "--tag", "test", "--logfile", logfile, "build/basicalpine"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
// Verify that OS and Arch are being set
|
||||||
|
inspect := podmanTest.PodmanNoCache([]string{"inspect", "test"})
|
||||||
|
inspect.WaitWithDefaultTimeout()
|
||||||
|
data := inspect.InspectImageJSON()
|
||||||
|
Expect(data[0].Os).To(Equal(runtime.GOOS))
|
||||||
|
Expect(data[0].Architecture).To(Equal(runtime.GOARCH))
|
||||||
|
|
||||||
|
st, err := os.Stat(logfile)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
Expect(st.Size()).To(Not(Equal(0)))
|
||||||
|
|
||||||
|
session = podmanTest.PodmanNoCache([]string{"rmi", "alpine"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
|
})
|
||||||
|
|
||||||
// If the context directory is pointing at a file and not a directory,
|
// If the context directory is pointing at a file and not a directory,
|
||||||
// that's a no no, fail out.
|
// that's a no no, fail out.
|
||||||
It("podman build context directory a file", func() {
|
It("podman build context directory a file", func() {
|
||||||
|
Reference in New Issue
Block a user