Skip to content

Commit

Permalink
refactor auto client and add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Dec 8, 2017
1 parent 805c7df commit 5e388c8
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 133 deletions.
105 changes: 3 additions & 102 deletions src/clients/ntp-client_auto.rb
Original file line number Diff line number Diff line change
@@ -1,105 +1,6 @@
# encoding: utf-8

# File: clients/ntp-client_auto.ycp
# Package: Configuration of ntp-client
# Summary: Client for autoinstallation
# Authors: Jiri Srain <jsrain@suse.cz>
#
# $Id$
#
# This is a client for autoinstallation. It takes its arguments,
# goes through the configuration and return the setting.
# Does not do any changes to the configuration.
require "yast"
require "y2ntp_client/client/auto"

require "fileutils"

module Yast
class NtpClientAutoClient < Client
def main
Yast.import "UI"
Yast.import "Mode"
Yast.import "Report"
Yast.import "Stage"
Yast.import "Installation"

textdomain "ntp-client"

Builtins.y2milestone("----------------------------------------")
Builtins.y2milestone("NtpClient auto started")

Yast.import "NtpClient"
Yast.include self, "ntp-client/wizards.rb"

@ret = nil
@func = ""
@param = {}

# Check arguments
if Ops.greater_than(Builtins.size(WFM.Args), 0) &&
Ops.is_string?(WFM.Args(0))
@func = Convert.to_string(WFM.Args(0))
if Ops.greater_than(Builtins.size(WFM.Args), 1) &&
Ops.is_map?(WFM.Args(1))
@param = Convert.to_map(WFM.Args(1))
end
end
Builtins.y2debug("func=%1", @func)
Builtins.y2debug("param=%1", @param)

# Create a summary
if @func == "Summary"
@ret = NtpClient.Summary
# Reset configuration
elsif @func == "Reset"
NtpClient.Import({})
@ret = {}
# Return required packages
elsif @func == "Packages"
@ret = NtpClient.AutoPackages
# Change configuration (run AutoSequence)
elsif @func == "Change"
NtpClientAutoSequence()
@ret = :next
# Import configuration
elsif @func == "Import"
@ret = NtpClient.Import(@param)
# Return actual state
elsif @func == "Export"
@ret = NtpClient.Export
# did configuration change
elsif @func == "GetModified"
@ret = NtpClient.modified
# set configuration as changed
elsif @func == "SetModified"
NtpClient.modified = true
@ret = true
# Read current state
elsif @func == "Read"
Yast.import "Progress"
@progress_orig = Progress.set(false)
@ret = NtpClient.Read
Progress.set(@progress_orig)
# Write givven settings
elsif @func == "Write"
Yast.import "Progress"
@progress_orig = Progress.set(false)
NtpClient.write_only = true
@ret = NtpClient.Write
Progress.set(@progress_orig)
else
Builtins.y2error("Unknown function: %1", @func)
@ret = false
end

Builtins.y2debug("ret=%1", @ret)
Builtins.y2milestone("NtpClient auto finished")
Builtins.y2milestone("----------------------------------------")

deep_copy(@ret)

# EOF
end
end
end

Yast::NtpClientAutoClient.new.main
Y2NtpClient::Client::Auto.run
31 changes: 0 additions & 31 deletions src/include/ntp-client/wizards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,5 @@ def NtpClientSequence
UI.CloseDialog
ret
end

# Autoyast configuration of ntp-client
# @return sequence result
def NtpClientAutoSequence
aliases = { "main" => lambda { Y2NtpClient::Dialog::Main.run } }

sequence = {
"ws_start" => "main",
"main" => { abort: :abort, next: :next }
}

# dialog caption
caption = _("NTP Client Configuration")
# label
contents = Label(_("Initializing ..."))

Wizard.CreateDialog
Wizard.SetDesktopTitleAndIcon("ntp-client")
Wizard.SetContentsButtons(
caption,
contents,
"",
Label.BackButton,
Label.NextButton
)

ret = Sequencer.Run(aliases, sequence)

UI.CloseDialog
ret
end
end
end
72 changes: 72 additions & 0 deletions src/lib/y2ntp_client/client/auto.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# encoding: utf-8

require "yast"
require "installation/auto_client"

Yast.import "NtpClient"
Yast.import "Progress"

require "y2ntp_client/dialog/main"

