summaryrefslogtreecommitdiff
path: root/spec/unit/resource/sudo_spec.rb
blob: adad4a30f8a7c942e8f21c1635327aede97acc95 (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
#
# Copyright:: Copyright 2018, Chef Software, Inc.
# 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"

describe Chef::Resource::Sudo do

  let(:node) { Chef::Node.new }
  let(:events) { Chef::EventDispatch::Dispatcher.new }
  let(:run_context) { Chef::RunContext.new(node, {}, events) }
  let(:resource) { Chef::Resource::Sudo.new("fakey_fakerton", run_context) }

  it "has a resource name of :sudo" do
    expect(resource.resource_name).to eql(:sudo)
  end

  it "the filename property is the name_property" do
    expect(resource.filename).to eql("fakey_fakerton")
  end

  it "sets the default action as :create" do
    expect(resource.action).to eql([:create])
  end

  it "supports :create, :delete, :install, :remove actions" do
    expect { resource.action :create }.not_to raise_error
    expect { resource.action :delete }.not_to raise_error
    expect { resource.action :install }.not_to raise_error
    expect { resource.action :remove }.not_to raise_error
  end

  it "coerces filename property values . & ~ to __" do
    resource.filename "something.something~"
    expect(resource.filename).to eql("something__something__")
  end

  it "supports the legacy 'user' property" do
    resource.user ["foo"]
    expect(resource.users).to eql(["foo"])
  end

  it "supports the legacy 'groups' property" do
    resource.group ["%foo"]
    expect(resource.groups).to eql(["%foo"])
  end

  it "coerces users & groups String vals to Arrays" do
    resource.users "something"
    resource.groups "%something"
    expect(resource.users).to eql(["something"])
    expect(resource.groups).to eql(["%something"])
  end

  it "coerces users & group String vals no matter the spacing" do
    resource.users "user1, user2 , user3 ,user4"
    resource.groups "group1, group2 , group3 ,group4"
    expect(resource.users).to eql(%w{user1 user2 user3 user4})
    expect(resource.groups).to eql(["%group1", "%group2", "%group3", "%group4"])
  end

  it "coerces groups values to properly start with %" do
    resource.groups ["foo", "%bar"]
    expect(resource.groups).to eql(["%foo", "%bar"])
  end

  it "it sets the config prefix to /etc on linux" do
    node.automatic[:platform_family] = "debian"
    expect(resource.config_prefix).to eql("/etc")
  end

  it "it sets the config prefix to /private/etc on macOS" do
    node.automatic[:platform_family] = "mac_os_x"
    expect(resource.config_prefix).to eql("/private/etc")
  end

  it "it sets the config prefix to /usr/local/etc on FreeBSD" do
    node.automatic[:platform_family] = "freebsd"
    expect(resource.config_prefix).to eql("/usr/local/etc")
  end

  it "it sets the config prefix to /opt/local/etc on smartos" do
    node.automatic[:platform_family] = "smartos"
    expect(resource.config_prefix).to eql("/opt/local/etc")
  end
end