summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/facts/facter_spec.rb
blob: 9f5a0249b9c7812237dc68ba3d5f446d90214211 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env rspec
#
#  Created by Luke Kanies on 2007-9-23.
#  Copyright (c) 2007. All rights reserved.

require 'spec_helper'

require 'puppet/indirector/facts/facter'

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

  it "should have documentation" do
    Puppet::Node::Facts::Facter.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::Facter.indirection.should equal(indirection)
  end

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

  it "should load facts on initialization" do
    Puppet::Node::Facts::Facter.expects(:load_fact_plugins)
    Puppet::Node::Facts::Facter.new
  end
end

describe Puppet::Node::Facts::Facter do
  before :each do
    @facter = Puppet::Node::Facts::Facter.new
    Facter.stubs(:to_hash).returns({})
    @name = "me"
    @request = stub 'request', :key => @name
  end

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

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

    it "should return the Facter facts as the values in the Facts instance" do
      Facter.expects(:to_hash).returns("one" => "two")
      facts = @facter.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)

      @facter.find(@request)
    end

    it "should convert all facts into strings" do
      facts = Puppet::Node::Facts.new("foo")
      Puppet::Node::Facts.expects(:new).returns facts
      facts.expects(:stringify)

      @facter.find(@request)
    end

    it "should call the downcase hook" do
      facts = Puppet::Node::Facts.new("foo")
      Puppet::Node::Facts.expects(:new).returns facts
      facts.expects(:downcase_if_necessary)

      @facter.find(@request)
    end
  end

  describe Puppet::Node::Facts::Facter, " when saving facts" do

    it "should fail" do
      proc { @facter.save(@facts) }.should raise_error(Puppet::DevError)
    end
  end

  describe Puppet::Node::Facts::Facter, " when destroying facts" do

    it "should fail" do
      proc { @facter.destroy(@facts) }.should raise_error(Puppet::DevError)
    end
  end

  it "should skip files when asked to load a directory" do
    FileTest.expects(:directory?).with("myfile").returns false

    Puppet::Node::Facts::Facter.load_facts_in_dir("myfile")
  end

  it "should load each ruby file when asked to load a directory" do
    FileTest.expects(:directory?).with("mydir").returns true
    Dir.expects(:chdir).with("mydir").yields

    Dir.expects(:glob).with("*.rb").returns %w{a.rb b.rb}

    Puppet::Node::Facts::Facter.expects(:load).with("a.rb")
    Puppet::Node::Facts::Facter.expects(:load).with("b.rb")

    Puppet::Node::Facts::Facter.load_facts_in_dir("mydir")
  end

  describe Puppet::Node::Facts::Facter, "when loading fact plugins from disk" do
    it "should load each directory in the Fact path" do
      Puppet.settings.stubs(:value).returns "foo"
      Puppet.settings.expects(:value).with(:factpath).returns("one#{File::PATH_SEPARATOR}two")

      Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("one")
      Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("two")

      Puppet::Node::Facts::Facter.load_fact_plugins
    end

    it "should load all facts from the modules" do
      Puppet.settings.stubs(:value).returns "foo"
      Puppet::Node::Facts::Facter.stubs(:load_facts_in_dir)

      Puppet.settings.expects(:value).with(:modulepath).returns("one#{File::PATH_SEPARATOR}two")

      Dir.stubs(:glob).returns []
      Dir.expects(:glob).with("one/*/lib/facter").returns %w{oneA oneB}
      Dir.expects(:glob).with("two/*/lib/facter").returns %w{twoA twoB}

      Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("oneA")
      Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("oneB")
      Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("twoA")
      Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("twoB")

      Puppet::Node::Facts::Facter.load_fact_plugins
    end
  end
end