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

« back to all changes in this revision

Viewing changes to .pc/secure-indirector-file-backed-terminus-base-cla.patch/spec/unit/indirector/file_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:
1
 
#!/usr/bin/env rspec
2
 
require 'spec_helper'
3
 
require 'puppet/indirector/file'
4
 
 
5
 
 
6
 
describe Puppet::Indirector::File do
7
 
  before :all do
8
 
    Puppet::Indirector::Terminus.stubs(:register_terminus_class)
9
 
    @model = mock 'model'
10
 
    @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
11
 
    Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
12
 
 
13
 
    module Testing; end
14
 
    @file_class = class Testing::MyFile < Puppet::Indirector::File
15
 
      self
16
 
    end
17
 
 
18
 
    @searcher = @file_class.new
19
 
 
20
 
    @path = "/my/file"
21
 
    @dir = "/my"
22
 
 
23
 
    @request = stub 'request', :key => @path
24
 
  end
25
 
 
26
 
  describe "when finding files" do
27
 
    it "should provide a method to return file contents at a specified path" do
28
 
      @searcher.should respond_to(:find)
29
 
    end
30
 
 
31
 
    it "should use the server data directory plus the indirection name if the run_mode is master" do
32
 
      Puppet.run_mode.expects(:master?).returns true
33
 
      Puppet.settings.expects(:value).with(:server_datadir).returns "/my/dir"
34
 
 
35
 
      @searcher.data_directory.should == File.join("/my/dir", "mystuff")
36
 
    end
37
 
 
38
 
    it "should use the client data directory plus the indirection name if the run_mode is not master" do
39
 
      Puppet.run_mode.expects(:master?).returns false
40
 
      Puppet.settings.expects(:value).with(:client_datadir).returns "/my/dir"
41
 
 
42
 
      @searcher.data_directory.should == File.join("/my/dir", "mystuff")
43
 
    end
44
 
 
45
 
    it "should use the newest file in the data directory matching the indirection key without extension" do
46
 
      @searcher.expects(:data_directory).returns "/data/dir"
47
 
      @request.stubs(:key).returns "foo"
48
 
      Dir.expects(:glob).with("/data/dir/foo.*").returns %w{/data1.stuff /data2.stuff}
49
 
 
50
 
      stat1 = stub 'data1', :mtime => (Time.now - 5)
51
 
      stat2 = stub 'data2', :mtime => Time.now
52
 
      File.expects(:stat).with("/data1.stuff").returns stat1
53
 
      File.expects(:stat).with("/data2.stuff").returns stat2
54
 
 
55
 
      @searcher.latest_path(@request).should == "/data2.stuff"
56
 
    end
57
 
 
58
 
    it "should return nil when no files are found" do
59
 
      @searcher.stubs(:latest_path).returns nil
60
 
 
61
 
      @searcher.find(@request).should be_nil
62
 
    end
63
 
 
64
 
    it "should determine the file format from the file extension" do
65
 
      @searcher.file_format("/data2.pson").should == "pson"
66
 
    end
67
 
 
68
 
    it "should fail if the model does not support the file format" do
69
 
      @searcher.stubs(:latest_path).returns "/my/file.pson"
70
 
 
71
 
      @model.expects(:support_format?).with("pson").returns false
72
 
 
73
 
      lambda { @searcher.find(@request) }.should raise_error(ArgumentError)
74
 
    end
75
 
  end
76
 
 
77
 
  describe "when saving files" do
78
 
    before do
79
 
      @content = "my content"
80
 
      @file = stub 'file', :content => @content, :path => @path, :name => @path, :render => "mydata"
81
 
      @request.stubs(:instance).returns @file
82
 
    end
83
 
 
84
 
    it "should provide a method to save file contents at a specified path" do
85
 
      @searcher.should respond_to(:save)
86
 
    end
87
 
 
88
 
    it "should choose the file extension based on the default format of the model" do
89
 
      @model.expects(:default_format).returns "pson"
