summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-03-23 11:35:27 -0700
committerTim Smith <tsmith84@gmail.com>2020-03-23 11:36:46 -0700
commite7e8781a5092cf67ef3900aa0b31e64971838a1b (patch)
tree5917a96733e8148d1bfd027240737d2fc29cde4e
parentb3509511cde8822693e86c98f9834e0508630f20 (diff)
downloadchef-e7e8781a5092cf67ef3900aa0b31e64971838a1b.tar.gz
Move cron validatioin methods into a mixin
This will allow us to use them in the other cron resource eventually, but definitely in the chef_client_cron resource today. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/mixin/cron_validations.rb90
-rw-r--r--lib/chef/resource/cron_d.rb78
-rw-r--r--spec/unit/mixin/cron_validations_spec.rb73
-rw-r--r--spec/unit/resource/cron_d_spec.rb54
4 files changed, 169 insertions, 126 deletions
diff --git a/lib/chef/mixin/cron_validations.rb b/lib/chef/mixin/cron_validations.rb
new file mode 100644
index 0000000000..e8bb279a5a
--- /dev/null
+++ b/lib/chef/mixin/cron_validations.rb
@@ -0,0 +1,90 @@
+#
+# Copyright:: Copyright 2020, 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.
+#
+
+class Chef
+ module Mixin
+ # a collection of methods for validating cron times. Used in the various cron-like resources
+ module CronValidations
+ # validate a provided value is between two other provided values
+ # we also allow * as a valid input
+ # @param spec the value to validate
+ # @param min the lowest value allowed
+ # @param max the highest value allowed
+ # @return [Boolean] valid or not?
+ def self.validate_numeric(spec, min, max)
+ return true if spec == "*"
+
+ # binding.pry
+ if spec.respond_to? :to_int
+ return false unless spec >= min && spec <= max
+
+ return true
+ end
+
+ # Lists of invidual values, ranges, and step values all share the validity range for type
+ spec.split(%r{\/|-|,}).each do |x|
+ next if x == "*"
+ return false unless x =~ /^\d+$/
+
+ x = x.to_i
+ return false unless x >= min && x <= max
+ end
+ true
+ end
+
+ # validate the provided month value to be jan - dec, 1 - 12, or *
+ # @param spec the value to validate
+ # @return [Boolean] valid or not?
+ def self.validate_month(spec)
+ return true if spec == "*"
+
+ if spec.respond_to? :to_int
+ validate_numeric(spec, 1, 12)
+ elsif spec.respond_to? :to_str
+ return true if spec == "*"
+ # Named abbreviations are permitted but not as part of a range or with stepping
+ return true if %w{jan feb mar apr may jun jul aug sep oct nov dec}.include? spec.downcase
+
+ # 1-12 are legal for months
+ validate_numeric(spec, 1, 12)
+ else
+ false
+ end
+ end
+
+ # validate the provided day of the week is sun-sat, 0-7, or *
+ # @param spec the value to validate
+ # @return [Boolean] valid or not?
+ def self.validate_dow(spec)
+ return true if spec == "*"
+
+ if spec.respond_to? :to_int
+ validate_numeric(spec, 0, 7)
+ elsif spec.respond_to? :to_str
+ return true if spec == "*"
+ # Named abbreviations are permitted but not as part of a range or with stepping
+ return true if %w{sun mon tue wed thu fri sat}.include? spec.downcase
+
+ # 0-7 are legal for days of week
+ validate_numeric(spec, 0, 7)
+ else
+ false
+ end
+ end
+ end
+ end
+end
diff --git a/lib/chef/resource/cron_d.rb b/lib/chef/resource/cron_d.rb
index befe951a35..98e962ac66 100644
--- a/lib/chef/resource/cron_d.rb
+++ b/lib/chef/resource/cron_d.rb
@@ -16,6 +16,7 @@
#
require_relative "../resource"
+require_relative "../mixin/cron_validations"
require "shellwords" unless defined?(Shellwords)
require_relative "../dist"
@@ -73,73 +74,6 @@ class Chef
```
DOC
- # validate a provided value is between two other provided values
- # we also allow * as a valid input
- # @param spec the value to validate
- # @param min the lowest value allowed
- # @param max the highest value allowed
- # @return [Boolean] valid or not?
- def self.validate_numeric(spec, min, max)
- return true if spec == "*"
-
- # binding.pry
- if spec.respond_to? :to_int
- return false unless spec >= min && spec <= max
-
- return true
- end
-
- # Lists of invidual values, ranges, and step values all share the validity range for type
- spec.split(%r{\/|-|,}).each do |x|
- next if x == "*"
- return false unless x =~ /^\d+$/
-
- x = x.to_i
- return false unless x >= min && x <= max
- end
- true
- end
-
- # validate the provided month value to be jan - dec, 1 - 12, or *
- # @param spec the value to validate
- # @return [Boolean] valid or not?
- def self.validate_month(spec)
- return true if spec == "*"
-
- if spec.respond_to? :to_int
- validate_numeric(spec, 1, 12)
- elsif spec.respond_to? :to_str
- return true if spec == "*"
- # Named abbreviations are permitted but not as part of a range or with stepping
- return true if %w{jan feb mar apr may jun jul aug sep oct nov dec}.include? spec.downcase
-
- # 1-12 are legal for months
- validate_numeric(spec, 1, 12)
- else
- false
- end
- end
-
- # validate the provided day of the week is sun-sat, 0-7, or *
- # @param spec the value to validate
- # @return [Boolean] valid or not?
- def self.validate_dow(spec)
- return true if spec == "*"
-
- if spec.respond_to? :to_int
- validate_numeric(spec, 0, 7)
- elsif spec.respond_to? :to_str
- return true if spec == "*"
- # Named abbreviations are permitted but not as part of a range or with stepping
- return true if %w{sun mon tue wed thu fri sat}.include? spec.downcase
-
- # 0-7 are legal for days of week
- validate_numeric(spec, 0, 7)
- else
- false
- end
- end
-
property :cron_name, String,
description: "An optional property to set the cron name if it differs from the resource block's name.",
name_property: true
@@ -153,31 +87,31 @@ class Chef
property :minute, [Integer, String],
description: "The minute at which the cron entry should run (0 - 59).",
default: "*", callbacks: {
- "should be a valid minute spec" => ->(spec) { validate_numeric(spec, 0, 59) },
+ "should be a valid minute spec" => ->(spec) { Chef::Mixin::CronValidations.validate_numeric(spec, 0, 59) },
}
property :hour, [Integer, String],
description: "The hour at which the cron entry is to run (0 - 23).",
default: "*", callbacks: {
- "should be a valid hour spec" => ->(spec) { validate_numeric(spec, 0, 23) },
+ "should be a valid hour spec" => ->(spec) { Chef::Mixin::CronValidations.validate_numeric(spec, 0, 23) },
}
property :day, [Integer, String],
description: "The day of month at which the cron entry should run (1 - 31).",
default: "*", callbacks: {
- "should be a valid day spec" => ->(spec) { validate_numeric(spec, 1, 31) },
+ "should be a valid day spec" => ->(spec) { Chef::Mixin::CronValidations.validate_numeric(spec, 1, 31) },
}
property :month, [Integer, String],
description: "The month in the year on which a cron entry is to run (1 - 12, jan-dec, or *).",
default: "*", callbacks: {
- "should be a valid month spec" => ->(spec) { validate_month(spec) },
+ "should be a valid month spec" => ->(spec) { Chef::Mixin::CronValidations.validate_month(spec) },
}
property :weekday, [Integer, String],
description: "The day of the week on which this entry is to run (0-7, mon-sun, or *), where Sunday is both 0 and 7.",
default: "*", callbacks: {
- "should be a valid weekday spec" => ->(spec) { validate_dow(spec) },
+ "should be a valid weekday spec" => ->(spec) { Chef::Mixin::CronValidations.validate_dow(spec) },
}
property :command, String,
diff --git a/spec/unit/mixin/cron_validations_spec.rb b/spec/unit/mixin/cron_validations_spec.rb
new file mode 100644
index 0000000000..409df2da1c
--- /dev/null
+++ b/spec/unit/mixin/cron_validations_spec.rb
@@ -0,0 +1,73 @@
+#
+# Copyright 2020, Chef Software, Inc <legal@chef.io>
+#
+# 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 "chef/mixin/cron_validations"
+
+describe Chef::Mixin::CronValidations do
+ context "#validate_dow" do
+ it "it accepts a string day" do
+ expect(Chef::Mixin::CronValidations.validate_dow("mon")).to be true
+ end
+
+ it "it accepts an integer day" do
+ expect(Chef::Mixin::CronValidations.validate_dow(0)).to be true
+ end
+
+ it "it accepts the string of *" do
+ expect(Chef::Mixin::CronValidations.validate_dow("*")).to be true
+ end
+
+ it "returns false for an out of range integer" do
+ expect(Chef::Mixin::CronValidations.validate_dow(8)).to be false
+ end
+
+ it "returns false for an invalid string" do
+ expect(Chef::Mixin::CronValidations.validate_dow("monday")).to be false
+ end
+ end
+
+ context "#validate_month" do
+ it "it accepts a string month" do
+ expect(Chef::Mixin::CronValidations.validate_month("feb")).to be true
+ end
+
+ it "it accepts an integer month" do
+ expect(Chef::Mixin::CronValidations.validate_month(2)).to be true
+ end
+
+ it "it accepts the string of *" do
+ expect(Chef::Mixin::CronValidations.validate_month("*")).to be true
+ end
+
+ it "returns false for an out of range integer" do
+ expect(Chef::Mixin::CronValidations.validate_month(13)).to be false
+ end
+
+ it "returns false for an invalid string" do
+ expect(Chef::Mixin::CronValidations.validate_month("janurary")).to be false
+ end
+ end
+
+ context "#validate_numeric" do
+ it "returns true if the value is in the allowed range" do
+ expect(Chef::Mixin::CronValidations.validate_numeric(5, 1, 100)).to be true
+ end
+
+ it "returns false if the value is out of the allowed range" do
+ expect(Chef::Mixin::CronValidations.validate_numeric(-1, 1, 100)).to be false
+ end
+ end
+end
diff --git a/spec/unit/resource/cron_d_spec.rb b/spec/unit/resource/cron_d_spec.rb
index 6d29a17aaf..7b1cf3d5ba 100644
--- a/spec/unit/resource/cron_d_spec.rb
+++ b/spec/unit/resource/cron_d_spec.rb
@@ -33,58 +33,4 @@ describe Chef::Resource::CronD do
it "the cron_name property is the name_property" do
expect(resource.cron_name).to eql("cronify")
end
-
- context "#validate_dow" do
- it "it accepts a string day" do
- expect(Chef::Resource::CronD.validate_dow("mon")).to be true
- end
-
- it "it accepts an integer day" do
- expect(Chef::Resource::CronD.validate_dow(0)).to be true
- end
-
- it "it accepts the string of *" do
- expect(Chef::Resource::CronD.validate_dow("*")).to be true
- end
-
- it "returns false for an out of range integer" do
- expect(Chef::Resource::CronD.validate_dow(8)).to be false
- end
-
- it "returns false for an invalid string" do
- expect(Chef::Resource::CronD.validate_dow("monday")).to be false
- end
- end
-
- context "#validate_month" do
- it "it accepts a string month" do
- expect(Chef::Resource::CronD.validate_month("feb")).to be true
- end
-
- it "it accepts an integer month" do
- expect(Chef::Resource::CronD.validate_month(2)).to be true
- end
-
- it "it accepts the string of *" do
- expect(Chef::Resource::CronD.validate_month("*")).to be true
- end
-
- it "returns false for an out of range integer" do
- expect(Chef::Resource::CronD.validate_month(13)).to be false
- end
-
- it "returns false for an invalid string" do
- expect(Chef::Resource::CronD.validate_month("janurary")).to be false
- end
- end
-
- context "#validate_numeric" do
- it "returns true if the value is in the allowed range" do
- expect(Chef::Resource::CronD.validate_numeric(5, 1, 100)).to be true
- end
-
- it "returns false if the value is out of the allowed range" do
- expect(Chef::Resource::CronD.validate_numeric(-1, 1, 100)).to be false
- end
- end
end