New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spec for be_enforcing, be_permissive and be_disabled matchers #64

Merged
merged 2 commits into from Apr 27, 2013

Conversation

Projects
None yet
2 participants
@kenjiskywalker
Copy link
Contributor

kenjiskywalker commented Apr 26, 2013

for SELinux mode check

  • be_enforcing
  • be_permissive
  • be_disabled

add.

@mizzy

This comment has been minimized.

Copy link
Owner

mizzy commented Apr 26, 2013

commands/base.rb と commands/redhat.rb について

commands/base.rb と commands/redhat.rb に追加されてる内容がまったく一緒なので、redhat.rb の方は追加不要ですね。redhat.rb は base.rb を継承してるので。

check_enforcing, check_permissive, check_disabled について

似たようなコマンドを3つ追加してるのは無駄なので、以下のようにひとつにまとめた方がいいです。

      def check_selinux mode
        "/usr/sbin/getenforce | grep -i '#{mode}'"
      end

その上で、matchers/be_disabled.rb を以下のように書き換えます。

backend.check_selinux(example, 'disabled')

example は他のマッチャーに倣って、必ず渡すようにしてください。

be_permissive, be_enforcing も同様に書き換えてください。

@kenjiskywalker

This comment has been minimized.

Copy link
Contributor

kenjiskywalker commented Apr 27, 2013

ありがとうございます!書き換えてみました!

下記の通りRedHatとSolarisのテストを同時に走らせると失敗します。
https://github.com/mizzy/serverspec/pull/52/files
こちらのpullreqを参考に書いてみましたが、実装方法に問題ありますでしょうか!

テスト結果

RedHatのみ[成功]

% rspec ./spec/redhat/commands_spec.rb ./spec/redhat/matchers_spec.rb                                                                                                                        [support-selinux-matchers ~/dev/serverspec]..................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

........................................

Finished in 0.13444 seconds
123 examples, 0 failures

Solarisのみ[成功]

% rspec ./spec/solaris/commands_spec.rb ./spec/solaris/matchers_spec.rb                                                                                                                      [support-selinux-matchers ~/dev/serverspec]................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.................................................

Finished in 0.14562 seconds

RedHat以外[成功]

% rspec ./spec/debian/commands_spec.rb ./spec/debian/matchers_spec.rb ./spec/gentoo/commands_spec.rb ./spec/gentoo/matchers_spec.rb ./spec/solaris/commands_spec.rb ./spec/solaris/matchers_spec.rb                               ............................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

...................................................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.....................................................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.................................................

Finished in 0.4381 seconds
360 examples, 0 failures

Solaris以外[成功]

rspec ./spec/debian/commands_spec.rb ./spec/debian/matchers_spec.rb ./spec/gentoo/commands_spec.rb ./spec/gentoo/matchers_spec.rb ./spec/redhat/commands_spec.rb ./spec/redhat/matchers_spec.rb                                 ............................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

...................................................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.......................................................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

........................................

Finished in 0.4149 seconds
353 examples, 0 failures

DebianとGentoo[成功]

% rspec ./spec/debian/commands_spec.rb ./spec/debian/matchers_spec.rb ./spec/gentoo/commands_spec.rb ./spec/gentoo/matchers_spec.rb                                                          [support-selinux-matchers ~/dev/serverspec]............................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

...................................................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.....................................

Finished in 0.27616 seconds
230 examples, 0 failures

RedHatとSolaris[失敗]

% rspec  ./spec/redhat/commands_spec.rb ./spec/redhat/matchers_spec.rb ./spec/solaris/commands_spec.rb ./spec/solaris/matchers_spec.rb                                                       [support-selinux-matchers ~/dev/serverspec]
..................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.........................FFF............................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.................................................