90
 
 
91
 
      @searcher.serialization_format.should == "pson"
92
 
    end
93
 
 
94
 
    it "should place the file in the data directory, named after the indirection, key, and format" do
95
 
      @searcher.stubs(:data_directory).returns "/my/dir"
96
 
      @searcher.stubs(:serialization_format).returns "pson"
97
 
 
98
 
      @request.stubs(:key).returns "foo"
99
 
      @searcher.file_path(@request).should == File.join("/my/dir", "foo.pson")
100
 
    end
101
 
 
102
 
    it "should fail intelligently if the file's parent directory does not exist" do
103
 
      @searcher.stubs(:file_path).returns "/my/dir/file.pson"
104
 
      @searcher.stubs(:serialization_format).returns "pson"
105
 
 
106
 
      @request.stubs(:key).returns "foo"
107
 
      File.expects(:directory?).with(File.join("/my/dir")).returns(false)
108
 
 
109
 
      proc { @searcher.save(@request) }.should raise_error(Puppet::Error)
110
 
    end
111
 
 
112
 
    it "should render the instance using the file format and print it to the file path" do
113
 
      @searcher.stubs(:file_path).returns "/my/file.pson"
114
 
      @searcher.stubs(:serialization_format).returns "pson"
115
 
 
116
 
      File.stubs(:directory?).returns true
117
 
 
118
 
      @request.instance.expects(:render).with("pson").returns "data"
119
 
 
120
 
      fh = mock 'filehandle'
121
 
      File.expects(:open).with("/my/file.pson", "w").yields fh
122
 
      fh.expects(:print).with("data")
123
 
 
124
 
      @searcher.save(@request)
125
 
    end
126
 
 
127
 
    it "should fail intelligently if a file cannot be written" do
128
 
      filehandle = mock 'file'
129
 
      File.stubs(:directory?).returns(true)
130
 
      File.stubs(:open).yields(filehandle)
131
 
      filehandle.expects(:print).raises(ArgumentError)
132
 
 
133
 
      @searcher.stubs(:file_path).returns "/my/file.pson"
134
 
      @model.stubs(:default_format).returns "pson"
135
 
 
136
 
      @instance.stubs(:render).returns "stuff"
137
 
 
138
 
      proc { @searcher.save(@request) }.should raise_error(Puppet::Error)
139
 
    end
140
 
  end
141
 
 
142
 
  describe "when removing files" do
143
 
    it "should provide a method to remove files" do
144
 
      @searcher.should respond_to(:destroy)
145
 
    end
146
 
 
147
 
    it "should remove files in all formats found in the data directory that match the request key" do
148
 
      @searcher.stubs(:data_directory).returns "/my/dir"
149
 
      @request.stubs(:key).returns "me"
150
 
 
151
 
      Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns %w{/one /two}
152
 
 
153
 
      File.expects(:unlink).with("/one")
154
 
      File.expects(:unlink).with("/two")
155
 
 
156
 
      @searcher.destroy(@request)
157
 
    end
158
 
 
159
 
    it "should throw an exception if no file is found" do
160
 
      @searcher.stubs(:data_directory).returns "/my/dir"
161
 
      @request.stubs(:key).returns "me"
162
 
 
163
 
      Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns []
164
 
 
165
 
      proc { @searcher.destroy(@request) }.should raise_error(Puppet::Error)
166
 
    end
167
 
 
168
 
    it "should fail intelligently if a file cannot be removed" do
169
 
      @searcher.stubs(:data_directory).returns "/my/dir"
170
 
      @request.stubs(:key).returns "me"
171
 
 
172
 
      Dir.expects(:glob).with(File.join("/my/dir", "me.*")).returns %w{/one}
173
 
 
174
 
      File.expects(:unlink).with("/one").raises ArgumentError
175
 
 
176
 
      proc { @searcher.destroy(@request) }.should raise_error(Puppet::Error)
177
 
    end
178
 
  end
179
 
end