summaryrefslogtreecommitdiff
path: root/spec/unit/provider/file/windows_spec.rb
blob: f6d7ef0e7528e91bfe1ebe51355ea31778061a0c (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
#! /usr/bin/env ruby

require 'spec_helper'
if Puppet.features.microsoft_windows?
  require 'puppet/util/windows'
  class WindowsSecurity
    extend Puppet::Util::Windows::Security
  end
end

describe Puppet::Type.type(:file).provider(:windows), :if => Puppet.features.microsoft_windows? do
  include PuppetSpec::Files

  let(:path) { tmpfile('windows_file_spec') }
  let(:resource) { Puppet::Type.type(:file).new :path => path, :mode => 0777, :provider => described_class.name }
  let(:provider) { resource.provider }
  let(:sid)      { 'S-1-1-50' }
  let(:account)  { 'quinn' }

  describe "#mode" do
    it "should return a string with the higher-order bits stripped away" do
      FileUtils.touch(path)
      WindowsSecurity.set_mode(0644, path)

      provider.mode.should == '644'
    end

    it "should return absent if the file doesn't exist" do
      provider.mode.should == :absent
    end
  end

  describe "#mode=" do
    it "should chmod the file to the specified value" do
      FileUtils.touch(path)
      WindowsSecurity.set_mode(0644, path)

      provider.mode = '0755'

      provider.mode.should == '755'
    end

    it "should pass along any errors encountered" do
      expect do
        provider.mode = '644'
      end.to raise_error(Puppet::Error, /failed to set mode/)
    end
  end

  describe "#id2name" do
    it "should return the name of the user identified by the sid" do
      Puppet::Util::Windows::SID.expects(:valid_sid?).with(sid).returns(true)
      Puppet::Util::Windows::SID.expects(:sid_to_name).with(sid).returns(account)

      provider.id2name(sid).should == account
    end

    it "should return the argument if it's already a name" do
      Puppet::Util::Windows::SID.expects(:valid_sid?).with(account).returns(false)
      Puppet::Util::Windows::SID.expects(:sid_to_name).never

      provider.id2name(account).should == account
    end

    it "should return nil if the user doesn't exist" do
      Puppet::Util::Windows::SID.expects(:valid_sid?).with(sid).returns(true)
      Puppet::Util::Windows::SID.expects(:sid_to_name).with(sid).returns(nil)

      provider.id2name(sid).should == nil
    end
  end

  describe "#name2id" do
    it "should delegate to name_to_sid" do
      Puppet::Util::Windows::SID.expects(:name_to_sid).with(account).returns(sid)

      provider.name2id(account).should == sid
    end
  end

  describe "#owner" do
    it "should return the sid of the owner if the file does exist" do
      FileUtils.touch(resource[:path])
      provider.stubs(:get_owner).with(resource[:path]).returns(sid)

      provider.owner.should == sid
    end

    it "should return absent if the file doesn't exist" do
      provider.owner.should == :absent
    end
  end

  describe "#owner=" do
    it "should set the owner to the specified value" do
      provider.expects(:set_owner).with(sid, resource[:path])
      provider.owner = sid
    end

    it "should propagate any errors encountered when setting the owner" do
      provider.stubs(:set_owner).raises(ArgumentError)

      expect {
        provider.owner = sid
      }.to raise_error(Puppet::Error, /Failed to set owner/)
    end
  end

  describe "#group" do
    it "should return the sid of the group if the file does exist" do
      FileUtils.touch(resource[:path])
      provider.stubs(:get_group).with(resource[:path]).returns(sid)

      provider.group.should == sid
    end

    it "should return absent if the file doesn't exist" do
      provider.group.should == :absent
    end
  end

  describe "#group=" do
    it "should set the group to the specified value" do
      provider.expects(:set_group).with(sid, resource[:path])
      provider.group = sid
    end

    it "should propagate any errors encountered when setting the group" do
      provider.stubs(:set_group).raises(ArgumentError)

      expect {
        provider.group = sid
      }.to raise_error(Puppet::Error, /Failed to set group/)
    end
  end

  describe "when validating" do
    {:owner => 'foo', :group => 'foo', :mode => 0777}.each do |k,v|
      it "should fail if the filesystem doesn't support ACLs and we're managing #{k}" do
        described_class.any_instance.stubs(:supports_acl?).returns false

        expect {
          Puppet::Type.type(:file).new :path => path, k => v
        }.to raise_error(Puppet::Error, /Can only manage owner, group, and mode on filesystems that support Windows ACLs, such as NTFS/)
      end
    end

    it "should not fail if the filesystem doesn't support ACLs and we're not managing permissions" do
      described_class.any_instance.stubs(:supports_acl?).returns false

      Puppet::Type.type(:file).new :path => path
    end
  end
end