Skip to content

Commit

Permalink
Move DiscoverContainerfile to define directory, so podman can use it
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Aug 12, 2021
1 parent 1e560e2 commit 3110432
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 54 deletions.
43 changes: 1 addition & 42 deletions cmd/buildah/bud.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budOptions) error {

if len(dockerfiles) == 0 {
// Try to find the Containerfile/Dockerfile within the contextDir
dockerFile, err := discoverContainerfile(contextDir)
dockerFile, err := define.DiscoverContainerfile(contextDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -381,44 +381,3 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budOptions) error {
_, _, err = imagebuildah.BuildDockerfiles(getContext(), store, options, dockerfiles...)
return err
}

// discoverContainerfile tries to find a Containerfile or a Dockerfile within the provided `path`.
func discoverContainerfile(path string) (foundCtrFile string, err error) {
// Test for existence of the file
target, err := os.Stat(path)
if err != nil {
return "", errors.Wrap(err, "discovering Containerfile")
}

switch mode := target.Mode(); {
case mode.IsDir():
// If the path is a real directory, we assume a Containerfile or a Dockerfile within it
ctrfile := filepath.Join(path, "Containerfile")

// Test for existence of the Containerfile file
file, err := os.Stat(ctrfile)
if err != nil {
// See if we have a Dockerfile within it
ctrfile = filepath.Join(path, "Dockerfile")

// Test for existence of the Dockerfile file
file, err = os.Stat(ctrfile)
if err != nil {
return "", errors.Wrap(err, "cannot find Containerfile or Dockerfile in context directory")
}
}

// The file exists, now verify the correct mode
if mode := file.Mode(); mode.IsRegular() {
foundCtrFile = ctrfile
} else {
return "", errors.Errorf("assumed Containerfile %q is not a file", ctrfile)
}

case mode.IsRegular():
// If the context dir is a file, we assume this as Containerfile
foundCtrFile = path
}

return foundCtrFile, nil
}
7 changes: 7 additions & 0 deletions cmd/buildah/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,10 @@ func pullTestImage(t *testing.T) string {
}
return id
}
func discoverContainerfileTest(t *testing.T) string {
_, err := DiscoverContainerfile("/")
if err == nil {
t.Fatal(err)
}

}
44 changes: 44 additions & 0 deletions define/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package define

import (
"io"
"os"
"path/filepath"
"time"

"github.com/containers/image/v5/types"
encconfig "github.com/containers/ocicrypt/config"
"github.com/containers/storage/pkg/archive"
"github.com/pkg/errors"
"golang.org/x/sync/semaphore"
)

Expand Down Expand Up @@ -235,3 +238,44 @@ type BuildOptions struct {
// Architecture fields above are ignored.
Platforms []struct{ OS, Arch, Variant string }
}

// DiscoverContainerfile tries to find a Containerfile or a Dockerfile within the provided `path`.
func DiscoverContainerfile(path string) (foundCtrFile string, err error) {
// Test for existence of the file
target, err := os.Stat(path)
if err != nil {
return "", errors.Wrap(err, "discovering Containerfile")
}

switch mode := target.Mode(); {
case mode.IsDir():
// If the path is a real directory, we assume a Containerfile or a Dockerfile within it
ctrfile := filepath.Join(path, "Containerfile")

// Test for existence of the Containerfile file
file, err := os.Stat(ctrfile)
if err != nil {
// See if we have a Dockerfile within it
ctrfile = filepath.Join(path, "Dockerfile")

// Test for existence of the Dockerfile file
file, err = os.Stat(ctrfile)
if err != nil {
return "", errors.Wrap(err, "cannot find Containerfile or Dockerfile in context directory")
}
}

// The file exists, now verify the correct mode
if mode := file.Mode(); mode.IsRegular() {
foundCtrFile = ctrfile
} else {
return "", errors.Errorf("assumed Containerfile %q is not a file", ctrfile)
}

case mode.IsRegular():
// If the context dir is a file, we assume this as Containerfile
foundCtrFile = path
}

return foundCtrFile, nil
}
24 changes: 12 additions & 12 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ symlink(subdir)"

@test "bud-from-scratch-layers" {
target=scratch-image
run_buildah bud --signature-policy ${TESTSDIR}/policy.json -f ${TESTSDIR}/bud/from-scratch/Dockerfile2 -t ${target} ${TESTSDIR}/bud/from-scratch
run_buildah bud --signature-policy ${TESTSDIR}/policy.json -f ${TESTSDIR}/bud/from-scratch/Dockerfile2 -t ${target} ${TESTSDIR}/bud/from-scratch
run_buildah bud --signature-policy ${TESTSDIR}/policy.json -f ${TESTSDIR}/bud/from-scratch/Containerfile2 -t ${target} ${TESTSDIR}/bud/from-scratch
run_buildah bud --signature-policy ${TESTSDIR}/policy.json -f ${TESTSDIR}/bud/from-scratch/Containerfile2 -t ${target} ${TESTSDIR}/bud/from-scratch
run_buildah from --quiet ${target}
cid=$output
run_buildah images
Expand Down Expand Up @@ -1064,14 +1064,14 @@ function _test_http() {

@test "bud with Dockerfile from valid URL" {
target=url-image
url=https://raw.githubusercontent.com/containers/buildah/main/tests/bud/from-scratch/Dockerfile
url=https://raw.githubusercontent.com/containers/buildah/main/tests/bud/from-scratch/Containerfile
run_buildah bud --signature-policy ${TESTSDIR}/policy.json -t ${target} ${url}
run_buildah from ${target}
}

@test "bud with Dockerfile from invalid URL" {
target=url-image
url=https://raw.githubusercontent.com/containers/buildah/main/tests/bud/from-scratch/Dockerfile.bogus
url=https://raw.githubusercontent.com/containers/buildah/main/tests/bud/from-scratch/Containerfile.bogus
run_buildah 125 bud --signature-policy ${TESTSDIR}/policy.json -t ${target} ${url}
}

Expand All @@ -1097,29 +1097,29 @@ function _test_http() {

@test "bud with --cpu-shares flag, no argument" {
target=bud-flag
run_buildah 125 bud --cpu-shares --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/from-scratch/Dockerfile ${TESTSDIR}/bud/from-scratch
run_buildah 125 bud --cpu-shares --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/from-scratch/Containerfile ${TESTSDIR}/bud/from-scratch
}

@test "bud with --cpu-shares flag, invalid argument" {
target=bud-flag
run_buildah 125 bud --cpu-shares bogus --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/from-scratch/Dockerfile ${TESTSDIR}/bud/from-scratch
run_buildah 125 bud --cpu-shares bogus --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/from-scratch/Containerfile ${TESTSDIR}/bud/from-scratch
expect_output --substring "invalid argument \"bogus\" for "
}

@test "bud with --cpu-shares flag, valid argument" {
target=bud-flag
run_buildah bud --cpu-shares 2 --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/from-scratch/Dockerfile ${TESTSDIR}/bud/from-scratch
run_buildah bud --cpu-shares 2 --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/from-scratch/Containerfile ${TESTSDIR}/bud/from-scratch
run_buildah from ${target}
}

@test "bud with --cpu-shares short flag (-c), no argument" {
target=bud-flag
run_buildah 125 bud -c --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/from-scratch/Dockerfile ${TESTSDIR}/bud/from-scratch
run_buildah 125 bud -c --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/from-scratch/Containerfile ${TESTSDIR}/bud/from-scratch
}

@test "bud with --cpu-shares short flag (-c), invalid argument" {
target=bud-flag
run_buildah 125 bud -c bogus --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/from-scratch/Dockerfile ${TESTSDIR}/bud/from-scratch
run_buildah 125 bud -c bogus --signature-policy ${TESTSDIR}/policy.json -t ${target} -f ${TESTSDIR}/bud/from-scratch/Containerfile ${TESTSDIR}/bud/from-scratch
expect_output --substring "invalid argument \"bogus\" for "
}

Expand Down Expand Up @@ -2294,7 +2294,7 @@ EOM

@test "bud with custom arch" {
run_buildah bud --signature-policy ${TESTSDIR}/policy.json \
-f ${TESTSDIR}/bud/from-scratch/Dockerfile \
-f ${TESTSDIR}/bud/from-scratch/Containerfile \
-t arch-test \
--arch=arm

Expand All @@ -2307,7 +2307,7 @@ EOM

@test "bud with custom os" {
run_buildah bud --signature-policy ${TESTSDIR}/policy.json \
-f ${TESTSDIR}/bud/from-scratch/Dockerfile \
-f ${TESTSDIR}/bud/from-scratch/Containerfile \
-t os-test \
--os=windows

Expand All @@ -2320,7 +2320,7 @@ EOM

@test "bud with custom platform" {
run_buildah bud --signature-policy ${TESTSDIR}/policy.json \
-f ${TESTSDIR}/bud/from-scratch/Dockerfile \
-f ${TESTSDIR}/bud/from-scratch/Containerfile \
-t platform-test \
--platform=windows/arm

Expand Down
3 changes: 3 additions & 0 deletions tests/bud/containerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is for testing Containerfile with Buildah
# Dockerfile should never be used
BOGUS

0 comments on commit 3110432

Please sign in to comment.