chef-infrachef-recipetest-kitchen

How do I test cookbooks in test kitchen that depend on tags?


I have a cookbook that depends on a tag. It searches for nodes with a given tag and gets their names and IPs using search(:node, 'tag:MyTag'). How would I mock this up in test kitchen?

Is there mock node objects I can use? It doesn't have to communicate with other nodes I just want to test that search() is successfully pulling down the right info.


Solution

  • When using serverspec, chef-zero will use the directory test/integration/ as the default repo location. This is where you can place some node files to simulate other nodes connected to the same chef server.

    ├── Berksfile
    ├── chefignore
    ├── metadata.rb
    ├── README.md
    ├── recipes
    │   └── default.rb
    └── test
        └── integration
            ├── default
            │   └── serverspec
            │       └── default_spec.rb
            ├── helpers
            │   └── serverspec
            │       └── spec_helper.rb
            └── nodes
                ├── demo1.json
                └── demo2.json
    

    demo1.json

    {
      "name": "demo1",
      "normal": {
        "tags": [
          "DEMO=1"
        ]
      }
    }
    

    demo2.json

    {
      "name": "demo2",
      "normal": {
        "tags": [
          "DEMO=1"
        ]
      }
    }
    

    Update

    I have a cookbook that tests Jenkins master and slaves using this technique