Module: ChefZero::RSpec

Extended by:
RSpecClassMethods
Defined in:
lib/chef_zero/rspec.rb

Defined Under Namespace

Modules: RSpecClassMethods, WhenTheChefServerClassMethods, WhenTheChefServerInstanceMethods

Instance Attribute Summary

Attributes included from RSpecClassMethods

#client_key, #request_log, #server

Instance Method Summary collapse

Methods included from RSpecClassMethods

clear_request_log, set_server_options

Instance Method Details

#when_the_chef_server(description, *tags, &block) ⇒ Object


44
45
46
47
48
49
50
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chef_zero/rspec.rb', line 44

def when_the_chef_server(description, *tags, &block)
  context "When the Chef server #{description}", *tags do
    extend WhenTheChefServerClassMethods
    include WhenTheChefServerInstanceMethods

    # Take the passed-in options

    define_singleton_method(:chef_server_options) {
      @chef_server_options ||= begin
        _chef_server_options = { port: 8900, signals: false, log_requests: true }
        _chef_server_options = _chef_server_options.merge(tags.last) if tags.last.is_a?(Hash)
        _chef_server_options = _chef_server_options.freeze
      end
    }

    # Merge in chef_server_options from let(:chef_server_options)
    def chef_server_options
      chef_server_options = self.class.chef_server_options.dup
      chef_server_options = chef_server_options.merge(chef_zero_opts) if self.respond_to?(:chef_zero_opts)
      chef_server_options
    end

    before chef_server_options[:server_scope] do
      if chef_server_options[:server_scope] != self.class.chef_server_options[:server_scope]
        raise "server_scope: #{chef_server_options[:server_scope]} will not be honored: it can only be set on when_the_chef_server!"
      end
      Log.debug("Starting Chef server with options #{chef_server_options}")

      ChefZero::RSpec.set_server_options(chef_server_options)

      if chef_server_options[:organization]
        organization chef_server_options[:organization]
      end

      if defined?(Chef::Config)
        @old_chef_server_url = Chef::Config.chef_server_url
        @old_node_name = Chef::Config.node_name
        @old_client_key = Chef::Config.client_key
        if chef_server_options[:organization]
          Chef::Config.chef_server_url = "#{ChefZero::RSpec.server.url}/organizations/#{chef_server_options[:organization]}"
        else
          Chef::Config.chef_server_url = ChefZero::RSpec.server.url
        end
        Chef::Config.node_name = 'admin'
        Chef::Config.client_key = ChefZero::RSpec.client_key.path
        Chef::Config.http_retry_count = 0
      end
    end

    if defined?(Chef::Config)
      after chef_server_options[:server_scope] do
        Chef::Config.chef_server_url = @old_chef_server_url
        Chef::Config.node_name = @old_node_name
        Chef::Config.client_key = @old_client_key
      end
    end

    instance_eval(&block)
  end
end