Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions lib/linux_admin/registration_system/rhn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,65 @@ def register(options)
run!(cmd, :params => params)
end

def subscribe(options)
raise ArgumentError, "channels, username and password are required" if options[:channels].blank? || options[:username].blank? || options[:password].blank?
cmd = "rhn-channel -a"
def enable_channel(repo, options)
cmd = "rhn-channel -a"
params = user_pwd(options).merge("--channel=" => repo)

channels = options[:channels].collect {|channel| ["--channel=", channel]}
run!(cmd, :params => params)
end
alias_method :subscribe, :enable_channel
alias_method :enable_repo, :enable_channel

params = {}
params["--user="] = options[:username]
params["--password="] = options[:password]
params = params.to_a + channels
def disable_channel(repo, options)
cmd = "rhn-channel -r"
params = user_pwd(options).merge("--channel=" => repo)

run!(cmd, :params => params)
end
alias_method :disable_repo, :disable_channel

def subscribed_products
def enabled_channels
cmd = "rhn-channel -l"

run!(cmd).output.split("\n").compact
end
alias_method :enabled_repos, :enabled_channels
alias_method :subscribed_products, :enabled_channels

def available_channels(options)
cmd = "rhn-channel -L"
params = user_pwd(options)

run!(cmd, :params => params).output.chomp.split("\n").compact
end

def all_repos(options)
available = available_channels_with_status(options)
merge_enabled_channels_with_status(available)
end

private

def available_channels_with_status(options)
available_channels(options).collect { |ac| {:repo_id => ac, :enabled => false} }
end

def merge_enabled_channels_with_status(available)
enabled_channels.each_with_object(available) do |enabled, all|
if repo = all.detect { |i| i[:repo_id] == enabled }
repo[:enabled] = true
else
all.push({:repo_id => enabled, :enabled => true})
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then this can be a method that modifies that hash as a parameter...like enable_channels_in_hash or something.

end

def user_pwd(options)
raise ArgumentError, "username and password are required" if options[:username].blank? || options[:password].blank?

{"--user=" => options[:username], "--password=" => options[:password]}
end

def systemid_file
"/etc/sysconfig/rhn/systemid"
end
Expand Down
26 changes: 26 additions & 0 deletions lib/linux_admin/registration_system/subscription_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ def available_subscriptions
parse_output(output).index_by {|i| i[:pool_id]}
end

def enable_repo(repo, options = nil)
cmd = "subscription-manager repos"
params = {"--enable=" => repo}

run!(cmd, :params => params)
end

def disable_repo(repo, options = nil)
cmd = "subscription-manager repos"
params = {"--disable=" => repo}

run!(cmd, :params => params)
end

def all_repos(options = nil)
cmd = "subscription-manager repos"
output = run!(cmd).output

parse_output(output)
end

def enabled_repos
all_repos.select { |i| i[:enabled] }.collect { |r| r[:repo_id] }
end

private

def parse_output(output)
Expand All @@ -101,6 +126,7 @@ def parse_content(content)
end

def format_values(content_group)
content_group[:enabled] = content_group[:enabled].to_i == 1 if content_group[:enabled]
content_group[:ends] = Date.strptime(content_group[:ends], "%m/%d/%Y") if content_group[:ends]
content_group[:starts] = Date.strptime(content_group[:starts], "%m/%d/%Y") if content_group[:starts]
content_group
Expand Down
4 changes: 4 additions & 0 deletions spec/data/rhn/output_rhn-channel_list_available
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rhel-x86_64-server-6-cf-me-2
rhel-x86_64-server-6-cf-me-2-beta
rhel-x86_64-server-6-cf-me-3
rhel-x86_64-server-6-cf-me-3-beta
18 changes: 18 additions & 0 deletions spec/data/subscription_manager/output_repos
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
+----------------------------------------------------------+
Available Repositories in /etc/yum.repos.d/redhat.repo
+----------------------------------------------------------+
Repo ID: some-repo-source-rpms
Repo Name: Some Repo (Source RPMs)
Repo URL: https://my.host.example.com/repos/some-repo/source/rpms
Enabled: 1

Repo ID: some-repo-rpms
Repo Name: Some Repo
Repo URL: https://my.host.example.com/repos/some-repo/rpms
Enabled: 1

Repo ID: some-repo-2-beta-rpms
Repo Name: Some Repo (Beta RPMs)
Repo URL: https://my.host.example.com/repos/some-repo-2/beta/rpms
Enabled: 0

71 changes: 56 additions & 15 deletions spec/rhn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,64 @@
end
end

context "#subscribe" do
it "without arguments" do
expect { described_class.new.subscribe({}) }.to raise_error(ArgumentError)
end
it "#enable_channel" do
described_class.any_instance.should_receive(:run!).once.with("rhn-channel -a", {:params=>{"--user="=>"SomeUser", "--password="=>"SomePass", "--channel="=>123}})

