File: exec_spec.rb

package info (click to toggle)
puppet 4.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 20,736 kB
  • ctags: 14,616
  • sloc: ruby: 236,754; xml: 1,586; sh: 1,178; lisp: 299; sql: 103; yacc: 72; makefile: 52
file content (87 lines) | stat: -rw-r--r-- 2,746 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
#! /usr/bin/env ruby
require 'spec_helper'

require 'puppet/indirector/node/exec'
require 'puppet/indirector/request'

describe Puppet::Node::Exec do
  before do
    @indirection = mock 'indirection'
    Puppet.settings[:external_nodes] = File.expand_path("/echo")
    @searcher = Puppet::Node::Exec.new
  end

  describe "when constructing the command to run" do
    it "should use the external_node script as the command" do
      Puppet[:external_nodes] = "/bin/echo"
      expect(@searcher.command).to eq(%w{/bin/echo})
    end

    it "should throw an exception if no external node command is set" do
      Puppet[:external_nodes] = "none"
      expect { @searcher.find(stub('request', :key => "foo")) }.to raise_error(ArgumentError)
    end
  end

  describe "when handling the results of the command" do
    let(:testing_env) { Puppet::Node::Environment.create(:testing, []) }
    let(:other_env) { Puppet::Node::Environment.create(:other, []) }

    before do
      @name = "yay"
      @node = Puppet::Node.new(@name)
      @node.stubs(:fact_merge)
      Puppet::Node.expects(:new).with(@name).returns(@node)
      @result = {}
      # Use a local variable so the reference is usable in the execute definition.
      result = @result
      @searcher.meta_def(:execute) do |command, arguments|
        return YAML.dump(result)
      end

      @request = Puppet::Indirector::Request.new(:node, :find, @name, nil)
    end

    around do |example|
      envs = Puppet::Environments::Static.new(testing_env, other_env)

      Puppet.override(:environments => envs) do
        example.run
      end
    end

    it "should translate the YAML into a Node instance" do
      # Use an empty hash
      expect(@searcher.find(@request)).to equal(@node)
    end

    it "should set the resulting parameters as the node parameters" do
      @result[:parameters] = {"a" => "b", "c" => "d"}
      @searcher.find(@request)
      expect(@node.parameters).to eq({"a" => "b", "c" => "d"})
    end

    it "should set the resulting classes as the node classes" do
      @result[:classes] = %w{one two}
      @searcher.find(@request)
      expect(@node.classes).to eq([ 'one', 'two' ])
    end

    it "should merge the node's facts with its parameters" do
      @node.expects(:fact_merge)
      @searcher.find(@request)
    end

    it "should set the node's environment if one is provided" do
      @result[:environment] = "testing"
      @searcher.find(@request)
      expect(@node.environment.name).to eq(:testing)
    end

    it "should set the node's environment based on the request if not otherwise provided" do
      @request.environment = "other"
      @searcher.find(@request)
      expect(@node.environment.name).to eq(:other)
    end
  end
end