Skip to content

Commit

Permalink
adapt tests and in some case also prepare code
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Oct 26, 2023
1 parent 08598cb commit 3ad4008
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 328 deletions.
6 changes: 4 additions & 2 deletions service/etc/agama.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
include:
- "../conf.d/*.yaml"
web:
ssl: null
ssl_cert: null
ssl_key: null
11 changes: 9 additions & 2 deletions service/lib/agama/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Agama
class Config
# @return [Hash] configuration data
attr_accessor :pure_data
attr_accessor :logger

class << self
attr_accessor :current, :base
Expand Down Expand Up @@ -96,7 +97,7 @@ def pick_product(product_id)
# hash of available base products for current architecture
def products
return @products if @products
products = ProductReader.new(@logger).load_products
products = ProductReader.new(logger: @logger).load_products

products.select! do |product|
product["archs"].nil? ||
Expand All @@ -119,7 +120,13 @@ def multi_product?
#
# @return [Config]
def copy
Marshal.load(Marshal.dump(self))
logger = self.logger
@logger = nil # cannot dump logger as it can contain IO
res = Marshal.load(Marshal.dump(self))
@logger = logger
res.logger = logger

res
end

# Returns a new {Config} with the merge of the given ones
Expand Down
6 changes: 2 additions & 4 deletions service/lib/agama/product_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ class ProductReader
# Constructor
#
# @param logger [Logger]
# @param workdir [String] Root directory to read the configuration from
def initialize(logger: nil, workdir: "/")
def initialize(logger: nil)
@logger = logger || ::Logger.new($stdout)
@workdir = workdir
end

def load_products
glob = File.join(default_path, "*.{yaml,yml}")
Dir.glob(glob).each_with_object do |path, result|
Dir.glob(glob).each_with_object([]) do |path, result|
# support also single product file
products = Array(load_file(path))
result.concat(products)
Expand Down
2 changes: 1 addition & 1 deletion service/lib/agama/security.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def write
end

def probe
selected_lsm = config.data["security"]["lsm"]
selected_lsm = config.data.dig("security", "lsm")
lsm_config.select(selected_lsm)

patterns = if selected_lsm.nil?
Expand Down
2 changes: 1 addition & 1 deletion service/lib/agama/software/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def initialize(config, logger)
@logger = logger
@languages = DEFAULT_LANGUAGES
@products = @config.products
if @config.multi_product?
if @config.multi_product? || @products.empty?
@product = nil
else
@product = @products.keys.first # use the available product as default
Expand Down
4 changes: 2 additions & 2 deletions service/products.d/opensuse.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- id: openSUSE-TW
- id: Tumbleweed
name: openSUSE Tumbleweed
description: 'The Tumbleweed distribution is a pure rolling release version
of openSUSE containing the latest "stable" versions of all software
Expand Down Expand Up @@ -139,7 +139,7 @@
- ext4
- xfs

- id: openSUSE-Leap16
- id: Leap16
name: openSUSE Leap 16.0
archs: x86_64,aarch64
description: '[Experimental project] openSUSE Leap 16 is built on top of the next generation Adaptable Linux Platform (ALP) from SUSE.'
Expand Down
18 changes: 18 additions & 0 deletions service/run_tests_in_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /bin/bash

set -ex
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
podman create -ti --rm --entrypoint '["sh", "-c"]' --name agama_ruby_tests -v $SCRIPT_DIR/..:/checkout registry.opensuse.org/yast/head/containers_tumbleweed/yast-ruby sh
podman start agama_ruby_tests
podman exec agama_ruby_tests zypper --non-interactive install yast2-iscsi-client ruby3.2-rubygem-eventmachine
if podman exec --workdir /checkout/service agama_ruby_tests rake test:unit; then
if [ "$KEEP_RUNNING" != "1" ]; then
podman stop agama_ruby_test
fi
echo "Tests passed"
else
echo "Tests failed"
echo "To get into container use: podman attach agama_ruby_tests"
echo "git checkout is located at /checkout"
echo "To remove container use: podman rm agama_ruby_tests"
fi
2 changes: 1 addition & 1 deletion service/test/agama/config_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
it "returns a Config object with the configuration read from the given file" do
config = subject.config_from_file(File.join(workdir, "etc", "agama.yaml"))
expect(config).to be_a(Agama::Config)
expect(config.data["products"].keys).to include("Tumbleweed")
expect(config.data["web"].keys).to include("ssl")
end
end

Expand Down
32 changes: 21 additions & 11 deletions service/test/agama/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
let(:config) { described_class.new("web" => { "ssl" => "SOMETHING" }) }

before do
allow_any_instance_of(Agama::ConfigReader).to receive(:config).and_return(config)
allow_any_instance_of(Agama::ProductReader).to receive(:load_products).and_return([])
end

describe ".load" do
Expand All @@ -53,12 +53,12 @@
File.join(FIXTURES_PATH, "root_dir", "etc", "agama.yaml")
)
expect(config).to be_a(described_class)
expect(config.data["products"].size).to eq(3)
end
end

