-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
720642f
commit 07986b4
Showing
22 changed files
with
292 additions
and
1,712 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package frontend houses a custom BuildKit frontend for projects that use | ||
// pack.yaml. | ||
package frontend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package frontend | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"strings" | ||
|
||
"github.com/EricHripko/pack.yaml/pkg/packer2llb" | ||
|
||
"github.com/EricHripko/buildkit-fdk/pkg/cib" | ||
"github.com/moby/buildkit/frontend/gateway/client" | ||
fsutil "github.com/tonistiigi/fsutil/types" | ||
) | ||
|
||
// Returned when no command was found in the produced image. | ||
var errNoCommand = errors.New("frontend: no command found") | ||
|
||
// Looks in the known install directory and attempts to automatically detect | ||
// the command for the image. | ||
func findCommand(ctx context.Context, ref client.Reference) (command string, err error) { | ||
prefix := packer2llb.DirInstall[1:] | ||
err = cib.WalkRecursive(ctx, ref, func(file *fsutil.Stat) error { | ||
// Must be in install location | ||
if !strings.HasPrefix(file.Path, prefix) { | ||
return nil | ||
} | ||
// Must not be a directory | ||
if os.FileMode(file.Mode).IsDir() { | ||
return nil | ||
} | ||
// Must be executable | ||
if file.Mode&0100 == 0 { | ||
return nil | ||
} | ||
// Multiple commands found | ||
if command != "" { | ||
return errors.New("frontend: multiple commands found (" + command + ")") | ||
} | ||
command = "/" + file.Path | ||
return nil | ||
}) | ||
if command == "" { | ||
err = errNoCommand | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package frontend | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
cib_mock "github.com/EricHripko/buildkit-fdk/pkg/cib/mock" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
fsutil "github.com/tonistiigi/fsutil/types" | ||
) | ||
|
||
type findCommandTestSuite struct { | ||
suite.Suite | ||
ctrl *gomock.Controller | ||
ctx context.Context | ||
ref *cib_mock.MockReference | ||
} | ||
|
||
func (suite *findCommandTestSuite) SetupTest() { | ||
suite.ctrl = gomock.NewController(suite.T()) | ||
suite.ctx = context.Background() | ||
suite.ref = cib_mock.NewMockReference(suite.ctrl) | ||
} | ||
|
||
func (suite *findCommandTestSuite) TearDownTest() { | ||
suite.ctrl.Finish() | ||
} | ||
|
||
func (suite *findCommandTestSuite) TestNotFound() { | ||
// Arrange | ||
files := []*fsutil.Stat{} | ||
suite.ref.EXPECT(). | ||
ReadDir(suite.ctx, gomock.Any()). | ||
Return(files, nil) | ||
|
||
// Act | ||
cmd, err := findCommand(suite.ctx, suite.ref) | ||
|
||
// Assert | ||
require.Empty(suite.T(), cmd) | ||
require.Same(suite.T(), errNoCommand, err) | ||
} | ||
|
||
func (suite *findCommandTestSuite) TestIgnoresNonPrefix() { | ||
// Arrange | ||
files := []*fsutil.Stat{ | ||
{Path: "README.md"}, | ||
} | ||
suite.ref.EXPECT(). | ||
ReadDir(suite.ctx, gomock.Any()). | ||
Return(files, nil) | ||
|
||
// Act | ||
cmd, err := findCommand(suite.ctx, suite.ref) | ||
|
||
// Assert | ||
require.Empty(suite.T(), cmd) | ||
require.Same(suite.T(), errNoCommand, err) | ||
} | ||
|
||
func (suite *findCommandTestSuite) TestIgnoresDirs() { | ||
// Arrange | ||
files := []*fsutil.Stat{ | ||
{Path: "usr/local/bin/data", Mode: uint32(os.ModeDir)}, | ||
} | ||
suite.ref.EXPECT(). | ||
ReadDir(suite.ctx, gomock.Any()). | ||
Return(files, nil) | ||
suite.ref.EXPECT(). | ||
ReadDir(suite.ctx, gomock.Any()). | ||
Return([]*fsutil.Stat{}, nil) | ||
|
||
// Act | ||
cmd, err := findCommand(suite.ctx, suite.ref) | ||
|
||
// Assert | ||
require.Empty(suite.T(), cmd) | ||
require.Same(suite.T(), errNoCommand, err) | ||
} | ||
|
||
func (suite *findCommandTestSuite) TestIgnoresNonExecutable() { | ||
// Arrange | ||
files := []*fsutil.Stat{ | ||
{Path: "usr/local/bin/README.md"}, | ||
} | ||
suite.ref.EXPECT(). | ||
ReadDir(suite.ctx, gomock.Any()). | ||
Return(files, nil) | ||
|
||
// Act | ||
cmd, err := findCommand(suite.ctx, suite.ref) | ||
|
||
// Assert | ||
require.Empty(suite.T(), cmd) | ||
require.Same(suite.T(), errNoCommand, err) | ||
} | ||
|
||
func (suite *findCommandTestSuite) TestMultipleCommands() { | ||
// Arrange | ||
files := []*fsutil.Stat{ | ||
{Path: "usr/local/bin/hello1", Mode: 0755}, | ||
{Path: "usr/local/bin/hello2", Mode: 0755}, | ||
} | ||
suite.ref.EXPECT(). | ||
ReadDir(suite.ctx, gomock.Any()). | ||
Return(files, nil) | ||
|
||
// Act | ||
_, err := findCommand(suite.ctx, suite.ref) | ||
|
||
// Assert | ||
require.NotNil(suite.T(), err) | ||
require.Contains(suite.T(), err.Error(), "multiple commands") | ||
require.Contains(suite.T(), err.Error(), files[0].Path) | ||
} | ||
|
||
func (suite *findCommandTestSuite) TestSucceeds() { | ||
// Arrange | ||
files := []*fsutil.Stat{ | ||
{Path: "usr/local/bin/hello", Mode: 0755}, | ||
} | ||
suite.ref.EXPECT(). | ||
ReadDir(suite.ctx, gomock.Any()). | ||
Return(files, nil) | ||
|
||
// Act | ||
cmd, err := findCommand(suite.ctx, suite.ref) | ||
|
||
// Assert | ||
require.Equal(suite.T(), "/usr/local/bin/hello", cmd) | ||
require.Nil(suite.T(), err) | ||
} | ||
|
||
func TestFindCommand(t *testing.T) { | ||
suite.Run(t, new(findCommandTestSuite)) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.