テストもやってみる

趣旨が変わってきたかもしれないけど、テストもやってみます。

テストを書く

Serverspec でテストするので、test/integration/default/serverspec/ ディレクトリを切っておきます。

$ mkdir -p test/integration/default/serverspec

ここに hello_spec.rb をつくります。

require 'serverspec'

set :backend, :exec

describe file('/hello.txt') do
  its(:content) { should match /Cookbook/ }
end

次にこのテストを実行するように設定します。.kitchen.yml で suites の inspec_test に項目を追加。

---
driver:
  name: vagrant

provisioner:
  name: chef_zero
  # You may wish to disable always updating cookbooks in CI or other testing environments.
  # For example:
  #   always_update_cookbooks: <%= !ENV['CI'] %>
  always_update_cookbooks: true

verifier:
  name: inspec

platforms:
  - name: ubuntu-16.04

suites:
  - name: default
    run_list:
      - recipe[cheftest::default]
      - recipe[cheftest::hello]
    verifier:
      inspec_tests:
        - test/smoke/default
        - test/integration/default/serverspec
    attributes:

こんな感じにしました。で、一度ぶち壊してからテストまでやってみます。ぶち壊すのは destroy、で前にやったようにつくるのが converge。で、テストするのは verify です。

$ kitchen destroy
-----> Starting Kitchen (v1.16.0)
-----> Destroying <default-ubuntu-1604>...
       ==> default: Forcing shutdown of VM...
       ==> default: Destroying VM and associated drives...
       Vagrant instance <default-ubuntu-1604> destroyed.
       Finished destroying <default-ubuntu-1604> (0m5.72s).
-----> Kitchen is finished. (0m7.53s)
$ kitchen converge
-----> Starting Kitchen (v1.16.0)
-----> Creating <default-ubuntu-1604>...
       Bringing machine 'default' up with 'virtualbox' provider...
       ==> default: Importing base box 'bento/ubuntu-16.04'...
==> default: Matching MAC address for NAT networking...
       ==> default: Checking if box 'bento/ubuntu-16.04' is up to date...
       ==> default: Setting the name of the VM: kitchen-cheftest-default-ubuntu-1604_default_1499839193211_39321
       ==> default: Clearing any previously set network interfaces...
       ==> default: Preparing network interfaces based on configuration...
           default: Adapter 1: nat
       ==> default: Forwarding ports...
           default: 22 (guest) => 2222 (host) (adapter 1)
       ==> default: Booting VM...
       ==> default: Waiting for machine to boot. This may take a few minutes...
           default: SSH address: 127.0.0.1:2222
           default: SSH username: vagrant
           default: SSH auth method: private key
           default: 
           default: Vagrant insecure key detected. Vagrant will automatically replace
           default: this with a newly generated keypair for better security.
           default: 
           default: Inserting generated public key within guest...
           default: Removing insecure key from the guest if it's present...
           default: Key inserted! Disconnecting and reconnecting using new SSH key...
       ==> default: Machine booted and ready!
       ==> default: Checking for guest additions in VM...
       ==> default: Setting hostname...
       ==> default: Mounting shared folders...
           default: /tmp/omnibus/cache => /home/gaziroh/.kitchen/cache
       ==> default: Machine not provisioned because `--no-provision` is specified.
       [SSH] Established
       Vagrant instance <default-ubuntu-1604> created.
       Finished creating <default-ubuntu-1604> (1m0.83s).
-----> Converging <default-ubuntu-1604>...
       Preparing files for transfer
       Preparing dna.json
       Resolving cookbook dependencies with Berkshelf 6.2.0...
       Removing non-cookbook files before transfer
       Preparing validation.pem
       Preparing client.rb
-----> Installing Chef Omnibus (install only if missing)
       Downloading https://omnitruck.chef.io/install.sh to file /tmp/install.sh
       Trying wget...
       Download complete.
       ubuntu 16.04 x86_64
       Getting information for chef stable  for ubuntu...
       downloading https://omnitruck.chef.io/stable/chef/metadata?v=&p=ubuntu&pv=16.04&m=x86_64
         to file /tmp/install.sh.1531/metadata.txt
       trying wget...
       sha1	2952362ec5d44ec776ace492c29a36c423437f39
       sha256	88cd274a694bfe23d255937794744d50af972097958fa681a544479e2bfb7f6b
       url	https://packages.chef.io/files/stable/chef/13.2.20/ubuntu/16.04/chef_13.2.20-1_amd64.deb
       version	13.2.20
       downloaded metadata file looks valid...
       /tmp/omnibus/cache/chef_13.2.20-1_amd64.deb already exists, verifiying checksum...
       Comparing checksum with sha256sum...
       checksum compare succeeded, using existing file!
       
       WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
       
       You are installing an omnibus package without a version pin.  If you are installing
       on production servers via an automated process this is DANGEROUS and you will
       be upgraded without warning on new releases, even to new major releases.
       Letting the version float is only appropriate in desktop, test, development or
       CI/CD environments.
       
       WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
       
       Installing chef 
       installing with dpkg...
       Selecting previously unselected package chef.
