aboutsummaryrefslogtreecommitdiff
path: root/spec/unit/provider/cron/crontab_spec.rb
blob: 4047f82c35c131ddf023590fe6fa70508a7086b5 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
require 'spec_helper'

describe Puppet::Type.type(:cron).provider(:crontab) do
  subject do
    provider = Puppet::Type.type(:cron).provider(:crontab)
    provider.initvars
    provider
  end

  def compare_crontab_text(have, want)
    # We should have four header lines, and then the text...
    expect(have.lines.to_a[0..3]).to(be_all { |x| x =~ %r{^# } })
    expect(have.lines.to_a[4..-1].join('')).to eq(want)
  end

  context 'with the simple samples' do
    FIELDS = {
      crontab: ['command', 'minute', 'hour', 'month', 'monthday', 'weekday'].map { |o| o.to_sym },
      environment: [:line],
      blank: [:line],
      comment: [:line],
    }.freeze

    def compare_crontab_record(have, want)
      want.each do |param, value|
        expect(have).to be_key param
        expect(have[param]).to eq(value)
      end

      (FIELDS[have[:record_type]] - want.keys).each do |name|
        expect(have[name]).to eq(:absent)
      end
    end

    ########################################################################
    # Simple input fixtures for testing.
    samples = YAML.load(File.read(my_fixture('single_line.yaml'))) # rubocop:disable Security/YAMLLoad

    samples.each do |name, data|
      it "parses crontab line #{name} correctly" do
        compare_crontab_record subject.parse_line(data[:text]), data[:record]
      end

      it "reconstructs the crontab line #{name} from the record" do
        expect(subject.to_line(data[:record])).to eq(data[:text])
      end
    end

    records = []
    text    = ''

    # Sorting is from the original, and avoids :empty being the last line,
    # since the provider will ignore that and cause this to fail.
    samples.sort_by { |x| x.first.to_s }.each do |_name, data|
      records << data[:record]
      text    << data[:text] + "\n"
    end

    it 'parses all sample records at once' do
      subject.parse(text).zip(records).each do |round|
        compare_crontab_record(*round)
      end
    end

    it 'reconstitutes the file from the records' do
      compare_crontab_text subject.to_file(records), text
    end

    context 'multi-line crontabs' do
      tests = { simple: [:spaces_in_command_with_times],
                with_name: [:name, :spaces_in_command_with_times],
                with_env: [:environment, :spaces_in_command_with_times],
                with_multiple_envs: [:environment, :lowercase_environment, :spaces_in_command_with_times],
                with_name_and_env: [:name_with_spaces, :another_env, :spaces_in_command_with_times],
                with_name_and_multiple_envs: [:long_name, :another_env, :fourth_env, :spaces_in_command_with_times] }

      all_records = []
      all_text    = ''

      tests.each do |name, content|
        data    = content.map { |x| samples[x] || raise("missing sample data #{x}") }
        text    = data.map { |x| x[:text] }.join("\n") + "\n"
        records = data.map { |x| x[:record] }

        # Capture the whole thing for later, too...
        all_records += records
        all_text    += text

        context name.to_s.tr('_', ' ') do
          it 'regenerates the text from the record' do
            compare_crontab_text subject.to_file(records), text
          end

          it 'parses the records from the text' do
            subject.parse(text).zip(records).each do |round|
              compare_crontab_record(*round)
            end
          end
        end
      end

      it 'parses the whole set of records from the text' do
        subject.parse(all_text).zip(all_records).each do |round|
          compare_crontab_record(*round)
        end
      end

      it 'regenerates the whole text from the set of all records' do
        compare_crontab_text subject.to_file(all_records), all_text
      end
    end
  end

  context 'when receiving a vixie cron header from the cron interface' do
    it 'does not write that header back to disk' do
      vixie_header = File.read(my_fixture('vixie_header.txt'))
      vixie_records = subject.parse(vixie_header)
      compare_crontab_text subject.to_file(vixie_records), ''
    end
  end

  context 'when adding a cronjob with the same command as an existing job' do
    let(:record) { { name: 'existing', user: 'root', command: '/bin/true', record_type: :crontab } }
    let(:resource) { Puppet::Type::Cron.new(name: 'test', user: 'root', command: '/bin/true') }
    let(:resources) { { 'test' => resource } }

    before :each do
      allow(subject).to receive(:prefetch_all_targets).and_return([record])
    end

    # this would be a more fitting test, but I haven't yet
    # figured out how to get it working
    #    it "should include both jobs in the output" do
    #      subject.prefetch(resources)
    #      class Puppet::Provider::ParsedFile
    #        def self.records
    #          @records
    #        end
    #      end
    #      subject.to_file(subject.records).should match /Puppet name: test/
    #    end

    it "does not base the new resource's provider on the existing record" do
      expect(subject).not_to receive(:new).with(record)
      allow(subject).to receive(:new)
      subject.prefetch(resources)
    end
  end

  context 'when prefetching an entry now managed for another user' do
    let(:resource) do
      s = instance_double('Puppet::Type::Crontab')
      allow(s).to receive(:[]).with(:user).and_return 'root'
      allow(s).to receive(:[]).with(:target).and_return 'root'
      s
    end

    let(:record) { { name: 'test', user: 'nobody', command: '/bin/true', record_type: :crontab } }
    let(:resources) { { 'test' => resource } }

    before :each do
      allow(subject).to receive(:prefetch_all_targets).and_return([record])
    end

    it 'tries and use the match method to find a more fitting record' do
      expect(subject).to receive(:match).with(record, resources)
      subject.prefetch(resources)
    end

    it 'does not match a provider to the resource' do
      expect(resource).not_to receive(:provider=)
      subject.prefetch(resources)
    end

    it 'does not find the resource when looking up the on-disk record' do
      subject.prefetch(resources)
      expect(subject.resource_for_record(record, resources)).to be_nil
    end
  end

  context 'when matching resources to existing crontab entries' do
    let(:first_resource) { Puppet::Type::Cron.new(name: :one, user: 'root', command: '/bin/true') }
    let(:second_resource) { Puppet::Type::Cron.new(name: :two, user: 'nobody', command: '/bin/false') }

    let(:resources) { { one: first_resource, two: second_resource } }

    describe 'with a record with a matching name and mismatching user (#2251)' do
      # Puppet::Resource objects have #should defined on them, so in these
      # examples we have to use the monkey patched `must` alias for the rspec
      # `should` method.

      it "doesn't match the record to the resource" do
        record = { name: :one, user: 'notroot', record_type: :crontab }
        expect(subject.resource_for_record(record, resources)).to be_nil
      end
    end

    describe 'with a record with a matching name and matching user' do
      it 'matches the record to the resource' do
        record = { name: :two, target: 'nobody', command: '/bin/false' }
        expect(subject.resource_for_record(record, resources)).to eq(second_resource)
      end
    end
  end

  context '#enumerate_crontabs' do
    before(:each) do
      allow(File).to receive(:readable?).with(subject.crontab_dir).and_return(true)
    end

    context 'only a hidden file' do
      let(:file) { '.keep_cronbase-0' }

      before(:each) do
        path = File.join(subject.crontab_dir, file)
        allow(Dir).to receive(:foreach).with(subject.crontab_dir).and_yield(file)
        allow(File).to receive(:file?).with(path).and_return(true)
        allow(File).to receive(:writable?).with(path).and_return(true)
      end

      it 'ignores .keep_* files' do
        expect { |b| described_class.enumerate_crontabs(&b) }.not_to yield_control
      end
    end

    context 'multiple files' do
      let(:files) { ['myuser', '.keep_cronbase-0'] }

      before(:each) do
        allow(Dir).to receive(:foreach).with(subject.crontab_dir).and_yield(files[0]).and_yield(files[1])

        files.each do |filename|
          path = File.join(subject.crontab_dir, filename)
          allow(File).to receive(:file?).with(path).and_return(true)
          allow(File).to receive(:writable?).with(path).and_return(true)
        end
      end

      it 'ignores .keep_* files' do
        expect { |b| described_class.enumerate_crontabs(&b) }.to yield_control.once
      end
    end
  end
end