summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Barnett <jason.w.barnett@gmail.com>2021-05-06 14:00:09 -0600
committerJason Barnett <jason.w.barnett@gmail.com>2021-08-11 16:14:03 -0600
commitdc275631251323d9ab82dfd65347738ef25c2e20 (patch)
tree3ccc70feaad38337b10500848461040e9003812e
parenta9a073b0a3e5bc9f73ac57e95393c0060cbe7809 (diff)
downloadchef-dc275631251323d9ab82dfd65347738ef25c2e20.tar.gz
Flush package cache after attaching to rhsm subscription
Signed-off-by: Jason Barnett <jason.w.barnett@gmail.com>
-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" }