summaryrefslogtreecommitdiff
path: root/spec/unit/provider/ifconfig/debian_spec.rb
blob: 160a0ed4eb93f94087207a76db1e8be795de4113 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#
# Author:: Xabier de Zuazo (xabier@onddo.com)
# Copyright:: Copyright (c) 2013 Onddo Labs, SL.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'
require 'chef/exceptions'

describe Chef::Provider::Ifconfig::Debian do

  let(:run_context) do
    node = Chef::Node.new
    cookbook_collection = Chef::CookbookCollection.new([])
    events = Chef::EventDispatch::Dispatcher.new
    Chef::RunContext.new(node, cookbook_collection, events)
  end

  let(:new_resource) do
    new_resource = Chef::Resource::Ifconfig.new("10.0.0.1", run_context)
    new_resource.mask "255.255.254.0"
    new_resource.metric "1"
    new_resource.mtu "1500"
    new_resource.device "eth0"
    new_resource
  end

  let(:current_resource) {  Chef::Resource::Ifconfig.new("10.0.0.1", run_context) }

  let(:provider) do
    status = double("Status", :exitstatus => 0)
    provider = Chef::Provider::Ifconfig::Debian.new(new_resource, run_context)
    provider.instance_variable_set("@status", status)
    provider.current_resource = current_resource
    allow(provider).to receive(:load_current_resource)
    allow(provider).to receive(:run_command)
    provider
  end

  let(:config_filename_ifaces) { "/etc/network/interfaces" }

  let(:config_filename_ifcfg) { "/etc/network/interfaces.d/ifcfg-#{new_resource.device}" }

  describe "generate_config" do

    context "when writing a file" do
      let(:tempfile) { Tempfile.new("rspec-chef-ifconfig-debian") }

      let(:tempdir_path) { Dir.mktmpdir("rspec-chef-ifconfig-debian-dir") }

      let(:config_filename_ifcfg) { "#{tempdir_path}/ifcfg-#{new_resource.device}" }

      before do
        stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_FILE", tempfile.path)
        stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_DOT_D_DIR", tempdir_path)
      end

      it "should write a network-script" do
        provider.run_action(:add)
        expect(File.read(config_filename_ifcfg)).to match(/^iface eth0 inet static\s*$/)
        expect(File.read(config_filename_ifcfg)).to match(/^\s+address 10\.0\.0\.1\s*$/)
        expect(File.read(config_filename_ifcfg)).to match(/^\s+netmask 255\.255\.254\.0\s*$/)
      end

      context "when the interface_dot_d directory does not exist" do
        before do
          FileUtils.rmdir tempdir_path
          expect(File.exists?(tempdir_path)).to be_false
        end

        it "should create the /etc/network/interfaces.d directory" do
          provider.run_action(:add)
          expect(File.exists?(tempdir_path)).to be_true
          expect(File.directory?(tempdir_path)).to be_true
        end

        it "should mark the resource as updated" do
          provider.run_action(:add)
          expect(new_resource.updated_by_last_action?).to be_true
        end
      end

      context "when the interface_dot_d directory exists" do
        before do
          expect(File.exists?(tempdir_path)).to be_true
        end

        it "should still mark the resource as updated (we still write a file to it)" do
          provider.run_action(:add)
          expect(new_resource.updated_by_last_action?).to be_true
        end
      end
    end

    context "when the file is up-to-date" do
      let(:tempfile) { Tempfile.new("rspec-chef-ifconfig-debian") }

      let(:tempdir_path) { Dir.mktmpdir("rspec-chef-ifconfig-debian-dir") }

      let(:config_filename_ifcfg) { "#{tempdir_path}/ifcfg-#{new_resource.device}" }

      before do
        stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_FILE", tempfile.path)
        stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_DOT_D_DIR", tempdir_path)
        config_file_ifcfg = StringIO.new(<<-EOF
iface eth0 inet static
  address 10.0.0.1
  netmask 255.255.254.0
EOF
        )
        expect(File.exists?(tempdir_path)).to be_true  # since the file exists, the enclosing dir must also exist
      end

      context "when the /etc/network/interfaces file has the source line" do
        let(:expected_string) do
          <<-EOF
