Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
finish removing extra pieces from entrypoint binary
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-prindle committed Jan 6, 2019
1 parent 06eaffe commit df2f486
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 63 deletions.
63 changes: 20 additions & 43 deletions pkg/entrypoint/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ limitations under the License.
package entrypoint

import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"time"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -54,15 +52,6 @@ const (
DefaultPostRunFile = "1"
)

var (
// errTimedOut is used as the command's error when the command
// is terminated after the timeout is reached
errTimedOut = errors.New("process timed out")
// errAborted is used as the command's error when the command
// is shut down by an external signal
errAborted = errors.New("process aborted")
)

// Run executes the test process then writes the exit code to the marker file.
// This function returns the status code that should be passed to os.Exit().
func (o Options) Run() int {
Expand All @@ -76,33 +65,33 @@ func (o Options) Run() int {
// ExecuteProcess creates the artifact directory then executes the process as
// configured, writing the output to the process log.
func (o Options) ExecuteProcess() (int, error) {
// ---
var commandErr error

done := make(chan error)
go func() {
done <- o.waitForPrevStep()
}()
// wait for previous step if specified
if o.ShouldWaitForPrevStep {
done := make(chan error)
go func() {
done <- o.waitForPrevStep()
}()
}

// ---
executable := o.Args[0]
var arguments []string
if len(o.Args) > 1 {
arguments = o.Args[1:]
}
command := exec.Command(executable, arguments...)
command := exec.Command(o.Args[0], arguments...)
if err := command.Start(); err != nil {
return InternalErrorCode, fmt.Errorf("could not start the process: %v", err)
}

// if we get asked to terminate we need to forward
// that to the wrapped process as if it timed out
done = make(chan error)
// execute the user specified command
done := make(chan error)
go func() {
done <- command.Wait()
}()
select {
case commandErr := <-done:
case commandErr = <-done:
// execute post run action if specified
if o.ShouldRunPostRun {
o.postRunWriteFile(0)
}
Expand All @@ -123,28 +112,16 @@ func (o Options) ExecuteProcess() (int, error) {
return returnCode, commandErr
}

// optionOrDefault defaults to a value if option
// is the zero value
func optionOrDefault(option, defaultValue time.Duration) time.Duration {
if option == 0 {
return defaultValue
}

return option
}

func (o *Options) waitForPrevStep() error {
// wait for a file to exist that the last step wrote in a mounted shared dir
if o.ShouldWaitForPrevStep {
for {
// TODO(aaron-prindle) check for non-zero returnCode only
// as PreRunFile will have returnCode as it's contents?
_, err := os.Stat(o.PreRunFile)
if err == nil {
break
} else if !os.IsNotExist(err) {
return err
}
for {
// TODO(aaron-prindle) check for non-zero returnCode only
// as PreRunFile will have returnCode as it's contents?
_, err := os.Stat(o.PreRunFile)
if err == nil {
break
} else if !os.IsNotExist(err) {
return err
}
}
return nil
Expand Down
27 changes: 7 additions & 20 deletions pkg/entrypoint/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"os"
"path"
"testing"
"time"

"github.com/knative/build/pkg/entrypoint/wrapper"
"github.com/sirupsen/logrus"
Expand All @@ -31,9 +30,6 @@ func TestOptions_Run(t *testing.T) {
var testCases = []struct {
name string
args []string
timeout time.Duration
gracePeriod time.Duration
expectedLog string
expectedShouldWaitForPrevStep bool
expectedPreRunFile string
expectedPostRunFile string
Expand All @@ -42,24 +38,20 @@ func TestOptions_Run(t *testing.T) {
{
name: "successful command",
args: []string{"sh", "-c", "exit 0"},
expectedLog: "",
expectedShouldRunPostRun: true,
expectedPostRunFile: "0",
},
{
name: "successful command with output",
args: []string{"echo", "test"},
expectedLog: "test\n",
name: "successful command with output",
args: []string{"echo", "test"},
},
{
name: "unsuccessful command",
args: []string{"sh", "-c", "exit 12"},
expectedLog: "",
name: "unsuccessful command",
args: []string{"sh", "-c", "exit 12"},
},
{
name: "unsuccessful command with output",
args: []string{"sh", "-c", "echo test && exit 12"},
expectedLog: "test\n",
name: "unsuccessful command with output",
args: []string{"sh", "-c", "echo test && exit 12"},
},
}

Expand All @@ -80,18 +72,13 @@ func TestOptions_Run(t *testing.T) {
}()

options := Options{
Args: testCase.args,
Timeout: testCase.timeout,
GracePeriod: testCase.gracePeriod,
Args: testCase.args,
Options: &wrapper.Options{
ProcessLog: path.Join(tmpDir, "process-log.txt"),
ShouldWaitForPrevStep: false,
PreRunFile: path.Join(tmpDir, "0"),
PostRunFile: path.Join(tmpDir, "0"),
},
}

compareFileContents(testCase.name, options.ProcessLog, testCase.expectedLog, t)
if options.ShouldWaitForPrevStep {
compareFileContents(testCase.name, options.PreRunFile,
testCase.expectedPreRunFile, t)
Expand Down

0 comments on commit df2f486

Please sign in to comment.