summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/facts/inventory_active_record_spec.rb
blob: 0d5573999a48193dfcc90289f7bb544fe6236cbc (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env ruby

require 'spec_helper'
begin
  require 'sqlite3'
rescue LoadError
end
require 'tempfile'
require 'puppet/rails'

describe "Puppet::Node::Facts::InventoryActiveRecord", :if => (Puppet.features.rails? and defined? SQLite3) do
  let(:terminus) { Puppet::Node::Facts::InventoryActiveRecord.new }

  before :all do
    require 'puppet/indirector/facts/inventory_active_record'
    @dbfile = Tempfile.new("testdb")
    @dbfile.close
  end

  after :all do
    Puppet::Node::Facts.indirection.reset_terminus_class
    @dbfile.unlink
  end

  before :each do
    Puppet::Node::Facts.indirection.terminus_class = :inventory_active_record
    Puppet[:dbadapter]  = 'sqlite3'
    Puppet[:dblocation] = @dbfile.path
    Puppet[:railslog] = "/dev/null"
    Puppet::Rails.init
  end

  after :each do
    Puppet::Rails.teardown
    ActiveRecord::Base.remove_connection
  end

  describe "#save" do
    it "should use an existing node if possible" do
      node = Puppet::Rails::InventoryNode.new(:name => "foo", :timestamp => Time.now)
      node.save
      facts = Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => "Darwin")
      Puppet::Node::Facts.indirection.save(facts)

      Puppet::Rails::InventoryNode.count.should == 1
      Puppet::Rails::InventoryNode.first.should == node
    end

    it "should create a new node if one can't be found" do
      # This test isn't valid if there are nodes to begin with
      Puppet::Rails::InventoryNode.count.should == 0

      facts = Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => "Darwin")
      Puppet::Node::Facts.indirection.save(facts)

      Puppet::Rails::InventoryNode.count.should == 1
      Puppet::Rails::InventoryNode.first.name.should == "foo"
    end

    it "should save the facts" do
      facts = Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => "Darwin")
      Puppet::Node::Facts.indirection.save(facts)

      Puppet::Rails::InventoryFact.all.map{|f| [f.name,f.value]}.should =~ [["uptime_days","60"],["kernel","Darwin"]]
    end

    it "should remove the previous facts for an existing node" do
      facts = Puppet::Node::Facts.new("foo", "uptime_days" => "30", "kernel" => "Darwin")
      Puppet::Node::Facts.indirection.save(facts)
      bar_facts = Puppet::Node::Facts.new("bar", "uptime_days" => "35", "kernel" => "Linux")
      foo_facts = Puppet::Node::Facts.new("foo", "uptime_days" => "60", "is_virtual" => "false")
      Puppet::Node::Facts.indirection.save(bar_facts)
      Puppet::Node::Facts.indirection.save(foo_facts)

      Puppet::Node::Facts.indirection.find("bar").should == bar_facts
      Puppet::Node::Facts.indirection.find("foo").should == foo_facts
      Puppet::Rails::InventoryFact.all.map{|f| [f.name,f.value]}.should_not include(["uptime_days", "30"], ["kernel", "Darwin"])
    end
  end

  describe "#find" do
    before do
      @foo_facts = Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => "Darwin")
      @bar_facts = Puppet::Node::Facts.new("bar", "uptime_days" => "30", "kernel" => "Linux")
      Puppet::Node::Facts.indirection.save(@foo_facts)
      Puppet::Node::Facts.indirection.save(@bar_facts)
    end

    it "should identify facts by node name" do
      Puppet::Node::Facts.indirection.find("foo").should == @foo_facts
    end

    it "should return nil if no node instance can be found" do
      Puppet::Node::Facts.indirection.find("non-existent node").should == nil
    end
  end

  describe "#search" do
    def search_request(conditions)
      Puppet::Indirector::Request.new(:facts, :search, nil, conditions)
    end

    before :each do
      @now = Time.now
      @foo = Puppet::Node::Facts.new("foo", "fact1" => "value1", "fact2" => "value2", "uptime_days" => "30")
      @bar = Puppet::Node::Facts.new("bar", "fact1" => "value1", "uptime_days" => "60")
      @baz = Puppet::Node::Facts.new("baz", "fact1" => "value2", "fact2" => "value1", "uptime_days" => "90")
      @bat = Puppet::Node::Facts.new("bat")
      @foo.timestamp = @now - 3600*1
      @bar.timestamp = @now - 3600*3
      @baz.timestamp = @now - 3600*5
      @bat.timestamp = @now - 3600*7
      [@foo, @bar, @baz, @bat].each {|facts| Puppet::Node::Facts.indirection.save(facts)}
    end

    it "should return node names that match 'equal' constraints" do
      request = search_request('facts.fact1.eq' => 'value1',
                               'facts.fact2.eq' => 'value2')
      terminus.search(request).should == ["foo"]
    end

    it "should return node names that match 'not equal' constraints" do
      request = search_request('facts.fact1.ne' => 'value2')
      terminus.search(request).should == ["bar","foo"]
    end

    it "should return node names that match strict inequality constraints" do
      request = search_request('facts.uptime_days.gt' => '20',
                               'facts.uptime_days.lt' => '70')
      terminus.search(request).should == ["bar","foo"]
    end

    it "should return node names that match non-strict inequality constraints" do
      request = search_request('facts.uptime_days.ge' => '30',
                               'facts.uptime_days.le' => '60')
      terminus.search(request).should == ["bar","foo"]
    end

    it "should return node names whose facts are within a given timeframe" do
      request = search_request('meta.timestamp.ge' => @now - 3600*5,
                               'meta.timestamp.le' => @now - 3600*1)
      terminus.search(request).should == ["bar","baz","foo"]
    end

    it "should return node names whose facts are from a specific time" do
      request = search_request('meta.timestamp.eq' => @now - 3600*3)
      terminus.search(request).should == ["bar"]
    end

    it "should return node names whose facts are not from a specific time" do
      request = search_request('meta.timestamp.ne' => @now - 3600*1)
      terminus.search(request).should == ["bar","bat","baz"]
    end

    it "should perform strict searches on nodes by timestamp" do
      request = search_request('meta.timestamp.gt' => @now - 3600*5,
                               'meta.timestamp.lt' => @now - 3600*1)
      terminus.search(request).should == ["bar"]
    end

    it "should search nodes based on both facts and timestamp values" do
      request = search_request('facts.uptime_days.gt' => '45',
                               'meta.timestamp.lt'    => @now - 3600*4)
      terminus.search(request).should == ["baz"]
    end
  end
end