summaryrefslogtreecommitdiff
path: root/spec/unit/indirector/resource/active_record_spec.rb
blob: 934c208e468ff5848cfeb4dfed5da21f5032a8a3 (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
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/node/facts'

describe "Puppet::Resource::ActiveRecord", :if => can_use_scratch_database? do
  include PuppetSpec::Files

  before :each do
    require 'puppet/indirector/resource/active_record'
    setup_scratch_database
  end

  after :each do
    Puppet::Rails.teardown
  end

  subject { Puppet::Resource.indirection.terminus(:active_record) }

  it "should issue a deprecation warning" do
    Puppet.expects(:deprecation_warning).with() { |msg| msg =~ /ActiveRecord-based storeconfigs and inventory are deprecated/ }
    Puppet::Resource::ActiveRecord.new
  end

  describe "#search" do
    def search(type, host = 'default.local', filter = nil)
      args = { :host => host, :filter => filter }
      subject.search(Puppet::Resource.indirection.request(:search, type, nil, args))
    end

    it "should return an empty array if no resources match" do
      search("Exec").should == []
    end

    # Assert that this is a case-insensitive rule, too.
    %w{and or AND OR And Or anD oR}.each do |op|
      it "should fail if asked to search with #{op.inspect}" do
        filter = [%w{tag == foo}, op, %w{title == bar}]
        expect { search("Notify", 'localhost', filter) }.
          to raise_error Puppet::Error, /not supported/
      end
    end

    context "with a matching resource" do
      before :each do
        host = Puppet::Rails::Host.create!(:name => 'one.local')
        Puppet::Rails::Resource.
          create!(:host     => host,
                  :restype  => 'Exec', :title => 'whammo',
                  :exported => true)

      end

      it "should return something responding to `to_resource` if a resource matches" do
        found = search("Exec")
        found.length.should == 1
        found.map do |item|
          item.should respond_to :to_resource
          item.restype.should == "Exec"
        end
      end

      it "should not filter resources that have been found before" do
        search("Exec").should == search("Exec")
      end
    end
  end

  describe "#build_active_record_query" do
    let :type do 'Notify' end

    def query(type, host, filter = nil)
      subject.send :build_active_record_query, type, host, filter
    end

    it "should exclude all database resources from the host" do
      host = Puppet::Rails::Host.create! :name => 'one.local'
      got = query(type, host.name)
      got.keys.should =~ [:conditions]
      got[:conditions][0] =~ /\(host_id != \?\)/
      got[:conditions].last.should == host.id
    end

    it "should join appropriately when filtering on parameters" do
      filter = %w{propname == propval}
      got = query(type, 'whatever', filter)
      got.keys.should =~ [:conditions, :joins]
      got[:joins].should == { :param_values => :param_name }
      got[:conditions][0].should =~ /param_names\.name = \?/
      got[:conditions][0].should =~ /param_values\.value = \?/
      got[:conditions].should be_include filter.first
      got[:conditions].should be_include filter.last
    end

    it "should join appropriately when filtering on tags" do
      filter = %w{tag == test}
      got = query(type, 'whatever', filter)
      got.keys.should =~ [:conditions, :joins]
      got[:joins].should == {:resource_tags => :puppet_tag}
      got[:conditions].first.should =~ /puppet_tags/
      got[:conditions].should_not be_include filter.first
      got[:conditions].should be_include filter.last
    end

    it "should only search for exported resources with the matching type" do
      got = query(type, 'whatever')
      got.keys.should =~ [:conditions]
      got[:conditions][0].should be_include "(exported=? AND restype=?)"
      got[:conditions][1].should == true
      got[:conditions][2].should == type.to_s.capitalize
    end

    it "should capitalize the type, since PGSQL is case sensitive" do
      got = query(type, 'whatever')
      got[:conditions][2].should == 'Notify'
    end
  end

  describe "#filter_to_active_record" do
    def filter_to_active_record(input)
      subject.send :filter_to_active_record, input
    end

    [nil, '', 'whatever', 12].each do |input|
      it "should fail if filter is not an array (with #{input.inspect})" do
        expect { filter_to_active_record(input) }.
          to raise_error ArgumentError, /must be arrays/
      end
    end

    # Not exhaustive, just indicative.
    ['=', '<>', '=~', '+', '-', '!'].each do |input|
      it "should fail with unexpected comparison operators (with #{input.inspect})" do
        expect { filter_to_active_record(["one", input, "two"]) }.
          to raise_error ArgumentError, /unknown operator/
      end
    end

    {
      ["title", "==", "whatever"] => ["title = ?", ["whatever"]],
      ["title", "!=", "whatever"] => ["title != ?", ["whatever"]],

      # Technically, these are not supported by Puppet yet, but as we pay
      # approximately zero cost other than a few minutes writing the tests,
      # and it would be *harder* to fail on them, nested queries.
      [["title", "==", "foo"], "or", ["title", "==", "bar"]] =>
        ["(title = ?) OR (title = ?)", ["foo", "bar"]],

      [["title", "==", "foo"], "or", ["tag", "==", "bar"]] =>
        ["(title = ?) OR (puppet_tags.name = ?)", ["foo", "bar"]],

      [["title", "==", "foo"], "or", ["param", "==", "bar"]] =>
        ["(title = ?) OR (param_names.name = ? AND param_values.value = ?)",
         ["foo", "param", "bar"]],

      [[["title","==","foo"],"or",["tag", "==", "bar"]],"and",["param","!=","baz"]] =>
      ["((title = ?) OR (puppet_tags.name = ?)) AND "+
       "(param_names.name = ? AND param_values.value != ?)",
       ["foo", "bar", "param", "baz"]]

    }.each do |input, expect|
      it "should map #{input.inspect} to #{expect.inspect}" do
        filter_to_active_record(input).should == expect
      end
    end
  end
end