summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Barnett <jason.w.barnett@gmail.com>2021-05-06 14:00:09 -0600
committerTim Smith <tsmith84@gmail.com>2021-09-17 10:18:31 -0700
commit973aa660698515683c8bb0050a31f009c9da7ee4 (patch)
tree2b0f8539c39846d6ab51b0d796504aedc9bd3943
parent22c77935d12567af1627202b8b26a52936bd3a58 (diff)
downloadchef-973aa660698515683c8bb0050a31f009c9da7ee4.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 15a4822ecd..14375fb152 100644
--- a/lib/chef/resource/rhsm_subscription.rb
+++ b/lib/chef/resource/rhsm_subscription.rb
@@ -34,11 +34,11 @@ class Chef
action :attach do
description "Attach the node to a subscription pool."
- 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" }