Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resource/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (a *Addr) Validate(sys *system.System) []TestResult {
a.Timeout = 500
}

sysAddr := sys.NewAddr(ctx, a.GetAddress(), sys, util.Config{Timeout: time.Duration(a.Timeout) * time.Millisecond, LocalAddress: a.LocalAddress})
sysAddr, _ := sys.NewAddr(ctx, a.GetAddress(), sys, util.Config{Timeout: time.Duration(a.Timeout) * time.Millisecond, LocalAddress: a.LocalAddress})

var results []TestResult
results = append(results, ValidateValue(a, "reachable", a.Reachable, sysAddr.Reachable, skip))
Expand Down
76 changes: 52 additions & 24 deletions resource/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"log"
"strings"
"time"

Expand All @@ -13,15 +14,15 @@ import (
)

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"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
id string `json:"-" yaml:"-"`
Exec util.ExecCommand `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 (
Expand All @@ -41,11 +42,14 @@ 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
func (c *Command) GetExec() interface{} {
if c.Exec.CmdStr != "" {
return c.Exec.CmdStr
} else if len(c.Exec.CmdSlice) > 0 {
return c.Exec.CmdSlice
} else {
return c.id
}
return c.id
}

func (c *Command) Validate(sys *system.System) []TestResult {
Expand All @@ -57,24 +61,48 @@ func (c *Command) Validate(sys *system.System) []TestResult {
}

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))
sysCommand, err := sys.NewCommand(ctx, c.GetExec(), sys, util.Config{Timeout: time.Duration(c.Timeout) * time.Millisecond})
if err != nil {
log.Printf("[ERROR] Could not create new command: %v", err)
startTime := time.Now()
results = append(
results,
TestResult{
Result: FAIL,
ResourceType: "Command",
ResourceId: c.id,
Title: c.Title,
Meta: c.Meta,
Property: "type",
Err: toValidateError(err),
StartTime: startTime,
EndTime: startTime,
Duration: startTime.Sub(startTime),
},
)
} else {
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()
var id string
if sysCommand.Command().CmdStr != "" {
id = sysCommand.Command().CmdStr
} else {
id = sysCommand.Command().CmdSlice[0]
}
exitStatus, err := sysCommand.ExitStatus()
c := &Command{
id: command,
id: id,
ExitStatus: exitStatus,
Stdout: "",
Stderr: "",
Expand Down
2 changes: 1 addition & 1 deletion resource/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (d *DNS) Validate(sys *system.System) []TestResult {
d.Timeout = 500
}

sysDNS := sys.NewDNS(ctx, d.GetResolve(), sys, util.Config{Timeout: time.Duration(d.Timeout) * time.Millisecond, Server: d.Server})
sysDNS, _ := sys.NewDNS(ctx, d.GetResolve(), sys, util.Config{Timeout: time.Duration(d.Timeout) * time.Millisecond, Server: d.Server})

var results []TestResult
// Backwards compatibility hack for now
Expand Down
2 changes: 1 addition & 1 deletion resource/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (f *File) GetPath() string {
func (f *File) Validate(sys *system.System) []TestResult {
ctx := context.WithValue(context.Background(), "id", f.ID())
skip := f.Skip
sysFile := sys.NewFile(ctx, f.GetPath(), sys, util.Config{})
sysFile, _ := sys.NewFile(ctx, f.GetPath(), sys, util.Config{})

var results []TestResult
results = append(results, ValidateValue(f, "exists", f.Exists, sysFile.Exists, skip))
Expand Down
2 changes: 1 addition & 1 deletion resource/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (g *Group) GetGroupname() string {
func (g *Group) Validate(sys *system.System) []TestResult {
ctx := context.WithValue(context.Background(), "id", g.ID())
skip := g.Skip
sysgroup := sys.NewGroup(ctx, g.GetGroupname(), sys, util.Config{})
sysgroup, _ := sys.NewGroup(ctx, g.GetGroupname(), sys, util.Config{})

var results []TestResult
results = append(results, ValidateValue(g, "exists", g.Exists, sysgroup.Exists, skip))
Expand Down
2 changes: 1 addition & 1 deletion resource/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (u *HTTP) Validate(sys *system.System) []TestResult {
if u.Timeout == 0 {
u.Timeout = 5000
}
sysHTTP := sys.NewHTTP(ctx, u.getURL(), sys, util.Config{
sysHTTP, _ := sys.NewHTTP(ctx, u.getURL(), sys, util.Config{
AllowInsecure: u.AllowInsecure,
CAFile: u.CAFile,
CertFile: u.CertFile,
Expand Down
2 changes: 1 addition & 1 deletion resource/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (i *Interface) GetName() string {
func (i *Interface) Validate(sys *system.System) []TestResult {
ctx := context.WithValue(context.Background(), "id", i.ID())
skip := i.Skip
sysInterface := sys.NewInterface(ctx, i.GetName(), sys, util.Config{})
sysInterface, _ := sys.NewInterface(ctx, i.GetName(), sys, util.Config{})

var results []TestResult
results = append(results, ValidateValue(i, "exists", i.Exists, sysInterface.Exists, skip))
Expand Down
2 changes: 1 addition & 1 deletion resource/kernel_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (k *KernelParam) GetName() string {
func (k *KernelParam) Validate(sys *system.System) []TestResult {
ctx := context.WithValue(context.Background(), "id", k.ID())
skip := k.Skip
sysKernelParam := sys.NewKernelParam(ctx, k.GetName(), sys, util.Config{})
sysKernelParam, _ := sys.NewKernelParam(ctx, k.GetName(), sys, util.Config{})

var results []TestResult
results = append(results, ValidateValue(k, "value", k.Value, sysKernelParam.Value, skip))
Expand Down
2 changes: 1 addition & 1 deletion resource/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (m *Mount) GetMountPoint() string {
func (m *Mount) Validate(sys *system.System) []TestResult {
ctx := context.WithValue(context.Background(), "id", m.ID())
skip := m.Skip
sysMount := sys.NewMount(ctx, m.GetMountPoint(), sys, util.Config{})
sysMount, _ := sys.NewMount(ctx, m.GetMountPoint(), sys, util.Config{})

var results []TestResult
results = append(results, ValidateValue(m, "exists", m.Exists, sysMount.Exists, skip))
Expand Down
2 changes: 1 addition & 1 deletion resource/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p *Package) GetName() string {
func (p *Package) Validate(sys *system.System) []TestResult {
ctx := context.WithValue(context.Background(), "id", p.ID())
skip := p.Skip
sysPkg := sys.NewPackage(ctx, p.GetName(), sys, util.Config{})
sysPkg, _ := sys.NewPackage(ctx, p.GetName(), sys, util.Config{})

var results []TestResult
results = append(results, ValidateValue(p, "installed", p.Installed, sysPkg.Installed, skip))
Expand Down
2 changes: 1 addition & 1 deletion resource/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p *Port) GetPort() string {
func (p *Port) Validate(sys *system.System) []TestResult {
ctx := context.WithValue(context.Background(), "id", p.ID())
skip := p.Skip
sysPort := sys.NewPort(ctx, p.GetPort(), sys, util.Config{})
sysPort, _ := sys.NewPort(ctx, p.GetPort(), sys, util.Config{})

var results []TestResult
results = append(results, ValidateValue(p, "listening", p.Listening, sysPort.Listening, skip))
Expand Down
2 changes: 1 addition & 1 deletion resource/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (p *Process) GetComm() string {
func (p *Process) Validate(sys *system.System) []TestResult {
ctx := context.WithValue(context.Background(), "id", p.ID())
skip := p.Skip
sysProcess := sys.NewProcess(ctx, p.GetComm(), sys, util.Config{})
sysProcess, _ := sys.NewProcess(ctx, p.GetComm(), sys, util.Config{})

var results []TestResult
results = append(results, ValidateValue(p, "running", p.Running, sysProcess.Running, skip))
Expand Down
Loading