diff --git a/cmd/buildah/bud.go b/cmd/buildah/bud.go index 759d18cc2bb..15592a6d5d9 100644 --- a/cmd/buildah/bud.go +++ b/cmd/buildah/bud.go @@ -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 := buildahcli.DiscoverContainerfile(contextDir) if err != nil { return err } @@ -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 -} diff --git a/pkg/cli/common.go b/pkg/cli/common.go index ac3baef8096..1ba764c72ca 100644 --- a/pkg/cli/common.go +++ b/pkg/cli/common.go @@ -7,6 +7,7 @@ package cli import ( "fmt" "os" + "path/filepath" "runtime" "strings" @@ -420,3 +421,44 @@ func AliasFlags(f *pflag.FlagSet, name string) pflag.NormalizedName { } return pflag.NormalizedName(name) } + +// 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 +} diff --git a/pkg/cli/common_test.go b/pkg/cli/common_test.go index a1c29a75da4..7aef74e2e8d 100644 --- a/pkg/cli/common_test.go +++ b/pkg/cli/common_test.go @@ -5,6 +5,7 @@ import ( "github.com/containers/common/pkg/completion" "github.com/spf13/pflag" + "github.com/stretchr/testify/assert" ) func testFlagCompletion(t *testing.T, flags pflag.FlagSet, flagCompletions completion.FlagCompletions) { @@ -53,3 +54,28 @@ func TestFromAndBudFlagsCompletions(t *testing.T) { flagCompletions := GetFromAndBudFlagsCompletions() testFlagCompletion(t, flags, flagCompletions) } + +func TestDiscoverContainerfile(t *testing.T) { + _, err := DiscoverContainerfile("./bogus") + assert.NotNil(t, err) + + _, err = DiscoverContainerfile("./") + assert.NotNil(t, err) + + name, err := DiscoverContainerfile("test/test1/Dockerfile") + assert.Nil(t, err) + assert.Equal(t, name, "test/test1/Dockerfile") + + name, err = DiscoverContainerfile("test/test1/Containerfile") + assert.Nil(t, err) + assert.Equal(t, name, "test/test1/Containerfile") + + name, err = DiscoverContainerfile("test/test1") + assert.Nil(t, err) + assert.Equal(t, name, "test/test1/Containerfile") + + name, err = DiscoverContainerfile("test/test2") + assert.Nil(t, err) + assert.Equal(t, name, "test/test2/Dockerfile") + +} diff --git a/pkg/cli/test/test1/Containerfile b/pkg/cli/test/test1/Containerfile new file mode 100644 index 00000000000..453e3aca602 --- /dev/null +++ b/pkg/cli/test/test1/Containerfile @@ -0,0 +1 @@ +from scratch diff --git a/pkg/cli/test/test1/Dockerfile b/pkg/cli/test/test1/Dockerfile new file mode 100644 index 00000000000..453e3aca602 --- /dev/null +++ b/pkg/cli/test/test1/Dockerfile @@ -0,0 +1 @@ +from scratch diff --git a/pkg/cli/test/test2/Dockerfile b/pkg/cli/test/test2/Dockerfile new file mode 100644 index 00000000000..453e3aca602 --- /dev/null +++ b/pkg/cli/test/test2/Dockerfile @@ -0,0 +1 @@ +from scratch diff --git a/tests/bud.bats b/tests/bud.bats index ae29ef4fa6d..10b9146b74f 100644 --- a/tests/bud.bats +++ b/tests/bud.bats @@ -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 @@ -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} } @@ -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 " } @@ -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 @@ -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 @@ -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 diff --git a/tests/bud/containerfile/Dockerfile b/tests/bud/containerfile/Dockerfile new file mode 100644 index 00000000000..f786ca9cff4 --- /dev/null +++ b/tests/bud/containerfile/Dockerfile @@ -0,0 +1,3 @@ +# This is for testing Containerfile with Buildah +# Dockerfile should never be used +BOGUS