Failures:

  1) Serverspec matchers of Red Hat family behaves like support be_enforcing matcher selinux Enforcing
     Failure/Error: it { should be_enforcing }
     Serverspec::Commands::Base::NotImplementedError:
       Serverspec::Commands::Base::NotImplementedError
     Shared Example Group: "support be_enforcing matcher" called from ./spec/redhat/matchers_spec.rb:57
     # ./lib/serverspec/commands/base.rb:121:in `check_selinux'
     # ./lib/serverspec/backend/exec.rb:29:in `check_zero'
     # ./lib/serverspec/backend/exec.rb:37:in `method_missing'
     # ./lib/serverspec/matchers/be_enforcing.rb:3:in `block (2 levels) in <top (required)>'
     # ./spec/support/shared_matcher_examples.rb:231:in `block (3 levels) in <top (required)>'

  2) Serverspec matchers of Red Hat family behaves like support be_permissive matcher selinux Permissive
     Failure/Error: it { should be_permissive }
     Serverspec::Commands::Base::NotImplementedError:
       Serverspec::Commands::Base::NotImplementedError
     Shared Example Group: "support be_permissive matcher" called from ./spec/redhat/matchers_spec.rb:58
     # ./lib/serverspec/commands/base.rb:121:in `check_selinux'
     # ./lib/serverspec/backend/exec.rb:29:in `check_zero'
     # ./lib/serverspec/backend/exec.rb:37:in `method_missing'
     # ./lib/serverspec/matchers/be_permissive.rb:3:in `block (2 levels) in <top (required)>'
     # ./spec/support/shared_matcher_examples.rb:237:in `block (3 levels) in <top (required)>'

  3) Serverspec matchers of Red Hat family behaves like support be_disabled matcher selinux Disabled
     Failure/Error: it { should be_disabled }
     Serverspec::Commands::Base::NotImplementedError:
       Serverspec::Commands::Base::NotImplementedError
     Shared Example Group: "support be_disabled matcher" called from ./spec/redhat/matchers_spec.rb:59
     # ./lib/serverspec/commands/base.rb:121:in `check_selinux'
     # ./lib/serverspec/backend/exec.rb:29:in `check_zero'
     # ./lib/serverspec/backend/exec.rb:37:in `method_missing'
     # ./lib/serverspec/matchers/be_disabled.rb:3:in `block (2 levels) in <top (required)>'
     # ./spec/support/shared_matcher_examples.rb:243:in `block (3 levels) in <top (required)>'

Finished in 0.2715 seconds
253 examples, 3 failures

Failed examples:

rspec ./spec/support/shared_matcher_examples.rb:231 # Serverspec matchers of Red Hat family behaves like support be_enforcing matcher selinux Enforcing
rspec ./spec/support/shared_matcher_examples.rb:237 # Serverspec matchers of Red Hat family behaves like support be_permissive matcher selinux Permissive
rspec ./spec/support/shared_matcher_examples.rb:243 # Serverspec matchers of Red Hat family behaves like support be_disabled matcher selinux Disabled

すべて[失敗]

% rspec ./spec/debian/commands_spec.rb ./spec/debian/matchers_spec.rb ./spec/gentoo/commands_spec.rb ./spec/gentoo/matchers_spec.rb ./spec/redhat/commands_spec.rb ./spec/redhat/matchers_spec.rb ./spec/solaris/commands_spec.rb ./spec/
solaris/matchers_spec.rb
............................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

...................................................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.......................................................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.........................FFF............................................................................................
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.
************************************
"get_stdout" matcher is deprecated.
Use "return_stdout" matcher instead.
************************************

.................................................

Failures:

  1) Serverspec matchers of Red Hat family behaves like support be_enforcing matcher selinux Enforcing
     Failure/Error: it { should be_enforcing }
     Serverspec::Commands::Base::NotImplementedError:
       Serverspec::Commands::Base::NotImplementedError
     Shared Example Group: "support be_enforcing matcher" called from ./spec/redhat/matchers_spec.rb:57
     # ./lib/serverspec/commands/base.rb:121:in `check_selinux'
     # ./lib/serverspec/backend/exec.rb:29:in `check_zero'
     # ./lib/serverspec/backend/exec.rb:37:in `method_missing'
     # ./lib/serverspec/matchers/be_enforcing.rb:3:in `block (2 levels) in <top (required)>'
     # ./spec/support/shared_matcher_examples.rb:231:in `block (3 levels) in <top (required)>'

  2) Serverspec matchers of Red Hat family behaves like support be_permissive matcher selinux Permissive
     Failure/Error: it { should be_permissive }
     Serverspec::Commands::Base::NotImplementedError:
       Serverspec::Commands::Base::NotImplementedError
     Shared Example Group: "support be_permissive matcher" called from ./spec/redhat/matchers_spec.rb:58
     # ./lib/serverspec/commands/base.rb:121:in `check_selinux'
     # ./lib/serverspec/backend/exec.rb:29:in `check_zero'
     # ./lib/serverspec/backend/exec.rb:37:in `method_missing'
     # ./lib/serverspec/matchers/be_permissive.rb:3:in `block (2 levels) in <top (required)>'
     # ./spec/support/shared_matcher_examples.rb:237:in `block (3 levels) in <top (required)>'

  3) Serverspec matchers of Red Hat family behaves like support be_disabled matcher selinux Disabled
     Failure/Error: it { should be_disabled }
     Serverspec::Commands::Base::NotImplementedError:
       Serverspec::Commands::Base::NotImplementedError
     Shared Example Group: "support be_disabled matcher" called from ./spec/redhat/matchers_spec.rb:59
     # ./lib/serverspec/commands/base.rb:121:in `check_selinux'
     # ./lib/serverspec/backend/exec.rb:29:in `check_zero'
     # ./lib/serverspec/backend/exec.rb:37:in `method_missing'
     # ./lib/serverspec/matchers/be_disabled.rb:3:in `block (2 levels) in <top (required)>'
     # ./spec/support/shared_matcher_examples.rb:243:in `block (3 levels) in <top (required)>'

