Skip to content

Commit

Permalink
Added unit test and rubocop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed May 31, 2021
1 parent a9e2ff1 commit f6ddd5b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
@@ -1,6 +1,6 @@
# use the shared Yast defaults
inherit_from:
/usr/share/YaST2/data/devtools/data/rubocop_yast_style.yml
/usr/share/YaST2/data/devtools/data/rubocop-0.71.0_yast_style.yml

# Offense count: 153
Metrics/AbcSize:
Expand Down Expand Up @@ -64,3 +64,6 @@ Style/VariableName:
- src/lib/**/*.rb
Exclude:
- src/lib/installation/clients/*.rb
# this needs more testing if we can have frozen string literals
Style/FrozenStringLiteralComment:
Enabled: false
17 changes: 5 additions & 12 deletions src/lib/installation/select_system_role.rb
@@ -1,4 +1,3 @@
# coding: utf-8
# Copyright (c) 2016 SUSE LLC.
# All Rights Reserved.

Expand Down Expand Up @@ -82,7 +81,7 @@ def help_text

def dialog_content
preselected_role_id = SystemRole.current
preselected_role_id ||= roles.first && roles.first.id if SystemRole.default?
preselected_role_id ||= roles.first&.id if SystemRole.default?

VBox(
Left(Label(Yast::ProductControl.GetTranslatedText("roles_text"))),
Expand Down Expand Up @@ -215,7 +214,7 @@ def run_clients(clients, going_back: false)
break unless (0..(clients.size - 1)).cover?(client_to_show)
end

client_to_show >= clients.size ? :next : :back
(client_to_show >= clients.size) ? :next : :back
end

def clear_role
Expand All @@ -229,7 +228,7 @@ def apply_role(role_id)
role = SystemRole.select(role_id)
role.overlay_features
adapt_services(role)
adapt_network_defaults(role)
role.adapt_network

select_packages
end
Expand Down Expand Up @@ -261,6 +260,8 @@ def select_packages

# for given role sets in {::Installation::Services} list of services to enable
# according to its config. Do not use alone and use apply_role instead.
#
# FIXME: duplicate code?
def adapt_services(role)
services = role["services"] || []

Expand All @@ -270,14 +271,6 @@ def adapt_services(role)
Installation::Services.enabled = to_enable
end

def adapt_network_defaults(role)
settings = Y2Network::ProposalSettings.instance

settings.modify_defaults # Load network global section defaults first
settings.modify_defaults(role["network"])
settings.apply_defaults
end

# Return the list of defined roles
#
# @param [Boolean] refresh Refresh system roles cache
Expand Down
17 changes: 11 additions & 6 deletions src/lib/installation/system_role.rb
@@ -1,5 +1,3 @@
# encoding: utf-8

# ------------------------------------------------------------------------------
# Copyright (c) 2017 SUSE LLC
#
Expand All @@ -22,6 +20,7 @@
require "yast"
require "installation/services"
require "installation/system_role_handlers_runner"
require "y2network/proposal_settings"

Yast.import "ProductControl"
Yast.import "ProductFeatures"
Expand Down Expand Up @@ -166,7 +165,7 @@ def from_control(raw_role)

role["additional_dialogs"] = raw_role["additional_dialogs"]
role["services"] = raw_role["services"] || []
role["network"] = raw_role["network"]
role["network"] = raw_role["network"] || {}
role["no_default"] = raw_role["no_default"] || false

role
Expand All @@ -186,9 +185,7 @@ def finish
#
# @return [Array] the list of services to be enable
def adapt_services
return [] if !self["services"]

to_enable = self["services"].map { |s| s["name"] }
to_enable = (self["services"] || []).map { |s| s["name"] }

log.info "enable for #{id} these services: #{to_enable.inspect}"

Expand All @@ -213,5 +210,13 @@ def overlay_features
NON_OVERLAY_ATTRIBUTES.each { |a| features.delete(a) }
Yast::ProductFeatures.SetOverlay(features)
end

def adapt_network
settings = Y2Network::ProposalSettings.instance
network = Yast::ProductFeatures.GetSection("network")

settings.modify_defaults(network.merge(self["network"] || {}))
settings.apply_defaults
end
end
end
40 changes: 35 additions & 5 deletions test/system_role_test.rb
Expand Up @@ -3,6 +3,7 @@
require_relative "./test_helper"
require "installation/services"
require "installation/system_role"
require "y2network/proposal_settings"

describe Installation::SystemRole do
let(:system_roles) do
Expand All @@ -17,7 +18,13 @@
"id" => "role_two",
"services" => [{ "name" => "service_one" }, { "name" => "service_two" }],
"order" => "100"
},
{
"id" => "role_three",
"network" => { "ipv4_forwarding" => true, "ipv6_forwarding" => true },
"order" => "50"
}

]
end

Expand All @@ -30,26 +37,27 @@
it "returns the roles from the control file" do
raw_roles = described_class.raw_roles

expect(raw_roles.size).to eql 2
expect(raw_roles.size).to eql 3
expect(raw_roles.first["id"]).to eql "role_one"
end
end

describe ".ids" do
it "returns a list with all the role ids declared in the control file" do
expect(described_class.ids).to match_array(["role_one", "role_two"])
expect(described_class.ids).to match_array(["role_one", "role_two", "role_three"])
end
end

describe ".all" do
it "returns an array of SystemRole objects for all the declared roles " do
expect(described_class.all.size).to eql(2)
expect(described_class.all.size).to eql(3)
expect(described_class.all.last.class).to eql(described_class)
end

it "returns array sorted by order" do
expect(described_class.all.first.id).to eql("role_two")
expect(described_class.all[1].id).to eql("role_one")
expect(described_class.all.first.id).to eql("role_three")
expect(described_class.all[1].id).to eql("role_two")
expect(described_class.all[2].id).to eql("role_one")
end
end

Expand Down Expand Up @@ -127,6 +135,28 @@
end
end

describe "#adapt_network" do
let(:settings) { Y2Network::ProposalSettings.instance }
let(:role) { described_class.find("role_three") }
before do
allow(Yast::ProductFeatures).to receive(:GetSection)
allow(Yast::ProductFeatures).to receive(:GetSection).with("network")
.and_return("ipv6_forwarding" => false)
end

it "modifies the network proposal settings with defaults from the control file" do
expect(settings).to receive(:modify_defaults)
.with("ipv4_forwarding" => true, "ipv6_forwarding" => true)

role.adapt_network
end

it "applies the network proposal settings defaults" do
expect(settings).to receive(:apply_defaults)
role.adapt_network
end
end

describe "#overlay_features" do
it "overlays the product features with the ones defined in the control file for this role" do
role = described_class.find("role_one")
Expand Down

0 comments on commit f6ddd5b

Please sign in to comment.