Skip to content

Commit

Permalink
Do not crash when the network config has not been read
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Feb 27, 2020
1 parent 6d9008e commit c96cf88
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 72 deletions.
74 changes: 2 additions & 72 deletions src/clients/inst_lan.rb
@@ -1,73 +1,3 @@
# ***************************************************************************
#
# Copyright (c) 2012 Novell, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#
# **************************************************************************
# File: clients/lan.ycp
# Package: Network configuration
# Summary: Network cards main file
# Authors: Michal Svec <msvec@suse.cz>
#
#
# Main file for network card configuration.
# Uses all other files.
module Yast
class InstLanClient < Client
include Logger
require "network/clients/inst_lan"

def main
Yast.import "UI"
Yast.import "Lan"
Yast.import "GetInstArgs"
Yast.import "NetworkService"

Yast.include self, "network/lan/wizards.rb"

textdomain "network"

log.info("----------------------------------------")
log.info("Lan module started")

manual_conf_request = GetInstArgs.argmap["skip_detection"] || false
log.info("Lan module forces manual configuration: #{manual_conf_request}")

# keep network configuration state in @@conf_net to gurantee same
# behavior when walking :back in installation workflow
if !defined?(@@network_configured)
@@network_configured =
NetworkService.network_manager? ? true : !Lan.yast_config.connections.empty?
end

log.info("Configured network found: #{@@network_configured}")

ret = if @@network_configured && !manual_conf_request
GetInstArgs.going_back ? :back : :next
else
LanSequence()
end

log.info("Lan module finished, ret = #{ret}")
log.info("----------------------------------------")

ret
end
end

Yast::InstLanClient.new.main
end
Yast::InstLanClient.new.main
91 changes: 91 additions & 0 deletions src/lib/network/clients/inst_lan.rb
@@ -0,0 +1,91 @@
# Copyright (c) [2020] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast"

Yast.import "UI"
Yast.import "Lan"
Yast.import "GetInstArgs"
Yast.import "NetworkService"

module Yast
class InstLanClient < Client
include Logger

class << self
attr_accessor :configured
end

def initialize
textdomain "network"

Yast.include self, "network/lan/wizards.rb"
end

def main
log.info("----------------------------------------")
log.info("Lan module started")

manual_conf_request = GetInstArgs.argmap["skip_detection"] || false
log.info("Lan module forces manual configuration: #{manual_conf_request}")

log.info("Configured network found: #{network_configured?}")

ret = if network_configured? && !manual_conf_request
GetInstArgs.going_back ? :back : :next
else
LanSequence()
end

log.info("Lan module finished, ret = #{ret}")
log.info("----------------------------------------")

ret
end

private

# Convenience method that checks whether there is some connection
# configuration present in the system
#
# @return [Boolean] true when there is some connection present in yast
# config; false otherwise
def connections_configured?
# TODO: Shall we read the configuration in this case?
!(Lan.yast_config&.connections || []).empty?
end

# It returns whether the network has been configured or not. It returns
# true in case NetworkManager is in use, otherwise returns whehter there is
# some connection configured
#
# @see connections_configured?
def network_configured?
# keep network configuration state in to gurantee same behavior when
# walking :back in installation workflow
return self.class.configured unless self.class.configured.nil?

self.class.configured = NetworkService.network_manager? ? true : connections_configured?
end

def reset_config_state
self.class.configured = nil
end
end
end
109 changes: 109 additions & 0 deletions test/inst_lan_test.rb
@@ -0,0 +1,109 @@
#!/usr/bin/env rspec

# Copyright (c) [2020] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "test_helper"

require "yast"
require "network/clients/inst_lan"

describe Yast::InstLanClient do
describe "#main" do
let(:argmap) { { "skip_detection" => force_config } }
let(:force_config) { false }
let(:config) { nil }
let(:going_back) { false }
let(:using_nm) { false }
let(:connections) { Y2Network::ConnectionConfigsCollection.new([]) }
let(:fake_conn) { instance_double("Y2Network::ConnectionConfig") }

before do
allow(Yast::GetInstArgs).to receive(:argmap).and_return(argmap)
allow(Yast::Lan).to receive(:yast_config).and_return(config)
allow(Yast::GetInstArgs).to receive(:going_back).and_return(going_back)
allow(Yast::NetworkService).to receive(:network_manager?).and_return(using_nm)
subject.send(:reset_config_state)
end

context "when the network was already configured by the client" do
before do
allow(subject).to receive(:network_configured?).and_return(true)
end

context "but a manual configuration is forced" do
let(:force_config) { true }

it "runs the network configuration sequence" do
expect(subject).to receive(:LanSequence)
subject.main
end
end

context "and a manual configuration is not forced" do
it "does not run the network configuration sequence" do
expect(subject).to_not receive(:LanSequence)

subject.main
end

context "and going back" do
let(:going_back) { true }

it "returns :back" do
expect(subject.main).to eq(:back)
end
end

it "returns :next" do
expect(subject.main).to eq(:next)
end
end
end

context "when the NetworkService is NetworkManager" do
let(:using_nm) { true }

it "does not run the network configuration sequence" do
expect(subject).to_not receive(:LanSequence)
subject.main
end
end

context "when the NetworkService is wicked" do
context "and there is some connection config already present in yast" do
let(:connections) { Y2Network::ConnectionConfigsCollection.new([fake_conn]) }
let(:config) { instance_double("Y2Network::Config", connections: connections) }

it "does not run the network configuration sequence" do
expect(subject).to_not receive(:LanSequence)

subject.main
end
end

context "and the network is unconfigured" do
it "runs the network configuration sequence" do
expect(subject).to receive(:LanSequence)
subject.main
end
end
end
end
end

0 comments on commit c96cf88

Please sign in to comment.