summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-01-29 12:19:27 -0800
committerTim Smith <tsmith84@gmail.com>2020-01-29 12:19:27 -0800
commit47aacfbd847516f1b2fbd30e2cf3af6219cc9d5e (patch)
treeef15c08e05403cb4ddb20254d82d1b7298258778
parent7b9a24982efb8f8c4f02ec70d0219e8ab7ada59a (diff)
downloadchef-47aacfbd847516f1b2fbd30e2cf3af6219cc9d5e.tar.gz
Add windows_nt_version and powershell_version helpers to chef-utilsmore_windows_helpers
These both return version objects which means they can be compared without creating version objects first or .to_i / .to_f messes. Much simpler for users. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--chef-utils/lib/chef-utils/dsl/windows.rb22
-rw-r--r--chef-utils/spec/unit/dsl/windows_spec.rb42
2 files changed, 53 insertions, 11 deletions
diff --git a/chef-utils/lib/chef-utils/dsl/windows.rb b/chef-utils/lib/chef-utils/dsl/windows.rb
index 86d8fb00bc..904e9ef126 100644
--- a/chef-utils/lib/chef-utils/dsl/windows.rb
+++ b/chef-utils/lib/chef-utils/dsl/windows.rb
@@ -20,6 +20,8 @@ require_relative "../internal"
module ChefUtils
module DSL
module Windows
+ require "chef-utils/version_string"
+
include Internal
# Determine if the current node is Windows Server Core.
@@ -52,6 +54,26 @@ module ChefUtils
node["kernel"]["product_type"] == "Server"
end
+ # Determine the current Windows NT version. The NT version often differs from the marketing version, but offers a good way to find desktop and server releases that are based on the same codebase. IE: NT 6.3 is Windows 8.1 and Windows 2012 R2.
+ #
+ # @param [Chef::Node] node
+ #
+ # @return [ChefUtils::VersionString]
+ #
+ def windows_nt_version(node = __getnode)
+ ChefUtils::VersionString.new(node["os_version"])
+ end
+
+ # Determine the installed version of PowerShell
+ #
+ # @param [Chef::Node] node
+ #
+ # @return [ChefUtils::VersionString]
+ #
+ def powershell_version(node = __getnode)
+ ChefUtils::VersionString.new(node["languages"]["powershell"]["version"])
+ end
+
extend self
end
end
diff --git a/chef-utils/spec/unit/dsl/windows_spec.rb b/chef-utils/spec/unit/dsl/windows_spec.rb
index e069c175ee..08ddb9c118 100644
--- a/chef-utils/spec/unit/dsl/windows_spec.rb
+++ b/chef-utils/spec/unit/dsl/windows_spec.rb
@@ -17,13 +17,15 @@
require "spec_helper"
+WINDOWS_BOOL_HELPERS = %i{windows_server_core? windows_server? windows_workstation?}.freeze
+
def windows_reports_true_for(*args)
args.each do |method|
it "reports true for #{method}" do
expect(described_class.send(method, node)).to be true
end
end
- (WINDOWS_HELPERS - args).each do |method|
+ (WINDOWS_BOOL_HELPERS - args).each do |method|
it "reports false for #{method}" do
expect(described_class.send(method, node)).to be false
end
@@ -43,21 +45,39 @@ RSpec.describe ChefUtils::DSL::Windows do
end
end
- context "on Windows Server Core" do
- let(:node) { { "kernel" => { "server_core" => true } } }
+ context "windows boolean helpers" do
+ context "on Windows Server Core" do
+ let(:node) { { "kernel" => { "server_core" => true } } }
- windows_reports_true_for(:windows_server_core?)
- end
+ windows_reports_true_for(:windows_server_core?)
+ end
+
+ context "on Windows Workstation" do
+ let(:node) { { "kernel" => { "product_type" => "Workstation" } } }
+
+ windows_reports_true_for(:windows_workstation?)
+ end
- context "on Windows Workstation" do
- let(:node) { { "kernel" => { "product_type" => "Workstation" } } }
+ context "on Windows Server" do
+ let(:node) { { "kernel" => { "product_type" => "Server" } } }
- windows_reports_true_for(:windows_workstation?)
+ windows_reports_true_for(:windows_server?)
+ end
end
- context "on Windows Server" do
- let(:node) { { "kernel" => { "product_type" => "Server" } } }
+ context "#windows_nt_version on Windows Server 2012 R2" do
+ let(:node) { { "os_version" => "6.3.9600" } }
+ it "it returns a ChefUtils::VersionString object with 6.3.9600" do
+ expect(described_class.send(:windows_nt_version, node)).to eq "6.3.9600"
+ expect(described_class.send(:windows_nt_version, node)).to be_a_kind_of ChefUtils::VersionString
+ end
+ end
- windows_reports_true_for(:windows_server?)
+ context "#powershell_version on Windows Server 2012 R2" do
+ let(:node) { { "languages" => { "powershell" => { "version" => "4.0" } } } }
+ it "it returns a ChefUtils::VersionString object with 4.0" do
+ expect(described_class.send(:powershell_version, node)).to eq "4.0"
+ expect(described_class.send(:powershell_version, node)).to be_a_kind_of ChefUtils::VersionString
+ end
end
end