summaryrefslogtreecommitdiff
path: root/spec/unit/resource/timezone_spec.rb
blob: 8ba033f072458b4bac1e81341a17251913633085 (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
#
# Copyright:: Copyright (c) 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::Timezone do
  let(:resource) { Chef::Resource::Timezone.new("fakey_fakerton") }

  let(:shellout_tzutil) do
    double("shell_out!", stdout: "UTC\n", exitstatus: 0, error?: false)
  end

  # note: This weird indention is correct
  let(:shellout_timedatectl) do
    double("shell_out!", exitstatus: 0, error?: false, stdout: <<-OUTPUT)
    Local time: Tue 2020-08-18 20:55:05 UTC
    Universal time: Tue 2020-08-18 20:55:05 UTC
          RTC time: Tue 2020-08-18 20:55:05
         Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
systemd-timesyncd.service active: yes
   RTC in local TZ: no
    OUTPUT
  end

  let(:shellout_systemsetup_fail) do
    double("shell_out!", stdout: "You need administrator access to run this tool... exiting!", exitstatus: 0, error?: false) # yes it's a non-error exit
  end

  let(:shellout_systemsetup) do
    double("shell_out!", stdout: "Time Zone: UTC", exitstatus: 0, error?: false)
  end

  it "sets resource name as :timezone" do
    expect(resource.resource_name).to eql(:timezone)
  end

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

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

  it "supports the :set action only" do
    expect { resource.action :set }.not_to raise_error
    expect { resource.action :unset }.to raise_error(Chef::Exceptions::ValidationFailed)
  end

  describe "#current_darwin_tz?" do
    context "with admin privs" do
      it "returns the TZ" do
        expect(resource).to receive(:shell_out!).and_return(shellout_systemsetup)
        expect(resource.current_darwin_tz).to eql("UTC")
      end
    end

    context "without admin privs" do
      it "returns the TZ" do
        expect(resource).to receive(:shell_out!).and_return(shellout_systemsetup_fail)
        expect { resource.current_darwin_tz }.to raise_error(RuntimeError)
      end
    end
  end

  describe "#current_systemd_tz?" do
    it "returns the TZ" do
      expect(resource).to receive(:shell_out!).and_return(shellout_timedatectl)
      expect(resource.current_systemd_tz).to eql("Etc/UTC")
    end
  end

  describe "#current_windows_tz?" do
    it "returns the TZ" do
      expect(resource).to receive(:shell_out!).and_return(shellout_tzutil)
      expect(resource.current_windows_tz).to eql("UTC")
    end
  end

  describe "#current_rhel_tz?" do
    it "returns the TZ" do
      allow(File).to receive(:exist?).with("/etc/sysconfig/clock").and_return true
      expect(File).to receive(:read).with("/etc/sysconfig/clock").and_return 'ZONE="UTC"'
      expect(resource.current_rhel_tz).to eql("UTC")
    end
  end
end