Skip to content

Commit

Permalink
chkutil: add CommandTimeout function
Browse files Browse the repository at this point in the history
  • Loading branch information
langston-barrett committed May 31, 2016
1 parent f2c1ed9 commit 224175c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
57 changes: 46 additions & 11 deletions chkutil/chkutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import (
"bytes"
"crypto/tls"
"errors"
"github.com/CiscoCloud/distributive/errutil"
"github.com/CiscoCloud/distributive/tabular"
log "github.com/Sirupsen/logrus"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"regexp"
"strconv"
"strings"
"time"

"github.com/CiscoCloud/distributive/errutil"
"github.com/CiscoCloud/distributive/tabular"
log "github.com/Sirupsen/logrus"
)

// Check is a unified interface for health checks, it defines only the minimal
Expand Down Expand Up @@ -40,16 +43,16 @@ type MakeCheckT func() Check
var registry = map[string]MakeCheckT{}

func Register(name string, check MakeCheckT) {
lname := strings.ToLower(name)
registry[lname] = check
lname := strings.ToLower(name)
registry[lname] = check
}

func LookupCheck(name string) Check {
lname := strings.ToLower(name)
if makeCheckFn, ok := registry[lname]; ok {
return makeCheckFn()
}
return nil
lname := strings.ToLower(name)
if makeCheckFn, ok := registry[lname]; ok {
return makeCheckFn()
}
return nil
}

//// STRING UTILITIES
Expand All @@ -65,6 +68,38 @@ func CommandOutput(cmd *exec.Cmd) string {
return outStr
}

// CommandTimeout runs a command, and either returns its output as a string, or
// kills it if it takes longer than the given timeout to complete, returning an
// error.
func CommandTimeout(cmd *exec.Cmd, timeout time.Duration) (string, error) {
timedOut := time.After(timeout)

out := make(chan struct {
Out string
Err error
}, 1)

// Run the command, send the output and error back on the channel `out`
go func() {
outBytes, err := cmd.CombinedOutput()
out <- struct {
Out string
Err error
}{string(outBytes), err}
}()

select {
case cmdOutput := <-out:
return cmdOutput.Out, cmdOutput.Err
case <-timedOut:
err := cmd.Process.Kill()
if err != nil {
return "", fmt.Errorf("Error while killing timed out process %v: %v", cmd, err)
}
return "", fmt.Errorf("Command timed out: %v", cmd)
}
}

// CommandColumnNoHeader returns a specified column of the output of a command,
// without that column's header. Useful for parsing the output of shell commands,
// which many of the Checks require.
Expand All @@ -73,7 +108,7 @@ func CommandColumnNoHeader(col int, cmd *exec.Cmd) []string {
return tabular.GetColumnNoHeader(col, tabular.StringToSlice(out))
}

// SeparateByteUnits: The integer part of a string representing a size unit,
// SeparateByteUnits gets the integer part of a string representing a size unit,
// the unit: b | kb | mb | gb | tb, and an error if applicable.
// 90KB -> (90, kb, nil), 800ads -> (0, "", error)
// NOTE: this doesn't differentiate between kb and kib, and I don't know how
Expand Down
12 changes: 12 additions & 0 deletions chkutil/chkutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os/exec"
"strings"
"testing"
"time"
)

func TestCommandOutput(t *testing.T) {
Expand All @@ -28,6 +29,17 @@ func TestCommandOutput(t *testing.T) {
}
}

func TestCommandTimeout(t *testing.T) {
t.Parallel()
timeout, _ := time.ParseDuration(".1s")
if _, err := CommandTimeout(exec.Command("echo"), timeout); err != nil {
t.Errorf("CommandTimeout failed on `echo` with timeout .2s. The error was: %v", err)
}
if _, err := CommandTimeout(exec.Command("sleep", ".2"), timeout); err == nil {
t.Errorf("CommandTimeout succeeded on `sleep .2` with timeout .1s")
}
}

func TestSeparateByteUnits(t *testing.T) {
t.Parallel()
inputs := []string{
Expand Down

0 comments on commit 224175c

Please sign in to comment.