Skip to content

Commit 29c436f

Browse files
authored
Add context to RunBinary interface (#5384)
Signed-off-by: khanhtc1202 <[email protected]>
1 parent 1907301 commit 29c436f

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

pkg/app/launcher/cmd/launcher/launcher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func (l *launcher) run(ctx context.Context, input cli.Input) error {
306306
}
307307

308308
// Start new piped process.
309-
runningPiped, err = l.launchNewPiped(version, config, workingDir, input.Logger)
309+
runningPiped, err = l.launchNewPiped(ctx, version, config, workingDir, input.Logger)
310310
if err != nil {
311311
input.Logger.Error("LAUNCHER: failed while launching new Piped", zap.Error(err))
312312
return err
@@ -404,7 +404,7 @@ func (l *launcher) cleanOldPiped(cmd *lifecycle.Command, workingDir string, logg
404404
return nil
405405
}
406406

407-
func (l *launcher) launchNewPiped(version string, config []byte, workingDir string, logger *zap.Logger) (*lifecycle.Command, error) {
407+
func (l *launcher) launchNewPiped(ctx context.Context, version string, config []byte, workingDir string, logger *zap.Logger) (*lifecycle.Command, error) {
408408
if err := os.MkdirAll(workingDir, 0755); err != nil {
409409
return nil, fmt.Errorf("could not create working directory %s (%w)", workingDir, err)
410410
}
@@ -436,7 +436,7 @@ func (l *launcher) launchNewPiped(version string, config []byte, workingDir stri
436436
args := makePipedArgs(os.Args[2:], configFilePath)
437437
logger.Info(fmt.Sprintf("LAUNCHER: start running Piped %s with args %v", version, args))
438438

439-
return lifecycle.RunBinary(pipedPath, args)
439+
return lifecycle.RunBinary(ctx, pipedPath, args)
440440
}
441441

442442
func (l *launcher) loadConfigData(ctx context.Context) ([]byte, error) {

pkg/lifecycle/binary.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func (c *Command) GracefulStop(period time.Duration) error {
7070
}
7171
}
7272

73-
func RunBinary(execPath string, args []string) (*Command, error) {
74-
cmd, err := backoff.NewRetry(runBinaryRetryCount, backoff.NewConstant(5*time.Second)).Do(context.Background(), func() (interface{}, error) {
73+
func RunBinary(ctx context.Context, execPath string, args []string) (*Command, error) {
74+
cmd, err := backoff.NewRetry(runBinaryRetryCount, backoff.NewConstant(5*time.Second)).Do(ctx, func() (interface{}, error) {
7575
cmd := exec.Command(execPath, args...)
7676
cmd.Stdin = os.Stdin
7777
cmd.Stdout = os.Stdout

pkg/lifecycle/binary_test.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
package lifecycle
1616

1717
import (
18+
"context"
19+
"net/http"
20+
"net/http/httptest"
1821
"strconv"
1922
"testing"
2023
"time"
2124

2225
"github.com/stretchr/testify/assert"
2326
"github.com/stretchr/testify/require"
27+
"go.uber.org/zap/zaptest"
2428
)
2529

2630
func TestGracefulStopCommand(t *testing.T) {
@@ -40,7 +44,7 @@ func TestGracefulStopCommand(t *testing.T) {
4044

4145
for _, tc := range testcases {
4246
t.Run(tc.name, func(t *testing.T) {
43-
cmd, err := RunBinary("sh", []string{"sleep", "1m"})
47+
cmd, err := RunBinary(context.TODO(), "sh", []string{"sleep", "1m"})
4448
require.NoError(t, err)
4549
require.NotNil(t, cmd)
4650

@@ -71,7 +75,7 @@ func TestGracefulStopCommandResult(t *testing.T) {
7175

7276
for _, tc := range testcases {
7377
t.Run(tc.name, func(t *testing.T) {
74-
cmd, err := RunBinary("sh", []string{"-c", "exit " + strconv.Itoa(tc.exitCode)})
78+
cmd, err := RunBinary(context.TODO(), "sh", []string{"-c", "exit " + strconv.Itoa(tc.exitCode)})
7579
require.NoError(t, err)
7680
require.NotNil(t, cmd)
7781

@@ -81,3 +85,42 @@ func TestGracefulStopCommandResult(t *testing.T) {
8185
})
8286
}
8387
}
88+
89+
func TestDownloadBinary(t *testing.T) {
90+
server := httpTestServer()
91+
defer server.Close()
92+
93+
logger := zaptest.NewLogger(t)
94+
destDir := t.TempDir()
95+
destFile := "test-binary"
96+
97+
t.Run("successful download", func(t *testing.T) {
98+
url := server.URL + "/binary"
99+
path, err := DownloadBinary(url, destDir, destFile, logger)
100+
require.NoError(t, err)
101+
assert.FileExists(t, path)
102+
})
103+
104+
t.Run("file already exists", func(t *testing.T) {
105+
url := server.URL + "/binary"
106+
path, err := DownloadBinary(url, destDir, destFile, logger)
107+
require.NoError(t, err)
108+
assert.FileExists(t, path)
109+
110+
// Try downloading again, should not error and file should still exist
111+
path, err = DownloadBinary(url, destDir, destFile, logger)
112+
require.NoError(t, err)
113+
assert.FileExists(t, path)
114+
})
115+
}
116+
117+
func httpTestServer() *httptest.Server {
118+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
119+
if r.URL.Path == "/binary" {
120+
w.WriteHeader(http.StatusOK)
121+
w.Write([]byte("test binary content"))
122+
} else {
123+
w.WriteHeader(http.StatusNotFound)
124+
}
125+
}))
126+
}

0 commit comments

Comments
 (0)