diff --git a/README.md b/README.md index faeb530..33e351c 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,15 @@ verifier: sudo_command: 'skittles' ``` +You can also specify the host and port to be used by InSpec when targeting the node. Otherwise, it defaults to the hostname and port used by kitchen for converging. + +```yaml +verifier: + name: inspec + host: 192.168.56.40 + port: 22 +``` + ### Directory Structure By default `kitchen-inspec` expects test to be in `test/integration/%suite%` directory structure (we use Chef as provisioner here): diff --git a/lib/kitchen/verifier/inspec.rb b/lib/kitchen/verifier/inspec.rb index b8b2e1a..75b11d9 100644 --- a/lib/kitchen/verifier/inspec.rb +++ b/lib/kitchen/verifier/inspec.rb @@ -169,8 +169,8 @@ def runner_options_for_ssh(config_data) # pass-in sudo config from kitchen verifier "sudo" => config[:sudo], "sudo_command" => config[:sudo_command], - "host" => kitchen[:hostname], - "port" => kitchen[:port], + "host" => config[:host] || kitchen[:hostname], + "port" => config[:port] || kitchen[:port], "user" => kitchen[:username], "keepalive" => kitchen[:keepalive], "keepalive_interval" => kitchen[:keepalive_interval], @@ -196,8 +196,8 @@ def runner_options_for_winrm(config_data) opts = { "backend" => "winrm", "logger" => logger, - "host" => URI(kitchen[:endpoint]).hostname, - "port" => URI(kitchen[:endpoint]).port, + "host" => config[:host] || URI(kitchen[:endpoint]).hostname, + "port" => config[:port] || URI(kitchen[:endpoint]).port, "user" => kitchen[:user], "password" => kitchen[:password] || kitchen[:pass], "connection_retries" => kitchen[:connection_retries], diff --git a/spec/kitchen/verifier/inspec_spec.rb b/spec/kitchen/verifier/inspec_spec.rb index d502526..2d9af8b 100644 --- a/spec/kitchen/verifier/inspec_spec.rb +++ b/spec/kitchen/verifier/inspec_spec.rb @@ -172,6 +172,23 @@ verifier.call(port: 123) end + it "constructs a Inspec::Runner using transport config data(host and port)" do + config[:host] = "192.168.33.40" + config[:port] = 222 + + expect(Inspec::Runner).to receive(:new) + .with( + hash_including( + "backend" => "ssh", + "host" => "192.168.33.40", + "port" => 222 + ) + ) + .and_return(runner) + + verifier.call(port: 123) + end + it "constructs an Inspec::Runner with a specified inspec output format" do config[:format] = "documentation" @@ -324,6 +341,23 @@ verifier.call(hostname: "win.dows", port: 123) end + + it "constructs a Inspec::Runner using transport config data(host and port)" do + config[:host] = "192.168.56.40" + config[:port] = 22 + + expect(Inspec::Runner).to receive(:new) + .with( + hash_including( + "backend" => "winrm", + "host" => "192.168.56.40", + "port" => 22 + ) + ) + .and_return(runner) + + verifier.call(hostname: "win.dows", port: 123) + end end context "with an unsupported transport" do