Test Kitichen を試す

cookbook をつくる

chef コマンドで cookbook をつくります。

$ chef generate cookbook cheftest
Generating cookbook cheftest
- Ensuring correct cookbook file content
- Ensuring delivery configuration
- Ensuring correct delivery build cookbook content

Your cookbook is ready. Type `cd cheftest` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local --help` to see a full list.

Why not start by writing a test? Tests for the default recipe are stored at:

test/smoke/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb

$ tree cheftest/
cheftest/
├── Berksfile
├── README.md
├── chefignore
├── metadata.rb
├── recipes
│   └── default.rb
├── spec
│   ├── spec_helper.rb
│   └── unit
│       └── recipes
│           └── default_spec.rb
└── test
    └── smoke
        └── default
            └── default_test.rb

7 directories, 8 files

こんな感じになります。以下、この cheftest の中で作業。

Test Kitchen の設定

Test Kitchen の設定は .kitchen.yml に書かれているらしい。内容は、

---
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
  - name: centos-7.3

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

という感じ。

driver
テスト環境のドライバ。vagrant の他に、EC2 とか docker とかあるよう。
provisioner
プロビジョナーとしては、chef-soloansible も使えるみたい。
verifier
Test Kitchen のベリファイステップに使うツールを指定する。Serverspec とかも使えるみたい。
platforms
テストをおこなうプラットホーム。複数定義できるけど今回は ubuntu-16.04 だけにする。
suites
テストスイート。とりあえずこのまま。

最終的に .kitchen.yml はこんな感じに。

---
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]
    verifier:
      inspec_tests:
        - test/smoke/default
    attributes:

とりあえず、ここまでの動作

動かしてみる。

$ kitchen verify
-----> Starting Kitchen (v1.16.0)
-----> Creating <default-ubuntu-1604>...
       Bringing machine 'default' up with 'virtualbox' provider...
       ==> default: Box 'bento/ubuntu-16.04' could not be found. Attempting to find and install...
           default: Box Provider: virtualbox
           default: Box Version: >= 0
       ==> default: Loading metadata for box 'bento/ubuntu-16.04'
           default: URL: https://vagrantcloud.com/bento/ubuntu-16.04
       ==> default: Adding box 'bento/ubuntu-16.04' (v2.3.7) for provider: virtualbox
           default: Downloading: https://app.vagrantup.com/bento/boxes/ubuntu-16.04/versions/2.3.7/providers/virtualbox.box
==> default: Successfully added box 'bento/ubuntu-16.04' (v2.3.7) for 'virtualbox'!
       ==> 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_1499824500109_57505
       ==> 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> (7m29.70s).
-----> 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.1535/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...
       downloading https://packages.chef.io/files/stable/chef/13.2.20/ubuntu/16.04/chef_13.2.20-1_amd64.deb
         to file /tmp/omnibus/cache/chef_13.2.20-1_amd64.deb
       trying wget...
       Comparing checksum with sha256sum...
       
       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"]
       Synchronizing Cookbooks:
         - cheftest (0.1.0)
       Installing Cookbook Gems:
       Compiling Cookbooks...
       Converging 0 resources
       
       Running handlers:
       Running handlers complete
       Chef Client finished, 0/0 resources updated in 01 seconds
       Finished converging <default-ubuntu-1604> (0m47.05s).
-----> Setting up <default-ubuntu-1604>...
       Finished setting up <default-ubuntu-1604> (0m0.00s).
-----> Verifying <default-ubuntu-1604>...
       Loaded tests from test/smoke/default 

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


  User root
     ↺  
  Port 80
     ↺  

Test Summary: 0 successful, 0 failures, 2 skipped
       Finished verifying <default-ubuntu-1604> (0m0.62s).
-----> Kitchen is finished. (8m19.13s)

長いけどこんな感じ。Chef 側から Vagrant をコントロールするという感じなんだろうか(まだ理解していない)。vagrant ssh のように ssh で入ることもできる。

$ kitchen login default-ubuntu-1604
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-81-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 packages can be updated.
0 updates are security updates.


Last login: Wed Jul 12 01:56:27 2017 from 10.0.2.2
vagrant@default-ubuntu-1604:~$ /opt/chef/bin/chef-client -v
Chef: 13.2.20

/opt/chef に Chef 関連のものが入っているようです。起動しているインスタンスのリストは以下のように確認できた(ホスト側からです)。

$ kitchen list
Instance             Driver   Provisioner  Verifier  Transport  Last Action  Last Error
default-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        Verified     <None>

次は実際にレシピをつくってみる。

コメント

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