Skip to content

Commit

Permalink
Moved efi detection to a module for sharing code
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Nov 19, 2021
1 parent ffa1786 commit 19a2907
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 36 deletions.
22 changes: 22 additions & 0 deletions src/lib/autoinstall/common_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Yast.import "Linuxrc"
Yast.import "Stage"

module Y2Autoinstallation
# This module defines some methods that are used by different classes
module CommonHelpers
# Use same approach than linuxrc for detecting the EFI boot in a running system but use
# install.inf in case of initial Stage.
EFI_VARS_DIRS = ["/sys/firmware/efi/efivars", "/sys/firmware/efi/vars/"].freeze

# Whether the system was booted using UEFI or not
#
# @return [Boolean] whether the system was booted using UEFI or not according to linuxrc
def boot_efi?
if Yast::Stage.initial
Yast::Linuxrc.InstallInf("EFI") == "1"
else
EFI_VARS_DIRS.any? { |d| Dir.exist?(d) }
end
end
end
end
9 changes: 2 additions & 7 deletions src/lib/autoinstall/y2erb.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "yast"
require "erb"
require "autoinstall/common_helpers"

module Y2Autoinstallation
class Y2ERB
Expand All @@ -11,13 +12,7 @@ def self.render(path)

class TemplateEnvironment
include Yast::Logger

# @return [Boolean] whether the system was booted using UEFI or not according to linuxrc
def efi?
Yast.import "Linuxrc"

Yast::Linuxrc.InstallInf("EFI") == "1"
end
include Y2Autoinstallation::CommonHelpers

def hardware
@hardware ||= Yast::SCR.Read(Yast::Path.new(".probe"))
Expand Down
10 changes: 3 additions & 7 deletions src/modules/AutoInstallRules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
# $Id$
require "yast"
require "autoinstall/xml_checks"
require "autoinstall/common_helpers"
require "yast2/popup"
require "y2storage"

module Yast
class AutoInstallRulesClass < Module
include Yast::Logger
include Y2Autoinstallation::CommonHelpers

def main
Yast.import "UI"
Expand Down Expand Up @@ -141,13 +143,6 @@ def StdErrLog(stderr)
nil
end

# Returns whether the system was booted using UEFI or not
#
# @return [Boolean] true when the system is booted using EFI
def boot_efi?
Yast::Linuxrc.InstallInf("EFI") == "1"
end

# getMAC()
# Return MAC address of active device
# @return [String] mac address
Expand Down Expand Up @@ -1134,6 +1129,7 @@ def hostaddress
publish variable: :mac, type: "string"
publish variable: :linux, type: "integer"
publish variable: :others, type: "integer"
publish variable: :efi, type: "string"
publish variable: :xserver, type: "string"
publish variable: :NonLinuxPartitions, type: "list"
publish variable: :LinuxPartitions, type: "list"
Expand Down
59 changes: 59 additions & 0 deletions test/lib/common_helpers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require_relative "../test_helper"
require "autoinstall/common_helpers"

describe Y2Autoinstallation::CommonHelpers do
class Dummy
include Y2Autoinstallation::CommonHelpers

def initialize
@name = "Dummy class"
end
end

subject { Dummy.new }

describe "#boot_efi?" do
let(:efi) { true }

context "when called in the initial Stage" do
before do
allow(Yast::Linuxrc).to receive(:InstallInf).with("EFI").and_return(efi)
end

context "and EFI is read as '1' from the Install.inf file" do
it "returns true" do
expect(subject.boot_efi?)
end
end

context "and EFI is not read as '1' from the Install.inf file" do
let(:efi) { false }

it "returns false" do
expect(subject.boot_efi?)
end
end
end

context "when called in normal Mode" do
before do
allow(Dir).to receive(:exist?)
end

described_class.const_get("EFI_VARS_DIRS").each do |dir|
it "returns true if '#{dir}' exists" do
expect(Dir).to receive(:exist?).with(dir).and_return(true)
expect(subject.boot_efi?).to eq(true)
end
end

it "returns false otherwise" do
described_class.const_get("EFI_VARS_DIRS").each do |dir|
allow(Dir).to receive(:exist?).with(dir).and_return(false)
end

expect(subject.boot_efi?).to eq(false)
end
end
end
end
22 changes: 0 additions & 22 deletions test/lib/y2erb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,26 +232,4 @@ def hardware_mock_data
expect(subject.os_release).to be_a(Hash)
end
end

describe "#efi?" do
let(:efi) { "0" }

before do
allow(Yast::Linuxrc).to receive(:InstallInf).with("EFI").and_return(efi)
end

context "when the system is boot using efi" do
let(:efi) { "1" }

it "returns true" do
expect(subject.efi?).to eq(true)
end
end

context "when the system is boot without UEFI" do
it "returns false" do
expect(subject.efi?).to eq(false)
end
end
end
end

0 comments on commit 19a2907

Please sign in to comment.