summaryrefslogtreecommitdiff
path: root/spec/unit/indirector/facts/network_device_spec.rb
blob: 5f1367e8840ad688e6c8e59a8a83b0fc16df9a54 (plain)
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
88
#! /usr/bin/env ruby
require 'spec_helper'

require 'puppet/util/network_device'
require 'puppet/indirector/facts/network_device'

describe Puppet::Node::Facts::NetworkDevice do
  it "should be a subclass of the Code terminus" do
    Puppet::Node::Facts::NetworkDevice.superclass.should equal(Puppet::Indirector::Code)
  end

  it "should have documentation" do
    Puppet::Node::Facts::NetworkDevice.doc.should_not be_nil
  end

  it "should be registered with the configuration store indirection" do
    indirection = Puppet::Indirector::Indirection.instance(:facts)
    Puppet::Node::Facts::NetworkDevice.indirection.should equal(indirection)
  end

  it "should have its name set to :facter" do
    Puppet::Node::Facts::NetworkDevice.name.should == :network_device
  end
end

describe Puppet::Node::Facts::NetworkDevice do
  before :each do
    @remote_device = stub 'remote_device', :facts => {}
    Puppet::Util::NetworkDevice.stubs(:current).returns(@remote_device)
    @device = Puppet::Node::Facts::NetworkDevice.new
    @name = "me"
    @request = stub 'request', :key => @name
  end

  describe Puppet::Node::Facts::NetworkDevice, " when finding facts" do
    it "should return a Facts instance" do
      @device.find(@request).should be_instance_of(Puppet::Node::Facts)
    end

    it "should return a Facts instance with the provided key as the name" do
      @device.find(@request).name.should == @name
    end

    it "should return the device facts as the values in the Facts instance" do
      @remote_device.expects(:facts).returns("one" => "two")
      facts = @device.find(@request)
      facts.values["one"].should == "two"
    end

    it "should add local facts" do
      facts = Puppet::Node::Facts.new("foo")
      Puppet::Node::Facts.expects(:new).returns facts
      facts.expects(:add_local_facts)

      @device.find(@request)
    end

    it "should convert facts into strings when stringify_facts is true" do
      Puppet[:stringify_facts] = true
      facts = Puppet::Node::Facts.new("foo")
      Puppet::Node::Facts.expects(:new).returns facts
      facts.expects(:stringify)

      @device.find(@request)
    end

    it "should sanitizer facts when stringify_facts is false" do
      Puppet[:stringify_facts] = false
      facts = Puppet::Node::Facts.new("foo")
      Puppet::Node::Facts.expects(:new).returns facts
      facts.expects(:sanitize)

      @device.find(@request)
    end
  end

  describe Puppet::Node::Facts::NetworkDevice, " when saving facts" do
    it "should fail" do
      proc { @device.save(@facts) }.should raise_error(Puppet::DevError)
    end
  end

  describe Puppet::Node::Facts::NetworkDevice, " when destroying facts" do
    it "should fail" do
      proc { @device.destroy(@facts) }.should raise_error(Puppet::DevError)
    end
  end
end