Skip to content

Instantly share code, notes, and snippets.

@kozy4324
Created July 10, 2013 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kozy4324/5962832 to your computer and use it in GitHub Desktop.
Save kozy4324/5962832 to your computer and use it in GitHub Desktop.
serverspecで特定ユーザーにおけるコマンド実行をテストしたい

Commandクラスにby_userメソッドを追加してみる.

module Serverspec
  module Type
    class Command
      def by_user(user)
        self.class.new("su -l '#{user}' -c '#{@name}'")
      end
    end
  end
end

特定ユーザーでログイン後のコマンド実行がテストできる.

describe command('echo $HOME').by_user("www") do
  it { should return_stdout "/home/www" }
end

この実装の問題点

ダブルクォートのクォーテーションはうまく行くが、

describe command('echo "a  b  c"').by_user("www") do
  it { should return_stdout "a  b  c" }
end

シングルクォートのクォーテーションはうまく出来ない.

describe command("echo 'a  b  c'").by_user("www") do
  it { should return_stdout "a  b  c" }
end

メッセージは以下. 変数展開ががが...

  1) Command "su -l 'www' -c 'echo 'a  b  c''" 
     Failure/Error: it { should return_stdout "a  b  c" }
       sudo su -l 'www' -c 'echo 'a  b  c''
       a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment