File: nfs_test.rb

package info (click to toggle)
vagrant 2.2.14%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,800 kB
  • sloc: ruby: 97,301; sh: 375; makefile: 16; lisp: 1
file content (386 lines) | stat: -rw-r--r-- 14,794 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
require_relative "../../../../base"
require_relative "../../../../../../plugins/hosts/linux/cap/nfs"
require "vagrant/util"

describe VagrantPlugins::HostLinux::Cap::NFS do

  include_context "unit"

  let(:caps) do
    VagrantPlugins::HostLinux::Plugin
      .components
      .host_capabilities[:linux]
  end

  let(:tmp_exports_path) do
    @tmp_exports ||= temporary_file
  end
  let(:exports_path){ VagrantPlugins::HostLinux::Cap::NFS::NFS_EXPORTS_PATH }
  let(:env){ double(:env) }
  let(:ui){ double(:ui) }
  let(:host){ double(:host) }

  before do
    @original_exports_path = VagrantPlugins::HostLinux::Cap::NFS::NFS_EXPORTS_PATH
    VagrantPlugins::HostLinux::Cap::NFS.send(:remove_const, :NFS_EXPORTS_PATH)
    VagrantPlugins::HostLinux::Cap::NFS.const_set(:NFS_EXPORTS_PATH, tmp_exports_path.to_s)
    allow(Vagrant::Util::Subprocess).to receive(:execute).with("systemctl", "list-units", any_args).
      and_return(Vagrant::Util::Subprocess::Result.new(1, "", ""))
    allow(Vagrant::Util::Platform).to receive(:systemd?).and_return(false)
  end

  after do
    VagrantPlugins::HostLinux::Cap::NFS.send(:remove_const, :NFS_EXPORTS_PATH)
    VagrantPlugins::HostLinux::Cap::NFS.const_set(:NFS_EXPORTS_PATH, @original_exports_path)
    VagrantPlugins::HostLinux::Cap::NFS.reset!
    File.unlink(tmp_exports_path.to_s) if File.exist?(tmp_exports_path.to_s)
    @tmp_exports = nil
  end

  describe ".nfs_service_name_systemd" do
    let(:cap){ VagrantPlugins::HostLinux::Cap::NFS }

    context "without service match" do
      it "should use default service name" do
        expect(cap.nfs_service_name_systemd).to eq(cap.const_get(:NFS_DEFAULT_NAME_SYSTEMD))
      end
    end

    context "with service match" do
      let(:custom_nfs_service_name){ "custom-nfs-server-service-name" }
      before{ expect(Vagrant::Util::Subprocess).to receive(:execute).with("systemctl", "list-units", any_args).
          and_return(Vagrant::Util::Subprocess::Result.new(0, custom_nfs_service_name, "")) }

      it "should use the matched service name" do
        expect(cap.nfs_service_name_systemd).to eq(custom_nfs_service_name)
      end
    end
  end

  describe ".nfs_service_name_sysv" do
    let(:cap){ VagrantPlugins::HostLinux::Cap::NFS }

    context "without service match" do
      it "should use default service name" do
        expect(cap.nfs_service_name_sysv).to eq(cap.const_get(:NFS_DEFAULT_NAME_SYSV))
      end
    end

    context "with service match" do
      let(:custom_nfs_service_name){ "/etc/init.d/custom-nfs-server-service-name" }
      before{ expect(Dir).to receive(:glob).with(/.+init\.d.+/).and_return([custom_nfs_service_name]) }

      it "should use the matched service name" do
        expect(cap.nfs_service_name_sysv).to eq(File.basename(custom_nfs_service_name))
      end
    end
  end

  describe ".nfs_check_command" do
    let(:cap){ caps.get(:nfs_check_command) }

    context "without systemd" do
      before{ expect(Vagrant::Util::Platform).to receive(:systemd?).and_return(false) }

      it "should use init.d script" do
        expect(cap.nfs_check_command(env)).to include("init.d")
      end
    end
    context "with systemd" do
      before do
        expect(Vagrant::Util::Platform).to receive(:systemd?).and_return(true)
      end

      it "should use systemctl" do
        expect(cap.nfs_check_command(env)).to include("systemctl")
      end
    end
  end

  describe ".nfs_start_command" do
    let(:cap){ caps.get(:nfs_start_command) }

    context "without systemd" do
      before{ expect(Vagrant::Util::Platform).to receive(:systemd?).and_return(false) }

      it "should use init.d script" do
        expect(cap.nfs_start_command(env)).to include("init.d")
      end
    end
    context "with systemd" do
      before{ expect(Vagrant::Util::Platform).to receive(:systemd?).and_return(true) }

      it "should use systemctl" do
        expect(cap.nfs_start_command(env)).to include("systemctl")
      end
    end
  end

  describe ".nfs_export" do

    let(:cap){ caps.get(:nfs_export) }

    before do
      allow(env).to receive(:host).and_return(host)
      allow(host).to receive(:capability).with(:nfs_apply_command).and_return("/bin/true")
      allow(host).to receive(:capability).with(:nfs_check_command).and_return("/bin/true")
      allow(host).to receive(:capability).with(:nfs_start_command).and_return("/bin/true")
      allow(ui).to receive(:info)
      allow(Vagrant::Util::Subprocess).to receive(:execute).and_call_original
      allow(Vagrant::Util::Subprocess).to receive(:execute).with("sudo", "/bin/true").and_return(double(:result, exit_code: 0))
      allow(Vagrant::Util::Subprocess).to receive(:execute).with("/bin/true").and_return(double(:result, exit_code: 0))
    end

    it "should export new entries" do
      cap.nfs_export(env, ui, SecureRandom.uuid, ["127.0.0.1", "127.0.0.1"], "tmp" => {:hostpath => "/tmp"})
      exports_content = File.read(exports_path)
      expect(exports_content.scan(/\/tmp.*127\.0\.0\.1/).length).to be(1)
    end

    it "should not remove existing entries" do
      File.write(exports_path, "/custom/directory hostname1(rw,sync,no_subtree_check)")
      cap.nfs_export(env, ui, SecureRandom.uuid, ["127.0.0.1", "127.0.0.1"], "tmp" => {:hostpath => "/tmp"})
      exports_content = File.read(exports_path)
      expect(exports_content.scan(/\/tmp.*127\.0\.0\.1/).length).to be(1)
      expect(exports_content).to match(/\/custom\/directory.*hostname1/)
    end

    it "should remove entries no longer valid" do
      valid_id = SecureRandom.uuid
      other_id = SecureRandom.uuid
      content =<<-EOH
