summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/resource_type/parser_spec.rb
blob: 4eaf680a2237a79f325a6777456dbb7b7e1ea4ab (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
#!/usr/bin/env ruby

require 'spec_helper'

require 'puppet/indirector/resource_type/parser'
require 'puppet_spec/files'

describe Puppet::Indirector::ResourceType::Parser do
  include PuppetSpec::Files

  before do
    @terminus = Puppet::Indirector::ResourceType::Parser.new
    @request = Puppet::Indirector::Request.new(:resource_type, :find, "foo")
    @krt = @request.environment.known_resource_types
  end

  it "should be registered with the resource_type indirection" do
    Puppet::Indirector::Terminus.terminus_class(:resource_type, :parser).should equal(Puppet::Indirector::ResourceType::Parser)
  end

  describe "when finding" do
    it "should return any found type from the request's environment" do
      type = Puppet::Resource::Type.new(:hostclass, "foo")
      @request.environment.known_resource_types.add(type)

      @terminus.find(@request).should == type
    end

    it "should attempt to load the type if none is found in memory" do
      dir = tmpdir("find_a_type")
      FileUtils.mkdir_p(dir)
      Puppet[:modulepath] = dir

      # Make a new request, since we've reset the env
      @request = Puppet::Indirector::Request.new(:resource_type, :find, "foo::bar")

      manifest_path = File.join(dir, "foo", "manifests")
      FileUtils.mkdir_p(manifest_path)

      File.open(File.join(manifest_path, "bar.pp"), "w") { |f| f.puts "class foo::bar {}" }

      result = @terminus.find(@request)
      result.should be_instance_of(Puppet::Resource::Type)
      result.name.should == "foo::bar"
    end

    it "should return nil if no type can be found" do
      @terminus.find(@request).should be_nil
    end

    it "should prefer definitions to nodes" do
      type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
      node = @krt.add(Puppet::Resource::Type.new(:node, "foo"))

      @terminus.find(@request).should == type
    end
  end

  describe "when searching" do
    before do
      @request.key = "*"
    end

    it "should use the request's environment's list of known resource types" do
      @request.environment.known_resource_types.expects(:hostclasses).returns({})

      @terminus.search(@request)
    end

    it "should return all results if '*' is provided as the search string" do
      @request.key = "*"
      type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
      node = @krt.add(Puppet::Resource::Type.new(:node, "bar"))
      define = @krt.add(Puppet::Resource::Type.new(:definition, "baz"))

      result = @terminus.search(@request)
      result.should be_include(type)
      result.should be_include(node)
      result.should be_include(define)
    end

    it "should treat any search string not '*' as a regex" do
      @request.key = "a"
      foo = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
      bar = @krt.add(Puppet::Resource::Type.new(:hostclass, "bar"))
      baz = @krt.add(Puppet::Resource::Type.new(:hostclass, "baz"))

      result = @terminus.search(@request)
      result.should be_include(bar)
      result.should be_include(baz)
      result.should_not be_include(foo)
    end

    it "should fail if a provided search string is not '*' and is not a valid regex" do
      @request.key = "*foo*"

      # Add one instance so we don't just get an empty array"
      @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
      lambda { @terminus.search(@request) }.should raise_error(ArgumentError)
    end

    it "should return all known types" do
      type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
      node = @krt.add(Puppet::Resource::Type.new(:node, "bar"))
      define = @krt.add(Puppet::Resource::Type.new(:definition, "baz"))

      result = @terminus.search(@request)
      result.should be_include(type)
      result.should be_include(node)
      result.should be_include(define)
    end

    it "should not return the 'main' class" do
      main = @krt.add(Puppet::Resource::Type.new(:hostclass, ""))

      # So there is a return value
      foo = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))

      @terminus.search(@request).should_not be_include(main)
    end

    it "should return nil if no types can be found" do
      @terminus.search(@request).should be_nil
    end

    it "should load all resource types from all search paths" do
      dir = tmpdir("searching_in_all")
      first = File.join(dir, "first")
      second = File.join(dir, "second")
      FileUtils.mkdir_p(first)
      FileUtils.mkdir_p(second)
      Puppet[:modulepath] = "#{first}:#{second}"

      # Make a new request, since we've reset the env
      @request = Puppet::Indirector::Request.new(:resource_type, :search, "*")

      onepath = File.join(first, "one", "manifests")
      FileUtils.mkdir_p(onepath)
      twopath = File.join(first, "two", "manifests")
      FileUtils.mkdir_p(twopath)

      File.open(File.join(onepath, "oneklass.pp"), "w") { |f| f.puts "class one::oneklass {}" }
      File.open(File.join(twopath, "twoklass.pp"), "w") { |f| f.puts "class two::twoklass {}" }

      result = @terminus.search(@request)
      result.find { |t| t.name == "one::oneklass" }.should be_instance_of(Puppet::Resource::Type)
      result.find { |t| t.name == "two::twoklass" }.should be_instance_of(Puppet::Resource::Type)
    end
  end
end