~ubuntu-branches/ubuntu/trusty/puppet/trusty

« back to all changes in this revision

Viewing changes to spec/unit/face/node_spec.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2011-10-22 14:08:22 UTC
  • mfrom: (1.1.25) (3.1.32 sid)
  • Revision ID: package-import@ubuntu.com-20111022140822-odxde5lohc45yhuz
Tags: 2.7.6-1
* New upstream release (CVE-2011-3872)
* Remove cherry-picked "groupadd_aix_warning" patch
* Install all new manpages

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
require 'puppet/face'
4
4
 
5
5
describe Puppet::Face[:node, '0.0.1'] do
6
 
  it "REVISIT: really should have some tests"
 
6
  after :all do
 
7
    Puppet::SSL::Host.ca_location = :none
 
8
  end
 
9
 
 
10
  describe '#cleanup' do
 
11
    it "should clean everything" do
 
12
      {
 
13
        "cert"         => ['hostname'],
 
14
        "cached_facts" => ['hostname'],
 
15
        "cached_node"  => ['hostname'],
 
16
        "reports"      => ['hostname'],
 
17
 
 
18
        # Support for cleaning storeconfigs has been temporarily suspended.
 
19
        # "storeconfigs" => ['hostname', :unexport]
 
20
      }.each { |k, v| subject.expects("clean_#{k}".to_sym).with(*v) }
 
21
      subject.cleanup('hostname', :unexport)
 
22
    end
 
23
  end
 
24
 
 
25
  describe 'when running #clean' do
 
26
    before :each do
 
27
      Puppet::Node::Facts.indirection.stubs(:terminus_class=)
 
28
      Puppet::Node::Facts.indirection.stubs(:cache_class=)
 
29
      Puppet::Node.stubs(:terminus_class=)
 
30
      Puppet::Node.stubs(:cache_class=)
 
31
    end
 
32
 
 
33
    it 'should invoke #cleanup' do
 
34
      subject.expects(:cleanup).with('hostname', nil)
 
35
      subject.clean('hostname')
 
36
    end
 
37
  end
 
38
 
 
39
  describe "clean action" do
 
40
    before :each do
 
41
      Puppet::Node::Facts.indirection.stubs(:terminus_class=)
 
42
      Puppet::Node::Facts.indirection.stubs(:cache_class=)
 
43
      Puppet::Node.stubs(:terminus_class=)
 
44
      Puppet::Node.stubs(:cache_class=)
 
45
      subject.stubs(:cleanup)
 
46
    end
 
47
 
 
48
    it "should have a clean action" do
 
49
      subject.should be_action :clean
 
50
    end
 
51
 
 
52
    it "should not accept a call with no arguments" do
 
53
      expect { subject.clean() }.should raise_error
 
54
    end
 
55
 
 
56
    it "should accept a node name" do
 
57
      expect { subject.clean('hostname') }.should_not raise_error
 
58
    end
 
59
 
 
60
    it "should accept more than one node name" do
 
61
      expect do
 
62
        subject.clean('hostname', 'hostname2', {})
 
63
      end.should_not raise_error
 
64
 
 
65
      expect do
 
66
        subject.clean('hostname', 'hostname2', 'hostname3', { :unexport => true })
 
67
      end.should_not raise_error
 
68
    end
 
69
 
 
70
    it "should accept the option --unexport" do
 
71
      expect { subject.help('hostname', :unexport => true) }.
 
72
        should_not raise_error ArgumentError
 
73
    end
 
74
 
 
75
    context "clean action" do
 
76
      subject { Puppet::Face[:node, :current] }
 
77
      before :each do
 
78
        Puppet::Util::Log.stubs(:newdestination)
 
79
        Puppet::Util::Log.stubs(:level=)
 
80
      end
 
81
 
 
82
      describe "during setup" do
 
83
        it "should set facts terminus and cache class to yaml" do
 
84
          Puppet::Node::Facts.indirection.expects(:terminus_class=).with(:yaml)
 
85
          Puppet::Node::Facts.indirection.expects(:cache_class=).with(:yaml)
 
86
 
 
87
          subject.clean('hostname')
 
88
        end
 
89
 
 
90
        it "should run in master mode" do
 
91
          subject.clean('hostname')
 
92
          $puppet_application_mode.name.should == :master
 
93
        end
 
94
 
 
95
        it "should set node cache as yaml" do
 
