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

« back to all changes in this revision

Viewing changes to .pc/CVE-2011-3848.patch/spec/unit/indirector/yaml_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
 
 
4
 
require 'puppet/indirector/yaml'
5
 
 
6
 
describe Puppet::Indirector::Yaml, " when choosing file location" do
7
 
  before :all do
8
 
    @indirection = stub 'indirection', :name => :my_yaml, :register_terminus_type => nil
9
 
    Puppet::Indirector::Indirection.expects(:instance).with(:my_yaml).returns(@indirection)
10
 
    module MyYaml; end
11
 
    @store_class = class MyYaml::MyType < Puppet::Indirector::Yaml
12
 
      self
13
 
    end
14
 
  end
15
 
  before :each do
16
 
    @store = @store_class.new
17
 
 
18
 
    @subject = Object.new
19
 
    @subject.singleton_class.send(:attr_accessor, :name)
20
 
    @subject.name = :me
21
 
 
22
 
    @dir = "/what/ever"
23
 
    Puppet.settings.stubs(:value).returns("fakesettingdata")
24
 
    Puppet.settings.stubs(:value).with(:clientyamldir).returns(@dir)
25
 
    Puppet.run_mode.stubs(:master?).returns false
26
 
 
27
 
    @request = stub 'request', :key => :me, :instance => @subject
28
 
  end
29
 
 
30
 
  describe Puppet::Indirector::Yaml, " when choosing file location" do
31
 
    it "should use the server_datadir if the run_mode is master" do
32
 
      Puppet.run_mode.expects(:master?).returns true
33
 
      Puppet.settings.expects(:value).with(:yamldir).returns "/server/yaml/dir"
34
 
      @store.path(:me).should =~ %r{^/server/yaml/dir}
35
 
    end
36
 
 
37
 
    it "should use the client yamldir if the run_mode is not master" do
38
 
      Puppet.run_mode.expects(:master?).returns false
39
 
      Puppet.settings.expects(:value).with(:clientyamldir).returns "/client/yaml/dir"
40
 
      @store.path(:me).should =~ %r{^/client/yaml/dir}
41
 
    end
42
 
 
43
 
    it "should use the extension if one is specified" do
44
 
      Puppet.run_mode.expects(:master?).returns true
45
 
      Puppet.settings.expects(:value).with(:yamldir).returns "/server/yaml/dir"
46
 
      @store.path(:me,'.farfignewton').should =~ %r{\.farfignewton$}
47
 
    end
48
 
 
49
 
    it "should assume an extension of .yaml if none is specified" do
50
 
      Puppet.run_mode.expects(:master?).returns true
51
 
      Puppet.settings.expects(:value).with(:yamldir).returns "/server/yaml/dir"
52
 
      @store.path(:me).should =~ %r{\.yaml$}
53
 
    end
54
 
 
55
 
    it "should store all files in a single file root set in the Puppet defaults" do
56
 
      @store.path(:me).should =~ %r{^#{@dir}}
57
 
    end
58
 
 
59
 
    it "should use the terminus name for choosing the subdirectory" do
60
 
      @store.path(:me).should =~ %r{^#{@dir}/my_yaml}
61
 
    end
62
 
 
63
 
    it "should use the object's name to determine the file name" do
64
 
      @store.path(:me).should =~ %r{me.yaml$}
65
 
    end
66
 
  end
67
 
 
68
 
  describe Puppet::Indirector::Yaml, " when storing objects as YAML" do
69
 
    it "should only store objects that respond to :name" do
70
 
      @request.stubs(:instance).returns Object.new
71
 
      proc { @store.save(@request) }.should raise_error(ArgumentError)
72
 
    end
73
 
 
74
 
    it "should convert Ruby objects to YAML and write them to disk using a write lock" do
75
 
      yaml = @subject.to_yaml
76
 
      file = mock 'file'
77
 
      path = @store.send(:path, @subject.name)
78
 
      FileTest.expects(:exist?).with(File.dirname(path)).returns(true)
79
 
      @store.expects(:writelock).with(path, 0660).yields(file)
80
 
      file.expects(:print).with(yaml)
81
 
 
82
 
      @store.save(@request)
83
 
    end
84
 
 
85
 
    it "should create the indirection subdirectory if it does not exist" do
86
 
      yaml = @subject.to_yaml
87
 
      file = mock 'file'
88
 
      path = @store.send(:path, @subject.name)
89
 
      dir = File.dirname(path)
90
 
 
91
 
      FileTest.expects(:exist?).with(dir).returns(false)
92
 
      Dir.expects(:mkdir).with(dir)
93
 
 
94
 
      @store.expects(:writelock).yields(file)
95
 
      file.expects(:print).with(yaml)
96
 
 
97
 
      @store.save(@request)
98
 
    end
99
 
  end
100
 
 
101
 
  describe Puppet::Indirector::Yaml, " when retrieving YAML" do
102
 
    it "should read YAML in from disk using a read lock and convert it to Ruby objects" do
103
 
      path = @store.send(:path, @subject.name)
104
 
 
105
 
      yaml = @subject.to_yaml
106
 
      FileTest.expects(:exist?).with(path).returns(true)
107
 
 
108
 
      fh = mock 'filehandle'
109
 
      @store.expects(:readlock).with(path).yields fh
110
 
      fh.expects(:read).returns yaml
111
 
 
112
 
      @store.find(@request).instance_variable_get("@name").should == :me
113
 
    end
114
 
 
115
 
    it "should fail coherently when the stored YAML is invalid" do
116
 
      path = @store.send(:path, @subject.name)
117
 
      FileTest.expects(:exist?).with(path).returns(true)
118
 
 
119
 
      # Something that will fail in yaml
120
 
      yaml = "--- !ruby/object:Hash"
121
 
 
122
 
      fh = mock 'filehandle'
123
 
      @store.expects(:readlock).yields fh
124
 
      fh.expects(:read).returns yaml
125
 
 
126
 
      proc { @store.find(@request) }.should raise_error(Puppet::Error)
127
 
    end
128
 
  end
129
 
 
130
 
  describe Puppet::Indirector::Yaml, " when searching" do
131
 
    it "should return an array of fact instances with one instance for each file when globbing *" do
132
 
      @request = stub 'request', :key => "*", :instance => @subject
133
 
      @one = mock 'one'
134
 
      @two = mock 'two'
135
 
      @store.expects(:path).with(@request.key,'').returns :glob
136
 
      Dir.expects(:glob).with(:glob).returns(%w{one.yaml two.yaml})
137
 
      YAML.expects(:load_file).with("one.yaml").returns @one;
138
 
      YAML.expects(:load_file).with("two.yaml").returns @two;
139
 
      @store.search(@request).should == [@one, @two]
140
 
    end
141
 
 
142
 
    it "should return an array containing a single instance of fact when globbing 'one*'" do
143
 
      @request = stub 'request', :key => "one*", :instance => @subject
144
 
      @one = mock 'one'
145
 
      @store.expects(:path).with(@request.key,'').returns :glob
146
 
      Dir.expects(:glob).with(:glob).returns(%w{one.yaml})
147
 
      YAML.expects(:load_file).with("one.yaml").returns @one;
148
 
      @store.search(@request).should == [@one]
149
 
    end
150
 
 
151
 
    it "should return an empty array when the glob doesn't match anything" do
152
 
      @request = stub 'request', :key => "f*ilglobcanfail*", :instance => @subject
153
 
      @store.expects(:path).with(@request.key,'').returns :glob
154
 
      Dir.expects(:glob).with(:glob).returns []
155
 
      @store.search(@request).should == []
156
 
    end
157
 
  end
158
 
end