Skip to content

Commit

Permalink
CHEF-16823: Introduce deprecation warning for core resources moving t…
Browse files Browse the repository at this point in the history
…o resource packs in InSpec 7 (inspec#7219)

* chore: add deprecation message for core resource that will be moved as resource pack

Signed-off-by: Sonu Saha <[email protected]>

* chore: invoke deprecation for docker resources

Signed-off-by: Sonu Saha <[email protected]>

* chore: invoke deprecation for mongodb resources

Signed-off-by: Sonu Saha <[email protected]>

* chore: invoke deprecation for rabbitmq resources

Signed-off-by: Sonu Saha <[email protected]>

* chore: invoke deprecation for ibmdb2 resources

Signed-off-by: Sonu Saha <[email protected]>

* chore: invoke deprecation for sybase resources

Signed-off-by: Sonu Saha <[email protected]>

* chore: expect deprecation for docker resources

Signed-off-by: Sonu Saha <[email protected]>

* chore: expect deprecation for mongodb resources

Signed-off-by: Sonu Saha <[email protected]>

* chore: expect deprecation for rabbitmq resources

Signed-off-by: Sonu Saha <[email protected]>

* chore: expect deprecation for ibmdb2 resources

Signed-off-by: Sonu Saha <[email protected]>

* chore: update keyword to display to change notice for core resources moving to resource pack

Signed-off-by: Sonu Saha <[email protected]>

* chore: add the common message to be displayed as part of the warning message

Signed-off-by: Sonu Saha <[email protected]>

* chore: update warning message for the core resources moving to resource pack

Signed-off-by: Sonu Saha <[email protected]>

* chore: add warning message for elasticsearch resource as it will be moved to resource pack in InSpec 7

Signed-off-by: Sonu Saha <[email protected]>

* chore: add warning message for podman resources as it will be moved to resource pack in InSpec 7

Signed-off-by: Sonu Saha <[email protected]>

* chore: add warning message for ssh resources as it will be moved to resource pack in InSpec 7

Signed-off-by: Sonu Saha <[email protected]>

* spec: update test for ignoring change notice warning

Signed-off-by: Sonu Saha <[email protected]>

* refactor: invoke warning during loading of the resource

Signed-off-by: Sonu Saha <[email protected]>

* revert: undo changes to unit test files of core resources

Signed-off-by: Sonu Saha <[email protected]>

* spec: add functional test to verify warning message and successful run

Signed-off-by: Sonu Saha <[email protected]>

* chore: add fixture profile for deprecated resource

Signed-off-by: Sonu Saha <[email protected]>

* chore: update profile fixture for deprecated core resources

Signed-off-by: Sonu Saha <[email protected]>

* chore: minor cleanup and doc change

Signed-off-by: Sonu Saha <[email protected]>

---------

Signed-off-by: Sonu Saha <[email protected]>
  • Loading branch information
ahasunos authored Jan 7, 2025
1 parent f047890 commit 21dc0a7
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 3 deletions.
5 changes: 5 additions & 0 deletions etc/deprecations.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
"action": "exit",
"suffix": "This resource was removed in InSpec 4.0."
},
"core_resource_moved_to_rp": {
"action": "warn",
"suffix": "This resource will be moved to a separate resource pack. Additional details will be provided with the InSpec 7 release.",
"comment": "Deprecation notice for core resource which are getting moved to resource packs."
},
"resource_iis_website": {
"action": "exit",
"suffix": "This resource was removed in InSpec 4.0.",
Expand Down
5 changes: 5 additions & 0 deletions lib/inspec/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "inspec/log"
require "inspec/plugin/v2"
require "inspec/utils/deprecated_cloud_resources_list"
require "inspec/utils/deprecated_core_resources_list"

module Inspec::DSL
attr_accessor :backend
Expand Down Expand Up @@ -38,6 +39,10 @@ def self.method_missing_resource(backend, id, *arguments)
return unless backend

begin
include DeprecatedCoreResourcesList
if CORE_RESOURCES_DEPRECATED.include? id
Inspec.deprecate(:core_resource_moved_to_rp, "The resource '#{id}' will not be part of the InSpec 7 core.")
end
require "inspec/resources/#{id}"
rescue LoadError => e
include DeprecatedCloudResourcesList
Expand Down
25 changes: 25 additions & 0 deletions lib/inspec/utils/deprecated_core_resources_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module DeprecatedCoreResourcesList
CORE_RESOURCES_DEPRECATED = %i{
docker_container
docker_image
docker_plugin
docker_service
elasticsearch
ibmdb2_conf
ibmdb2_session
mongodb
mongodb_conf
mongodb_session
podman
podman_container
podman_image
podman_network
podman_pod
podman_volume
rabbitmq_config
ssh_config
ssh_key
sybase_conf
sybase_session
}.freeze
end
3 changes: 2 additions & 1 deletion lib/inspec/utils/deprecation/deprecator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def assemble_message(message, group, opts)

suffix += (" (used at " + opts[:used_at_stack_frame].path + ":" + opts[:used_at_stack_frame].lineno.to_s + ")") if opts.key?(:used_at_stack_frame)

"DEPRECATION: " + prefix + message + suffix
keyword = group.name.to_s == "core_resource_moved_to_rp" ? "CHANGE NOTICE: " : "DEPRECATION: "
keyword + prefix + message + suffix
end

def called_from_control?
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/profiles/deprecated-core-resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# InSpec Profile for Deprecated Resources

This example demonstrates the implementation of an InSpec profile that includes deprecated resource.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# copyright: 2018, The Authors

title "sample section"

control "mongodb_conf" do
impact 1.0
title "Verifies the configuration of MongoDB"
desc "Verifies the configuration of MongoDB"

describe mongodb_conf("./test/fixtures/files/mongod.conf") do
its(%w{storage dbPath}) { should eq "/var/lib/mongodb" }
end
end
10 changes: 10 additions & 0 deletions test/fixtures/profiles/deprecated-core-resources/inspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: deprecated-core-resources
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: [email protected]
license: Apache-2.0
summary: An InSpec Compliance Profile
version: 0.1.0
supports:
platform: os
4 changes: 4 additions & 0 deletions test/functional/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def stderr_ignore_deprecations(result)
stderr.split("\n").reject { |l| l.include? " DEPRECATION: " }.join("\n") + suffix
end

def ignore_resource_pack_warning(output)
output.split("\n").reject { |l| l.include? " CHANGE NOTICE: " }.join("\n")
end

def assert_json_controls_passing(_result = nil) # dummy arg
# Strategy: assemble an array of tests that failed or skipped, and insist it is empty
# @json['profiles'][0]['controls'][0]['results'][0]['status']
Expand Down
2 changes: 1 addition & 1 deletion test/functional/inspec_exec_junit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
describe "when running the v2 junit reporter" do
let(:schema) { Nokogiri::XML::Schema(File.read("#{repo_path}/lib/plugins/inspec-reporter-junit/test/fixtures/schema/JUnit-162a883.xsd")) }
let(:run_result) { run_inspec_process("exec #{profile_name} --reporter junit2") }
let(:doc) { Nokogiri::XML(run_result.stdout) }
let(:doc) { Nokogiri::XML(ignore_resource_pack_warning(run_result.stdout)) }

describe "when running a basic profile" do
let(:profile_name) { complete_profile }
Expand Down
13 changes: 12 additions & 1 deletion test/functional/inspec_exec_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ def stderr
let(:controls) { json["profiles"][0]["controls"] }

it "completes the run with failed controls but no exception" do
_(stderr).must_be_empty
_(ignore_resource_pack_warning(stderr)).must_be_empty

_(controls.count).must_equal 10
_(controls.select { |c| c["results"][0]["status"] == "failed" }.count).must_be :>, 1
Expand Down Expand Up @@ -1604,4 +1604,15 @@ def stderr
end
end
end

describe "when running a profile with a core resource that will be deprecated in InSpec 7" do
let(:out) { inspec("exec " + File.join(profile_path, "deprecated-core-resources") + " --no-create-lockfile") }
it "issues a change notice warning and runs the profile successfully" do
_(stdout).must_include "WARN: CHANGE NOTICE:"
_(stdout).must_include "will not be part of the InSpec 7 core."
_(stdout).must_include "This resource will be moved to a separate resource pack."
_(stdout).must_include "Additional details will be provided with the InSpec 7 release."
assert_exit_code 0, out
end
end
end

0 comments on commit 21dc0a7

Please sign in to comment.