summaryrefslogtreecommitdiff
path: root/spec/unit/plugins/windows/dmi_spec.rb
blob: 451850d9c5af0b3aa9b12d891328bfd247947a28 (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
#
# Author:: Pete Higgins (pete@peterhiggins.org)
# 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 Ohai::System, "DMI", :windows_only do
  let(:plugin) { get_plugin("windows/dmi") }

  before do
    require "wmi-lite/wmi"

    empty_wmi_object = WmiLite::Wmi::Instance.new(double(properties_: []))
    %w{Processor Bios ComputerSystemProduct BaseBoard}.each do |type|
      expect_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_#{type}").and_return([empty_wmi_object])
    end
  end

  context "when property names are different types of camel casing" do
    # Each test case has 3 elements:
    # * The name of the property as it comes from the Windows APIs
    # * The transformed snake-case version of the property name
    # * A unique dummy value per test case
    CASES = [
      %w{Depth depth aaa},
      %w{PartNumber part_number bbb},
      %w{NumberOfPowerCords number_of_power_cords ccc},
      %w{SKU sku ddd},
      %w{SMBIOSAssetTag smbios_asset_tag eee},
      %w{DeviceID device_id fff},
      %w{L2CacheSize l2_cache_size ggg},
    ].freeze

    before do
      properties = CASES.map { |name, _, _| double(name: name) }
      wmi_ole_object = double properties_: properties

      CASES.each do |name, _, value|
        allow(wmi_ole_object).to receive(:invoke).with(name).and_return(value)
      end

      wmi_object = WmiLite::Wmi::Instance.new(wmi_ole_object)
      expect_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_SystemEnclosure").and_return([wmi_object])

      plugin.run
    end

    CASES.each do |name, transformed_name, value|
      it "adds #{name} to :all_records" do
        expect(plugin[:dmi][:chassis][:all_records].first[name]).to eq(value)
      end

      it "adds #{transformed_name} to the root" do
        expect(plugin[:dmi][:chassis][transformed_name]).to eq(value)
      end
    end
  end

  context "when multiple objects of one type are returned from the Windows API" do
    before do
      properties = [
        double(name: "UniqueProperty"),
        double(name: "SharedProperty"),
      ]

      wmi_ole_objects = %w{tacos nachos}.map do |value|
        object = double properties_: properties
        allow(object).to receive(:invoke).with("UniqueProperty").and_return(value)
        allow(object).to receive(:invoke).with("SharedProperty").and_return("Taco Bell")
        object
      end

      wmi_objects = wmi_ole_objects.map { |o| WmiLite::Wmi::Instance.new(o) }
      expect_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_SystemEnclosure").and_return(wmi_objects)

      plugin.run
    end

    it "adds unique values to :all_records" do
      values = plugin[:dmi][:chassis][:all_records].map { |r| r["UniqueProperty"] }
      expect(values).to eq(%w{tacos nachos})
    end

    it "adds shared values to the root with snake case key" do
      expect(plugin[:dmi][:chassis]["shared_property"]).to eq("Taco Bell")
    end
  end

  context "with extra information that should be filtered out" do
    FILTERED_KEYS = [
      %w{Caption caption aaa},
      %w{CreationClassName creation_class_name bbb},
      %w{SystemCreationClassName system_creation_class_name ccc},
    ].freeze

    before do
      properties = FILTERED_KEYS.map { |name, _, _| double(name: name) }
      wmi_ole_object = double properties_: properties

      FILTERED_KEYS.each do |name, _, value|
        allow(wmi_ole_object).to receive(:invoke).with(name).and_return(value)
      end

      wmi_object = WmiLite::Wmi::Instance.new(wmi_ole_object)
      expect_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_SystemEnclosure").and_return([wmi_object])

      plugin.run
    end

    FILTERED_KEYS.each do |name, transformed_name, value|
      it "adds #{name} to :all_records" do
        expect(plugin[:dmi][:chassis][:all_records].first[name]).to eq(value)
      end

      it "does not add #{transformed_name} to the root" do
        expect(plugin[:dmi][:chassis]).not_to have_key(transformed_name)
      end
    end
  end

  context "with information that should be made to match other platforms" do
    RENAMED_KEYS = [
      %w{Vendor vendor manufacturer aaa},
      %w{IdentifyingNumber identifying_number serial_number bbb},
      %w{Name name family ccc},
    ].freeze

    before do
      properties = RENAMED_KEYS.map { |name, _, _, _| double(name: name) }
      wmi_ole_object = double properties_: properties

      RENAMED_KEYS.each do |name, _, _, value|
        allow(wmi_ole_object).to receive(:invoke).with(name).and_return(value)
      end

      wmi_object = WmiLite::Wmi::Instance.new(wmi_ole_object)
      expect_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with("Win32_SystemEnclosure").and_return([wmi_object])

      plugin.run
    end

    RENAMED_KEYS.each do |name, transformed_name, renamed_name, value|
      it "adds #{name} to :all_records" do
        expect(plugin[:dmi][:chassis][:all_records].first[name]).to eq(value)
      end

      it "adds #{renamed_name} to the root" do
        expect(plugin[:dmi][:chassis][renamed_name]).to eq(value)
      end

      it "does not add #{transformed_name} to the root" do
        expect(plugin[:dmi][:chassis]).not_to have_key(transformed_name)
      end
    end
  end
end