Sha256: 713e4b86491aea4d3ac6268186460fe3303c28791ca7380143cc737487d1b748

Contents?: true

Size: 1.45 KB

Versions: 19

Compression:

Stored size: 1.45 KB

Contents

require 'chef_zero/server'

module Berkshelf::RSpec
  module ChefServer
    PORT = 8889

    class << self
      def clear_request_log
        @request_log = Array.new
      end

      def request_log
        @request_log ||= Array.new
      end

      def server
        @server ||= ChefZero::Server.new(port: PORT, generate_real_keys: false)
      end

      def server_url
        (@server && @server.url) || "http://localhost:#{PORT}"
      end

      def start
        server.start_background
        server.on_response do |request, response|
          request_log << [ request, response ]
        end
        clear_request_log

        server
      end

      def stop
        @server.stop if running?
      end

      def running?
        @server && @server.running?
      end
    end

    def chef_server
      ChefServer.server
    end

    def chef_client(name, hash = Hash.new)
      load_data(:clients, name, hash)
    end

    def chef_data_bag(name, hash = Hash.new)
      ChefServer.server.load_data({ 'data' => { name => hash }})
    end

    def chef_environment(name, hash = Hash.new)
      load_data(:environments, name, hash)
    end

    def chef_node(name, hash = Hash.new)
      load_data(:nodes, name, hash)
    end

    def chef_role(name, hash = Hash.new)
      load_data(:roles, name, hash)
    end

    private

      def load_data(key, name, hash)
        ChefServer.server.load_data({ key.to_s => { name => MultiJson.encode(hash) }})
      end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
berkshelf-2.0.18 spec/support/chef_server.rb
berkshelf-2.0.17 spec/support/chef_server.rb
berkshelf-2.0.16 spec/support/chef_server.rb
berkshelf-2.0.15 spec/support/chef_server.rb
berkshelf-2.0.14 spec/support/chef_server.rb
berkshelf-2.0.13 spec/support/chef_server.rb
berkshelf-2.0.12 spec/support/chef_server.rb
berkshelf-2.0.11 spec/support/chef_server.rb
berkshelf-2.0.10 spec/support/chef_server.rb
berkshelf-2.0.9 spec/support/chef_server.rb
berkshelf-2.0.8 spec/support/chef_server.rb
berkshelf-2.0.7 spec/support/chef_server.rb
berkshelf-2.0.6 spec/support/chef_server.rb
berkshelf-2.0.5 spec/support/chef_server.rb
berkshelf-2.0.4 spec/support/chef_server.rb
berkshelf-1.4.6 spec/support/chef_server.rb
berkshelf-2.0.3 spec/support/chef_server.rb
berkshelf-2.0.1 spec/support/chef_server.rb
berkshelf-2.0.0 spec/support/chef_server.rb