Finished in 0.56124 seconds
483 examples, 3 failures

Failed examples:

rspec ./spec/support/shared_matcher_examples.rb:231 # Serverspec matchers of Red Hat family behaves like support be_enforcing matcher selinux Enforcing
rspec ./spec/support/shared_matcher_examples.rb:237 # Serverspec matchers of Red Hat family behaves like support be_permissive matcher selinux Permissive
rspec ./spec/support/shared_matcher_examples.rb:243 # Serverspec matchers of Red Hat family behaves like support be_disabled matcher selinux Disabled
@mizzy

This comment has been minimized.

Copy link
Owner

mizzy commented Apr 27, 2013

お、ほんとだ。何かおかしいですね。 @kenjiskywalker さんのコードに問題があるわけではなさそうですね。確認しますー。

@mizzy

This comment has been minimized.

Copy link
Owner

mizzy commented Apr 27, 2013

現状だと、rake spec を実行すると、

$ rspec ./spec/debian/commands_spec.rb ./spec/debian/matchers_spec.rb ./spec/gentoo/commands_spec.rb ./spec/gentoo/matchers_spec.rb ./spec/redhat/commands_spec.rb ./spec/redhat/matchers_spec.rb ./spec/solaris/commands_spec.rb ./spec/
solaris/matchers_spec.rb

といった感じで一気に実行してるんですが、これが良くないですね。これは僕が書いた元々のRakefileがおかしいですね。今まで問題が出なかったのが不思議なくらいです。なので、こちらでRakefile修正しますね。問題点のご指摘、感謝です。

それ以外の @kenjiskywalker さんが書いたコードはおおむね問題なさそうなので、こちらの方でちょっとだけ修正してマージしますー。

@kenjiskywalker

This comment has been minimized.

Copy link
Contributor

kenjiskywalker commented Apr 27, 2013

よかった!ありがとうございます!

写経でいけるかと思ったんですが、やっぱりRSpecわかった上で書かないとダメですね。
次のpullreqの時はビシっとキメれるように精進して参ります。

勉強になりました。ありがとうございます!

@mizzy

This comment has been minimized.

Copy link
Owner

mizzy commented Apr 27, 2013

Rakefile というか、僕の spec の書き方の問題でした。

spec/debian/commands_spec.rb とか spec/redhat/commands_spec.rb とかで、それぞれの OS 用ヘルパーを直接 include してるんですが、同じ commands というメソッドを定義してるので、先に include された方がずっと有効になるようで、後から include した方では、期待通りの動作にならないみたいです。 なので、直接ヘルパーを include するのはやめて、

describe 'be_enabled', :os => :redhat do
  subject { commands.check_enabled('httpd') }
  it { should eq 'chkconfig --list httpd | grep 3:on' }
end

といった感じで書かないとダメなようです。これは今回のプルリクとは関係なく、元の僕のコードの問題なので、こちらで修正しますー。

写経でいけるかと思ったんですが、やっぱりRSpecわかった上で書かないとダメですね。

僕もまだまだわかってないので、一緒に学んでいきましょう!

次のpullreqの時はビシっとキメれるように精進して参ります。

期待してます!

勉強になりました。ありがとうございます!

こちらこそ、学ぶ点が色々ありました。またよろしくお願いします!

@mizzy mizzy merged commit 69170ea into mizzy:master Apr 27, 2013

1 check failed

default The Travis build failed
Details
@kenjiskywalker

This comment has been minimized.

Copy link
Contributor

kenjiskywalker commented Apr 27, 2013

なるほどなるほど!ありがとうございます!

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment