Module: ChefSpec::ServerMethods

Included in:
ServerRunner
Defined in:
lib/chefspec/server_methods.rb

Overview

This module contains the list of methods that are specific to creating and managing resources within an Chef Zero instance. It is designed to be included in a class which exposes a `server` instance variable or method that returns a ChefZero::Server instance.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.entity(method, klass, key) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chefspec/server_methods.rb', line 57

def self.entity(method, klass, key)
  class_eval <<-EOH, __FILE__, __LINE__ + 1
    def create_#{method}(name, data = {})
      # Automatically set the "name" if no explicit one was given
      data[:name] ||= name

      # Convert it to JSON
      data = JSON.fast_generate(data)

      load_data(name, '#{key}', data)
    end

    def get_#{method}(name)
      data = get('#{key}', name)
      json = JSON.parse(data)

      if #{klass}.respond_to?(:json_create)
        #{klass}.json_create(json)
      else
        #{klass}.new(json)
      end
    rescue ChefZero::DataStore::DataNotFoundError
      nil
    end

    def get_#{key}
      get('#{key}')
    end

    def has_#{method}?(name)
      !get('#{key}', name).nil?
    rescue ChefZero::DataStore::DataNotFoundError
      false
    end
  EOH
end

Instance Method Details

#client(name) ⇒ Chef::Client?

Find a client at the given name


94
# File 'lib/chefspec/server_methods.rb', line 94

entity :client,      Chef::Client, 'clients'

#clientsArray<Hash>

The list of client on the Chef Server


94
# File 'lib/chefspec/server_methods.rb', line 94

entity :client,      Chef::Client, 'clients'

#create_client(name, data = {}) ⇒ Object

Create a new client on the Chef Server


94
# File 'lib/chefspec/server_methods.rb', line 94

entity :client,      Chef::Client, 'clients'

#create_data_bag(name, data = {}) ⇒ Object

Create a new data_bag on the Chef Server. This overrides the method created by entity


109
# File 'lib/chefspec/server_methods.rb', line 109

entity :data_bag,    Chef::DataBag, 'data'

#create_environment(name, data = {}) ⇒ Object

Create a new environment on the Chef Server


96
# File 'lib/chefspec/server_methods.rb', line 96

entity :environment, Chef::Environment, 'environments'

#create_node(object, data = {}) ⇒ Object

Create a new node on the Chef Server. This overrides the method created by entity, permitting users to pass a raw Chef::Node object in addition to a hash.

Examples:

Create a node from a hash


create_node('bacon', attribute: 'value')

Create a node from a Chef::Node object


node = stub_node('bacon', platform: 'ubuntu', version: '12.04')
create_node(node)

134
# File 'lib/chefspec/server_methods.rb', line 134

entity :node,        Chef::Node, 'nodes'

#create_role(name, data = {}) ⇒ Object

Create a new role on the Chef Server


98
# File 'lib/chefspec/server_methods.rb', line 98

entity :role,        Chef::Role, 'roles'

#dataArray<Hash>

The list of data_bag on the Chef Server


95
# File 'lib/chefspec/server_methods.rb', line 95

entity :data_bag,    Chef::DataBag, 'data'

#data_bag(name) ⇒ Chef::DataBag?

Find a data_bag at the given name


95
# File 'lib/chefspec/server_methods.rb', line 95

entity :data_bag,    Chef::DataBag, 'data'

#environment(name) ⇒ Chef::Environment?

Find a environment at the given name


96
# File 'lib/chefspec/server_methods.rb', line 96

entity :environment, Chef::Environment, 'environments'

#environmentsArray<Hash>

The list of environment on the Chef Server


96
# File 'lib/chefspec/server_methods.rb', line 96

entity :environment, Chef::Environment, 'environments'

#get(*args) ⇒ Object

Get the path to an item in the data store.


165
166
167
168
169
170
171
172
173
# File 'lib/chefspec/server_methods.rb', line 165

def get(*args)
  args.unshift('organizations', 'chef')

  if args.size == 3
    @server.data_store.list(args)
  else
    @server.data_store.get(args)
  end
end

#has_client?(name) ⇒ true, false

Determine if the Chef Server has the given client


94
# File 'lib/chefspec/server_methods.rb', line 94

entity :client,      Chef::Client, 'clients'

#has_data_bag?(name) ⇒ true, false

Determine if the Chef Server has the given data_bag


95
# File 'lib/chefspec/server_methods.rb', line 95

entity :data_bag,    Chef::DataBag, 'data'

#has_environment?(name) ⇒ true, false

Determine if the Chef Server has the given environment


96
# File 'lib/chefspec/server_methods.rb', line 96

entity :environment, Chef::Environment, 'environments'

#has_node?(name) ⇒ true, false

Determine if the Chef Server has the given node


97
# File 'lib/chefspec/server_methods.rb', line 97

entity :node,        Chef::Node, 'nodes'

#has_role?(name) ⇒ true, false

Determine if the Chef Server has the given role


98
# File 'lib/chefspec/server_methods.rb', line 98

entity :role,        Chef::Role, 'roles'

#load_data(name, key, data = {}) ⇒ Object

Shortcut method for loading data into Chef Zero.


158
159
160
# File 'lib/chefspec/server_methods.rb', line 158

def load_data(name, key, data = {})
  @server.load_data({ key => { name => data } })
end

#node(name) ⇒ Chef::Node?

Find a node at the given name


97
# File 'lib/chefspec/server_methods.rb', line 97

entity :node,        Chef::Node, 'nodes'

#nodesArray<Hash>

The list of node on the Chef Server


97
# File 'lib/chefspec/server_methods.rb', line 97

entity :node,        Chef::Node, 'nodes'

#role(name) ⇒ Chef::Role?

Find a role at the given name


98
# File 'lib/chefspec/server_methods.rb', line 98

entity :role,        Chef::Role, 'roles'

#rolesArray<Hash>

The list of role on the Chef Server


98
# File 'lib/chefspec/server_methods.rb', line 98

entity :role,        Chef::Role, 'roles'

#serverChefZero::Server

The actual Chef Zero Server object.


12
13
14
15
16
17
18
19
20
# File 'lib/chefspec/server_methods.rb', line 12

def server
  @server ||= ChefZero::Server.new(
    # Set the log level from RSpec, defaulting to warn
    log_level:  RSpec.configuration.log_level || :warn,

    # Set a random port so ChefSpec may be run in multiple contexts
    port: port,
  )
end