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


51
52
53
54
55
56
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
# File 'lib/chefspec/server_methods.rb', line 51

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)

      case
      when #{klass}.respond_to?(:json_create)
        #{klass}.json_create(json)
      when #{klass}.respond_to?(:from_hash)
        #{klass}.from_hash(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

Parameters:

  • name (String)

    the name of the client

Returns:

  • (Chef::Client, nil)

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

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


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

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


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

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


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

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


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

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: '18.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


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

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


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

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


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

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)

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

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)

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

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


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

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

#get(*args) ⇒ Object

Get the path to an item in the data store.


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

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)

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

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)

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

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)

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

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)

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

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)

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

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


156
157
158
# File 'lib/chefspec/server_methods.rb', line 156

def load_data(name, key, data = {})
  ChefSpec::ZeroServer.load_data(name, key, 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)

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

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


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

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)

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

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


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

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

#serverChefZero::Server

The actual Chef Zero Server object.

Returns:

  • (ChefZero::Server)

12
13
14
# File 'lib/chefspec/server_methods.rb', line 12

def server
  ChefSpec::ZeroServer.server
end