(Reading database ... 37882 files and directories currently installed.)
       Preparing to unpack .../cache/chef_13.2.20-1_amd64.deb ...
       Unpacking chef (13.2.20-1) ...
       Setting up chef (13.2.20-1) ...
       Thank you for installing Chef!
       Transferring files to <default-ubuntu-1604>
       Starting Chef Client, version 13.2.20
       Creating a new client identity for default-ubuntu-1604 using the validator key.
       resolving cookbooks for run list: ["cheftest::default", "cheftest::hello"]
       Synchronizing Cookbooks:
         - cheftest (0.1.0)
       Installing Cookbook Gems:
       Compiling Cookbooks...
       Converging 1 resources
       Recipe: cheftest::hello
         * file[hello.txt] action create
           - create new file hello.txt
           - update content in file hello.txt from none to adeeb2
           --- hello.txt	2017-07-12 06:01:14.166550688 +0000
           +++ ./.chef-hello20170712-1619-184x4eh.txt	2017-07-12 06:01:14.166550688 +0000
           @@ -1 +1,2 @@
           +Hello, Kitchen
       
       Running handlers:
       Running handlers complete
       Chef Client finished, 1/1 resources updated in 01 seconds
       Finished converging <default-ubuntu-1604> (0m37.47s).
-----> Kitchen is finished. (1m40.08s)
$ kitchen verify
-----> Starting Kitchen (v1.16.0)
-----> Setting up <default-ubuntu-1604>...
       Finished setting up <default-ubuntu-1604> (0m0.00s).
-----> Verifying <default-ubuntu-1604>...
       Detected alternative framework tests for `serverspec`
       Loaded tests from test/smoke/default 
       Loaded tests from test/integration/default/serverspec 

Profile: tests from test/smoke/default
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2222


  User root
     ↺  
  Port 80
     ↺  


Profile: tests from test/integration/default/serverspec
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2222


  File /hello.txt
     ∅  content should match /Cookbook/
     expected "Hello, Kitchen\n" to match /Cookbook/
     Diff:
     @@ -1,2 +1,2 @@
     -/Cookbook/
     +Hello, Kitchen
     

Test Summary: 0 successful, 1 failures, 2 skipped
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: 1 actions failed.
>>>>>>     Verify failed on instance <default-ubuntu-1604>.  Please see .kitchen/logs/default-ubuntu-1604.log for more details
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try running `kitchen diagnose --all` for configuration

と、エラーが出ました。そういえば、レシピで出力していたのは「Kitchen」でした。ということで、レシピを書き換えます。

file 'hello.txt' do
  content "Hello, Cookbook\n"
end

もう一度、converge して verify します。

$ kitchen converge
-----> Starting Kitchen (v1.16.0)
-----> Converging <default-ubuntu-1604>...
       Preparing files for transfer
       Preparing dna.json
       Resolving cookbook dependencies with Berkshelf 6.2.0...
       Removing non-cookbook files before transfer
       Preparing validation.pem
       Preparing client.rb
-----> Chef Omnibus installation detected (install only if missing)
       Transferring files to <default-ubuntu-1604>
       Starting Chef Client, version 13.2.20
       resolving cookbooks for run list: ["cheftest::default", "cheftest::hello"]
       Synchronizing Cookbooks:
         - cheftest (0.1.0)
       Installing Cookbook Gems:
       Compiling Cookbooks...
       Converging 1 resources
       Recipe: cheftest::hello
         * file[hello.txt] action create
           - update content in file hello.txt from adeeb2 to e4bcdb
           --- hello.txt	2017-07-12 06:01:14.166550688 +0000
           +++ ./.chef-hello20170712-1962-1h94fq6.txt	2017-07-12 06:14:23.260897999 +0000
           @@ -1,2 +1,2 @@
           -Hello, Kitchen
           +Hello, Cookbook
       
       Running handlers:
       Running handlers complete
       Chef Client finished, 1/1 resources updated in 01 seconds
       Finished converging <default-ubuntu-1604> (0m6.36s).
-----> Kitchen is finished. (0m8.20s)
$ kitchen verify
-----> Starting Kitchen (v1.16.0)
-----> Setting up <default-ubuntu-1604>...
       Finished setting up <default-ubuntu-1604> (0m0.00s).
-----> Verifying <default-ubuntu-1604>...
       Detected alternative framework tests for `serverspec`
       Loaded tests from test/smoke/default 
       Loaded tests from test/integration/default/serverspec 

Profile: tests from test/smoke/default
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2222


  User root
     ↺  
  Port 80
     ↺  


Profile: tests from test/integration/default/serverspec
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2222


  File /hello.txt
     ✔  content should match /Cookbook/

Test Summary: 1 successful, 0 failures, 2 skipped
       Finished verifying <default-ubuntu-1604> (0m0.92s).
-----> Kitchen is finished. (0m2.72s)

テストが通りました。

コメント

タイトルとURLをコピーしました