96
          Puppet::Node.indirection.expects(:terminus_class=).with(:yaml)
 
97
          Puppet::Node.indirection.expects(:cache_class=).with(:yaml)
 
98
 
 
99
          subject.clean('hostname')
 
100
        end
 
101
 
 
102
        it "should manage the certs if the host is a CA" do
 
103
          Puppet::SSL::CertificateAuthority.stubs(:ca?).returns(true)
 
104
          Puppet::SSL::Host.expects(:ca_location=).with(:local)
 
105
          subject.clean('hostname')
 
106
        end
 
107
 
 
108
        it "should not manage the certs if the host is not a CA" do
 
109
          Puppet::SSL::CertificateAuthority.stubs(:ca?).returns(false)
 
110
          Puppet::SSL::Host.expects(:ca_location=).with(:none)
 
111
          subject.clean('hostname')
 
112
        end
 
113
      end
 
114
 
 
115
      describe "when cleaning certificate" do
 
116
        before :each do
 
117
          Puppet::SSL::Host.stubs(:destroy)
 
118
          @ca = mock()
 
119
          Puppet::SSL::CertificateAuthority.stubs(:instance).returns(@ca)
 
120
        end
 
121
 
 
122
        it "should send the :destroy order to the ca if we are a CA" do
 
123
          Puppet::SSL::CertificateAuthority.stubs(:ca?).returns(true)
 
124
          @ca.expects(:revoke).with(@host)
 
125
          @ca.expects(:destroy).with(@host)
 
126
          subject.clean_cert(@host)
 
127
        end
 
128
 
 
129
        it "should not destroy the certs if we are not a CA" do
 
130
          Puppet::SSL::CertificateAuthority.stubs(:ca?).returns(false)
 
131
          @ca.expects(:revoke).never
 
132
          @ca.expects(:destroy).never
 
133
          subject.clean_cert(@host)
 
134
        end
 
135
      end
 
136
 
 
137
      describe "when cleaning cached facts" do
 
138
        it "should destroy facts" do
 
139
          @host = 'node'
 
140
          Puppet::Node::Facts.indirection.expects(:destroy).with(@host)
 
141
 
 
142
          subject.clean_cached_facts(@host)
 
143
        end
 
144
      end
 
145
 
 
146
      describe "when cleaning cached node" do
 
147
        it "should destroy the cached node" do
 
148
          Puppet::Node.indirection.expects(:destroy).with(@host)
 
149
          subject.clean_cached_node(@host)
 
150
        end
 
151
      end
 
152
 
 
153
      describe "when cleaning archived reports" do
 
154
        it "should tell the reports to remove themselves" do
 
155
          Puppet::Transaction::Report.indirection.stubs(:destroy).with(@host)
 
156
 
 
157
          subject.clean_reports(@host)
 
158
        end
 
159
      end
 
160
 
 
161
      # describe "when cleaning storeconfigs entries for host", :if => Puppet.features.rails? do
 
162
      #   before :each do
 
163
      #     # Stub this so we don't need access to the DB
 
164
      #     require 'puppet/rails/host'
 
165
      #
 
166
      #     Puppet.stubs(:[]).with(:storeconfigs).returns(true)
 
167
      #
 
168
      #     Puppet::Rails.stubs(:connect)
 
169
      #     @rails_node = stub_everything 'rails_node'
 
170
      #     Puppet::Rails::Host.stubs(:find_by_name).returns(@rails_node)
 
171
      #   end
 
172
      #
 
173
      #   it "should connect to the database" do
 
174
      #     Puppet::Rails.expects(:connect)
 
175
      #     subject.clean_storeconfigs(@host, false)
 
176
      #   end
 
177
      #
 
178
      #   it "should find the right host entry" do
 
179
      #     Puppet::Rails::Host.expects(:find_by_name).with(@host).returns(@rails_node)
 
180
      #     subject.clean_storeconfigs(@host, false)
 
181
      #   end
 
182
      #
 
183
      #   describe "without unexport" do
 
184
      #     it "should remove the host and it's content" do
 
185
      #       @rails_node.expects(:destroy)
 
186
      #       subject.clean_storeconfigs(@host, false)
 
187
      #     end
 
188
      #   end
 
189
      #
 
190
      #   describe "with unexport" do
 
191
      #     before :each do
 
192
      #       @rails_node.stubs(:id).returns(1234)
 
193
      #
 
194
      #       @type = stub_everything 'type'
 
