summaryrefslogtreecommitdiff
path: root/spec/unit/plugins/windows/filesystem_spec.rb
blob: e1f33b70efa454a3098a51a0935f0c4ad7fecfaf (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#
#  Author:: Nimesh Pathi <nimesh.patni@msystechnologies.com>
#  Copyright:: Copyright (c) 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"
require "wmi-lite/wmi"

describe Ohai::System, "Windows Filesystem Plugin", :windows_only do
  let(:plugin) { get_plugin("windows/filesystem") }

  let(:success) { true }

  let(:logical_disks_instances) do
    [
      {
        "caption" => "C:",
        "deviceid" => "C:",
        "size" => "10000000",
        "filesystem" => "NTFS",
        "freespace" => "100000",
        "name" => "C:",
        "volumename " => "",
      },
      {
        "caption" => "D:",
        "deviceid" => "D:",
        "size" => "10000000",
        "filesystem" => "FAT32",
        "freespace" => "100000",
        "name" => "D:",
        # Lets not pass "volumename" for this drive
      },
    ]
  end

  let(:encryptable_volume_instances) do
    [
      {
        "conversionstatus" => 0,
        "driveletter" => "C:",
      },
      {
        "conversionstatus" => 2,
        "driveletter" => "D:",
      },
    ]
  end

  let(:wmi_exception) do
    namespace = "Exception while testing"
    exception = WIN32OLERuntimeError.new(namespace)
    WmiLite::WmiException.new(exception, :ConnectServer, @namespace)
  end

  before(:each) do
    allow(plugin).to receive(:collect_os).and_return(:windows)
  end

  describe "#logical_properties" do
    let(:disks) { logical_disks_instances }
    let(:logical_props) { %i{kb_size kb_available kb_used percent_used mount fs_type volume_name} }

    it "Returns a mash" do
      expect(plugin.logical_properties(disks)).to be_a(Mash)
    end

    it "Returns an empty mash when blank array is passed" do
      expect(plugin.logical_properties([])).to be_a(Mash)
      expect(plugin.logical_properties([])).to be_empty
    end

    it "Returns properties without values when there is no disk information" do
      data = plugin.logical_properties([{}])
      expect(data[nil].symbolize_keys.keys).to eq(logical_props)
      expect(data[nil]["kb_used"]).to eq(0)
      expect(data[nil]["fs_type"]).to be_empty
    end

    it "Refines required logical properties out of given instance" do
      data = plugin.logical_properties(disks)
      expect(data["C:"].symbolize_keys.keys).to eq(logical_props)
      expect(data["D:"].symbolize_keys.keys).to eq(logical_props)
    end

    it "Calculates logical properties out of given instance" do
      data = plugin.logical_properties(disks)
      expect(data["C:"]["kb_used"]).to eq(data["D:"]["kb_used"]).and eq(9900)
      expect(data["C:"]["percent_used"]).to eq(data["D:"]["percent_used"]).and eq(99)
      expect(data["C:"]["fs_type"]).to eq("ntfs")
      expect(data["D:"]["fs_type"]).to eq("fat32")
    end
  end

  describe "#logical_info" do
    it "Returns an empty mash when wmi namespace does not exists" do
      allow(WmiLite::Wmi).to receive(:new).and_raise(wmi_exception)
      expect(plugin.logical_info).to be_a(Mash)
      expect(plugin.logical_info).to be_empty
    end

    it "Returns an empty mash when Win32_LogicalDisk could not be processed" do
      allow(WmiLite::Wmi).to receive(:new).and_return(success)
      allow(success)
        .to receive(:instances_of)
        .with("Win32_LogicalDisk")
        .and_raise(wmi_exception)
      expect(plugin.logical_info).to be_a(Mash)
      expect(plugin.logical_info).to be_empty
    end

    it "Returns a Mash loaded with logical details" do
      allow(WmiLite::Wmi).to receive(:new).and_return(success)
      allow(success)
        .to receive(:instances_of)
        .with("Win32_LogicalDisk")
        .and_return(logical_disks_instances)
      expect(plugin.logical_info).to be_a(Mash)
      expect(plugin.logical_info).not_to be_empty
    end
  end

  describe "#encryption_properties" do
    let(:disks) { encryptable_volume_instances }
    let(:encryption_props) { [:encryption_status] }

    it "Returns a mash" do
      expect(plugin.encryption_properties(disks)).to be_a(Mash)
    end

    it "Returns an empty mash when blank array is passed" do
      expect(plugin.encryption_properties([])).to be_a(Mash)
      expect(plugin.encryption_properties([])).to be_empty
    end

    it "Returns properties without values when there is no disk information" do
      data = plugin.encryption_properties([{}])
      expect(data[nil].symbolize_keys.keys).to eq(encryption_props)
      expect(data[nil]["encryption_status"]).to be_empty
    end

    it "Refines required encryption properties out of given instance" do
      data = plugin.encryption_properties(disks)
      expect(data["C:"].symbolize_keys.keys).to eq(encryption_props)
      expect(data["D:"].symbolize_keys.keys).to eq(encryption_props)
    end

    it "Calculates encryption properties out of given instance" do
      data = plugin.encryption_properties(disks)
      expect(data["C:"]["encryption_status"]).to eq("FullyDecrypted")
      expect(data["D:"]["encryption_status"]).to eq("EncryptionInProgress")
    end
  end

  describe "#encryptable_info" do
    it "Returns an empty mash when wmi namespace does not exists" do
      allow(WmiLite::Wmi).to receive(:new).and_raise(wmi_exception)
      expect(plugin.encryptable_info).to be_a(Mash)
      expect(plugin.encryptable_info).to be_empty
    end

    it "Returns an empty mash when Win32_EncryptableVolume could not be processed" do
      allow(WmiLite::Wmi).to receive(:new).and_return(success)
      allow(success)
        .to receive(:instances_of)
        .with("Win32_EncryptableVolume")
        .and_raise(wmi_exception)
      expect(plugin.encryptable_info).to be_a(Mash)
      expect(plugin.encryptable_info).to be_empty
    end

    it "Returns a Mash loaded with encryption details" do
      allow(WmiLite::Wmi).to receive(:new).and_return(success)
      allow(success)
        .to receive(:instances_of)
        .with("Win32_EncryptableVolume")
        .and_return(encryptable_volume_instances)
      expect(plugin.encryptable_info).to be_a(Mash)
      expect(plugin.encryptable_info).not_to be_empty
    end
  end

  describe "#merge_info" do
    let(:info1) do
      { "drive1" => { "x" => 10, "y" => "test1" },
        "drive2" => { "x" => 20, "z" => "test2" } }
    end
    let(:info2) do
      { "drive1" => { "k" => 10, "l" => "test1" },
        "drive2" => { "l" => 20, "m" => "test2" } }
    end
    let(:info3) { { "drive1" => { "o" => 10, "p" => "test1" } } }
    let(:info4) { { "drive2" => { "q" => 10, "r" => "test1" } } }

    it "Returns an empty mash when no info is passed" do
      expect(plugin.merge_info([])).to be_a(Mash)
      expect(plugin.merge_info([])).to be_empty
    end

    it "Merges all the various properties of filesystems" do
      expect(plugin.merge_info([info1, info2, info3, info4]))
        .to eq("drive1" => { "x" => 10, "y" => "test1", "k" => 10, "l" => "test1", "o" => 10, "p" => "test1" },
               "drive2" => { "x" => 20, "z" => "test2", "l" => 20, "m" => "test2", "q" => 10, "r" => "test1" })
    end

    it "Does not affect any core information after processing" do
      expect(plugin.merge_info([info3, info4])).to eq("drive1" => { "o" => 10, "p" => "test1" },
                                                      "drive2" => { "q" => 10, "r" => "test1" })
      expect(info3).to eq("drive1" => { "o" => 10, "p" => "test1" })
      expect(info4).to eq("drive2" => { "q" => 10, "r" => "test1" })
    end
  end
end