File: config_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (232 lines) | stat: -rw-r--r-- 6,071 bytes parent folder | download | duplicates (2)
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
# coding: utf-8
require 'spec_helper'
require 'puppet/application/config'

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

  # different UTF-8 widths
  # 1-byte A
  # 2-byte ۿ - http://www.fileformat.info/info/unicode/char/06ff/index.htm - 0xDB 0xBF / 219 191
  # 3-byte ᚠ - http://www.fileformat.info/info/unicode/char/16A0/index.htm - 0xE1 0x9A 0xA0 / 225 154 160
  # 4-byte 𠜎 - http://www.fileformat.info/info/unicode/char/2070E/index.htm - 0xF0 0xA0 0x9C 0x8E / 240 160 156 142
  MIXED_UTF8 = "A\u06FF\u16A0\u{2070E}" # Aۿᚠ𠜎

  let(:app) { Puppet::Application[:config] }

  before :each do
    Puppet[:config] = tmpfile('config')
  end

  def initialize_app(args)
    app.command_line.args = args
    # ensure global defaults are initialized prior to app defaults
    Puppet.initialize_settings(args)
  end

  def read_utf8(path)
    File.read(path, :encoding => 'UTF-8')
  end

  def write_utf8(path, content)
    File.write(path, content, 0, :encoding => 'UTF-8')
  end

  context "when printing" do
    it "prints a value" do
      initialize_app(%w[print certname])

      expect {
        app.run
      }.to exit_with(0)
       .and output(a_string_matching(Puppet[:certname])).to_stdout
    end

    it "prints a value from a section" do
      File.write(Puppet[:config], <<~END)
        [main]
        external_nodes=none
        [server]
        external_nodes=exec
      END

      initialize_app(%w[print external_nodes --section server])

      expect {
        app.run
      }.to exit_with(0)
       .and output(a_string_matching('exec')).to_stdout
    end

    it "doesn't require the environment to exist" do
      initialize_app(%w[print certname --environment doesntexist])

      expect {
        app.run
      }.to exit_with(0)
       .and output(a_string_matching(Puppet[:certname])).to_stdout
    end
  end

  context "when setting" do
    it "sets a value in its config file" do
      initialize_app(%w[set certname www.example.com])

      expect {
        app.run
      }.to exit_with(0)

      expect(File.read(Puppet[:config])).to eq("[main]\ncertname = www.example.com\n")
    end

    it "sets a value in the server section" do
      initialize_app(%w[set external_nodes exec --section server])

      expect {
        app.run
      }.to exit_with(0)

      expect(File.read(Puppet[:config])).to eq("[server]\nexternal_nodes = exec\n")
    end

    {
      %w[certname WWW.EXAMPLE.COM] => /Certificate names must be lower case/,
      %w[log_level all] => /Invalid loglevel all/,
      %w[disable_warnings true] => /Cannot disable unrecognized warning types 'true'/,
      %w[strict on] => /Invalid value 'on' for parameter strict/,
      %w[digest_algorithm rot13] => /Invalid value 'rot13' for parameter digest_algorithm/,
      %w[http_proxy_password a#b] => /Passwords set in the http_proxy_password setting must be valid as part of a URL/,
    }.each_pair do |args, message|
      it "rejects #{args.join(' ')}" do
        initialize_app(['set', *args])

        expect {
          app.run
        }.to exit_with(1)
         .and output(message).to_stderr
      end
    end

    it 'sets unknown settings' do
      initialize_app(['set', 'notarealsetting', 'true'])

      expect {
        app.run
      }.to exit_with(0)

      expect(File.read(Puppet[:config])).to eq("[main]\nnotarealsetting = true\n")
    end
  end

  context "when deleting" do
    it "deletes a value" do
      initialize_app(%w[delete external_nodes])

      File.write(Puppet[:config], <<~END)
        [main]
        external_nodes=none
      END

      expect {
        app.run
      }.to exit_with(0)
       .and output(/Deleted setting from 'main': 'external_nodes=none'/).to_stdout

      expect(File.read(Puppet[:config])).to eq("[main]\n")
    end

    it "warns when deleting a value that isn't set" do
      initialize_app(%w[delete external_nodes])

      File.write(Puppet[:config], "")

      expect {
        app.run
      }.to exit_with(0)
       .and output(a_string_matching("Warning: No setting found in configuration file for section 'main' setting name 'external_nodes'")).to_stderr

      expect(File.read(Puppet[:config])).to eq("")
    end

    it "deletes a value from main" do
      initialize_app(%w[delete external_nodes])

      File.write(Puppet[:config], <<~END)
        [main]
        external_nodes=none
      END

      expect {
        app.run
      }.to exit_with(0)
       .and output(/Deleted setting from 'main': 'external_nodes=none'/).to_stdout

      expect(File.read(Puppet[:config])).to eq("[main]\n")
    end

    it "deletes a value from main a section" do
      initialize_app(%w[delete external_nodes --section server])

      File.write(Puppet[:config], <<~END)
        [main]
        external_nodes=none
        [server]
        external_nodes=exec
      END

      expect {
        app.run
      }.to exit_with(0)
       .and output(/Deleted setting from 'server': 'external_nodes'/).to_stdout

      expect(File.read(Puppet[:config])).to eq("[main]\nexternal_nodes=none\n[server]\n")
    end
  end

  context "when managing UTF-8 values" do
    it "reads a UTF-8 value" do
      write_utf8(Puppet[:config], <<~EOF)
        [main]
        tags=#{MIXED_UTF8}
      EOF

      initialize_app(%w[print tags])

      expect {
        app.run
      }.to exit_with(0)
       .and output("#{MIXED_UTF8}\n").to_stdout
    end

    it "sets a UTF-8 value" do
      initialize_app(['set', 'tags', MIXED_UTF8])

      expect {
        app.run
      }.to exit_with(0)

      expect(read_utf8(Puppet[:config])).to eq(<<~EOF)
        [main]
        tags = #{MIXED_UTF8}
      EOF
    end

    it "deletes a UTF-8 value" do
      initialize_app(%w[delete tags])

      write_utf8(Puppet[:config], <<~EOF)
        [main]
        tags=#{MIXED_UTF8}
      EOF

      expect {
        app.run
      }.to exit_with(0)
       .and output(/Deleted setting from 'main': 'tags=#{MIXED_UTF8}'/).to_stdout

      expect(read_utf8(Puppet[:config])).to eq(<<~EOF)
        [main]
      EOF
    end
  end
end