Skip to content

Commit

Permalink
Added README and renamed std*Path to std*File.
Browse files Browse the repository at this point in the history
  • Loading branch information
chhsia0 committed Aug 16, 2020
1 parent 2ae293e commit 8b70a13
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
9 changes: 9 additions & 0 deletions cmd/entrypoint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ The following flags are available :
- `-wait_file_content`: excepts the `wait_file` to add actual
content. It will continue watching for `wait_file` until it has
content.
- `-stdout_file`: If specified, the stdout of the sub-process will be
copied to the given path on the local filesystem. The default values
can be set by the `TEKTON_STDOUT_FILE` environment variable.
- `-stderr_file`: If specified, the stderr of the sub-process will be
copied to the given path on the local filesystem. It can be set to the
same value as `{{stdout_file}}` so both streams are copied to the same
file. However, there is no ordering guarantee on data copied from both
streams. The default values can be set by the `TEKTON_STDERR_FILE`
environment variable.

The following example of usage for `entrypoint`, wait's for
`/tekton/downward/ready` file to exists and have some content before
Expand Down
8 changes: 4 additions & 4 deletions cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ var (
postFile = flag.String("post_file", "", "If specified, file to write upon completion")
terminationPath = flag.String("termination_path", "/tekton/termination", "If specified, file to write upon termination")
results = flag.String("results", "", "If specified, list of file names that might contain task results")
stdoutPath = flag.String("stdout_path", os.Getenv("TEKTON_STDOUT_PATH"), "If specified, file to copy stdout to")
stderrPath = flag.String("stderr_path", os.Getenv("TEKTON_STDERR_PATH"), "If specified, file to copy stdout to")
stdoutFile = flag.String("stdout_file", os.Getenv("TEKTON_STDOUT_FILE"), "If specified, file to copy stdout to")
stderrFile = flag.String("stderr_file", os.Getenv("TEKTON_STDERR_FILE"), "If specified, file to copy stderr to")
waitPollingInterval = time.Second
)

Expand Down Expand Up @@ -70,8 +70,8 @@ func main() {
Args: flag.Args(),
Waiter: &realWaiter{},
Runner: &realRunner{
stdoutPath: *stdoutPath,
stderrPath: *stderrPath,
stdoutFile: *stdoutFile,
stderrFile: *stderrFile,
},
PostWriter: &realPostWriter{},
Results: strings.Split(*results, ","),
Expand Down
16 changes: 8 additions & 8 deletions cmd/entrypoint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
// realRunner actually runs commands.
type realRunner struct {
signals chan os.Signal
stdoutPath string
stderrPath string
stdoutFile string
stderrFile string
}

var _ entrypoint.Runner = (*realRunner)(nil)
Expand All @@ -40,9 +40,9 @@ func (rr *realRunner) Run(args ...string) error {

cmd.Stdout = os.Stdout
var stdoutFile *os.File
if rr.stdoutPath != "" {
if rr.stdoutFile != "" {
var err error
if stdoutFile, err = os.Create(rr.stdoutPath); err != nil {
if stdoutFile, err = os.Create(rr.stdoutFile); err != nil {
return err
}
defer stdoutFile.Close()
Expand All @@ -59,15 +59,15 @@ func (rr *realRunner) Run(args ...string) error {

cmd.Stderr = os.Stderr
var stderrFile *os.File
if rr.stderrPath != "" {
if rr.stderrFile != "" {
var err error
if rr.stderrPath == rr.stdoutPath {
if rr.stderrFile == rr.stdoutFile {
fd, err := syscall.Dup(int(stdoutFile.Fd()))
if err != nil {
return err
}
stderrFile = os.NewFile(uintptr(fd), rr.stderrPath)
} else if stderrFile, err = os.Create(rr.stderrPath); err != nil {
stderrFile = os.NewFile(uintptr(fd), rr.stderrFile)
} else if stderrFile, err = os.Create(rr.stderrFile); err != nil {
return err
}
defer stderrFile.Close()
Expand Down
26 changes: 13 additions & 13 deletions cmd/entrypoint/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestRealRunnerSignalForwarding(t *testing.T) {
}
}

func TestRealRunnerStdoutAndStderrPaths(t *testing.T) {
func TestRealRunnerStdoutAndStderrFiles(t *testing.T) {
tmp, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
Expand All @@ -37,8 +37,8 @@ func TestRealRunnerStdoutAndStderrPaths(t *testing.T) {

expectedString := "hello world"
rr := realRunner{
stdoutPath: filepath.Join(tmp, "stdout"),
stderrPath: filepath.Join(tmp, "stderr"),
stdoutFile: filepath.Join(tmp, "stdout"),
stderrFile: filepath.Join(tmp, "stderr"),
}
if err := rr.Run("sh", "-c", fmt.Sprintf("echo %s && echo %s >&2", expectedString, expectedString)); err != nil {
t.Fatalf("Unexpected error: %v", err)
Expand All @@ -53,52 +53,52 @@ func TestRealRunnerStdoutAndStderrPaths(t *testing.T) {
}
}

func TestRealRunnerStdoutAndStderrSamePath(t *testing.T) {
func TestRealRunnerStdoutAndStderrSameFile(t *testing.T) {
tmp, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer os.RemoveAll(tmp)

path := filepath.Join(tmp, "logs")
file := filepath.Join(tmp, "logs")
expectedString := "hello world"
rr := realRunner{
stdoutPath: path,
stderrPath: path,
stdoutFile: file,
stderrFile: file,
}
if err := rr.Run("sh", "-c", fmt.Sprintf("echo %s && echo %s >&2", expectedString, expectedString)); err != nil {
t.Fatalf("Unexpected error: %v", err)
}

// Since writes to stdout and stderr might be racy, we only check for lengths here.
expectedSize := (len(expectedString) + 1) * 2
if got, err := ioutil.ReadFile(path); err != nil {
if got, err := ioutil.ReadFile(file); err != nil {
t.Fatalf("Unexpected error: %v", err)
} else if gotSize := len(got); gotSize != expectedSize {
t.Errorf("got: %v, wanted: %v", gotSize, expectedSize)
}
}

func TestRealRunnerStdoutPathWithSignal(t *testing.T) {
func TestRealRunnerStdoutFileWithSignal(t *testing.T) {
tmp, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer os.RemoveAll(tmp)

signals := make(chan os.Signal, 1)
path := filepath.Join(tmp, "stdout")
file := filepath.Join(tmp, "stdout")
rr := realRunner{
signals: signals,
stdoutPath: path,
stdoutFile: file,
}

expectedString := "hello world"
expectedError := "signal: interrupt"
go func() {
timer := time.Tick(100 * time.Millisecond)
for {
if stat, err := os.Stat(path); err != nil {
if stat, err := os.Stat(file); err != nil {
if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("Unexpected error: %v", err)
}
Expand All @@ -113,7 +113,7 @@ func TestRealRunnerStdoutPathWithSignal(t *testing.T) {
if err := rr.Run("sh", "-c", fmt.Sprintf("echo %s && sleep 3600", expectedString)); err.Error() != expectedError {
t.Errorf("Expected error %v but got %v", expectedError, err)
}
if got, err := ioutil.ReadFile(path); err != nil {
if got, err := ioutil.ReadFile(file); err != nil {
t.Fatalf("Unexpected error: %v", err)
} else if gotString := strings.TrimSpace(string(got)); gotString != expectedString {
t.Errorf("got: %v, wanted: %v", gotString, expectedString)
Expand Down

0 comments on commit 8b70a13

Please sign in to comment.