summaryrefslogtreecommitdiff
path: root/spec/unit
diff options
context:
space:
mode:
authorThom May <thom@may.lt>2017-04-06 14:15:38 +0100
committerGitHub <noreply@github.com>2017-04-06 14:15:38 +0100
commit4e9305e8474a1faa72b7d89a2836a907909609af (patch)
tree717c9213fbb1861b90fdecba739b8fef42b84eac /spec/unit
parent1e6273029283af6efbedb8fb5ecc8840f90051dd (diff)
parentd656b364bd9cd0cbe06eb9eee372997e6110e1ae (diff)
downloadchef-4e9305e8474a1faa72b7d89a2836a907909609af.tar.gz
Merge pull request #5886 from MsysTechnologiesllc/nim/windows_task
Adding windows_task resource
Diffstat (limited to 'spec/unit')
-rw-r--r--spec/unit/provider/windows_task_spec.rb392
-rw-r--r--spec/unit/resource/windows_task_spec.rb213
2 files changed, 605 insertions, 0 deletions
diff --git a/spec/unit/provider/windows_task_spec.rb b/spec/unit/provider/windows_task_spec.rb
new file mode 100644
index 0000000000..80038aa6db
--- /dev/null
+++ b/spec/unit/provider/windows_task_spec.rb
@@ -0,0 +1,392 @@
+#
+# Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>)
+# Copyright:: Copyright 2008-2016, 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::Provider::WindowsTask do
+ let(:new_resource) { Chef::Resource::WindowsTask.new("sample_task") }
+
+ let(:provider) do
+ node = Chef::Node.new
+ events = Chef::EventDispatch::Dispatcher.new
+ run_context = Chef::RunContext.new(node, {}, events)
+ Chef::Provider::WindowsTask.new(new_resource, run_context)
+ end
+
+ let(:task_hash) do
+ {
+ :"" => "",
+ :Folder => "\\",
+ :HostName => "NIMISHA-PC",
+ :TaskName => "\\sample_task",
+ :NextRunTime => "3/30/2017 2:42:00 PM",
+ :Status => "Ready",
+ :LogonMode => "Interactive/Background",
+ :LastRunTime => "3/30/2017 2:27:00 PM",
+ :LastResult => "1",
+ :Author => "Administrator",
+ :TaskToRun => "chef-client",
+ :StartIn => "N/A",
+ :Comment => "N/A",
+ :ScheduledTaskState => "Enabled",
+ :IdleTime => "Disabled",
+ :PowerManagement => "Stop On Battery Mode, No Start On Batteries",
+ :RunAsUser => "SYSTEM",
+ :DeleteTaskIfNotRescheduled => "Enabled",
+ :StopTaskIfRunsXHoursandXMins => "72:00:00",
+ :Schedule => "Scheduling data is not available in this format.",
+ :ScheduleType => "One Time Only, Minute",
+ :StartTime => "1:12:00 PM",
+ :StartDate => "3/30/2017",
+ :EndDate => "N/A",
+ :Days => "N/A",
+ :Months => "N/A",
+ :"Repeat:Every" => "0 Hour(s), 15 Minute(s)",
+ :"Repeat:Until:Time" => "None",
+ :"Repeat:Until:Duration" => "Disabled",
+ :"Repeat:StopIfStillRunning" => "Disabled",
+ :run_level => "HighestAvailable",
+ :repetition_interval => "PT15M",
+ :execution_time_limit => "PT72H",
+ }
+ end
+
+ let(:task_xml) do
+ "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\r\r\n<Task version=\"1.2\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\r\r\n <RegistrationInfo>\r\r\n <Date>2017-03-31T15:34:44</Date>\r\r\n <Author>Administrator</Author>\r\r\n </RegistrationInfo>\r\r\n<Triggers>\r\r\n <TimeTrigger>\r\r\n <Repetition>\r\r\n <Interval>PT15M</Interval>\r\r\n <StopAtDurationEnd>false</StopAtDurationEnd>\r\r\n </Repetition>\r\r\n <StartBoundary>2017-03-31T15:34:00</StartBoundary>\r\r\n <Enabled>true</Enabled>\r\r\n </TimeTrigger>\r\r\n </Triggers>\r\r\n <Principals>\r\r\n <Principal id=\"Author\">\r\r\n <RunLevel>HighestAvailable</RunLevel>\r\r\n <UserId>S-1-5-18</UserId>\r\r\n </Principal>\r\r\n </Principals>\r\r\n <Settings>\r\r\n <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\r\r\n <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>\r\r\n <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>\r\r\n <AllowHardTerminate>true</AllowHardTerminate>\r\r\n <StartWhenAvailable>false</StartWhenAvailable>\r\r\n <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>\r\r\n <IdleSettings>\r\r\n <Duration>PT10M</Duration>\r\r\n<WaitTimeout>PT1H</WaitTimeout>\r\r\n <StopOnIdleEnd>true</StopOnIdleEnd>\r\r\n <RestartOnIdle>false</RestartOnIdle>\r\r\n </IdleSettings>\r\r\n <AllowStartOnDemand>true</AllowStartOnDemand>\r\r\n <Enabled>true</Enabled>\r\r\n <Hidden>false</Hidden>\r\r\n<RunOnlyIfIdle>false</RunOnlyIfIdle>\r\r\n <WakeToRun>false</WakeToRun>\r\r\n <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\r\r\n<Priority>7</Priority>\r\r\n </Settings>\r\r\n <Actions Context=\"Author\">\r\r\n <Exec>\r\r\n <Command>chef-client</Command>\r\r\n </Exec>\r\r\n </Actions>\r\r\n</Task>"
+ end
+
+ describe "#load_current_resource" do
+ it "returns a current_resource" do
+ allow(provider).to receive(:load_task_hash)
+ expect(provider.load_current_resource).to be_kind_of(Chef::Resource::WindowsTask)
+ end
+
+ context "if the given task name already exists" do
+ before do
+ allow(provider).to receive(:load_task_hash).and_return({ :TaskName => "\\sample_task" })
+ end
+
+ it "calls set_current_resource" do
+ expect(provider).to receive(:set_current_resource)
+ provider.load_current_resource
+ end
+ end
+
+ it "sets the attributes of current_resource" do
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ current_resource = provider.load_current_resource
+ expect(current_resource.exists).to be(true)
+ expect(current_resource.command).to eq("chef-client")
+ expect(current_resource.user).to eq("SYSTEM")
+ expect(current_resource.run_level).to eq(:highest)
+ expect(current_resource.frequency).to eq(:minute)
+ expect(current_resource.frequency_modifier).to eq(15)
+ expect(current_resource.execution_time_limit).to eq("PT72H")
+ expect(current_resource.enabled).to be(true)
+ end
+ end
+
+ describe "#action_create" do
+ it "doesn't create the same task if it's already existing" do
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+ allow(provider).to receive(:task_need_update?).and_return(false)
+ provider.run_action(:create)
+ expect(new_resource).not_to be_updated_by_last_action
+ end
+
+ context "when task is not existing" do
+ before do
+ allow(provider).to receive(:load_task_hash)
+ provider.load_current_resource
+ end
+
+ it "creates the task if it's not already existing" do
+ allow(provider).to receive(:task_need_update?).and_return(true)
+ expect(provider).to receive(:run_schtasks).with("CREATE", { "F" => "", "SC" => :hourly, "MO" => 1, "TR" => nil, "RU" => "SYSTEM" })
+ provider.run_action(:create)
+ expect(new_resource).to be_updated_by_last_action
+ end
+
+ it "updates the task XML if random_delay is provided" do
+ new_resource.random_delay "20"
+ allow(provider).to receive(:task_need_update?).and_return(true)
+ expect(provider).to receive(:run_schtasks).with("CREATE", { "F" => "", "SC" => :hourly, "MO" => 1, "TR" => nil, "RU" => "SYSTEM" })
+ expect(provider).to receive(:update_task_xml)
+ provider.run_action(:create)
+ expect(new_resource).to be_updated_by_last_action
+ end
+
+ it "updates the task XML if execution_time_limit is provided" do
+ new_resource.execution_time_limit "20"
+ allow(provider).to receive(:task_need_update?).and_return(true)
+ expect(provider).to receive(:run_schtasks).with("CREATE", { "F" => "", "SC" => :hourly, "MO" => 1, "TR" => nil, "RU" => "SYSTEM" })
+ expect(provider).to receive(:update_task_xml)
+ provider.run_action(:create)
+ expect(new_resource).to be_updated_by_last_action
+ end
+ end
+ end
+
+ describe "#action_run" do
+ it "does nothing if the task doesn't exist" do
+ allow(provider).to receive(:load_task_hash)
+ provider.load_current_resource
+ provider.run_action(:run)
+ expect(new_resource).not_to be_updated_by_last_action
+ end
+
+ context "when the task exists" do
+ it "does nothing if the task is already running" do
+ task_hash[:Status] = "Running"
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+ provider.run_action(:run)
+ expect(new_resource).not_to be_updated_by_last_action
+ end
+
+ it "runs the task" do
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+ expect(provider).to receive(:run_schtasks).with("RUN")
+ provider.run_action(:run)
+ expect(new_resource).to be_updated_by_last_action
+ end
+ end
+ end
+
+ describe "#action_delete" do
+ it "deletes the task if it exists" do
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+ expect(provider).to receive(:run_schtasks).with("DELETE", { "F" => "" })
+ provider.run_action(:delete)
+ expect(new_resource).to be_updated_by_last_action
+ end
+
+ it "does nothing if the task doesn't exist" do
+ allow(provider).to receive(:load_task_hash)
+ provider.load_current_resource
+ provider.run_action(:delete)
+ expect(new_resource).not_to be_updated_by_last_action
+ end
+ end
+
+ describe "#action_end" do
+ it "does nothing if the task doesn't exist" do
+ allow(provider).to receive(:load_task_hash)
+ provider.load_current_resource
+ provider.run_action(:end)
+ expect(new_resource).not_to be_updated_by_last_action
+ end
+
+ context "when the task exists" do
+ it "does nothing if the task is not running" do
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+ provider.run_action(:end)
+ expect(new_resource).not_to be_updated_by_last_action
+ end
+
+ it "ends the task if it's running" do
+ task_hash[:Status] = "Running"
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+ expect(provider).to receive(:run_schtasks).with("END")
+ provider.run_action(:end)
+ expect(new_resource).to be_updated_by_last_action
+ end
+ end
+ end
+
+ describe "#action_enable" do
+ it "raises error if the task doesn't exist" do
+ allow(provider).to receive(:load_task_hash)
+ provider.load_current_resource
+ expect { provider.run_action(:enable) }.to raise_error(Errno::ENOENT)
+ end
+
+ context "when the task exists" do
+ it "does nothing if the task is already enabled" do
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+ provider.run_action(:enable)
+ expect(new_resource).not_to be_updated_by_last_action
+ end
+
+ it "enables the task if it exists" do
+ task_hash[:ScheduledTaskState] = "Disabled"
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+ expect(provider).to receive(:run_schtasks).with("CHANGE", { "ENABLE" => "" })
+ provider.run_action(:enable)
+ expect(new_resource).to be_updated_by_last_action
+ end
+ end
+ end
+
+ describe "#action_disable" do
+ it "does nothing if the task doesn't exist" do
+ allow(provider).to receive(:load_task_hash)
+ provider.load_current_resource
+ provider.run_action(:disable)
+ expect(new_resource).not_to be_updated_by_last_action
+ end
+
+ context "when the task exists" do
+ it "disables the task if it's enabled" do
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+ expect(provider).to receive(:run_schtasks).with("CHANGE", { "DISABLE" => "" })
+ provider.run_action(:disable)
+ expect(new_resource).to be_updated_by_last_action
+ end
+
+ it "does nothing if the task is already disabled" do
+ task_hash[:ScheduledTaskState] = "Disabled"
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+ provider.run_action(:disable)
+ expect(new_resource).not_to be_updated_by_last_action
+ end
+ end
+ end
+
+ describe "#run_schtasks" do
+ before do
+ @task_action = "CREATE"
+ @options = { "F" => "", "SC" => :minute, "MO" => 15, "TR" => "chef-client", "RU" => "SYSTEM", "RL" => "HIGHEST" }
+ @cmd = "schtasks /CREATE /TN \"sample_task\" /F /SC \"minute\" /MO \"15\" /TR \"chef-client\" /RU \"SYSTEM\" /RL \"HIGHEST\" "
+ end
+
+ it "forms the command properly from the given options" do
+ expect(provider).to receive(:shell_out!).with(@cmd, { :returns => [0] })
+ provider.send(:run_schtasks, @task_action, @options)
+ end
+ end
+
+ describe "#task_need_update?" do
+ context "when task doesn't exist" do
+ before do
+ allow(provider).to receive(:load_task_hash)
+ provider.load_current_resource
+ end
+
+ it "returns true" do
+ new_resource.command "chef-client"
+ expect(provider.send(:task_need_update?)).to be(true)
+ end
+ end
+
+ context "when the task exists" do
+ before do
+ allow(provider).to receive(:load_task_hash).and_return(task_hash)
+ provider.load_current_resource
+
+ new_resource.command "chef-client"
+ new_resource.run_level :highest
+ new_resource.frequency :minute
+ new_resource.frequency_modifier 15
+ new_resource.user "SYSTEM"
+ new_resource.execution_time_limit "PT72H"
+ end
+
+ context "when no attributes are modified" do
+ it "returns false" do
+ expect(provider.send(:task_need_update?)).to be(false)
+ end
+ end
+
+ context "when frequency_modifier is updated" do
+ it "returns true" do
+ new_resource.frequency_modifier 25
+ expect(provider.send(:task_need_update?)).to be(true)
+ end
+ end
+
+ context "when months are updated" do
+ it "returns true" do
+ new_resource.months "JAN"
+ expect(provider.send(:task_need_update?)).to be(true)
+ end
+ end
+ end
+ end
+
+ describe "#update_task_xml" do
+ before do
+ new_resource.command "chef-client"
+ new_resource.run_level :highest
+ new_resource.frequency :minute
+ new_resource.frequency_modifier 15
+ new_resource.user "SYSTEM"
+ new_resource.random_delay "20"
+ end
+
+ it "does nothing if the task doesn't exist" do
+ task_xml = double("xml", :exitstatus => 1)
+ allow(provider).to receive(:powershell_out).and_return(task_xml)
+ output = provider.send(:update_task_xml, ["random_delay"])
+ expect(output).to be(nil)
+ end
+
+ it "updates the task XML if random_delay is passed" do
+ shell_out_obj = double("xml", :exitstatus => 0, :stdout => task_xml)
+ allow(provider).to receive(:powershell_out).and_return(shell_out_obj)
+ expect(::File).to receive(:join)
+ expect(::File).to receive(:open)
+ expect(::File).to receive(:delete)
+ expect(provider).to receive(:run_schtasks).twice
+ output = provider.send(:update_task_xml, ["random_delay"])
+ end
+ end
+
+ describe "#load_task_hash" do
+ it "returns false if the task doesn't exist" do
+ allow(provider).to receive_message_chain(:powershell_out, :stdout, :force_encoding).and_return("")
+ allow(provider).to receive(:load_task_xml)
+ expect(provider.send(:load_task_hash, "chef-client")).to be(false)
+ end
+
+ it "returns task hash if the task exists" do
+ powershell_output = "\r\nFolder: \\\r\nHostName: NIMISHA-PC\r\nTaskName: \\chef-client\r\n"
+ task_h = { :"" => "", :Folder => "\\", :HostName => "NIMISHA-PC", :TaskName => "\\chef-client" }
+ allow(provider).to receive_message_chain(:powershell_out, :stdout, :force_encoding).and_return(powershell_output)
+ allow(provider).to receive(:load_task_xml).with("chef-client")
+ expect(provider.send(:load_task_hash, "chef-client")).to eq(task_h)
+ end
+ end
+
+ describe "#frequency_modifier_allowed" do
+ it "returns true for frequency :hourly" do
+ new_resource.frequency :hourly
+ expect(provider.send(:frequency_modifier_allowed)).to be(true)
+ end
+
+ it "returns true for frequency :monthly if frequency_modifier is THIRD" do
+ new_resource.frequency :monthly
+ new_resource.frequency_modifier "THIRD"
+ expect(provider.send(:frequency_modifier_allowed)).to be(true)
+ end
+
+ it "returns false for frequency :once" do
+ new_resource.frequency :once
+ expect(provider.send(:frequency_modifier_allowed)).to be(false)
+ end
+ end
+end
diff --git a/spec/unit/resource/windows_task_spec.rb b/spec/unit/resource/windows_task_spec.rb
new file mode 100644
index 0000000000..fa2d458bbb
--- /dev/null
+++ b/spec/unit/resource/windows_task_spec.rb
@@ -0,0 +1,213 @@
+#
+# Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>)
+# Copyright:: Copyright 2008-2017, 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::WindowsTask do
+ let(:resource) { Chef::Resource::WindowsTask.new("sample_task") }
+
+ it "creates a new Chef::Resource::WindowsTask" do
+ expect(resource).to be_a_kind_of(Chef::Resource)
+ expect(resource).to be_a_instance_of(Chef::Resource::WindowsTask)
+ end
+
+ it "sets resource name as :windows_task" do
+ expect(resource.resource_name).to eql(:windows_task)
+ end
+
+ it "sets the task_name as it's name" do
+ expect(resource.task_name).to eql("sample_task")
+ end
+
+ it "sets the default action as :create" do
+ expect(resource.action).to eql(:create)
+ end
+
+ it "sets the default user as System" do
+ expect(resource.user).to eql("SYSTEM")
+ end
+
+ it "sets the default run_level as :limited" do
+ expect(resource.run_level).to eql(:limited)
+ end
+
+ it "sets the default force as false" do
+ expect(resource.force).to eql(false)
+ end
+
+ it "sets the default interactive_enabled as false" do
+ expect(resource.interactive_enabled).to eql(false)
+ end
+
+ it "sets the default frequency_modifier as 1" do
+ expect(resource.frequency_modifier).to eql(1)
+ end
+
+ it "sets the default frequency as :hourly" do
+ expect(resource.frequency).to eql(:hourly)
+ end
+
+ context "when random_delay is passed" do
+ it "raises error if frequency is `:once`" do
+ resource.frequency :once
+ resource.random_delay "20"
+ expect { resource.after_created }.to raise_error(Chef::Exceptions::ArgumentError, "`random_delay` property is supported only for frequency :minute, :hourly, :daily, :weekly and :monthly")
+ end
+
+ it "raises error for invalid random_delay" do
+ resource.frequency :monthly
+ resource.random_delay "xyz"
+ expect { resource.after_created }.to raise_error(Chef::Exceptions::ArgumentError, "Invalid value passed for `random_delay`. Please pass seconds as a String e.g. '60'.")
+ end
+
+ it "converts seconds into iso8601 format" do
+ resource.frequency :monthly
+ resource.random_delay "60"
+ resource.after_created
+ expect(resource.random_delay).to eq("PT60S")
+ end
+ end
+
+ context "when execution_time_limit is passed" do
+ it "sets the deafult value as PT72H" do
+ resource.after_created
+ expect(resource.execution_time_limit).to eq("PT72H")
+ end
+
+ it "raises error for invalid execution_time_limit" do
+ resource.execution_time_limit "abc"
+ expect { resource.after_created }.to raise_error(Chef::Exceptions::ArgumentError, "Invalid value passed for `execution_time_limit`. Please pass seconds as a String e.g. '60'.")
+ end
+
+ it "converts seconds into iso8601 format" do
+ resource.execution_time_limit "60"
+ resource.after_created
+ expect(resource.execution_time_limit).to eq("PT60S")
+ end
+ end
+
+ context "#validate_start_time" do
+ it "raises error if start_time is nil" do
+ expect { resource.send(:validate_start_time, nil) }.to raise_error(Chef::Exceptions::ArgumentError, "`start_time` needs to be provided with `frequency :once`")
+ end
+ end
+
+ context "#validate_start_day" do
+ it "raise error if start_day is passed with frequency :on_logon" do
+ resource.frequency :on_logon
+ expect { resource.send(:validate_start_day, "Wed", :on_logon) }.to raise_error(Chef::Exceptions::ArgumentError, "`start_day` property is not supported with frequency: on_logon")
+ end
+ end
+
+ context "#validate_user_and_password" do
+ context "when password is not passed" do
+ it "raises error with non-system users" do
+ allow(resource).to receive(:use_password?).and_return(true)
+ expect { resource.send(:validate_user_and_password, "Testuser", nil) }.to raise_error("Can't specify a non-system user without a password!")
+ end
+ end
+ end
+
+ context "#validate_interactive_setting" do
+ it "raises error when interactive_enabled is passed without password" do
+ expect { resource.send(:validate_interactive_setting, true, nil) }.to raise_error("Please provide the password when attempting to set interactive/non-interactive.")
+ end
+ end
+
+ context "#validate_create_frequency_modifier" do
+ context "when frequency is :minute" do
+ it "raises error if frequency_modifier > 1439" do
+ expect { resource.send(:validate_create_frequency_modifier, :minute, 1500) }.to raise_error("frequency_modifier value 1500 is invalid. Valid values for :minute frequency are 1 - 1439.")
+ end
+ end
+
+ context "when frequency is :hourly" do
+ it "raises error if frequency_modifier > 23" do
+ expect { resource.send(:validate_create_frequency_modifier, :hourly, 24) }.to raise_error("frequency_modifier value 24 is invalid. Valid values for :hourly frequency are 1 - 23.")
+ end
+ end
+
+ context "when frequency is :daily" do
+ it "raises error if frequency_modifier > 365" do
+ expect { resource.send(:validate_create_frequency_modifier, :daily, 366) }.to raise_error("frequency_modifier value 366 is invalid. Valid values for :daily frequency are 1 - 365.")
+ end
+ end
+
+ context "when frequency is :weekly" do
+ it "raises error if frequency_modifier > 52" do
+ expect { resource.send(:validate_create_frequency_modifier, :weekly, 53) }.to raise_error("frequency_modifier value 53 is invalid. Valid values for :weekly frequency are 1 - 52.")
+ end
+ end
+
+ context "when frequency is :monthly" do
+ it "raises error if frequency_modifier > 12" do
+ expect { resource.send(:validate_create_frequency_modifier, :monthly, 14) }.to raise_error("frequency_modifier value 14 is invalid. Valid values for :monthly frequency are 1 - 12, 'FIRST', 'SECOND', 'THIRD', 'FOURTH', 'LAST', 'LASTDAY'.")
+ end
+
+ it "raises error if frequency_modifier is invalid" do
+ expect { resource.send(:validate_create_frequency_modifier, :monthly, "abc") }.to raise_error("frequency_modifier value abc is invalid. Valid values for :monthly frequency are 1 - 12, 'FIRST', 'SECOND', 'THIRD', 'FOURTH', 'LAST', 'LASTDAY'.")
+ end
+ end
+ end
+
+ context "#validate_create_day" do
+ it "raises error if frequency is not :weekly" do
+ expect { resource.send(:validate_create_day, "Mon", :monthly) }.to raise_error("day attribute is only valid for tasks that run weekly")
+ end
+
+ it "accepts a valid single day" do
+ expect { resource.send(:validate_create_day, "Mon", :weekly) }.not_to raise_error
+ end
+
+ it "accepts a comma separated list of valid days" do
+ expect { resource.send(:validate_create_day, "Mon, tue, THU", :weekly) }.not_to raise_error
+ end
+
+ it "raises error for invalid day value" do
+ expect { resource.send(:validate_create_day, "xyz", :weekly) }.to raise_error("day attribute invalid. Only valid values are: MON, TUE, WED, THU, FRI, SAT, SUN and *. Multiple values must be separated by a comma.")
+ end
+ end
+
+ context "#validate_create_months" do
+ it "raises error if frequency is not :monthly" do
+ expect { resource.send(:validate_create_months, "Jan", :once) }.to raise_error("months attribute is only valid for tasks that run monthly")
+ end
+
+ it "accepts a valid single month" do
+ expect { resource.send(:validate_create_months, "Feb", :monthly) }.not_to raise_error
+ end
+
+ it "accepts a comma separated list of valid months" do
+ expect { resource.send(:validate_create_months, "Jan, mar, AUG", :monthly) }.not_to raise_error
+ end
+
+ it "raises error for invalid month value" do
+ expect { resource.send(:validate_create_months, "xyz", :monthly) }.to raise_error("months attribute invalid. Only valid values are: JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC and *. Multiple values must be separated by a comma.")
+ end
+ end
+
+ context "#validate_idle_time" do
+ it "raises error if frequency is not :on_idle" do
+ expect { resource.send(:validate_idle_time, 5, :hourly) }.to raise_error("idle_time attribute is only valid for tasks that run on_idle")
+ end
+
+ it "raises error if idle_time > 999" do
+ expect { resource.send(:validate_idle_time, 1000, :on_idle) }.to raise_error("idle_time value 1000 is invalid. Valid values for :on_idle frequency are 1 - 999.")
+ end
+ end
+end