Skip to content

Commit

Permalink
Merge pull request #243 from kacf/1.2.x
Browse files Browse the repository at this point in the history
1.2.x cherry-picks
  • Loading branch information
kacf authored Aug 30, 2017
2 parents ac73f5c + 58b0add commit 23ea2cf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
27 changes: 27 additions & 0 deletions statescript/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package statescript

import (
"io"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -169,6 +170,16 @@ func execute(name string, timeout time.Duration) int {

cmd := exec.Command(name)

var stderr io.ReadCloser
var err error

if !strings.HasPrefix(name, "Idle") && !strings.HasPrefix(name, "Sync") {
stderr, err = cmd.StderrPipe()
if err != nil {
log.Errorf("statescript: %v", err)
}
}

// As child process gets the same PGID as the parent by default, in order
// to avoid killing Mender when killing process group we are setting
// new PGID for the executed script and its children.
Expand All @@ -178,6 +189,22 @@ func execute(name string, timeout time.Duration) int {
return retCode(err)
}

var bts []byte
if stderr != nil {
bts, err = ioutil.ReadAll(stderr)
if err != nil {
log.Error(err)
}
}

if len(bts) > 0 {
if len(bts) > 10*1024 {
log.Errorf("stderr collected while running script %s [%s] (Truncated to 10KB)", name, bts[:10*1024])
} else {
log.Errorf("stderr collected while running script %s [%s]", name, string(bts))
}
}

timer := time.AfterFunc(timeout, func() {
// In addition to kill a single process we are sending SIGKILL to
// process group making sure we are killing the hanging script and
Expand Down
21 changes: 21 additions & 0 deletions statescript/statescript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"testing"

"github.com/mendersoftware/log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -190,6 +191,26 @@ func TestExecutor(t *testing.T) {
sysInstallScripts, _, err = e.get("ArtifactInstall", "Leave")
testArtifactArrayEquals(t, scriptArr[1:], sysInstallScripts)
assert.NoError(t, err)

// Test script logging
var buf bytes.Buffer
oldOut := log.Log.Out
defer log.SetOutput(oldOut)
log.SetOutput(&buf)
fileP, err := createArtifactTestScript(tmpArt, "ArtifactInstall_Leave_00", "#!/bin/bash \necho 'error data' >&2")
assert.NoError(t, err)
res := execute(fileP.Name(), 100) // give the script plenty of time to run
assert.Equal(t, 0, res)
assert.Contains(t, buf.String(), "error data")

buf.Reset()

// write more than 10KB to stderr
fileP, err = createArtifactTestScript(tmpArt, "ArtifactInstall_Leave_11", "#!/bin/bash \nhead -c 89999 </dev/urandom >&2\n exit 1")
assert.NoError(t, err)
res = execute(fileP.Name(), 100)
assert.Equal(t, 1, res)
assert.Contains(t, buf.String(), "Truncated to 10KB")
}

func TestVersion(t *testing.T) {
Expand Down

0 comments on commit 23ea2cf

Please sign in to comment.