Skip to content

Commit

Permalink
Update control file during self-update
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Feb 20, 2020
1 parent d8a4a38 commit 12f2384
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/lib/installation/updates_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class CouldNotFetchUpdateFromRepo < RepoError; end
# Repo is unreachable (name solving issues, etc.).
class CouldNotProbeRepo < RepoError; end

# The control file was not updated due to some problem
class CouldNotUpdateControl < RepoError; end

DRIVER_UPDATES_PATHS = [Pathname("/update"), Pathname("/download")].freeze

# Constructor
Expand Down Expand Up @@ -110,14 +113,30 @@ def add_repository(uri)
# @see Installation::UpdateRepository#apply
# @see Installation::DriverUpdate#apply
# @see #repositories
# @raise CouldNotUpdateControl
def apply_all
(repositories + driver_updates).each(&:apply)
repositories.each(&:cleanup)
replace_control_file
end

# Determines whether the manager has repositories with updates
def repositories?
!repositories.empty?
end

private

NEW_CONTROL_FILE_PATH = "/usr/lib/skelcd/CD1/control.xml"
APPLY_CMD = "/sbin/adddir %<source>s /".freeze

# Replaces the control file with the the updated one (if it exists)
def replace_control_file
return unless File.exist?(NEW_CONTROL_FILE_PATH)
cmd = format(APPLY_CMD, source: File.dirname(NEW_CONTROL_FILE_PATH))
out = Yast::SCR.Execute(Yast::Path.new(".target.bash_output"), cmd)
log.info("Updating control.xml file in inst-sys '#{cmd}': #{out}")
raise CouldNotUpdateControl unless out["exit"].zero?
end
end
end
27 changes: 27 additions & 0 deletions test/updates_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@
end

describe "#apply_all" do
let(:new_control_file?) { false }

before do
allow(manager).to receive(:repositories).and_return([repo0, repo1])
allow(File).to receive(:exist?).with("/usr/lib/skelcd/CD1/control.xml")
.and_return(new_control_file?)
end

it "applies all the updates" do
Expand All @@ -124,6 +128,29 @@
manager.apply_all
end
end

context "when a new control file is available" do
let(:new_control_file?) { true }
let(:exit_code) { 0 }

before do
allow(Yast::SCR).to receive(:Execute).and_return({"exit" => exit_code})
end

it "updates the control file if needed" do
expect(Yast::SCR).to receive(:Execute)
.with(Yast::Path.new(".target.bash_output"), "/sbin/adddir /usr/lib/skelcd/CD1 /")
manager.apply_all
end

context "and updating the control file fails" do
let(:exit_code) { 1 }

it "raises an exception" do
expect { manager.apply_all }.to raise_error(Installation::UpdatesManager::CouldNotUpdateControl)
end
end
end
end

describe "#repositories?" do
Expand Down

0 comments on commit 12f2384

Please sign in to comment.