it "with channels" do
described_class.any_instance.should_receive(:run!).once.with("rhn-channel -a", {:params=>[["--user=", "SomeUser"], ["--password=", "SomePass"], ["--channel=", 123], ["--channel=", 456]]})
described_class.new.subscribe({
:username => "SomeUser",
:password => "SomePass",
:channels => [123, 456]
})
end
described_class.new.enable_channel(123, :username => "SomeUser", :password => "SomePass")
end

it "#subscribed_products" do
it "#enabled_channels" do
described_class.any_instance.should_receive(:run!).once.with("rhn-channel -l").and_return(double(:output => sample_output("rhn/output_rhn-channel_list")))
expect(described_class.new.subscribed_products).to eq(["rhel-x86_64-server-6", "rhel-x86_64-server-6-cf-me-2"])

expect(described_class.new.enabled_channels).to eq(["rhel-x86_64-server-6", "rhel-x86_64-server-6-cf-me-2"])
end

it "#disable_channel" do
described_class.any_instance.should_receive(:run!).once.with("rhn-channel -r", {:params=>{"--user="=>"SomeUser", "--password="=>"SomePass", "--channel="=>123}})

described_class.new.disable_channel(123, :username => "SomeUser", :password => "SomePass")
end

it "#available_channels" do
credentials = {
:username => "some_user",
:password => "password"
}
expected = [
"rhel-x86_64-server-6-cf-me-2",
"rhel-x86_64-server-6-cf-me-2-beta",
"rhel-x86_64-server-6-cf-me-3",
"rhel-x86_64-server-6-cf-me-3-beta"
]
cmd = "rhn-channel -L"
params = {
:params => {
"--user=" => "some_user",
"--password=" => "password"
}
}

described_class.any_instance.should_receive(:run!).once.with(cmd, params).and_return(double(:output => sample_output("rhn/output_rhn-channel_list_available")))

expect(described_class.new.available_channels(credentials)).to eq(expected)
end

it "#all_repos" do
credentials = {
:username => "some_user",
:password => "password"
}
expected = [
{:repo_id => "rhel-x86_64-server-6-cf-me-2", :enabled => true},
{:repo_id => "rhel-x86_64-server-6-cf-me-2-beta", :enabled => false},
{:repo_id => "rhel-x86_64-server-6-cf-me-3", :enabled => false},
{:repo_id => "rhel-x86_64-server-6-cf-me-3-beta", :enabled => false},
{:repo_id => "rhel-x86_64-server-6", :enabled => true}
]

described_class.any_instance.should_receive(:run!).once.and_return(double(:output => sample_output("rhn/output_rhn-channel_list_available")))
described_class.any_instance.should_receive(:run!).once.with("rhn-channel -l").and_return(double(:output => sample_output("rhn/output_rhn-channel_list")))

expect(described_class.new.all_repos(credentials)).to eq(expected)
end
end
end
47 changes: 47 additions & 0 deletions spec/subscription_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,51 @@
expect { described_class.new.organizations({:username=>"BadUser", :password=>"BadPass"}) }.to raise_error(LinuxAdmin::CredentialError)
end
end

it "#enable_repo" do
described_class.any_instance.should_receive(:run!).once.with("subscription-manager repos", {:params=>{"--enable="=>"abc"}})

described_class.new.enable_repo("abc")
end

it "#disable_repo" do
described_class.any_instance.should_receive(:run!).once.with("subscription-manager repos", {:params=>{"--disable="=>"abc"}})

described_class.new.disable_repo("abc")
end

it "#all_repos" do
expected = [
{
:repo_id => "some-repo-source-rpms",
:repo_name => "Some Repo (Source RPMs)",
:repo_url => "https://my.host.example.com/repos/some-repo/source/rpms",
:enabled => true
},
{
:repo_id => "some-repo-rpms",
:repo_name => "Some Repo",
:repo_url => "https://my.host.example.com/repos/some-repo/rpms",
:enabled => true
},
{
:repo_id => "some-repo-2-beta-rpms",
:repo_name => "Some Repo (Beta RPMs)",
:repo_url => "https://my.host.example.com/repos/some-repo-2/beta/rpms",
:enabled => false
}
]

described_class.any_instance.should_receive(:run!).once.with("subscription-manager repos").and_return(double(:output => sample_output("subscription_manager/output_repos")))

expect(described_class.new.all_repos).to eq(expected)
end

it "#enabled_repos" do
expected = ["some-repo-source-rpms", "some-repo-rpms"]

described_class.any_instance.should_receive(:run!).once.with("subscription-manager repos").and_return(double(:output => sample_output("subscription_manager/output_repos")))

expect(described_class.new.enabled_repos).to eq(expected)
end
end