summaryrefslogtreecommitdiff
path: root/spec/unit/resource/systemd_unit_spec.rb
blob: 7e46872525e1a07028250dce214379772eec703a (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
#
# Author:: Nathan Williams (<nath.e.will@gmail.com>)
# Copyright:: Copyright 2016, Nathan Williams
# 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::SystemdUnit do
  before(:each) do
    @resource = Chef::Resource::SystemdUnit.new("sysstat-collect.timer")
  end

  let(:unit_content_string) { "[Unit]\nDescription = Run system activity accounting tool every 10 minutes\n\n[Timer]\nOnCalendar = *:00/10\n\n[Install]\nWantedBy = sysstat.service\n" }

  let(:unit_content_hash) do
    {
      "Unit" => {
        "Description" => "Run system activity accounting tool every 10 minutes",
      },
      "Timer" => {
        "OnCalendar" => "*:00/10",
      },
      "Install" => {
        "WantedBy" => "sysstat.service",
      },
    }
  end

  it "creates a new Chef::Resource::SystemdUnit" do
    expect(@resource).to be_a_kind_of(Chef::Resource)
    expect(@resource).to be_a_kind_of(Chef::Resource::SystemdUnit)
  end

  it "should have a name" do
    expect(@resource.name).to eql("sysstat-collect.timer")
  end

  it "has a default action of nothing" do
    expect(@resource.action).to eql([:nothing])
  end

  it "supports appropriate unit actions" do
    expect { @resource.action :create }.not_to raise_error
    expect { @resource.action :delete }.not_to raise_error
    expect { @resource.action :enable }.not_to raise_error
    expect { @resource.action :disable }.not_to raise_error
    expect { @resource.action :mask }.not_to raise_error
    expect { @resource.action :unmask }.not_to raise_error
    expect { @resource.action :start }.not_to raise_error
    expect { @resource.action :stop }.not_to raise_error
    expect { @resource.action :restart }.not_to raise_error
    expect { @resource.action :reload }.not_to raise_error
    expect { @resource.action :wrong }.to raise_error(ArgumentError)
  end

  it "accepts boolean state properties" do
    expect { @resource.active false }.not_to raise_error
    expect { @resource.active true }.not_to raise_error
    expect { @resource.active "yes" }.to raise_error(ArgumentError)

    expect { @resource.enabled true }.not_to raise_error
    expect { @resource.enabled false }.not_to raise_error
    expect { @resource.enabled "no" }.to raise_error(ArgumentError)

    expect { @resource.masked true }.not_to raise_error
    expect { @resource.masked false }.not_to raise_error
    expect { @resource.masked :nope }.to raise_error(ArgumentError)

    expect { @resource.static true }.not_to raise_error
    expect { @resource.static false }.not_to raise_error
    expect { @resource.static "yep" }.to raise_error(ArgumentError)
  end

  it "accepts the content property" do
    expect { @resource.content nil }.not_to raise_error
    expect { @resource.content "test" }.not_to raise_error
    expect { @resource.content({}) }.not_to raise_error
    expect { @resource.content 5 }.to raise_error(ArgumentError)
  end

  it "accepts the user property" do
    expect { @resource.user nil }.not_to raise_error
    expect { @resource.user "deploy" }.not_to raise_error
    expect { @resource.user 5 }.to raise_error(ArgumentError)
  end

  it "accepts the triggers_reload property" do
    expect { @resource.triggers_reload true }.not_to raise_error
    expect { @resource.triggers_reload false }.not_to raise_error
    expect { @resource.triggers_reload "no" }.to raise_error(ArgumentError)
  end

  it "reports its state" do
    @resource.active true
    @resource.enabled true
    @resource.masked false
    @resource.static false
    @resource.content "test"
    state = @resource.state
    expect(state[:active]).to eq(true)
    expect(state[:enabled]).to eq(true)
    expect(state[:masked]).to eq(false)
    expect(state[:static]).to eq(false)
    expect(state[:content]).to eq("test")
  end

  it "returns the unit name as its identity" do
    expect(@resource.identity).to eq("sysstat-collect.timer")
  end

  it "serializes to ini with a string-formatted content property" do
    @resource.content(unit_content_string)
    expect(@resource.to_ini).to eq unit_content_string
  end

  it "serializes to ini with a hash-formatted content property" do
    @resource.content(unit_content_hash)
    expect(@resource.to_ini).to eq unit_content_string
  end
end