1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
package resource
import (
"bufio"
"context"
"fmt"
"io"
"strings"
"time"
"github.com/goss-org/goss/system"
"github.com/goss-org/goss/util"
)
type Command struct {
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
id string `json:"-" yaml:"-"`
Exec string `json:"exec,omitempty" yaml:"exec,omitempty"`
ExitStatus matcher `json:"exit-status" yaml:"exit-status"`
Stdout matcher `json:"stdout" yaml:"stdout"`
Stderr matcher `json:"stderr" yaml:"stderr"`
Timeout int `json:"timeout" yaml:"timeout"`
Skip bool `json:"skip,omitempty" yaml:"skip,omitempty"`
}
const (
CommandResourceKey = "command"
CommandResourceName = "Command"
)
func init() {
registerResource(CommandResourceKey, &Command{})
}
func (c *Command) ID() string { return c.id }
func (c *Command) SetID(id string) { c.id = id }
func (c *Command) SetSkip() { c.Skip = true }
func (c *Command) TypeKey() string { return CommandResourceKey }
func (c *Command) TypeName() string { return CommandResourceName }
func (c *Command) GetTitle() string { return c.Title }
func (c *Command) GetMeta() meta { return c.Meta }
func (c *Command) GetExec() string {
if c.Exec != "" {
return c.Exec
}
return c.id
}
func (c *Command) Validate(sys *system.System) []TestResult {
ctx := context.WithValue(context.Background(), idKey{}, c.ID())
skip := c.Skip
if c.Timeout == 0 {
c.Timeout = 10000
}
var results []TestResult
sysCommand := sys.NewCommand(ctx, c.GetExec(), sys, util.Config{Timeout: time.Duration(c.Timeout) * time.Millisecond})
cExitStatus := deprecateAtoI(c.ExitStatus, fmt.Sprintf("%s: command.exit-status", c.ID()))
results = append(results, ValidateValue(c, "exit-status", cExitStatus, sysCommand.ExitStatus, skip))
if isSet(c.Stdout) {
results = append(results, ValidateValue(c, "stdout", c.Stdout, sysCommand.Stdout, skip))
}
if isSet(c.Stderr) {
results = append(results, ValidateValue(c, "stderr", c.Stderr, sysCommand.Stderr, skip))
}
return results
}
func NewCommand(sysCommand system.Command, config util.Config) (*Command, error) {
command := sysCommand.Command()
exitStatus, err := sysCommand.ExitStatus()
c := &Command{
id: command,
ExitStatus: exitStatus,
Stdout: "",
Stderr: "",
Timeout: config.TimeOutMilliSeconds(),
}
if !contains(config.IgnoreList, "stdout") {
stdout, _ := sysCommand.Stdout()
outSlice := readerToSlice(stdout)
if len(outSlice) != 0 {
c.Stdout = outSlice
}
}
if !contains(config.IgnoreList, "stderr") {
stderr, _ := sysCommand.Stderr()
errSlice := readerToSlice(stderr)
if len(errSlice) != 0 {
c.Stderr = errSlice
}
}
return c, err
}
func escapePattern(s string) string {
if strings.HasPrefix(s, "!") || strings.HasPrefix(s, "/") {
return "\\" + s
}
return s
}
func readerToSlice(reader io.Reader) []string {
scanner := bufio.NewScanner(reader)
slice := []string{}
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
line = escapePattern(line)
if line != "" {
slice = append(slice, line)
}
}
return slice
}
|