diff --git a/library/system/src/lib/yast2/fs_snapshot_store.rb b/library/system/src/lib/yast2/fs_snapshot_store.rb index b14871f94..9b6462e35 100644 --- a/library/system/src/lib/yast2/fs_snapshot_store.rb +++ b/library/system/src/lib/yast2/fs_snapshot_store.rb @@ -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. @@ -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), @@ -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 diff --git a/library/system/test/fs_snapshot_store_test.rb b/library/system/test/fs_snapshot_store_test.rb index c8d47078c..8b65ef207 100755 --- a/library/system/test/fs_snapshot_store_test.rb +++ b/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 @@ -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