# VAGRANT-BEGIN: #{Process.uid} #{other_id}
"/tmp" 127.0.0.1(rw,no_subtree_check,all_squash,anonuid=,anongid=,fsid=)
# VAGRANT-END: #{Process.uid} #{other_id}
# VAGRANT-BEGIN: #{Process.uid} #{valid_id}
"/var" 127.0.0.1(rw,no_subtree_check,all_squash,anonuid=,anongid=,fsid=)
# VAGRANT-END: #{Process.uid} #{valid_id}
EOH
      File.write(exports_path, content)
      cap.nfs_export(env, ui, valid_id, ["127.0.0.1"], "home" => {:hostpath => "/home"})
      exports_content = File.read(exports_path)
      expect(exports_content).to include("/home")
      expect(exports_content).to include("/tmp")
      expect(exports_content).not_to include("/var")
    end

    it "throws an exception with at least 2 different nfs options" do
      folders = {"/vagrant"=>
                 {:hostpath=>"/home/vagrant",
                  :linux__nfs_options=>["rw","all_squash"]},
                 "/var/www/project"=>
                 {:hostpath=>"/home/vagrant",
                  :linux__nfs_options=>["rw","sync"]}}

      expect { cap.nfs_export(env, ui, SecureRandom.uuid, ["127.0.0.1"], folders) }.
        to raise_error Vagrant::Errors::NFSDupePerms
    end

    it "writes only 1 hostpath for multiple exports" do
      folders = {"/vagrant"=>
                 {:hostpath=>"/home/vagrant",
                  :linux__nfs_options=>["rw","all_squash"]},
                 "/var/www/otherproject"=>
                 {:hostpath=>"/newhome/otherproject",
                  :linux__nfs_options=>["rw","all_squash"]},
                 "/var/www/project"=>
                 {:hostpath=>"/home/vagrant",
                  :linux__nfs_options=>["rw","all_squash"]}}
      valid_id = SecureRandom.uuid
      content =<<-EOH