module Y2NtpClient
module Client
# This is a client for autoinstallation. It takes its arguments,
# goes through the configuration and return the setting.
# Does not do any changes to the configuration.
class Auto < ::Installation::AutoClient
def initialize
textdomain "ntp-client"
end

def summary
Yast::NtpClient.Summary
end

def import(profile)
Yast::NtpClient.Import(profile)
end

def export
Yast::NtpClient.Export
end

def reset
Yast::NtpClient.Import({})
end

def change
Y2NtpClient::Dialog::Main.run
end

def write
progress_orig = Yast::Progress.set(false)
Yast::NtpClient.write_only = true
ret = Yast::NtpClient.Write
Yast::Progress.set(progress_orig)

ret
end

def read
progress_orig = Yast::Progress.set(false)
Yast::NtpClient.write_only = true
ret = Yast::NtpClient.Read
Yast::Progress.set(progress_orig)

ret
end

def packages
Yast::NtpClient.AutoPackages
end

def modified
Yast::NtpClient.modified = true
end

def modified?
Yast::NtpClient.modified
end
end
end
end
120 changes: 120 additions & 0 deletions test/y2ntp_client/client/auto_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
require_relative "../../test_helper"

require "y2ntp_client/client/auto"

describe Y2NtpClient::Client::Auto do
let(:data_dir) { File.join(File.dirname(__FILE__), "../../data") }

around do |example|
::FileUtils.cp(File.join(data_dir, "scr_root/etc/chrony.conf.original"),
File.join(data_dir, "scr_root/etc/chrony.conf"))
change_scr_root(File.join(data_dir, "scr_root"), &example)
::FileUtils.rm(File.join(data_dir, "scr_root/etc/chrony.conf"))
end

before do
allow(Yast::Service).to receive(:Enabled).with("chronyd").and_return(true)
allow(Yast::NetworkInterfaces).to receive(:Read)
allow(Yast::PackageSystem).to receive(:CheckAndInstallPackagesInteractive)
.with(["chrony"]).and_return(true)
Yast::NtpClient.Read
end

describe "#summary" do
it "returns string" do
expect(subject.summary).to be_a(::String)
end
end

describe "#import" do
it "pass its arguments to NtpClient.Import" do
arguments = {"ntp_policy" => "auto"}
expect(Yast::NtpClient).to receive(:Import).with(arguments)
subject.import(arguments)
end

it "returns true if import success" do
allow(Yast::NtpClient).to receive(:Import).and_return(true)
expect(subject.import({})).to eq true
end

it "returns false if import failed" do
allow(Yast::NtpClient).to receive(:Import).and_return(false)
expect(subject.import({})).to eq false
end
end

describe "#export" do
it "returns hash with options" do
expect(subject.export).to be_a(::Hash)
end
end

describe "#reset" do
it "import empty hash to set defaults" do
expect(Yast::NtpClient).to receive(:Import).with({})
subject.reset
end
end

describe "#change" do
it "opens main dialog" do
expect(Y2NtpClient::Dialog::Main).to receive(:run)
subject.change
end
end

describe "#write" do
before do
allow(Yast::NtpClient).to receive(:Write)
end

it "calls NtpClient write" do
expect(Yast::NtpClient).to receive(:Write)
subject.write
end

it "disables progress" do
expect(Yast::Progress).to receive(:set).with(false).and_return(false).twice
subject.write
end
end

describe "#read" do
before do
allow(Yast::NtpClient).to receive(:Read)
end

it "calls NtpClient read" do
expect(Yast::NtpClient).to receive(:Read)
subject.read
end

it "disables progress" do
expect(Yast::Progress).to receive(:set).with(false).and_return(false).twice
subject.read
end
end

describe "#packages" do
it "returns hash with \"install\" and \"remove\" lists of packagesn" do
expect(subject.packages["install"]).to be_a(::Array)
expect(subject.packages["remove"]).to be_a(::Array)
end
end

describe "#modified?" do
it "returns value of NtpClient modified" do
Yast::NtpClient.modified = true
expect(subject.modified?).to eq true
end
end

describe "#modified" do
it "sets value of NtpClient modified to true" do
Yast::NtpClient.modified = false
subject.modified
expect(Yast::NtpClient.modified).to eq true
end
end
end

0 comments on commit 5e388c8

Please sign in to comment.