File: parser_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (180 lines) | stat: -rw-r--r-- 6,189 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
require 'spec_helper'

require 'puppet/file_serving/configuration/parser'


module FSConfigurationParserTesting
  def write_config_file(content)
    # We want an array, but we actually want our carriage returns on all of it.
    File.open(@path, 'w') {|f| f.puts content}
  end
end

describe Puppet::FileServing::Configuration::Parser do
  include PuppetSpec::Files

  before :each do
    @path = tmpfile('fileserving_config')
    FileUtils.touch(@path)
    @parser = Puppet::FileServing::Configuration::Parser.new(@path)
  end

  describe Puppet::FileServing::Configuration::Parser, " when parsing" do
    include FSConfigurationParserTesting

    it "should allow comments" do
      write_config_file("# this is a comment\n")
      expect { @parser.parse }.not_to raise_error
    end

    it "should allow blank lines" do
      write_config_file("\n")
      expect { @parser.parse }.not_to raise_error
    end

    it "should return a hash of the created mounts" do
      mount1 = double('one', :validate => true)
      mount2 = double('two', :validate => true)
      expect(Puppet::FileServing::Mount::File).to receive(:new).with("one").and_return(mount1)
      expect(Puppet::FileServing::Mount::File).to receive(:new).with("two").and_return(mount2)
      write_config_file "[one]\n[two]\n"

      result = @parser.parse
      expect(result["one"]).to equal(mount1)
      expect(result["two"]).to equal(mount2)
    end

    it "should only allow mount names that are alphanumeric plus dashes" do
      write_config_file "[a*b]\n"
      expect { @parser.parse }.to raise_error(ArgumentError)
    end

    it "should fail if the value for path/allow/deny starts with an equals sign" do
      write_config_file "[one]\npath = /testing"
      expect { @parser.parse }.to raise_error(ArgumentError)
    end

    it "should validate each created mount" do
      mount1 = double('one')
      expect(Puppet::FileServing::Mount::File).to receive(:new).with("one").and_return(mount1)
      write_config_file "[one]\n"

      expect(mount1).to receive(:validate)

      @parser.parse
    end

    it "should fail if any mount does not pass validation" do
      mount1 = double('one')
      expect(Puppet::FileServing::Mount::File).to receive(:new).with("one").and_return(mount1)
      write_config_file "[one]\n"

      expect(mount1).to receive(:validate).and_raise(RuntimeError)

      expect { @parser.parse }.to raise_error(RuntimeError)
    end

    it "should return comprehensible error message, if invalid line detected" do
      write_config_file "[one]\n\n\x01path /etc/puppetlabs/puppet/files\n\x01allow *\n"

      expect { @parser.parse }.to raise_error(ArgumentError, /Invalid entry at \(file: .*, line: 3\): .*/)
    end
  end

  describe Puppet::FileServing::Configuration::Parser, " when parsing mount attributes" do
    include FSConfigurationParserTesting

    before do
      @mount = double('testmount', :name => "one", :validate => true)
      expect(Puppet::FileServing::Mount::File).to receive(:new).with("one").and_return(@mount)
      allow(@parser).to receive(:add_modules_mount)
    end

    it "should set the mount path to the path attribute from that section" do
      write_config_file "[one]\npath /some/path\n"

      expect(@mount).to receive(:path=).with("/some/path")
      @parser.parse
    end

    [:allow,:deny].each { |acl_type|
      it "should support inline comments in #{acl_type}" do
        write_config_file "[one]\n#{acl_type} something \# will it work?\n"

      expect(@mount).to receive(:info)
      expect(@mount).to receive(acl_type).with("something")
      @parser.parse
      end

      it "should tell the mount to #{acl_type} from ACLs with varying spacing around commas" do
        write_config_file "[one]\n#{acl_type} someone,sometwo, somethree , somefour ,somefive\n"

        expect(@mount).to receive(:info).exactly(5).times
        expect(@mount).to receive(acl_type).exactly(5).times.with(eq('someone').or eq('sometwo').or eq('somethree').or eq('somefour').or eq('somefive'))
        @parser.parse
      end

      # each ip, with glob in the various octet positions
      ['100','4','42','*'].permutation.map {|permutes| permutes.join('.') }.each { |ip_pattern|
        it "should tell the mount to #{acl_type} from ACLs with glob at #{ip_pattern}" do
          write_config_file "[one]\n#{acl_type} #{ip_pattern}\n"

          expect(@mount).to receive(:info)
          expect(@mount).to receive(acl_type).with(ip_pattern)
          @parser.parse
        end
      }
    }

    it "should return comprehensible error message, if failed on invalid attribute" do
      write_config_file "[one]\ndo something\n"

      expect { @parser.parse }.to raise_error(ArgumentError, /Invalid argument 'do' at \(file: .*, line: 2\)/)
    end
  end

  describe Puppet::FileServing::Configuration::Parser, " when parsing the modules mount" do
    include FSConfigurationParserTesting

    before do
      @mount = double('modulesmount', :name => "modules", :validate => true)
    end

    it "should create an instance of the Modules Mount class" do
      write_config_file "[modules]\n"

      expect(Puppet::FileServing::Mount::Modules).to receive(:new).with("modules").and_return(@mount)
      @parser.parse
    end

    it "should warn if a path is set" do
      write_config_file "[modules]\npath /some/path\n"
      expect(Puppet::FileServing::Mount::Modules).to receive(:new).with("modules").and_return(@mount)

      expect(Puppet).to receive(:warning)
      @parser.parse
    end
  end

  describe Puppet::FileServing::Configuration::Parser, " when parsing the plugins mount" do
    include FSConfigurationParserTesting

    before do
      @mount = double('pluginsmount', :name => "plugins", :validate => true)
    end

    it "should create an instance of the Plugins Mount class" do
      write_config_file "[plugins]\n"

      expect(Puppet::FileServing::Mount::Plugins).to receive(:new).with("plugins").and_return(@mount)
      @parser.parse
    end

    it "should warn if a path is set" do
      write_config_file "[plugins]\npath /some/path\n"

      expect(Puppet).to receive(:warning)
      @parser.parse
    end
  end
end