summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2021-08-16 10:53:18 -0700
committerGitHub <noreply@github.com>2021-08-16 10:53:18 -0700
commit8f4fcb9188da05973f8f2530243e43d42cf33246 (patch)
tree476a8633df5d385fd9ad0e579fb0ada7010d9ce0
parent15541a8a549db0117d52faefd31fec6d76e822ad (diff)
parentdc275631251323d9ab82dfd65347738ef25c2e20 (diff)
downloadchef-8f4fcb9188da05973f8f2530243e43d42cf33246.tar.gz
Merge pull request #11534 from jasonwbarnett/flush-cache-after-subscription-is-added
Flush package cache after attaching to rhsm subscription
-rw-r--r--lib/chef/resource/rhsm_subscription.rb10
-rw-r--r--spec/unit/resource/rhsm_subscription_spec.rb53
2 files changed, 55 insertions, 8 deletions
diff --git a/lib/chef/resource/rhsm_subscription.rb b/lib/chef/resource/rhsm_subscription.rb
index 5ae04bbfcd..d4eb49250b 100644
--- a/lib/chef/resource/rhsm_subscription.rb
+++ b/lib/chef/resource/rhsm_subscription.rb
@@ -32,11 +32,11 @@ class Chef
name_property: true
action :attach, description: "Attach the node to a subscription pool." do
- execute "Attach subscription pool #{new_resource.pool_id}" do
- command "subscription-manager attach --pool=#{new_resource.pool_id}"
- default_env true
- action :run
- not_if { subscription_attached?(new_resource.pool_id) }
+ unless subscription_attached?(new_resource.pool_id)
+ converge_by("attach subscription pool #{new_resource.pool_id}") do
+ shell_out!("subscription-manager attach --pool=#{new_resource.pool_id}")
+ build_resource(:package, "rhsm_subscription-#{new_resource.pool_id}-flush_cache").run_action(:flush_cache)
+ end
end
end
diff --git a/spec/unit/resource/rhsm_subscription_spec.rb b/spec/unit/resource/rhsm_subscription_spec.rb
index 7f81cec497..a2949dea2c 100644
--- a/spec/unit/resource/rhsm_subscription_spec.rb
+++ b/spec/unit/resource/rhsm_subscription_spec.rb
@@ -18,15 +18,24 @@
require "spec_helper"
describe Chef::Resource::RhsmSubscription do
- let(:resource) { Chef::Resource::RhsmSubscription.new("fakey_fakerton") }
- let(:provider) { resource.provider_for_action(:attach) }
+ let(:event_dispatch) { Chef::EventDispatch::Dispatcher.new }
+ let(:node) { Chef::Node.new }
+ let(:run_context) { Chef::RunContext.new(node, {}, event_dispatch) }
+
+ let(:pool_id) { "8a8dd78c766232550226b46e59404aba" }
+ let(:resource) { Chef::Resource::RhsmSubscription.new(pool_id, run_context) }
+ let(:provider) { resource.provider_for_action(Array(resource.action).first) }
+
+ before do
+ allow(resource).to receive(:provider_for_action).with(:attach).and_return(provider)
+ end
it "has a resource name of :rhsm_subscription" do
expect(resource.resource_name).to eql(:rhsm_subscription)
end
it "the pool_id property is the name_property" do
- expect(resource.pool_id).to eql("fakey_fakerton")
+ expect(resource.pool_id).to eql(pool_id)
end
it "sets the default action as :attach" do
@@ -38,6 +47,44 @@ describe Chef::Resource::RhsmSubscription do
expect { resource.action :remove }.not_to raise_error
end
+ describe "#action_attach" do
+ let(:yum_package_double) { instance_double("Chef::Resource::YumPackage") }
+ let(:so_double) { instance_double("Mixlib::ShellOut", stdout: "Successfully attached a subscription for: My Subscription", exitstatus: 0, error?: false) }
+
+ before do
+ allow(provider).to receive(:shell_out!).with("subscription-manager attach --pool=#{resource.pool_id}").and_return(so_double)
+ allow(provider).to receive(:build_resource).with(:package, "rhsm_subscription-#{pool_id}-flush_cache").and_return(yum_package_double)
+ allow(yum_package_double).to receive(:run_action).with(:flush_cache)
+ end
+
+ context "when already attached to pool" do
+ before do
+ allow(provider).to receive(:subscription_attached?).with(resource.pool_id).and_return(true)
+ end
+
+ it "does not attach to pool" do
+ expect(provider).not_to receive(:shell_out!)
+ resource.run_action(:attach)
+ end
+ end
+
+ context "when not attached to pool" do
+ before do
+ allow(provider).to receive(:subscription_attached?).with(resource.pool_id).and_return(false)
+ end
+
+ it "attaches to pool" do
+ expect(provider).to receive(:shell_out!).with("subscription-manager attach --pool=#{resource.pool_id}")
+ resource.run_action(:attach)
+ end
+
+ it "flushes package provider cache" do
+ expect(yum_package_double).to receive(:run_action).with(:flush_cache)
+ resource.run_action(:attach)
+ end
+ end
+ end
+
describe "#subscription_attached?" do
let(:cmd) { double("cmd") }
let(:output) { "Pool ID: pool123" }