mirror of
https://github.com/inspec/inspec
synced 2024-11-10 07:04:15 +00:00
CHEF-7670 Ability to set feature preview flag using ENV (#6833)
* Implemented ability to add feature flags in Inspec feature subsystem Signed-off-by: Nik08 <nikita.mathur@progress.com> * Added usage of env_preview for conditionalising features Signed-off-by: Nik08 <nikita.mathur@progress.com> * Test cases added for feature preview flag ability Signed-off-by: Nik08 <nikita.mathur@progress.com> * Minor chnages - Better commenting & smarter methods Signed-off-by: Nik08 <nikita.mathur@progress.com> --------- Signed-off-by: Nik08 <nikita.mathur@progress.com>
This commit is contained in:
parent
95c586afaa
commit
456fa5c8f5
5 changed files with 104 additions and 2 deletions
|
@ -7,11 +7,28 @@ module Inspec
|
|||
end
|
||||
|
||||
class Feature
|
||||
attr_reader :name, :description
|
||||
attr_reader :name, :description, :env_preview
|
||||
def initialize(feature_name, feature_yaml_opts)
|
||||
@name = feature_name
|
||||
feature_yaml_opts ||= {}
|
||||
@description = feature_yaml_opts["description"]
|
||||
@env_preview = feature_yaml_opts["env_preview"]
|
||||
end
|
||||
|
||||
def previewable?
|
||||
# If the feature is previewable in config (features.yaml) & has an environment value set to use previewed feature
|
||||
!!env_preview && !env_preview_value.nil?
|
||||
end
|
||||
|
||||
def no_preview?
|
||||
env_preview.nil?
|
||||
end
|
||||
|
||||
def env_preview_value
|
||||
# Examples: If feature name is "inspec-test-feature"
|
||||
# ENV used for this feature preview would be CHEF_PREVIEW_TEST_FEATURE
|
||||
env_preview_feature_name = name.to_s.split("inspec-")[-1]
|
||||
ENV["CHEF_PREVIEW_#{env_preview_feature_name.gsub("-", "_").upcase}"]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,7 +14,12 @@ module Inspec
|
|||
logger.warn "Unrecognized feature name '#{feature_name}'"
|
||||
end
|
||||
|
||||
yield feature_implementation
|
||||
# If the feature is not recognized
|
||||
# If the feature has no env_preview flag set in config
|
||||
# If the feature is previewable
|
||||
if feature.nil? || feature&.no_preview? || feature&.previewable?
|
||||
yield feature_implementation
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
6
test/fixtures/features-02.sig
vendored
Normal file
6
test/fixtures/features-02.sig
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
zLNcjwWCxg642VkKw7lOANqmzHHI38Ro0v3+CFuFjpQTIGUiPsNFKj6Jpza7
|
||||
LpdvDVpRw2yBy46h+M/49/4PA2DDLpiT2M3QCkViQoF2Ue5FgAOsqwUuEhYr
|
||||
PoxtihyJmJVfMsRUuxmaiquYYjESvm1pPV5kP06BeZpwhPXuKc18m5KxN5yU
|
||||
VxqWi/RlejpNe2BJSW24kfdqhEYCDRFZ4A01OvjARritVciGYMX7tfcDT7yP
|
||||
2ogLxEXBFNItzHw/zAUykL9V0uEquiG4WfDc+94Iw6srbKiZWkr5bc+WnVT0
|
||||
6MhUM+xrTlxt38mHoPI2yxNqjDb2Adah0MGq++Bd3w==
|
13
test/fixtures/features-02.yaml
vendored
Normal file
13
test/fixtures/features-02.yaml
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
---
|
||||
features:
|
||||
inspec-test-feature-01:
|
||||
description: Fun for the whole security organization
|
||||
env_preview: true
|
||||
inspec-test-feature-02:
|
||||
description: Fun for the whole security organization
|
||||
env_preview: true
|
||||
inspec-test-feature-03:
|
||||
description: A great use of your time
|
||||
env_preview: false
|
||||
inspec-test-feature-04:
|
||||
description: A great use of your time and also fun
|
|
@ -130,5 +130,66 @@ describe "Inspec::Feature" do
|
|||
_ { Inspec::Feature::Config.new(tampered_config_file) }.must_raise(Inspec::FeatureConfigTamperedError)
|
||||
end
|
||||
end
|
||||
|
||||
describe "When using feature preview flag" do
|
||||
let(:feature_config_file) { File.join(fixtures_path, "features-02.yaml") }
|
||||
# you should be able to load it from a test file
|
||||
let(:cfg) { Inspec::Feature::Config.new(feature_config_file) }
|
||||
|
||||
before do
|
||||
# Setting ENV value for test feature 1
|
||||
ENV["CHEF_PREVIEW_TEST_FEATURE_01"] = "1"
|
||||
end
|
||||
after do
|
||||
ENV.delete("CHEF_PREVIEW_TEST_FEATURE_01")
|
||||
end
|
||||
|
||||
it "yields block only when previewable and ENV value is set" do
|
||||
test_feature_01 = cfg.features[0]
|
||||
test_feature_01_called = false
|
||||
Inspec.with_feature("inspec-test-feature-01", config: cfg) do
|
||||
test_feature_01_called = true
|
||||
end
|
||||
_(test_feature_01_called).must_equal true
|
||||
_(test_feature_01.previewable?).must_equal true
|
||||
end
|
||||
|
||||
it "does not yields block when preview_env true but ENV value is not set" do
|
||||
test_feature_02 = cfg.features[1]
|
||||
test_feature_02_called = false
|
||||
Inspec.with_feature("inspec-test-feature-02", config: cfg) do
|
||||
test_feature_02_called = true
|
||||
end
|
||||
_(test_feature_02_called).must_equal false
|
||||
_(test_feature_02.previewable?).must_equal false
|
||||
end
|
||||
|
||||
it "does not yields block when not previewable" do
|
||||
test_feature_03 = cfg.features[2]
|
||||
test_feature_03_called = false
|
||||
Inspec.with_feature("inspec-test-feature-03", config: cfg) do
|
||||
test_feature_03_called = true
|
||||
end
|
||||
_(test_feature_03_called).must_equal false
|
||||
_(test_feature_03.previewable?).must_equal false
|
||||
end
|
||||
end
|
||||
|
||||
describe "When not using feature preview flag" do
|
||||
let(:feature_config_file) { File.join(fixtures_path, "features-02.yaml") }
|
||||
# you should be able to load it from a test file
|
||||
let(:cfg) { Inspec::Feature::Config.new(feature_config_file) }
|
||||
|
||||
it "yields block when flag not set" do
|
||||
test_feature_04 = cfg.features[3]
|
||||
test_feature_04_called = false
|
||||
Inspec.with_feature("inspec-test-feature-04", config: cfg) do
|
||||
test_feature_04_called = true
|
||||
end
|
||||
_(test_feature_04_called).must_equal true
|
||||
_(test_feature_04.previewable?).must_equal false
|
||||
_(test_feature_04.no_preview?).must_equal true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue