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 "    def create_\#{method}(name, data = {})\n      # Automatically set the \"name\" if no explicit one was given\n      data[:name] ||= name\n\n      # Convert it to JSON\n      data = JSON.fast_generate(data)\n\n      load_data(name, '\#{key}', data)\n    end\n\n    def get_\#{method}(name)\n      data = get('\#{key}', name)\n      json = JSON.parse(data)\n\n      if \#{klass}.respond_to?(:json_create)\n        \#{klass}.json_create(json)\n      else\n        \#{klass}.new(json)\n      end\n    rescue ChefZero::DataStore::DataNotFoundError\n      nil\n    end\n\n    def get_\#{key}\n      get('\#{key}')\n    end\n\n    def has_\#{method}?(name)\n      !get('\#{key}', name).nil?\n    rescue ChefZero::DataStore::DataNotFoundError\n      false\n    end\n  EOH\nend\n", __FILE__, __LINE__ + 1

Instance Method Details

#client(name) ⇒ Chef::Client?

Find a client at the given name

Parameters:

  • name (String)

    the name of the client

Returns:



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

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

#clientsArray<Hash>

The list of client on the Chef Server

Returns:

  • (Array<Hash>)

    all the 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

Parameters:

  • name (String)

    the name of the client

  • data (Hash) (defaults to: {})

    the list of data to load



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

Parameters:

  • name (String)

    the name of the data bag

  • data (Hash) (defaults to: {})

    the data to load into the data bag



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

Parameters:

  • name (String)

    the name of the environment

  • data (Hash) (defaults to: {})

    the list of data to load



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

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

#create_node(object, data = {}) ⇒ Object Also known as: update_node

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)

Parameters:

  • object (String, Chef::Node)

    the object to create; this can be the name of the node, or an actual Chef::Node object

  • data (Hash) (defaults to: {})

    the list of data to populate the node with; this is ignored if an actual node object is given



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

Parameters:

  • name (String)

    the name of the role

  • data (Hash) (defaults to: {})

    the list of data to load



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

Returns:

  • (Array<Hash>)

    all the 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

Parameters:

  • name (String)

    the name of the data_bag

Returns:

  • (Chef::DataBag, nil)


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

Parameters:

  • name (String)

    the name of the environment

Returns:

  • (Chef::Environment, nil)


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

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

#environmentsArray<Hash>

The list of environment on the Chef Server

Returns:

  • (Array<Hash>)

    all the 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.



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

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

Parameters:

  • name (String)

    the name of the client to find

Returns:

  • (true, false)


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

Parameters:

  • name (String)

    the name of the data_bag to find

Returns:

  • (true, false)


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

Parameters:

  • name (String)

    the name of the environment to find

Returns:

  • (true, false)


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

Parameters:

  • name (String)

    the name of the node to find

Returns:

  • (true, false)


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

Parameters:

  • name (String)

    the name of the role to find

Returns:

  • (true, false)


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.

Parameters:

  • name (String)

    the name or id of the item to load

  • key (String, Symbol)

    the key to load

  • data (Hash) (defaults to: {})

    the data for the object, which will be converted to JSON and uploaded to the server



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

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

#node(name) ⇒ Chef::Node?

Find a node at the given name

Parameters:

  • name (String)

    the name of the node

Returns:

  • (Chef::Node, nil)


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

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

#nodesArray<Hash>

The list of node on the Chef Server

Returns:

  • (Array<Hash>)

    all the 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

Parameters:

  • name (String)

    the name of the role

Returns:

  • (Chef::Role, nil)


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

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

#rolesArray<Hash>

The list of role on the Chef Server

Returns:

  • (Array<Hash>)

    all the 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.

Returns:

  • (ChefZero::Server)


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