File: resource_spec.rb

package info (click to toggle)
puppet-agent 8.10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,392 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (232 lines) | stat: -rw-r--r-- 8,297 bytes parent folder | download
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require 'spec_helper'

require 'puppet/application/resource'
require 'puppet_spec/character_encoding'

describe Puppet::Application::Resource do
  include PuppetSpec::Files

  before :each do
    @resource_app = Puppet::Application[:resource]
    allow(Puppet::Util::Log).to receive(:newdestination)
  end

  describe "in preinit" do
    it "should include provider parameter by default" do
      @resource_app.preinit
      expect(@resource_app.extra_params).to eq([:provider])
    end
  end

  describe "when handling options" do
    [:debug, :verbose, :edit].each do |option|
      it "should store argument value when calling handle_#{option}" do
        expect(@resource_app.options).to receive(:[]=).with(option, 'arg')
        @resource_app.send("handle_#{option}".to_sym, 'arg')
      end
    end

    it "should load a display all types with types option" do
      type1 = double('type1', :name => :type1)
      type2 = double('type2', :name => :type2)
      allow(Puppet::Type).to receive(:loadall)
      allow(Puppet::Type).to receive(:eachtype).and_yield(type1).and_yield(type2)
      expect(@resource_app).to receive(:puts).with(['type1','type2'])
      expect { @resource_app.handle_types(nil) }.to exit_with 0
    end

    it "should add param to extra_params list" do
      @resource_app.extra_params = [ :param1 ]
      @resource_app.handle_param("whatever")

      expect(@resource_app.extra_params).to eq([ :param1, :whatever ])
    end

    it "should get a parameter in the printed data if extra_params are passed" do
      tty  = double("tty",  :tty? => true )
      path = tmpfile('testfile')
      command_line = Puppet::Util::CommandLine.new("puppet", [ 'resource', 'file', path ], tty )
      allow(@resource_app).to receive(:command_line).and_return(command_line)

      # provider is a parameter that should always be available
      @resource_app.extra_params = [ :provider ]

      expect {
        @resource_app.main
      }.to output(/provider\s+=>/).to_stdout
    end
  end

  describe "during setup" do
    before :each do
      allow(Puppet::Log).to receive(:newdestination)
    end

    it "should set console as the log destination" do
      expect(Puppet::Log).to receive(:newdestination).with(:console)

      @resource_app.setup
    end

    it "should set log level to debug if --debug was passed" do
      allow(@resource_app.options).to receive(:[]).with(:debug).and_return(true)
      @resource_app.setup
      expect(Puppet::Log.level).to eq(:debug)
    end

    it "should set log level to info if --verbose was passed" do
      allow(@resource_app.options).to receive(:[]).with(:debug).and_return(false)
      allow(@resource_app.options).to receive(:[]).with(:verbose).and_return(true)
      @resource_app.setup
      expect(Puppet::Log.level).to eq(:info)
    end
  end

  describe "when running" do
    before :each do
      @type = double('type', :properties => [])
      allow(@resource_app.command_line).to receive(:args).and_return(['mytype'])
      allow(Puppet::Type).to receive(:type).and_return(@type)

      @res = double("resource")
      allow(@res).to receive(:prune_parameters).and_return(@res)
      allow(@res).to receive(:to_manifest).and_return("resource")
      @report = double("report")

      allow(@resource_app).to receive(:puts)
    end

    it "should raise an error if no type is given" do
      allow(@resource_app.command_line).to receive(:args).and_return([])
      expect { @resource_app.main }.to raise_error(RuntimeError, "You must specify the type to display")
    end

    it "should raise an error if the type is not found" do
      allow(Puppet::Type).to receive(:type).and_return(nil)

      expect { @resource_app.main }.to raise_error(RuntimeError, 'Could not find type mytype')
    end

    it "should search for resources" do
      expect(Puppet::Resource.indirection).to receive(:search).with('mytype/', {}).and_return([])
      @resource_app.main
    end

    it "should describe the given resource" do
      allow(@resource_app.command_line).to receive(:args).and_return(['type','name'])
      expect(Puppet::Resource.indirection).to receive(:find).with('type/name').and_return(@res)
      @resource_app.main
    end

    before :each do
      allow(@res).to receive(:ref).and_return("type/name")
    end

    it "should add given parameters to the object" do
      allow(@resource_app.command_line).to receive(:args).and_return(['type','name','param=temp'])

      expect(Puppet::Resource.indirection).to receive(:save).with(@res, 'type/name').and_return([@res, @report])
      expect(Puppet::Resource).to receive(:new).with('type', 'name', {:parameters => {'param' => 'temp'}}).and_return(@res)

      resource_status = instance_double('Puppet::Resource::Status')
      allow(@report).to receive(:resource_statuses).and_return({'type/name' => resource_status})
      allow(resource_status).to receive(:failed?).and_return(false)
      @resource_app.main
    end
  end

  describe "when printing output" do
    Puppet::Type.newtype(:stringify) do
      ensurable
      newparam(:name, isnamevar: true)
      newproperty(:string)
    end

    Puppet::Type.type(:stringify).provide(:stringify) do
      def exists?
        true
      end

      def string=(value)
      end

      def string
        Puppet::Util::Execution::ProcessOutput.new('test', 0)
      end
    end
    
    it "should not emit puppet class tags when printing yaml when strict mode is off" do
      Puppet[:strict] = :warning

      @resource_app.options[:to_yaml] = true
      allow(@resource_app.command_line).to receive(:args).and_return(['stringify', 'hello', 'ensure=present', 'string=asd'])
      expect(@resource_app).to receive(:puts).with(<<~YAML)
      ---
      stringify:
        hello:
          ensure: present
          string: test
      YAML
      expect { @resource_app.main }.not_to raise_error
    end

    it "should ensure all values to be printed are in the external encoding" do
      resources = [
        Puppet::Type.type(:user).new(:name => "\u2603".force_encoding(Encoding::UTF_8)).to_resource,
        Puppet::Type.type(:user).new(:name => "Jos\xE9".force_encoding(Encoding::ISO_8859_1)).to_resource
      ]
      expect(Puppet::Resource.indirection).to receive(:search).with('user/', {}).and_return(resources)
      allow(@resource_app.command_line).to receive(:args).and_return(['user'])

      # All of our output should be in external encoding
      expect(@resource_app).to receive(:puts) { |args| expect(args.encoding).to eq(Encoding::ISO_8859_1) }

      # This would raise an error if we weren't handling it
      PuppetSpec::CharacterEncoding.with_external_encoding(Encoding::ISO_8859_1) do
        expect { @resource_app.main }.not_to raise_error
      end
    end
  end

  describe "when handling file type" do
    before :each do
      allow(Facter).to receive(:loadfacts)
      @resource_app.preinit
    end

    it "should raise an exception if no file specified" do
      allow(@resource_app.command_line).to receive(:args).and_return(['file'])

      expect { @resource_app.main }.to raise_error(RuntimeError, /Listing all file instances is not supported/)
    end

    it "should output a file resource when given a file path" do
      path = File.expand_path('/etc')
      res = Puppet::Type.type(:file).new(:path => path).to_resource
      expect(Puppet::Resource.indirection).to receive(:find).and_return(res)

      allow(@resource_app.command_line).to receive(:args).and_return(['file', path])
      expect(@resource_app).to receive(:puts).with(/file \{ '#{Regexp.escape(path)}'/m)

      @resource_app.main
    end
  end

  describe 'when handling a custom type' do
    it 'the Puppet::Pops::Loaders instance is available' do
      Puppet::Type.newtype(:testing) do
        newparam(:name) do
          isnamevar
        end
        def self.instances
          fail('Loader not found') unless Puppet::Pops::Loaders.find_loader(nil).is_a?(Puppet::Pops::Loader::Loader)
          @instances ||= [new(:name => name)]
        end
      end

      allow(@resource_app.command_line).to receive(:args).and_return(['testing', 'hello'])
      expect(@resource_app).to receive(:puts).with("testing { 'hello':\n}")
      expect { @resource_app.main }.not_to raise_error
    end
  end
end