LoginSignup
9
5

More than 5 years have passed since last update.

serverspecでローカルのコマンドを実行するには?(sshログインテスト)

Posted at

はじめに

非常に短いエントリですが同じことで困っている人もいるかも。と思いましてメモしておきます。

  • serverspecでsshのログインテストをやりたかったのですが、みなさんどうやってやっているのでしょうか。
  • よいアイディアがあれば教えてください。

私はこうやってやってみました。

  • expect使ってもよかったのですがshellを用意するのも面倒だったので、sshpassを使いました。
  • ansibleでいうlocal_actionがなかったので、systemを使ってローカルでコマンドを実行し、実行結果の比較はrspecの構文で実装してみました。

ソース

describe 'ssh login test' do
  stdout = system("sshpass -p 'infrainfra' ssh -t #{ENV['TARGET_HOST']} -l infra hostname")
  it 'login succeeded' do
    expect(stdout).to eq true
  end
end

実行結果

  • 成功時
ssh login test
  login succeeded
  • 失敗時
ssh login test
  login succeeded (FAILED - 1)

Failures:

  1) ssh login test login succeeded
     Failure/Error: expect(stdout).to eq true

       expected: true
            got: false

       (compared using ==)
     # ./spec/webserver/user_spec.rb:34:in `block (2 levels) in <top (required)>'
9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5