Skip to content

Commit

Permalink
Merge 7c987e1 into 50e0d4c
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Dec 19, 2019
2 parents 50e0d4c + 7c987e1 commit 7b5743d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
29 changes: 21 additions & 8 deletions library/system/src/lib/yast2/fs_snapshot_store.rb
Expand Up @@ -22,6 +22,8 @@

require "yast"

Yast.import "FileUtils"

module Yast2
# Goal of this module is to provide easy to use api to store id of pre
# snapshots, so post snapshots can be then easy to make.
Expand All @@ -30,6 +32,7 @@ module FsSnapshotStore
# @param[String] purpose of snapshot like "upgrade"
# @raise[RuntimeError] if writing to file failed
def self.save(purpose, snapshot_id)
ensure_snapshot_store_path
result = Yast::SCR.Write(
Yast::Path.new(".target.string"),
snapshot_path(purpose),
Expand Down Expand Up @@ -59,18 +62,28 @@ def self.clean(purpose)
Yast::SCR.Execute(Yast::Path.new(".target.remove"), snapshot_path(purpose))
end

STORE_PATH = "/var/lib/YaST2".freeze
def self.snapshot_store_path
Yast.import "Stage"
return STORE_PATH unless Yast::Stage.initial || !Yast::WFM.scr_chrooted?

Yast.import "Installation"
::File.join(Yast::Installation.destdir, STORE_PATH)
end
private_class_method :snapshot_store_path

# Path where is stored given purpose
def self.snapshot_path(purpose)
path = "/var/lib/YaST2/pre_snapshot_#{purpose}.id"
File.join(snapshot_store_path, "pre_snapshot_#{purpose}.id")
end
private_class_method :snapshot_path

Yast.import "Stage"
if Yast::Stage.initial && !Yast::WFM.scr_chrooted?
Yast.import "Installation"
path = ::File.join(Yast::Installation.destdir, path)
end
# Ensures that the snapshots base path exists
def self.ensure_snapshot_store_path
return if Yast::FileUtils.Exists(snapshot_store_path)

path
Yast::SCR.Execute(Yast::Path.new(".target.mkdir"), snapshot_store_path)
end
private_class_method :snapshot_path
private_class_method :ensure_snapshot_store_path
end
end
27 changes: 21 additions & 6 deletions library/system/test/fs_snapshot_store_test.rb
@@ -1,18 +1,28 @@
#!/usr/bin/env rspec

require_relative "test_helper"
require "tmpdir"
require "fileutils"
require "yast2/fs_snapshot_store"

describe Yast2::FsSnapshotStore do
describe ".save" do
it "stores snapshot id to file identified by purpose" do
expect(Yast::SCR).to receive(:Write).with(
path(".target.string"),
"/var/lib/YaST2/pre_snapshot_test.id",
"42"
).and_return(true)
let(:root_dir) { Dir.mktmpdir }

before do
allow(Yast::SCR).to receive(:Write).and_call_original
end

around do |example|
change_scr_root(root_dir, &example)
ensure
::FileUtils.remove_entry(root_dir) if Dir.exist?(root_dir)
end

it "stores snapshot id to file identified by purpose" do
described_class.save("test", 42)
snapshot_id_file = File.join(root_dir, "/var/lib/YaST2/pre_snapshot_test.id")
expect(File.read(snapshot_id_file)).to eq("42")
end

it "raises exception if writing failed" do
Expand All @@ -24,6 +34,11 @@

expect { described_class.save("test", 42) }.to raise_error(/Failed to write/)
end

it "ensures that the data directory exists" do
described_class.save("test", 42)
expect(Dir).to exist(File.join(root_dir, "/var/lib/YaST2"))
end
end

describe ".load" do
Expand Down

0 comments on commit 7b5743d

Please sign in to comment.