-
Notifications
You must be signed in to change notification settings - Fork 500
Command optional exec style #1016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,7 @@ definitions: | |
| default: 500 | ||
| examples: | ||
| - 500 | ||
| # optional attributes | ||
| # optional attributesf | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it was a typo. |
||
| local-address: | ||
| type: string | ||
| default: 127.0.0.1 | ||
|
|
@@ -47,8 +47,12 @@ definitions: | |
| type: integer | ||
| description: "Validates the exit-status and output of a command" | ||
| exec: | ||
| description: "command to execute, defaults to the hash key" | ||
| type: string | ||
| description: "command to execute, defaults to the hash key, see https://goss.rocks/gossfile#command" | ||
| oneOf: | ||
| - type: string | ||
| - type: array | ||
| items: | ||
| type: string | ||
| stdout: | ||
| type: array | ||
| description: "can be a string or pattern, see https://goss.rocks/gossfile#patterns" | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |||||||||||||||||||||
| "context" | ||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||
| "io" | ||||||||||||||||||||||
| "log" | ||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||
| "time" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -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 ( | ||||||||||||||||||||||
|
|
@@ -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() any { | ||||||||||||||||||||||
| if c.Exec.CmdStr != "" { | ||||||||||||||||||||||
| return c.Exec.CmdStr | ||||||||||||||||||||||
| } else if len(c.Exec.CmdSlice) > 0 { | ||||||||||||||||||||||
| return c.Exec.CmdSlice | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| return c.id | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+48
to
52
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
| return c.id | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func (c *Command) Validate(sys *system.System) []TestResult { | ||||||||||||||||||||||
|
|
@@ -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: "", | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.