# VAGRANT-BEGIN: #{Process.uid} #{valid_id}
"/home/vagrant" 127.0.0.1(rw,all_squash,anonuid=,anongid=,fsid=)
"/newhome/otherproject" 127.0.0.1(rw,all_squash,anonuid=,anongid=,fsid=)
# VAGRANT-END: #{Process.uid} #{valid_id}
EOH

      cap.nfs_export(env, ui, valid_id, ["127.0.0.1"], folders)
      exports_content = File.read(exports_path)
      expect(exports_content).to eq(content)
    end

  end

  describe ".nfs_prune" do

    let(:cap){ caps.get(:nfs_prune) }

    before do
      allow(ui).to receive(:info)
      allow(Vagrant::Util::Subprocess).to receive(:execute).with("mv", any_args).
        and_call_original
    end

    it "should remove entries no longer valid" do
      invalid_id = SecureRandom.uuid
      valid_id = SecureRandom.uuid
      content =<<-EOH
# VAGRANT-BEGIN: #{Process.uid} #{invalid_id}
"/tmp" 127.0.0.1(rw,no_subtree_check,all_squash,anonuid=,anongid=,fsid=)
# VAGRANT-END: #{Process.uid} #{invalid_id}
# VAGRANT-BEGIN: #{Process.uid} #{valid_id}
"/var" 127.0.0.1(rw,no_subtree_check,all_squash,anonuid=,anongid=,fsid=)
# VAGRANT-END: #{Process.uid} #{valid_id}
EOH
      File.write(exports_path, content)
      cap.nfs_prune(env, ui, [valid_id])
      exports_content = File.read(exports_path)
      expect(exports_content).to include(valid_id)
      expect(exports_content).not_to include(invalid_id)
      expect(exports_content).to include("/var")
      expect(exports_content).not_to include("/tmp")
    end
  end

  describe ".nfs_write_exports" do

    before do
      File.write(tmp_exports_path, "original content")
      allow(Vagrant::Util::Subprocess).to receive(:execute).with("mv", any_args).
        and_call_original
    end

    it "should write updated contents to file" do
      described_class.nfs_write_exports("new content")
      exports_content = File.read(exports_path)
      expect(exports_content).to include("new content")
      expect(exports_content).not_to include("original content")
    end

    it "should only update contents if different" do
      original_stat = File.stat(exports_path)
      described_class.nfs_write_exports("original content")
      updated_stat = File.stat(exports_path)
      expect(original_stat).to eq(updated_stat)
    end

    it "should retain existing file permissions" do
      File.chmod(0600, exports_path)
      original_stat = File.stat(exports_path)
      described_class.nfs_write_exports("original content")
      updated_stat = File.stat(exports_path)
      expect(original_stat.mode).to eq(updated_stat.mode)
    end

    it "should raise exception when failing to move new exports file" do
      expect(Vagrant::Util::Subprocess).to receive(:execute).and_return(
        Vagrant::Util::Subprocess::Result.new(1, "Failed to move file", "")
      )
      expect{ described_class.nfs_write_exports("new content") }.to raise_error(Vagrant::Errors::NFSExportsFailed)
    end

    context "exports file modification" do
      let(:tmp_stat) { double("tmp_stat", uid: 100, gid: 100, mode: tmp_mode) }
      let(:tmp_mode) { 0 }
      let(:exports_stat) { double("stat", uid: exports_uid, gid: exports_gid, mode: exports_mode) }
      let(:exports_uid) { -1 }
      let(:exports_gid) { -1 }
      let(:exports_mode) { 0 }
      let(:new_exports_file) { double("new_exports_file", path: "/dev/null/exports") }

      before do
        allow(File).to receive(:stat).with(new_exports_file.path).and_return(tmp_stat)
        allow(File).to receive(:stat).with(tmp_exports_path.to_s).and_return(exports_stat)
        allow(new_exports_file).to receive(:puts)
        allow(new_exports_file).to receive(:close)
        allow(Vagrant::Util::Subprocess).to receive(:execute).and_return(Vagrant::Util::Subprocess::Result.new(0, "", ""))
        allow(Tempfile).to receive(:create).with("vagrant").and_return(new_exports_file)
      end

      it "should retain existing file owner and group IDs" do
        expect(Vagrant::Util::Subprocess).to receive(:execute) { |*args|
          expect(args).to include("sudo")
          expect(args).to include("chown")
        }.and_return(Vagrant::Util::Subprocess::Result.new(0, "", ""))
        described_class.nfs_write_exports("new content")
      end

      it "should raise custom exception when chown fails" do
        expect(Vagrant::Util::Subprocess).to receive(:execute) { |*args|
          expect(args).to include("sudo")
          expect(args).to include("chown")
        }.and_return(Vagrant::Util::Subprocess::Result.new(1, "", ""))
        expect { described_class.nfs_write_exports("new content") }.to raise_error(Vagrant::Errors::NFSExportsFailed)
      end

      context "when user has write access to exports file" do
        let(:file_writable?) { true }
        let(:dir_writable?) { false }
        let(:exports_pathname) { double("exports_pathname", writable?: file_writable?, dirname: exports_dir_pathname) }
        let(:exports_dir_pathname) { double("exports_dir_pathname", writable?: dir_writable?) }

        before do
          allow(File).to receive(:stat).and_return(exports_stat)
          allow(File).to receive(:exist?).and_return(false)
          allow(Pathname).to receive(:new).with(tmp_exports_path.to_s).and_return(exports_pathname)
        end

        it "should use sudo when moving new file" do
          expect(Vagrant::Util::Subprocess).to receive(:execute) { |*args|
            expect(args).to include("sudo")
            expect(args).to include("mv")
          }.and_return(Vagrant::Util::Subprocess::Result.new(0, "", ""))
          described_class.nfs_write_exports("new content")
        end

        context "and write access to exports parent directory" do
          let(:dir_writable?) { true }

          it "should not use sudo when moving new file" do
            expect(Vagrant::Util::Subprocess).to receive(:execute) { |*args|
              expect(args).not_to include("sudo")
              expect(args).to include("mv")
            }.and_return(Vagrant::Util::Subprocess::Result.new(0, "", ""))
            described_class.nfs_write_exports("new content")
          end
        end
      end
    end
  end

  describe ".modinfo_path" do
    let(:cap){ VagrantPlugins::HostLinux::Cap::NFS }

    context "with modinfo on PATH" do
      before do
        expect(Vagrant::Util::Which).to receive(:which).with("modinfo").and_return("/usr/bin/modinfo")
      end

      it "should use full path to modinfo" do
        expect(cap.modinfo_path).to eq("/usr/bin/modinfo")
      end
    end

    context "with modinfo at /sbin/modinfo" do
      before do
        expect(Vagrant::Util::Which).to receive(:which).with("modinfo").and_return(nil)
        expect(File).to receive(:file?).with("/sbin/modinfo").and_return(true)
      end

      it "should use /sbin/modinfo" do
        expect(cap.modinfo_path).to eq("/sbin/modinfo")
      end
    end

    context "modinfo not found" do
      before do
        expect(Vagrant::Util::Which).to receive(:which).with("modinfo").and_return(nil)
        expect(File).to receive(:file?).with("/sbin/modinfo").and_return(false)
      end

      it "should use modinfo" do
        expect(cap.modinfo_path).to eq("modinfo")
      end
    end

    context "with cached value for modinfo_path" do
      before do
        cap.instance_variable_set(:@_modinfo_path, "/usr/local/bin/modinfo")
      end

      it "should use cached value" do
        expect(cap.modinfo_path).to eq("/usr/local/bin/modinfo")
      end
    end
  end
end