Search code examples
rubyregexchef-infraserverspec

Chef ServerSpec Regex Match Not Working


I'm trying to math a regex to the stdout of a command. Even though the regex should match it doesn't seem to be doing as such:

> [#]        Command "pm2 list"
> [#]          stdout
> [#]            should match /.*online.*/ (FAILED - 1)
> [#]
> [#]        Failures:
> [#]
> [#]          1) Command "pm2 list" stdout should match /.*online.*/
> [#]             Failure/Error: its(:stdout) { should match /.*online.*/}
> [#]             ArgumentError:
> [#]               invalid byte sequence in US-ASCII
> [#]               /bin/sh -c pm2\ list
> [#]               ┌──────────┬────┬──────┬──────┬────────┬───────────┬────────┬─────────────┬─────────────┐
> [#]        │ App name │ id │ mode │ PID  │ status │ restarted │ uptime │      memory │    watching │
> [#]        ├──────────┼────┼──────┼──────┼────────┼───────────┼────────┼─────────────┼─────────────┤
> [#]        │ app      │ 0  │ fork │ 3684 │ online │         0 │ 5m     │ 13.898 MB   │ unactivated │
> [#]        └──────────┴────┴──────┴──────┴────────┴───────────┴────────┴─────────────┴─────────────┘
> [#]         Use `pm2 desc[ribe] <id>` to get more details
> [#]
> [#]             # /tmp/busser/suites/serverspec/localhost/nodejs_spec.rb:10:in `block (2 levels) in <top (required)>'
> [#]
> [#]        Finished in 0.77549 seconds (files took 0.19867 seconds to load)
> [#]        3 examples, 1 failure

Why is the regex not matching?

A similar regex running inside the cookbook works just fine:

...
process_check = Mixlib::ShellOut.new("pm2 list")
process_check.run_command
if process_check.stdout =~ /(stopped|online)/
...

Solution

  • The actual error is the invalid byte sequence in US-ASCII. Mixlib-shellout is very careful to be encoding-aware, but this isn't what Serverspec uses to run commands AFAIK. The matcher is trying to convert (probably) UTF-8 data to an ASCII string, which is failing. Try should include('online') perhaps? You would have to dive the matcher code in rspec to be certain of how they process strings.