195
      #       @type.stubs(:validattr?).with(:ensure).returns(true)
 
196
      #
 
197
      #       @ensure_name = stub_everything 'ensure_name', :id => 23453
 
198
      #       Puppet::Rails::ParamName.stubs(:find_or_create_by_name).returns(@ensure_name)
 
199
      #
 
200
      #       @param_values = stub_everything 'param_values'
 
201
      #       @resource = stub_everything 'resource', :param_values => @param_values, :restype => "File"
 
202
      #       Puppet::Rails::Resource.stubs(:find).returns([@resource])
 
203
      #     end
 
204
      #
 
205
      #     it "should find all resources" do
 
206
      #       Puppet::Rails::Resource.expects(:find).with(:all, {:include => {:param_values => :param_name}, :conditions => ["exported=? AND host_id=?", true, 1234]}).returns([])
 
207
      #
 
208
      #       subject.clean_storeconfigs(@host, true)
 
209
      #     end
 
210
      #
 
211
      #     describe "with an exported native type" do
 
212
      #       before :each do
 
213
      #         Puppet::Type.stubs(:type).returns(@type)
 
214
      #         @type.expects(:validattr?).with(:ensure).returns(true)
 
215
      #       end
 
216
      #
 
217
      #       it "should test a native type for ensure as an attribute" do
 
218
      #         subject.clean_storeconfigs(@host, true)
 
219
      #       end
 
220
      #
 
221
      #       it "should delete the old ensure parameter" do
 
222
      #         ensure_param = stub 'ensure_param', :id => 12345, :line => 12
 
223
      #         @param_values.stubs(:find).returns(ensure_param)
 
224
      #         Puppet::Rails::ParamValue.expects(:delete).with(12345);
 
225
      #         subject.clean_storeconfigs(@host, true)
 
226
      #       end
 
227
      #
 
228
      #       it "should add an ensure => absent parameter" do
 
229
      #         @param_values.expects(:create).with(:value => "absent",
 
230
      #                                             :line => 0,
 
231
      #                                             :param_name => @ensure_name)
 
232
      #         subject.clean_storeconfigs(@host, true)
 
233
      #       end
 
234
      #     end
 
235
      #
 
236
      #     describe "with an exported definition" do
 
237
      #       it "should try to lookup a definition and test it for the ensure argument" do
 
238
      #         Puppet::Type.stubs(:type).returns(nil)
 
239
      #         definition = stub_everything 'definition', :arguments => { 'ensure' => 'present' }
 
240
      #         Puppet::Resource::TypeCollection.any_instance.expects(:find_definition).with('', "File").returns(definition)
 
241
      #         subject.clean_storeconfigs(@host, true)
 
242
      #       end
 
243
      #     end
 
244
      #
 
245
      #     it "should not unexport the resource of an unknown type" do
 
246
      #       Puppet::Type.stubs(:type).returns(nil)
 
247
      #       Puppet::Resource::TypeCollection.any_instance.expects(:find_definition).with('', "File").returns(nil)
 
248
      #       Puppet::Rails::ParamName.expects(:find_or_create_by_name).never
 
249
      #       subject.clean_storeconfigs(@host)
 
250
      #     end
 
251
      #
 
252
      #     it "should not unexport the resource of a not ensurable native type" do
 
253
      #       Puppet::Type.stubs(:type).returns(@type)
 
254
      #       @type.expects(:validattr?).with(:ensure).returns(false)
 
255
      #       Puppet::Resource::TypeCollection.any_instance.expects(:find_definition).with('', "File").returns(nil)
 
256
      #       Puppet::Rails::ParamName.expects(:find_or_create_by_name).never
 
257
      #       subject.clean_storeconfigs(@host, true)
 
258
      #     end
 
259
      #
 
260
      #     it "should not unexport the resource of a not ensurable definition" do
 
261
      #       Puppet::Type.stubs(:type).returns(nil)
 
262
      #       definition = stub_everything 'definition', :arguments => { 'foobar' => 'someValue' }
 
263
      #       Puppet::Resource::TypeCollection.any_instance.expects(:find_definition).with('', "File").returns(definition)
 
264
      #       Puppet::Rails::ParamName.expects(:find_or_create_by_name).never
 
265
      #       subject.clean_storeconfigs(@host, true)
 
266
      #     end
 
267
      #   end
 
268
      # end
 
269
    end
 
270
  end
7
271
end