-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor auto client and add tests for it
- Loading branch information
1 parent
805c7df
commit 5e388c8
Showing
4 changed files
with
195 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |