/
ntp_conf_test.rb
460 lines (379 loc) · 11.4 KB
/
ntp_conf_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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
require_relative "../test_helper"
require "cfa/memory_file"
require "cfa/ntp_conf"
def ntp_disk_content
path = File.expand_path("../../fixtures/cfa/ntp.conf", __FILE__)
File.read(path)
end
def ntp_file(content)
CFA::MemoryFile.new(content)
end
def ntp_conf(file)
CFA::NtpConf.new(file_handler: file)
end
xdescribe CFA::NtpConf do
subject(:ntp) { ntp_conf(file) }
let(:file) { ntp_file(content) }
before do
ntp.load
end
describe "#load" do
context "when there is only one entry of a collection" do
let(:content) do
"server 127.127.1.0\n"
end
it "fixes the key of the collection" do
expect(ntp.records.first.augeas[:key]).to eq("server[]")
end
end
end
describe "#records" do
let(:content) { ntp_disk_content }
it "obtains the corrent amount of records" do
expect(ntp.records.count).to eq(18)
end
it "obtains a collection of records" do
expect(ntp.records).to be_a(CFA::NtpConf::RecordCollection)
end
end
describe "#save" do
let(:content) do
"server 0.pool.ntp.org\n" \
"server 1.pool.ntp.org\n" \
"server 2.pool.ntp.org\n"
end
context "when a record is added" do
it "writes a new entry" do
record = CFA::NtpConf::ServerRecord.new
record.value = "3.pool.ntp.org"
ntp.records << record
ntp.save
expect(file.content.lines).to include("server 3.pool.ntp.org\n")
end
it "writes it together with its options" do
record = CFA::NtpConf::ServerRecord.new
record.value = "3.pool.ntp.org"
record.raw_options = "iburst dynamic"
ntp.records << record
ntp.save
expect(file.content.lines).to include("server 3.pool.ntp.org iburst dynamic\n")
end
it "writes comment from entry" do
record = CFA::NtpConf::ServerRecord.new
record.value = "3.pool.ntp.org"
record.comment = "# test comment"
ntp.records << record
ntp.save
expect(file.content.lines).to include("server 3.pool.ntp.org# test comment\n")
end
it "can write multi lines comments from autoyast profiles" do
record = CFA::NtpConf::ServerRecord.new
record.value = "3.pool.ntp.org"
record.comment = "test comment\ntest comment 2\ntest comment3"
ntp.records << record
expect(record.comment).to eq "test comment\ntest comment 2\ntest comment3"
ntp.save
expect(file.content.lines).to include("server 3.pool.ntp.org\n")
expect(file.content.lines).to include("#test comment\n")
expect(file.content.lines).to include("#test comment 2\n")
expect(file.content.lines).to include("#test comment3\n")
end
end
context "when a record is deleted" do
it "removes an entry" do
record = ntp.records.find { |r| r.value == "0.pool.ntp.org" }
ntp.records.delete(record)
ntp.save
expect(file.content.lines).not_to include("server 0.pool.ntp.org\n")
end
end
context "when a record is updated" do
it "modifies an entry" do
record = ntp.records.find { |r| r.value == "0.pool.ntp.org" }
record.value = "10.pool.ntp.org"
ntp.save
expect(file.content.lines).not_to include("server 0.pool.ntp.org\n")
expect(file.content.lines).to include("server 10.pool.ntp.org\n")
end
end
end
end
xdescribe CFA::NtpConf::RecordCollection do
let(:ntp) { ntp_conf(file) }
let(:file) { ntp_file(content) }
let(:new_record) do
record = CFA::NtpConf::ServerRecord.new
record.value = "3.pool.ntp.org"
record
end
let(:existing_record) { ntp.records.first }
let(:content) do
"server 0.pool.ntp.org\n" \
"server 1.pool.ntp.org\n" \
"server 2.pool.ntp.org\n"
end
before do
ntp.load
end
context "#<<" do
context "when does not exist the record to add" do
it "adds the record" do
ntp.records << new_record
expect(ntp.records.count(new_record)).to eq(1)
end
end
context "when exists the record to add" do
it "adds the record too" do
ntp.records << existing_record
expect(ntp.records.count(existing_record)).to eq(2)
end
end
end
context "#delete" do
context "when does not exist the record to delete" do
it "does anything" do
records = ntp.records
ntp.records.delete(new_record)
expect(ntp.records).to eq(records)
end
end
context "when exists the record to delete" do
it "deletes the record" do
ntp.records.delete(existing_record)
expect(ntp.records.include?(existing_record)).to be(false)
end
end
end
context "#delete_if" do
it "deletes all records that satisfy the condition" do
ntp.records.delete_if { |record| record.type == "server" }
expect(ntp.records.any? { |record| record.type == "server" }).to eq(false)
end
end
end
xdescribe CFA::NtpConf::Record do
let(:ntp) { ntp_conf(file) }
let(:file) { ntp_file(content) }
let(:content) do
"server 0.pool.ntp.org iburst\n" \
"server 1.pool.ntp.org #sample comment\n" \
"server 2.pool.ntp.org\n"
end
before do
ntp.load
end
describe ".new_from_augeas" do
let(:augeas_element) do
{
key: "server[]",
value: "3.pool.ntp.org"
}
end
subject(:record) { described_class.new_from_augeas(augeas_element) }
it "creates a record of the correct class" do
expect(record).to be_a(CFA::NtpConf::ServerRecord)
end
it "creates the record with correct data" do
expect(record.augeas).to eq(augeas_element)
end
end
describe "#value" do
it "obtains the value of the record" do
record = ntp.records.first
expect(record.value).to eq("0.pool.ntp.org")
end
end
describe "#value=" do
it "sets the value of the record" do
value = "3.pool.ntp.org"
record = ntp.records.first
record.value = value
expect(record.value).to eq(value)
ntp.save
expect(file.content).to include("server 3.pool.ntp.org iburst\n")
end
end
describe "#comment" do
context "when the record has not comment" do
it "obtains nil" do
record = ntp.records.first
expect(record.comment).to eq(nil)
end
end
context "when the record has comment" do
it "obtains the comment" do
record = ntp.records.to_a[1]
expect(record.comment).to eq("#sample comment")
end
end
end
describe "#comment=" do
it "sets a comment to the record" do
comment = "#sample comment"
record = ntp.records.first
record.comment = comment
expect(record.comment).to eq(comment)
ntp.save
expect(file.content).to include("server 0.pool.ntp.org iburst#sample comment\n")
end
end
describe "#==" do
it "returns true for equal records" do
record = ntp.records.first
expect(record == record.dup).to be(true)
end
it "returns false for different records" do
records = ntp.records.to_a
expect(records[0] == records[1]).to be(false)
end
end
describe "#type" do
it "obtains the type of the record" do
expect(ntp.records.first.type).to eq("server")
end
end
describe "#raw_options" do
it "obtains options as string" do
expect(ntp.records.first.raw_options).to eq("iburst")
end
end
describe "#raw_options=" do
it "sets options from a string" do
record = ntp.records.first
record.raw_options = "iburst prefer"
expect(record.options).to eq(["iburst", "prefer"])
ntp.save
expect(file.content).to include("server 0.pool.ntp.org iburst prefer\n")
end
end
end
xdescribe CFA::NtpConf::CommandRecord do
let(:ntp) { ntp_conf(file) }
let(:file) { ntp_file(content) }
let(:content) do
"server 0.pool.ntp.org iburst\n"
end
before do
ntp.load
end
subject(:record) { ntp.records.first }
describe "#options" do
it "obtains the options of the record" do
expect(record.options).to eq(["iburst"])
end
end
describe "#options=" do
it "sets options to the record" do
record.options = ["iburst", "version", "3", "prefer"]
expect(record.options).to eq(["iburst", "version", "3", "prefer"])
ntp.save
expect(file.content).to include("server 0.pool.ntp.org iburst version 3 prefer\n")
end
end
end
xdescribe CFA::NtpConf::FudgeRecord do
let(:ntp) { ntp_conf(file) }
let(:file) { ntp_file(content) }
let(:content) do
"fudge 127.127.1.0 stratum 10\n"
end
before do
ntp.load
end
subject(:record) { ntp.records.first }
describe "#options" do
it "obtains the options of the record" do
expect(record.options).to eq("stratum" => "10")
end
end
context "#options=" do
it "sets options to the record" do
options = { "stratum" => "11" }
record.options = options
expect(record.options).to eq(options)
ntp.save
expect(file.content).to include("fudge 127.127.1.0 stratum 11\n")
end
end
describe "#raw_options" do
it "obtains options as string" do
expect(record.raw_options).to eq("stratum 10")
end
end
describe "#raw_options=" do
it "sets options from a string" do
record.raw_options = "stratum 11"
expect(record.options).to eq("stratum" => "11")
ntp.save
expect(file.content).to include("fudge 127.127.1.0 stratum 11\n")
end
end
end
xdescribe CFA::NtpConf::RestrictRecord do
let(:ntp) { ntp_conf(file) }
let(:file) { ntp_file(content) }
let(:content) do
"restrict -4 default notrap nomodify nopeer\n"
end
before do
ntp.load
end
subject(:record) { ntp.records.first }
describe "#options" do
it "obtains the options of the record" do
expect(record.options).to eq(%w(ipv4 notrap nomodify nopeer))
end
end
context "#options=" do
it "sets options to the record" do
options = ["ipv4", "notrap"]
record.options = options
expect(record.options).to eq(options)
ntp.save
expect(file.content).to include("restrict -4 default notrap\n")
end
end
describe "#raw_options" do
it "obtains options as string" do
expect(record.raw_options).to eq("ipv4 notrap nomodify nopeer")
end
end
describe "#raw_options=" do
it "sets options from a string" do
record.raw_options = "ipv4 notrap"
expect(record.options).to eq(["ipv4", "notrap"])
ntp.save
expect(file.content).to include("restrict -4 default notrap\n")
end
end
context "when creating a new record from scratch" do
it "sets values correctly" do
record = CFA::NtpConf::RestrictRecord.new
record.value = "default"
record.options = ["ipv6", "notrap"]
ntp.records << record
ntp.save
expect(file.content).to include("restrict -6 default notrap\n")
end
end
end
xdescribe CFA::NtpConf::TrustedkeyRecord do
let(:ntp) { ntp_conf(file) }
let(:file) { ntp_file("") }
it "adds proper type to conf when saving" do
ntp.records << subject
record = ntp.records.last
record.value = "1"
record.raw_options = ""
record.comment = "# path to keys file"
expect(record.value).to eq "1"
ntp.save
expect(file.content.lines).to include("trustedkey 1# path to keys file\n")
end
it "loads properly when it is defined in file" do
file.content << "##comment1\ntrustedkey 1\n"
ntp.load
trustedkey = ntp.records.last
expect(trustedkey.value).to eq "1"
end
end