Skip to content

Commit

Permalink
Command empty strings (#839)
Browse files Browse the repository at this point in the history
* Added ctx object to all system struct's constructors

The idea behind this commit is to set the stage for adding aditional
logging metadata.

* Changed: Improved logging

* Added new flags `--log-level` and `-l` to be consistent with other goss
flags.
  * `--loglevel` and `-L` will be deprecated in a future release
* Log level accepts any casing, so DEBUG and debug are both valid cli opts
* Add log level support for `add` and `autoadd` commands

* Changed: Goss add command default to empty string

Changed the behavior of `goss add` on a command, if the value of stdout or stderr is
empty, it will be defaulted to an empty string instead of an empty
array. The behavior remains unchanged when there is output; the value
will be an array.

This approach provides the following benefits:
* Tests will fail if an empty string receives a value.
* Arrays allows for simpler deletion of lines that are not relevant
to the test assertion.

closes #820

* fix build error
  • Loading branch information
aelsabbahy authored Sep 14, 2023
1 parent b1ffb25 commit b30f3fa
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 48 deletions.
12 changes: 6 additions & 6 deletions integration-tests/goss/alpine3/goss-expected-q.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ package:
vim-tiny:
installed: false
addr:
tcp://google.com:22:
tcp://httpbin:22:
reachable: false
timeout: 1000
tcp://google.com:443:
tcp://httpbin:80:
reachable: true
timeout: 1000
udp://8.8.8.8:53:
Expand Down Expand Up @@ -49,13 +49,13 @@ group:
command:
echo 'hi':
exit-status: 0
stdout: []
stderr: []
stdout: ""
stderr: ""
timeout: 10000
foobar:
exit-status: 127
stdout: []
stderr: []
stdout: ""
stderr: ""
timeout: 10000
dns:
CAA:dnstest.io:
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/goss/alpine3/goss-expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package:
vim-tiny:
installed: false
addr:
tcp://google.com:22:
tcp://httpbin:22:
reachable: false
timeout: 1000
tcp://google.com:443:
tcp://httpbin:80:
reachable: true
timeout: 1000
udp://8.8.8.8:53:
Expand Down Expand Up @@ -62,11 +62,11 @@ command:
exit-status: 0
stdout:
- hi
stderr: []
stderr: ""
timeout: 10000
foobar:
exit-status: 127
stdout: []
stdout: ""
stderr:
- 'sh: foobar: not found'
timeout: 10000
Expand Down
12 changes: 6 additions & 6 deletions integration-tests/goss/centos7/goss-expected-q.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ package:
vim-tiny:
installed: false
addr:
tcp://google.com:22:
tcp://httpbin:22:
reachable: false
timeout: 1000
tcp://google.com:443:
tcp://httpbin:80:
reachable: true
timeout: 1000
udp://8.8.8.8:53:
Expand Down Expand Up @@ -49,13 +49,13 @@ group:
command:
echo 'hi':
exit-status: 0
stdout: []
stderr: []
stdout: ""
stderr: ""
timeout: 10000
foobar:
exit-status: 127
stdout: []
stderr: []
stdout: ""
stderr: ""
timeout: 10000
dns:
CAA:dnstest.io:
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/goss/centos7/goss-expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package:
vim-tiny:
installed: false
addr:
tcp://google.com:22:
tcp://httpbin:22:
reachable: false
timeout: 1000
tcp://google.com:443:
tcp://httpbin:80:
reachable: true
timeout: 1000
udp://8.8.8.8:53:
Expand Down Expand Up @@ -68,11 +68,11 @@ command:
exit-status: 0
stdout:
- hi
stderr: []
stderr: ""
timeout: 10000
foobar:
exit-status: 127
stdout: []
stdout: ""
stderr:
- 'sh: foobar: command not found'
timeout: 10000
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/goss/generate_goss.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ done
[[ $OS == "centos7" ]] && user="apache" || user="www-data"
goss a "${args[@]}" package $package foobar vim-tiny

goss a "${args[@]}" addr --timeout 1s google.com:443 google.com:22
goss a "${args[@]}" addr --timeout 1s httpbin:80 httpbin:22

goss a "${args[@]}" addr --timeout 1s udp://8.8.8.8:53

Expand Down
6 changes: 3 additions & 3 deletions integration-tests/goss/goss-shared.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ service:
runlevels: {{toJson $runlevels}}
{{end}}
addr:
tcp://google.com:22:
tcp://httpbin:22:
reachable: false
timeout: 1000
tcp://google.com:443:
tcp://httpbin:80:
reachable: true
timeout: 5000
tcp://google.com:80:
tcp://httpbin:999:
reachable: false
timeout: 5000
local-address: 127.0.0.1
Expand Down
12 changes: 6 additions & 6 deletions integration-tests/goss/trusty/goss-expected-q.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ package:
vim-tiny:
installed: false
addr:
tcp://google.com:22:
tcp://httpbin:22:
reachable: false
timeout: 1000
tcp://google.com:443:
tcp://httpbin:80:
reachable: true
timeout: 1000
udp://8.8.8.8:53:
Expand Down Expand Up @@ -49,13 +49,13 @@ group:
command:
echo 'hi':
exit-status: 0
stdout: []
stderr: []
stdout: ""
stderr: ""
timeout: 10000
foobar:
exit-status: 127
stdout: []
stderr: []
stdout: ""
stderr: ""
timeout: 10000
dns:
CAA:dnstest.io:
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/goss/trusty/goss-expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package:
vim-tiny:
installed: false
addr:
tcp://google.com:22:
tcp://httpbin:22:
reachable: false
timeout: 1000
tcp://google.com:443:
tcp://httpbin:80:
reachable: true
timeout: 1000
udp://8.8.8.8:53:
Expand Down Expand Up @@ -68,11 +68,11 @@ command:
exit-status: 0
stdout:
- hi
stderr: []
stderr: ""
timeout: 10000
foobar:
exit-status: 127
stdout: []
stdout: ""
stderr:
- 'sh: 1: foobar: not found'
timeout: 10000
Expand Down
12 changes: 6 additions & 6 deletions integration-tests/goss/wheezy/goss-expected-q.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ package:
vim-tiny:
installed: false
addr:
tcp://google.com:22:
tcp://httpbin:22:
reachable: false
timeout: 1000
tcp://google.com:443:
tcp://httpbin:80:
reachable: true
timeout: 1000
udp://8.8.8.8:53:
Expand Down Expand Up @@ -49,13 +49,13 @@ group:
command:
echo 'hi':
exit-status: 0
stdout: []
stderr: []
stdout: ""
stderr: ""
timeout: 10000
foobar:
exit-status: 127
stdout: []
stderr: []
stdout: ""
stderr: ""
timeout: 10000
dns:
CAA:dnstest.io:
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/goss/wheezy/goss-expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package:
vim-tiny:
installed: false
addr:
tcp://google.com:22:
tcp://httpbin:22:
reachable: false
timeout: 1000
tcp://google.com:443:
tcp://httpbin:80:
reachable: true
timeout: 1000
udp://8.8.8.8:53:
Expand Down Expand Up @@ -68,11 +68,11 @@ command:
exit-status: 0
stdout:
- hi
stderr: []
stderr: ""
timeout: 10000
foobar:
exit-status: 127
stdout: []
stdout: ""
stderr:
- 'sh: 1: foobar: not found'
timeout: 10000
Expand Down
14 changes: 10 additions & 4 deletions resource/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,24 @@ func NewCommand(sysCommand system.Command, config util.Config) (*Command, error)
c := &Command{
id: command,
ExitStatus: exitStatus,
Stdout: []string{},
Stderr: []string{},
Stdout: "",
Stderr: "",
Timeout: config.TimeOutMilliSeconds(),
}

if !contains(config.IgnoreList, "stdout") {
stdout, _ := sysCommand.Stdout()
c.Stdout = readerToSlice(stdout)
outSlice := readerToSlice(stdout)
if len(outSlice) != 0 {
c.Stdout = outSlice
}
}
if !contains(config.IgnoreList, "stderr") {
stderr, _ := sysCommand.Stderr()
c.Stderr = readerToSlice(stderr)
errSlice := readerToSlice(stderr)
if len(errSlice) != 0 {
c.Stderr = errSlice
}
}

return c, err
Expand Down

0 comments on commit b30f3fa

Please sign in to comment.