-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from umputun/program
Program provider
- Loading branch information
Showing
7 changed files
with
184 additions
and
10 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
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,67 @@ | ||
package external | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// ProgramProvider is an external service that runs a command and checks the exit code. | ||
type ProgramProvider struct { | ||
WithShell bool | ||
TimeOut time.Duration | ||
} | ||
|
||
// Status returns the status of the execution of the command from the request. | ||
// url looks like this: program://cat?args=/tmp/foo | ||
func (p *ProgramProvider) Status(req Request) (*Response, error) { | ||
st := time.Now() | ||
ctx, cancel := context.WithTimeout(context.Background(), p.TimeOut) | ||
defer cancel() | ||
|
||
resp := Response{ | ||
Name: req.Name, | ||
StatusCode: 200, | ||
} | ||
|
||
command := strings.TrimPrefix(req.URL, "program://") | ||
args := "" | ||
if strings.Contains(command, "?args=") { | ||
elems := strings.Split(command, "?args=") | ||
command, args = elems[0], elems[1] | ||
} | ||
|
||
log.Printf("[DEBUG] command: %s %s", command, args) | ||
|
||
cmd := exec.CommandContext(ctx, command, args) //nolint:gosec // we trust the command as it comes from the config | ||
if p.WithShell { | ||
command = fmt.Sprintf("sh -c %q", command+" "+args) | ||
} | ||
stdOut, stdErr := bytes.NewBuffer(nil), bytes.NewBuffer(nil) | ||
cmd.Stdout = stdOut | ||
cmd.Stderr = stdErr | ||
cmd.Stdin = os.Stdin | ||
|
||
err := cmd.Run() | ||
resp.ResponseTime = time.Since(st).Milliseconds() | ||
|
||
res := map[string]interface{}{ | ||
"command": command + " " + args, | ||
"stdout": stdOut.String(), | ||
"stderr": stdErr.String(), | ||
"status": "ok", | ||
} | ||
|
||
if err != nil { | ||
res["status"] = err.Error() | ||
resp.StatusCode = 500 | ||
} | ||
|
||
resp.Body = res | ||
return &resp, nil | ||
} |
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,64 @@ | ||
package external | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestProgram_StatusWithShell(t *testing.T) { | ||
p := ProgramProvider{WithShell: true, TimeOut: time.Second} | ||
|
||
{ | ||
req := Request{Name: "test", URL: `program://ls?args=-la`} | ||
resp, err := p.Status(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "test", resp.Name) | ||
assert.Equal(t, 200, resp.StatusCode) | ||
assert.Equal(t, "ok", resp.Body["status"]) | ||
assert.Contains(t, resp.Body["stdout"], "program.go") | ||
t.Logf("%+v", resp) | ||
} | ||
{ | ||
req := Request{Name: "test", URL: `program://testdata/test.sh`} | ||
resp, err := p.Status(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "test", resp.Name) | ||
assert.Equal(t, 200, resp.StatusCode) | ||
assert.Equal(t, "ok", resp.Body["status"]) | ||
assert.Contains(t, resp.Body["stdout"], "Hello, World!") | ||
} | ||
{ | ||
req := Request{Name: "test", URL: `program://blah?args=-la`} | ||
resp, err := p.Status(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "test", resp.Name) | ||
assert.Equal(t, 500, resp.StatusCode) | ||
assert.Contains(t, resp.Body["status"], "file not found") | ||
} | ||
} | ||
|
||
func TestProgram_StatusWithoutShell(t *testing.T) { | ||
p := ProgramProvider{WithShell: true, TimeOut: time.Second} | ||
|
||
{ | ||
req := Request{Name: "test", URL: `program://cat?args=program.go`} | ||
resp, err := p.Status(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "test", resp.Name) | ||
assert.Equal(t, 200, resp.StatusCode) | ||
assert.Equal(t, "ok", resp.Body["status"]) | ||
assert.Contains(t, resp.Body["stdout"], "CommandContext") | ||
} | ||
{ | ||
req := Request{Name: "test", URL: `program://cat?args=blah`} | ||
resp, err := p.Status(req) | ||
require.NoError(t, err) | ||
assert.Equal(t, "test", resp.Name) | ||
assert.Equal(t, 500, resp.StatusCode) | ||
assert.Contains(t, resp.Body["status"], "exit status 1", resp.Body["status"]) | ||
t.Logf("%+v", resp) | ||
} | ||
} |
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 @@ | ||
#!/usr/bin/env sh | ||
|
||
echo "Hello, World!" |