simsaull
simsaull

Reputation: 329

Kitchen: add a role to a node

One of our app uses gunicorn and is configured via a chef cookbook.

The cookbook has some attributes, one of them is the gunicorn version:

grep 'version' attributes/default.rb
default['myapp']['gunicorn']['version'] = '19.1.1'

I want to use version 19.3.0 only if a node is part of a specific role. I created a role and gave it an attribute:

cat ../../roles/gunicorn-19.3.0.rb
name 'gunicorn-19.3.0'

default_attributes(
  'mcnulty' => {
    'gunicorn' => {
      'version' => '19.3.0'
    }
  }
)

Given that roles attribute get precedence over cookbook attributes, this should work. right??

Now I'd like to test that with kitchen. In our kichen.yml we already have a default suite, I copied and created a gunicorn-19.3.0 suite:

- name: gunicorn-19.3.0
    roles_path: '../../roles'
    run_list:
      - recipe[apt]
      - recipe[build-essential]
      - recipe[myapp]
    attributes: &attributes
      myapp:
        gunicorn:
          query:
            timeout: 5
            workers: 2
            sockdir: /var/run/myapp
          web:
            timeout: 5
            workers: 2
            sockdir: /var/run/myapp

Now I cannot figure out how to mimic the fact this host is part of the gunicorn-19.3.0 role...

Any help is appreciated.

Best.

Upvotes: 1

Views: 692

Answers (2)

simsaull
simsaull

Reputation: 329

Thank you Mark, that was spot on:

cat test/integration/roles/gunicorn-19-3-0.json
{
  "name": "gunicorn-19-3-0",
  "override_attributes": {
    "myapp": {
      "gunicorn": {
        "version": "19.3.0"
      }
    }
  }
}

cat .kitchen.yml
 - name: gunicorn-19.3.0
    #roles_path: '../../roles'
    run_list:
      - recipe[apt]
      - recipe[build-essential]
      - recipe[python]
      - recipe[myapp::default]
      - role[gunicorn-19-3-0]
    attributes: &attributes
      myapp:
        gunicorn:
          query:
            timeout: 5
            workers: 2
            sockdir: /var/run/myapp
          web:
            timeout: 5
            workers: 2
            sockdir: /var/run/myapp

After a kitchen converge, gunicorn 19.3.0 is installed. thanks!

Upvotes: 0

Mark O'Connor
Mark O'Connor

Reputation: 77951

Put your role under the test/integration directory and it will be picked up automatically by chef-zero:

├── .kitchen.yml
└── test
    └── integration
        └── roles
            └── myrole.json

Then in your kitchen file create two test suites, one using the cookbook recipe, the other using the role:

suites:
  - name: default
    run_list:
      - recipe[mycookbook::default]
  - name: withrole
    run_list:
      - role[myrole]

Example using role to manage tomcat attributes:

Upvotes: 2

Related Questions