-
Notifications
You must be signed in to change notification settings - Fork 15
/
ntp_client_proposal_test.rb
executable file
·322 lines (257 loc) · 9.86 KB
/
ntp_client_proposal_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#! /usr/bin/env rspec
require_relative "test_helper"
require_relative "../src/clients/ntp-client_proposal.rb"
Yast.import "Packages"
describe Yast::NtpClientProposalClient do
subject do
client = described_class.new
client.main
client
end
let(:dhcp_ntp_servers) { [] }
before do
allow(Yast::Lan).to receive(:dhcp_ntp_servers)
.and_return(dhcp_ntp_servers)
end
describe "#main" do
let(:client) { described_class.new }
let(:func) { "dhcp_ntp_servers" }
before do
allow(Yast::WFM).to receive(:Args).with(no_args).and_return([func])
allow(Yast::WFM).to receive(:Args).with(0).and_return(func)
end
context "when call with 'dhcp_ntp_servers' argument" do
let(:dhcp_ntp_servers) { ["test.example.net", "test2.example.net"] }
it "returns servers found via DHCP" do
expect(client.main).to eql(dhcp_ntp_servers)
end
end
end
describe "#MakeProposal" do
let(:config_was_read?) { false }
let(:ntp_was_selected?) { false }
before do
allow(Yast::NtpClient).to receive(:country_ntp_servers).with("de")
.and_return([Y2Network::NtpServer.new("de.pool.ntp.org")])
allow(Yast::Timezone).to receive(:timezone).and_return("Europe/Berlin")
allow(Yast::Timezone).to receive(:GetCountryForTimezone)
.with("Europe/Berlin").and_return("de")
allow(Yast::NtpClient).to receive(:config_has_been_read).and_return(config_was_read?)
allow(Yast::NtpClient).to receive(:ntp_selected).and_return(ntp_was_selected?)
allow(Yast::NtpClient).to receive(:GetUsedNtpServers)
.and_return(["de.pool.ntp.org"])
allow(subject).to receive(:select_ntp_server).and_return(true)
allow(Yast::Stage).to receive(:initial).and_return(true)
end
context "when NTP servers were found via DHCP" do
let(:dhcp_ntp_servers) { ["test.example.net"] }
it "proposes only the found servers" do
expect(Yast::UI).to receive(:ChangeWidget) do |*args|
items = args.last
hostnames = items.map { |i| i[1] }
expect(hostnames).to eq(
["test.example.net"]
)
end
subject.MakeProposal
end
end
context "when no NTP server were found via DHCP" do
let(:dhcp_ntp_servers) { [] }
it "proposes the known public servers for the current timezone" do
expect(Yast::UI).to receive(:ChangeWidget) do |*args|
items = args.last
hostnames = items.map { |i| i[1] }
expect(hostnames).to eq(["de.pool.ntp.org"])
end
subject.MakeProposal
end
end
context "when the NTP configuration has been read (from chrony)" do
let(:config_was_read?) { true }
it "proposes the known public servers for the current timezone" do
expect(Yast::UI).to receive(:ChangeWidget) do |*args|
items = args.last
hostnames = items.map { |i| i[1] }
expect(hostnames).to eq(["de.pool.ntp.org"])
end
subject.MakeProposal
end
end
context "when the NTP server was already selected" do
let(:ntp_was_selected?) { true }
it "proposes the known public servers for the current timezone" do
expect(Yast::UI).to receive(:ChangeWidget) do |*args|
items = args.last
hostnames = items.map { |i| i[1] }
expect(hostnames).to eq(["de.pool.ntp.org"])
end
subject.MakeProposal
end
end
end
describe "#Write" do
let(:ntp_server) { "fake.pool.ntp.org" }
let(:write_only) { false }
let(:ntpdate_only) { false }
let(:params) do
{
"server" => ntp_server,
"write_only" => write_only,
"ntpdate_only" => ntpdate_only
}
end
let(:ntp_client) { Yast::NtpClient }
let(:initial_stage) { false }
let(:network_running) { false }
before do
allow(subject).to receive(:WriteNtpSettings)
allow(Yast::Stage).to receive(:initial).and_return(initial_stage)
allow(Yast::PackageSystem).to receive(:CheckAndInstallPackages)
allow(Yast::Report).to receive(:Error)
allow(Yast::NetworkService).to receive(:isNetworkRunning).and_return(network_running)
allow(Yast::Service).to receive(:Active).with(ntp_client.service_name).and_return(false)
allow(Yast::NtpClient).to receive(:dhcp_ntp_servers).and_return([])
allow(Yast::Timezone).to receive(:GetCountryForTimezone).and_return("de")
end
context "with a not valid hostname" do
let(:ntp_server) { "not_valid" }
it "does not write settings" do
expect(subject).to_not receive(:WriteNtpSettings)
subject.Write(params)
end
it "returns :invalid_hostname" do
expect(subject.Write(params)).to eq(:invalid_hostname)
end
end
context "with valid hostname" do
before do
allow(subject).to receive(:ValidateSingleServer).and_return(true)
end
context "but in 'write_only' mode" do
let(:write_only) { true }
it "only writes settings" do
expect(subject).to receive(:WriteNtpSettings).once
expect(Yast::Stage).to_not receive(:initial)
expect(Yast::NetworkService).to_not receive(:isNetworkRunning)
subject.Write(params)
end
it "returns :success" do
expect(subject.Write(params)).to eq(:success)
end
end
context "but 'run_service' param is not given" do
it "uses the current value of NtpClient.run_service" do
ntp_client.run_service = true
expect(subject).to receive(:WriteNtpSettings).with(anything, anything, true)
subject.Write({})
ntp_client.run_service = false
expect(subject).to receive(:WriteNtpSettings).with(anything, anything, false)
subject.Write({})
end
end
context "and is in the initial stage" do
let(:initial_stage) { true }
it "imports Yast::Packages" do
allow(Yast).to receive(:import).and_call_original
expect(Yast).to receive(:import).with("Packages")
subject.Write(params)
end
it "adds the additional package" do
expect(Yast::Packages).to receive(:addAdditionalPackage)
subject.Write(params)
end
end
context "and is not in the initial stage" do
it "asks user to confirm the package installation" do
expect(Yast::PackageSystem).to receive(:CheckAndInstallPackages)
subject.Write(params)
end
context "but user rejects the package installation" do
before do
allow(Yast::PackageSystem).to receive(:CheckAndInstallPackages).and_return(false)
end
it "reports an error" do
expect(Yast::Report).to receive(:Error).with(/Synchronization with NTP server is not/)
subject.Write(params)
end
end
end
context "and network is not available" do
it "does not performs the ntp syncronization" do
expect(Yast::NtpClient).to_not receive(:sync_once)
subject.Write(params)
end
it "returns :success" do
expect(subject.Write(params)).to eq(:success)
end
end
context "and network is available" do
let(:network_running) { true }
it "returns :ntpdate_failed if synchronization fails" do
allow(Yast::NtpClient).to receive(:sync_once).and_return(1)
expect(subject.Write(params)).to eq(:ntpdate_failed)
end
it "returns :success if synchronization was successfully" do
allow(Yast::NtpClient).to receive(:sync_once).and_return(0)
expect(subject.Write(params)).to eq(:success)
end
end
context "and user only wants to synchronize date" do
let(:ntpdate_only) { true }
it "does not write settings" do
expect(subject).to_not receive(:WriteNtpSettings)
subject.Write(params)
end
it "does not try to synchronize if the service is running" do
allow(Yast::Service).to receive(:Active).with(ntp_client.service_name).and_return(true)
expect(Yast::NtpClient).to_not receive(:sync_once)
subject.Write(params)
end
end
context "and user wants to synchronize on boot" do
it "writes settings only once" do
expect(subject).to receive(:WriteNtpSettings).once
subject.Write(params)
end
end
end
end
describe "#select_ntp_server" do
before do
allow(Yast::Timezone).to receive(:GetCountryForTimezone).and_return("de")
end
context "there are already more than one ntp server defined" do
it "returns false" do
allow(Yast::NtpClient).to receive(:GetUsedNtpServers).and_return(["n1", "n2"])
expect(subject.send(:select_ntp_server)).to eq(false)
end
end
context "there is no ntp server defined" do
it "returns true" do
allow(Yast::NtpClient).to receive(:GetUsedNtpServers).and_return([])
expect(subject.send(:select_ntp_server)).to eq(true)
end
end
context "there is ONE ntp server defined" do
before do
allow(Yast::NtpClient).to receive(:dhcp_ntp_servers).and_return([])
end
context "and defined server is not in the selection list" do
it "returns false" do
allow(Yast::NtpClient).to receive(:GetUsedNtpServers).and_return(["not_found"])
expect(subject.send(:select_ntp_server)).to eq(false)
end
end
context "and defined server is in the selection list" do
it "returns true" do
allow(Yast::NtpClient).to receive(:GetNtpServersByCountry).and_return(
[Item(Id("de.pool.ntp.org"), "de.pool.ntp.org", true)]
)
allow(Yast::NtpClient).to receive(:GetUsedNtpServers).and_return(["de.pool.ntp.org"])
expect(subject.send(:select_ntp_server)).to eq(true)
end
end
end
end
end