File: base_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 (167 lines) | stat: -rw-r--r-- 6,068 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
require 'spec_helper'

require 'puppet/file_serving/base'

describe Puppet::FileServing::Base do
  let(:path) { File.expand_path('/module/dir/file') }
  let(:file) { File.expand_path('/my/file') }

  it "should accept a path" do
    expect(Puppet::FileServing::Base.new(path).path).to eq(path)
  end

  it "should require that paths be fully qualified" do
    expect { Puppet::FileServing::Base.new("module/dir/file") }.to raise_error(ArgumentError)
  end

  it "should allow specification of whether links should be managed" do
    expect(Puppet::FileServing::Base.new(path, :links => :manage).links).to eq(:manage)
  end

  it "should have a :source attribute" do
    file = Puppet::FileServing::Base.new(path)
    expect(file).to respond_to(:source)
    expect(file).to respond_to(:source=)
  end

  it "should consider :ignore links equivalent to :manage links" do
    expect(Puppet::FileServing::Base.new(path, :links => :ignore).links).to eq(:manage)
  end

  it "should fail if :links is set to anything other than :manage, :follow, or :ignore" do
    expect { Puppet::FileServing::Base.new(path, :links => :else) }.to raise_error(ArgumentError)
  end

  it "should allow links values to be set as strings" do
    expect(Puppet::FileServing::Base.new(path, :links => "follow").links).to eq(:follow)
  end

  it "should default to :manage for :links" do
    expect(Puppet::FileServing::Base.new(path).links).to eq(:manage)
  end

  it "should allow specification of a path" do
    allow(Puppet::FileSystem).to receive(:exist?).and_return(true)
    expect(Puppet::FileServing::Base.new(path, :path => file).path).to eq(file)
  end

  it "should allow specification of a relative path" do
    allow(Puppet::FileSystem).to receive(:exist?).and_return(true)
    expect(Puppet::FileServing::Base.new(path, :relative_path => "my/file").relative_path).to eq("my/file")
  end

  it "should have a means of determining if the file exists" do
    expect(Puppet::FileServing::Base.new(file)).to respond_to(:exist?)
  end

  it "should correctly indicate if the file is present" do
    expect(Puppet::FileSystem).to receive(:lstat).with(file).and_return(double('stat'))
    expect(Puppet::FileServing::Base.new(file).exist?).to be_truthy
  end

  it "should correctly indicate if the file is absent" do
    expect(Puppet::FileSystem).to receive(:lstat).with(file).and_raise(RuntimeError)
    expect(Puppet::FileServing::Base.new(file).exist?).to be_falsey
  end

  describe "when setting the relative path" do
    it "should require that the relative path be unqualified" do
      @file = Puppet::FileServing::Base.new(path)
      allow(Puppet::FileSystem).to receive(:exist?).and_return(true)
      expect { @file.relative_path = File.expand_path("/qualified/file") }.to raise_error(ArgumentError)
    end
  end

  describe "when determining the full file path" do
    let(:path) { File.expand_path('/this/file') }
    let(:file) { Puppet::FileServing::Base.new(path) }

    it "should return the path if there is no relative path" do
      expect(file.full_path).to eq(path)
    end

    it "should return the path if the relative_path is set to ''" do
      file.relative_path = ""
      expect(file.full_path).to eq(path)
    end

    it "should return the path if the relative_path is set to '.'" do
      file.relative_path = "."
      expect(file.full_path).to eq(path)
    end

    it "should return the path joined with the relative path if there is a relative path and it is not set to '/' or ''" do
      file.relative_path = "not/qualified"
      expect(file.full_path).to eq(File.join(path, "not/qualified"))
    end

    it "should strip extra slashes" do
      file = Puppet::FileServing::Base.new(File.join(File.expand_path('/'), "//this//file"))
      expect(file.full_path).to eq(path)
    end
  end

  describe "when handling a UNC file path on Windows" do
    let(:path) { '//server/share/filename' }
    let(:file) { Puppet::FileServing::Base.new(path) }

    it "should preserve double slashes at the beginning of the path" do
      allow(Puppet.features).to receive(:microsoft_windows?).and_return(true)
      expect(file.full_path).to eq(path)
    end

    it "should strip double slashes not at the beginning of the path" do
      allow(Puppet.features).to receive(:microsoft_windows?).and_return(true)
      file = Puppet::FileServing::Base.new('//server//share//filename')
      expect(file.full_path).to eq(path)
    end
  end


  describe "when stat'ing files" do
    let(:path) { File.expand_path('/this/file') }
    let(:file) { Puppet::FileServing::Base.new(path) }
    let(:stat) { double('stat', :ftype => 'file' ) }
    let(:stubbed_file) { double(path, :stat => stat, :lstat => stat)}

    it "should stat the file's full path" do
      expect(Puppet::FileSystem).to receive(:lstat).with(path).and_return(stat)
      file.stat
    end

    it "should fail if the file does not exist" do
      expect(Puppet::FileSystem).to receive(:lstat).with(path).and_raise(Errno::ENOENT)
      expect { file.stat }.to raise_error(Errno::ENOENT)
    end

    it "should use :lstat if :links is set to :manage" do
      expect(Puppet::FileSystem).to receive(:lstat).with(path).and_return(stubbed_file)
      file.stat
    end

    it "should use :stat if :links is set to :follow" do
      expect(Puppet::FileSystem).to receive(:stat).with(path).and_return(stubbed_file)
      file.links = :follow
      file.stat
    end
  end

  describe "#absolute?" do
    it "should be accept POSIX paths" do
      expect(Puppet::FileServing::Base).to be_absolute('/')
    end

    it "should accept Windows paths on Windows" do
      allow(Puppet.features).to receive(:microsoft_windows?).and_return(true)
      allow(Puppet.features).to receive(:posix?).and_return(false)

      expect(Puppet::FileServing::Base).to be_absolute('c:/foo')
    end

    it "should reject Windows paths on POSIX" do
      allow(Puppet.features).to receive(:microsoft_windows?).and_return(false)

      expect(Puppet::FileServing::Base).not_to be_absolute('c:/foo')
    end
  end
end