a line
source #{tempdir_path}/*
another line
EOF
        end

        before do
          tempfile.write(expected_string)
          tempfile.close

          expect(provider).not_to receive(:converge_by).with(/modifying #{tempfile.path} to source #{tempdir_path}/)
        end

        it "should preserve all the contents" do
          provider.run_action(:add)
          expect(IO.read(tempfile.path)).to eq(expected_string)
        end

        it "should not mark the resource as updated" do
          provider.run_action(:add)
          pending "superclass ifconfig provider is not idempotent"
          expect(new_resource.updated_by_last_action?).to be_false
        end
      end

      context "when the /etc/network/interfaces file does not have the source line" do
        let(:expected_string) do
          <<-EOF
a line
another line
source #{tempdir_path}/*
EOF
        end

        before do
          tempfile.write("a line\nanother line\n")
          tempfile.close

          allow(provider).to receive(:converge_by).and_call_original
          expect(provider).to receive(:converge_by).with(/modifying #{tempfile.path} to source #{tempdir_path}/).and_call_original
        end

        it "should preserve the original contents and add the source line" do
          provider.run_action(:add)
          expect(IO.read(tempfile.path)).to eq(expected_string)
        end

        it "should mark the resource as updated" do
          provider.run_action(:add)
          expect(new_resource.updated_by_last_action?).to be_true
        end
      end
    end

    describe "when running under why run" do

      before do
        Chef::Config[:why_run] = true
      end

      after do
        Chef::Config[:why_run] = false
      end

      context "when writing a file" do
        let(:config_file_ifcfg) { StringIO.new }

        let(:tempfile) { Tempfile.new("rspec-chef-ifconfig-debian") }

        let(:tempdir_path) { Dir.mktmpdir("rspec-chef-ifconfig-debian-dir") }

        let(:config_filename_ifcfg) { "#{tempdir_path}/ifcfg-#{new_resource.device}" }

        before do
          stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_FILE", tempfile.path)
          stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_DOT_D_DIR", tempdir_path)
          expect(File).not_to receive(:new).with(config_filename_ifcfg, "w")
        end

        it "should write a network-script" do
          provider.run_action(:add)
          expect(config_file_ifcfg.string).not_to match(/^iface eth0 inet static\s*$/)
          expect(config_file_ifcfg.string).not_to match(/^\s+address 10\.0\.0\.1\s*$/)
          expect(config_file_ifcfg.string).not_to match(/^\s+netmask 255\.255\.254\.0\s*$/)
        end

        context "when the interface_dot_d directory does not exist" do
          before do
            FileUtils.rmdir tempdir_path
            expect(File.exists?(tempdir_path)).to be_false
          end

          it "should not create the /etc/network/interfaces.d directory" do
            provider.run_action(:add)
            expect(File.exists?(tempdir_path)).not_to be_true
          end

          it "should mark the resource as updated" do
            provider.run_action(:add)
            expect(new_resource.updated_by_last_action?).to be_true
          end
        end

        context "when the interface_dot_d directory exists" do
          before do
            expect(File.exists?(tempdir_path)).to be_true
          end

          it "should still mark the resource as updated (we still write a file to it)" do
            provider.run_action(:add)
            expect(new_resource.updated_by_last_action?).to be_true
          end
        end
      end

      context "when the file is up-to-date" do
        let(:tempfile) { Tempfile.new("rspec-chef-ifconfig-debian") }

        let(:tempdir_path) { Dir.mktmpdir("rspec-chef-ifconfig-debian-dir") }

        let(:config_filename_ifcfg) { "#{tempdir_path}/ifcfg-#{new_resource.device}" }

        before do
          stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_FILE", tempfile.path)
          stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_DOT_D_DIR", tempdir_path)
          config_file_ifcfg = StringIO.new(<<-EOF
iface eth0 inet static
  address 10.0.0.1
  netmask 255.255.254.0
                                           EOF
                                          )
          expect(File).not_to receive(:new).with(config_filename_ifcfg, "w")
          expect(File.exists?(tempdir_path)).to be_true  # since the file exists, the enclosing dir must also exist
        end

        context "when the /etc/network/interfaces file has the source line" do
          let(:expected_string) do
            <<-EOF
a line
source #{tempdir_path}/*
another line
            EOF
          end

          before do
            tempfile.write(expected_string)
            tempfile.close
          end

          it "should preserve all the contents" do
            provider.run_action(:add)
            expect(IO.read(tempfile.path)).to eq(expected_string)
          end

          it "should not mark the resource as updated" do
            provider.run_action(:add)
            pending "superclass ifconfig provider is not idempotent"
            expect(new_resource.updated_by_last_action?).to be_false
          end
        end

        context "when the /etc/network/interfaces file does not have the source line" do
          let(:expected_string) do
            <<-EOF
a line
another line
source #{tempdir_path}/*
            EOF
          end

          before do
            tempfile.write("a line\nanother line\n")
            tempfile.close
          end

          it "should preserve the original contents and not add the source line" do
            provider.run_action(:add)
            expect(IO.read(tempfile.path)).to eq("a line\nanother line\n")
          end

          it "should mark the resource as updated" do
            provider.run_action(:add)
            expect(new_resource.updated_by_last_action?).to be_true
          end
        end
      end
    end
  end

  describe "delete_config for action_delete" do

    let(:tempfile) { Tempfile.new("rspec-chef-ifconfig-debian") }

    let(:tempdir_path) { Dir.mktmpdir("rspec-chef-ifconfig-debian-dir") }

    let(:config_filename_ifcfg) { "#{tempdir_path}/ifcfg-#{new_resource.device}" }

    before do
      stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_FILE", tempfile.path)
      stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_DOT_D_DIR", tempdir_path)
      File.open(config_filename_ifcfg, "w") do |fh|
        fh.write "arbitrary text\n"
        fh.close
      end
    end

    after do
      Dir.rmdir(tempdir_path)
    end

    it "should delete network-script if it exists" do
      current_resource.device new_resource.device

      # belt and suspenders testing?
      expect_any_instance_of(Chef::Util::Backup).to receive(:do_backup).and_call_original

      # internal implementation detail of Ifconfig.
      expect_any_instance_of(Chef::Provider::File).to receive(:action_delete).and_call_original

      expect(File.exist?(config_filename_ifcfg)).to be_true
      provider.run_action(:delete)
      expect(File.exist?(config_filename_ifcfg)).to be_false
    end
  end

end