describe ".reset" do
it "resets base and current configuration" do
allow_any_instance_of(Agama::ConfigReader).to receive(:config).and_return(config)
described_class.load
expect { described_class.reset }.to change { described_class.base }.from(config).to(nil)
.and change { described_class.current }.to(nil)
Expand Down Expand Up @@ -91,17 +91,25 @@

describe "#products" do
it "returns products available for current hardware" do
subject = described_class.from_file(File.join(FIXTURES_PATH, "agama-archs.yaml"))
expect(subject.products.size).to eq 2
allow_any_instance_of(Agama::ProductReader).to receive(:load_products).and_return([
{
"id" => "test",
"archs" => "x86_64"
},
{
"id" => "test2",
"archs" => "s390x"
}
])
expect(Yast2::ArchFilter).to receive(:from_string).twice.and_return(double(match?: true), double(match?: false))
expect(subject.products.size).to eq 1
end
end

describe "#multi_product?" do
context "when more than one product is defined" do
subject do
described_class.from_file(
File.join(FIXTURES_PATH, "root_dir", "etc", "agama.yaml")
)
before do
allow_any_instance_of(Agama::ProductReader).to receive(:load_products).and_call_original
end

it "returns true" do
Expand All @@ -110,11 +118,13 @@
end

context "when just one product is defined" do
subject do
described_class.from_file(File.join(FIXTURES_PATH, "agama-single.yaml"))
before do
allow_any_instance_of(Agama::ProductReader).to receive(:load_products).and_call_original
products = Agama::ProductReader.new.load_products
allow_any_instance_of(Agama::ProductReader).to receive(:load_products).and_return([products.first])
end

it "returns true" do
it "returns false" do
expect(subject.multi_product?).to eq(false)
end
end
Expand Down
2 changes: 1 addition & 1 deletion service/test/agama/dbus/server_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
describe "#start_server" do
it "starts the dbus-daemon and returns the PID" do
expect(Process).to receive(:spawn)
.with(/dbus-daemon/, "--config-file", /dbus.conf/, any_args)
.with(/dbus-daemon/, "--config-file", any_args) # config file loc depends on pwd
.and_return(1000)
expect(Process).to receive(:detach).with(1000)
expect(subject.start_server).to eq(1000)
Expand Down
21 changes: 9 additions & 12 deletions service/test/agama/software/manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
before do
allow(Yast::Pkg).to receive(:TargetInitialize)
allow(Yast::Pkg).to receive(:ImportGPGKey)
# allow glob to work for other calls
allow(Dir).to receive(:glob).and_call_original
allow(Dir).to receive(:glob).with(/keys/).and_return(gpg_keys)
allow(Yast::Packages).to receive(:Proposal).and_return({})
allow(Yast::InstURL).to receive(:installInf2Url).with("")
Expand All @@ -79,6 +81,7 @@
allow(Agama::DBus::Clients::Questions).to receive(:new).and_return(questions_client)
allow(Agama::Software::RepositoriesManager).to receive(:new).and_return(repositories)
allow(Agama::Software::Proposal).to receive(:new).and_return(proposal)
allow_any_instance_of(Agama::ProductReader).to receive(:load_products).and_call_original
end

describe "#probe" do
Expand Down Expand Up @@ -116,7 +119,7 @@
end

it "registers the repository from config" do
expect(repositories).to receive(:add).with(/tumbleweed/)
expect(repositories).to receive(:add).with(/Dolomite/)
expect(repositories).to receive(:load)
subject.probe
end
Expand All @@ -126,9 +129,8 @@
it "returns the list of known products" do
products = subject.products
expect(products.size).to eq(3)
id, data = products.first
expect(id).to eq("Tumbleweed")
expect(data).to include(
expect(products["Tumbleweed"]).to_not eq nil
expect(products["Tumbleweed"]).to include(
"name" => "openSUSE Tumbleweed",
"description" => String
)
Expand All @@ -152,16 +154,11 @@
expect(proposal).to receive(:set_resolvables)
.with("agama", :pattern, ["enhanced_base"])
expect(proposal).to receive(:set_resolvables)
.with("agama", :pattern, ["optional_base"], optional: true)
.with("agama", :pattern, [], {optional: true})
expect(proposal).to receive(:set_resolvables)
.with("agama", :package, ["mandatory_pkg"])
.with("agama", :package, ["NetworkManager"])
expect(proposal).to receive(:set_resolvables)
.with("agama", :package, ["optional_pkg"], optional: true)
subject.propose

expect(Yast::Arch).to receive(:s390).and_return(true)
expect(proposal).to receive(:set_resolvables)
.with("agama", :package, ["mandatory_pkg", "mandatory_pkg_s390"])
.with("agama", :package, [], {optional: true})
subject.propose
end
end
Expand Down
Loading

0 comments on commit 3ad4008

Please sign in to comment.