From b30f3fa72ba510e01f9a757bba355b0c2f81fa24 Mon Sep 17 00:00:00 2001 From: Ahmed Elsabbahy Date: Thu, 14 Sep 2023 12:23:10 -0700 Subject: [PATCH] Command empty strings (#839) * 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 --- .../goss/alpine3/goss-expected-q.yaml | 12 ++++++------ integration-tests/goss/alpine3/goss-expected.yaml | 8 ++++---- .../goss/centos7/goss-expected-q.yaml | 12 ++++++------ integration-tests/goss/centos7/goss-expected.yaml | 8 ++++---- integration-tests/goss/generate_goss.sh | 2 +- integration-tests/goss/goss-shared.yaml | 6 +++--- integration-tests/goss/trusty/goss-expected-q.yaml | 12 ++++++------ integration-tests/goss/trusty/goss-expected.yaml | 8 ++++---- integration-tests/goss/wheezy/goss-expected-q.yaml | 12 ++++++------ integration-tests/goss/wheezy/goss-expected.yaml | 8 ++++---- resource/command.go | 14 ++++++++++---- 11 files changed, 54 insertions(+), 48 deletions(-) diff --git a/integration-tests/goss/alpine3/goss-expected-q.yaml b/integration-tests/goss/alpine3/goss-expected-q.yaml index e6f17cf03..075377f84 100644 --- a/integration-tests/goss/alpine3/goss-expected-q.yaml +++ b/integration-tests/goss/alpine3/goss-expected-q.yaml @@ -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: @@ -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: diff --git a/integration-tests/goss/alpine3/goss-expected.yaml b/integration-tests/goss/alpine3/goss-expected.yaml index ae02df57a..b9cea18c8 100644 --- a/integration-tests/goss/alpine3/goss-expected.yaml +++ b/integration-tests/goss/alpine3/goss-expected.yaml @@ -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: @@ -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 diff --git a/integration-tests/goss/centos7/goss-expected-q.yaml b/integration-tests/goss/centos7/goss-expected-q.yaml index 349eda8f9..1ea0ac5c7 100644 --- a/integration-tests/goss/centos7/goss-expected-q.yaml +++ b/integration-tests/goss/centos7/goss-expected-q.yaml @@ -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: @@ -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: diff --git a/integration-tests/goss/centos7/goss-expected.yaml b/integration-tests/goss/centos7/goss-expected.yaml index b64092f12..b9c60d9b9 100644 --- a/integration-tests/goss/centos7/goss-expected.yaml +++ b/integration-tests/goss/centos7/goss-expected.yaml @@ -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: @@ -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 diff --git a/integration-tests/goss/generate_goss.sh b/integration-tests/goss/generate_goss.sh index 93e822fb6..16ff04186 100755 --- a/integration-tests/goss/generate_goss.sh +++ b/integration-tests/goss/generate_goss.sh @@ -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 diff --git a/integration-tests/goss/goss-shared.yaml b/integration-tests/goss/goss-shared.yaml index cb3a0acd4..4fdef9148 100644 --- a/integration-tests/goss/goss-shared.yaml +++ b/integration-tests/goss/goss-shared.yaml @@ -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 diff --git a/integration-tests/goss/trusty/goss-expected-q.yaml b/integration-tests/goss/trusty/goss-expected-q.yaml index 9554f8e82..507dcb7a5 100644 --- a/integration-tests/goss/trusty/goss-expected-q.yaml +++ b/integration-tests/goss/trusty/goss-expected-q.yaml @@ -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: @@ -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: diff --git a/integration-tests/goss/trusty/goss-expected.yaml b/integration-tests/goss/trusty/goss-expected.yaml index 864969477..5b2ad9b00 100644 --- a/integration-tests/goss/trusty/goss-expected.yaml +++ b/integration-tests/goss/trusty/goss-expected.yaml @@ -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: @@ -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 diff --git a/integration-tests/goss/wheezy/goss-expected-q.yaml b/integration-tests/goss/wheezy/goss-expected-q.yaml index 9554f8e82..507dcb7a5 100644 --- a/integration-tests/goss/wheezy/goss-expected-q.yaml +++ b/integration-tests/goss/wheezy/goss-expected-q.yaml @@ -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: @@ -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: diff --git a/integration-tests/goss/wheezy/goss-expected.yaml b/integration-tests/goss/wheezy/goss-expected.yaml index 495c87274..209ae8564 100644 --- a/integration-tests/goss/wheezy/goss-expected.yaml +++ b/integration-tests/goss/wheezy/goss-expected.yaml @@ -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: @@ -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 diff --git a/resource/command.go b/resource/command.go index 6bd75f70c..79a1671e7 100644 --- a/resource/command.go +++ b/resource/command.go @@ -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