Skip to content

Instantly share code, notes, and snippets.

@sonots
Last active August 29, 2015 14:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonots/b806e5e6beeedf69570a to your computer and use it in GitHub Desktop.
Save sonots/b806e5e6beeedf69570a to your computer and use it in GitHub Desktop.
why-run でテストできない事象はあるのか

結論:みあたらないので誰か

@sonots
Copy link
Author

sonots commented Sep 8, 2014

http://tkak.hatenablog.com/entry/2012/10/08/094053

chefのテストにはカバーできない部分がある。
UnitTestの中でChefのResource機能の動作をテストするのは無駄。
why-run機能を使いなよ。

chef の中の人の言葉

@sonots
Copy link
Author

sonots commented Sep 8, 2014

http://qiita.com/sawanoboly/items/24aca274168675a2b85f

昔のだと

## Failするリソース(親パスが無い)
file '/tmp/tmp/tmp/hoge' do
  content 'mission failure...'
end

ちなみに--why-runではこういうFailケースはスルーされる、仕方ないね。
$ sudo FAIL=true chef-client -z recipe.rb --why-run
Chef Client finished, 3/3 resources would have been updated

とのことだったが、いまの chef だと parent directory does not exist 的なメッセージが出てちゃんとテストできる

@sonots
Copy link
Author

sonots commented Sep 8, 2014

http://blog.afistfulofservers.net/post/2012/12/21/promises-lies-and-dryrun-mode/

これは real run する前の why-run と real run では食い違いがあるというやつ。

real run した後に why-run をテストに使う分には食い違いが出ないので違う。

@sonots
Copy link
Author

sonots commented Sep 8, 2014

package "mysqld" do
  action :install
end

service "mysqld" do
  action :start
end

依存しているようなものがある場合、2つ目は最初にちゃんとチェックされない。
が、package インストールされている状態で why-run すればチェックされる。

ので、あるべき姿になっているかのテストは why-run で十分できそう(全部通れば良い)

[vagrant@localhost chef-yohoushi]$ bundle exec knife solo cook --why-run localhost
  * package[mysqld] action install
    * No version specified, and no candidate version available for mysqld
    * Assuming a repository that offers mysqld would have been configured
    - Would install  package mysqld
  * service[mysqld] action start
    * service[mysqld]: unable to locate the init.d script!
    * Assuming service would be disabled. The init script is not presently installed.
    - Would start service service[mysqld]

@sonots
Copy link
Author

sonots commented Sep 9, 2014

これはできない。

execute "hack the planet" do
  command "/bin/echo HACKING THE PLANET > foo"
end

が、only_if/not_if をちゃんと書けば、only_if/not_if がテストになるので、why-run でテストできる。

execute "hack the planet" do
  command "/bin/echo HACKING THE PLANET > foo"
  not_if "/usr/bin/test -e foo"
end

ちゃんと中身までチェックするならそういう not_if にしないといけない。

@sonots
Copy link
Author

sonots commented Sep 9, 2014

FileEdit でファイルの内容を置換した場合は、why-run で確認できるのか??

=> これも only_if 次第

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