-
Notifications
You must be signed in to change notification settings - Fork 34
/
config_test.rb
586 lines (496 loc) · 18.6 KB
/
config_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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# Copyright (c) [2019] 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 "y2network/config"
require "y2network/driver"
require "y2network/routing_table"
require "y2network/interface"
require "y2network/interfaces_collection"
require "y2network/connection_config/bridge"
require "y2network/connection_config/ethernet"
require "y2network/connection_configs_collection"
require "y2network/sysconfig/config_reader"
require "y2network/sysconfig/config_writer"
require "y2network/network_manager/config_writer"
describe Y2Network::Config do
before do
Y2Network::Config.reset
end
subject(:config) do
described_class.new(
interfaces: interfaces, connections: connections, routing: routing,
drivers: drivers, source: :sysconfig
)
end
let(:route1) { Y2Network::Route.new }
let(:route2) { Y2Network::Route.new }
let(:table1) { Y2Network::RoutingTable.new([route1]) }
let(:table2) { Y2Network::RoutingTable.new([route2]) }
let(:eth0) { Y2Network::PhysicalInterface.new("eth0") }
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0]) }
let(:eth0_conn) do
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = "eth0"
conn.name = "eth0"
end
end
let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn]) }
let(:virtio_net) { Y2Network::Driver.new("virtio_net", "csum=1") }
let(:drivers) { [virtio_net] }
let(:routing) { Y2Network::Routing.new(tables: [table1, table2]) }
describe ".from" do
let(:reader) do
instance_double(Y2Network::Sysconfig::ConfigReader, config: config)
end
before do
allow(Y2Network::ConfigReader).to receive(:for).with(:sysconfig)
.and_return(reader)
end
it "returns the configuration from the given reader" do
expect(described_class.from(:sysconfig)).to eq(config)
end
end
describe ".add" do
it "adds the configuration to the register" do
expect { Y2Network::Config.add(:yast, config) }
.to change { Y2Network::Config.find(:yast) }
.from(nil).to(config)
end
end
describe ".find" do
before do
Y2Network::Config.add(:yast, config)
end
it "returns the registered config with the given ID" do
expect(Y2Network::Config.find(:yast)).to eq(config)
end
context "when a configuration with the given ID does not exist" do
it "returns nil" do
expect(Y2Network::Config.find(:test)).to be_nil
end
end
end
describe "#routes" do
it "returns routes from all tables" do
expect(config.routing.routes).to eq([route1, route2])
end
end
describe "#write" do
let(:sysconfig_writer) { instance_double(Y2Network::Sysconfig::ConfigWriter) }
let(:nm_writer) { instance_double(Y2Network::NetworkManager::ConfigWriter) }
before do
allow(Y2Network::ConfigWriter).to receive(:for).with(:sysconfig)
.and_return(sysconfig_writer)
allow(Y2Network::ConfigWriter).to receive(:for).with(:network_manager)
.and_return(nm_writer)
end
it "writes the configuration using the writer for the given target" do
expect(nm_writer).to receive(:write).with(config, nil, only: nil)
config.write(target: :network_manager)
end
context "when the original configuration is given" do
let(:original) { instance_double(described_class) }
it "passes that configuration to the writer" do
expect(sysconfig_writer).to receive(:write).with(config, original, only: nil)
config.write(original: original)
end
end
context "when a set of sections is given" do
it "applies the configuration for those sections only" do
expect(sysconfig_writer).to receive(:write).with(config, nil, only: [:dns])
config.write(only: [:dns])
end
end
context "when no target is given" do
context "and a backend is set" do
before { config.backend = :network_manager }
it "writes the config using the writer for the backend" do
expect(nm_writer).to receive(:write).with(config, nil, only: nil)
config.write
end
end
context "and no backend is set" do
it "writes the config using the writer for the source" do
expect(sysconfig_writer).to receive(:write).with(config, nil, only: nil)
config.write
end
end
end
end
describe "#copy" do
it "returns a copy of the object" do
copy = config.copy
expect(copy).to_not be(config)
expect(copy.routing.tables.size).to eq(2)
end
it "returns a copy whose changes won't affect to the original object" do
copy = config.copy
copy.routing.tables.clear
expect(copy.routing.tables).to be_empty
expect(config.routing.tables.size).to eq(2)
end
end
describe "#==" do
let(:bond0) { Y2Network::VirtualInterface.new("bond0") }
let(:bond0_conn) do
Y2Network::ConnectionConfig::Bonding.new.tap do |conn|
conn.interface = "bond0"
conn.name = "bond0"
conn.slaves = ["eth0"]
end
end
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0, bond0]) }
let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn, bond0_conn]) }
let(:copy) { config.copy }
context "when both configuration contains the same information" do
it "returns true" do
expect(copy).to eq(config)
end
end
context "when interfaces list is different" do
it "returns false" do
copy.interfaces = Y2Network::InterfacesCollection.new([Y2Network::Interface.new("eth1")])
expect(copy).to_not eq(config)
end
end
context "when connection list is different" do
it "returns false" do
modified_bond = copy.connections.by_name("bond0")
modified_bond.options = "mode=active-backup miimon=300"
expect(copy).to_not eq(config)
end
end
context "when routing information is different" do
it "returns false" do
copy.routing.forward_ipv4 = !config.routing.forward_ipv4
expect(copy).to_not eq(config)
end
end
context "when hostname information is different" do
it "returns false" do
copy.hostname.static = "dummy"
expect(copy).to_not eq(config)
end
end
end
describe "#rename_interface" do
it "adjusts the interface name" do
config.rename_interface("eth0", "eth1", :mac)
eth1 = config.interfaces.by_name("eth1")
expect(eth1.renaming_mechanism).to eq(:mac)
end
it "adjusts the connection configurations for that interface" do
config.rename_interface("eth0", "eth1", :mac)
eth1_conns = config.connections.by_interface("eth1")
expect(eth1_conns).to_not be_empty
end
context "when the interface is renamed twice" do
it "adjusts the interface name to the last name" do
config.rename_interface("eth0", "eth1", :mac)
config.rename_interface("eth1", "eth2", :bios_id)
eth2 = config.interfaces.by_name("eth2")
expect(eth2.renaming_mechanism).to eq(:bios_id)
end
it "adjusts the connection configurations for that interface using the last name" do
config.rename_interface("eth0", "eth1", :mac)
config.rename_interface("eth1", "eth2", :mac)
eth2_conns = config.connections.by_interface("eth2")
expect(eth2_conns).to_not be_empty
end
end
context "when the old name is nil" do
it "adjust renaming mechanism only" do
config.rename_interface(nil, "eth0", :mac)
eth1 = config.interfaces.by_name("eth0")
expect(eth1.renaming_mechanism).to eq(:mac)
end
end
context "when dhcp_hostname points to the renamed interface" do
before do
allow(config.hostname).to receive(:dhcp_hostname).and_return("eth0")
end
it "adjusts the dhcp_hostname" do
expect(config.hostname).to receive(:dhcp_hostname=).with("eth1")
config.rename_interface("eth0", "eth1", :mac)
end
end
context "when dhcp_hostname does not point to the renamed interface" do
before do
allow(config.hostname).to receive(:dhcp_hostname).and_return(:any)
end
it "does not adjust the dhcp_hostname" do
expect(config.hostname).to_not receive(:dhcp_hostname=)
config.rename_interface("eth0", "eth1", :mac)
end
end
context "when interface is used in bridge" do
let(:br0) { Y2Network::VirtualInterface.new("br0") }
let(:br0_conn) do
Y2Network::ConnectionConfig::Bridge.new.tap do |conn|
conn.interface = "br0"
conn.name = "br0"
conn.ports = ["eth0"]
end
end
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0, br0]) }
let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn, br0_conn]) }
it "updates interface name in its ports" do
expect { config.rename_interface("eth0", "eth1", :mac) }.to change { br0_conn.ports }
.from(["eth0"]).to(["eth1"])
end
end
context "when interface is used in bonding" do
let(:bond0) { Y2Network::VirtualInterface.new("bond0") }
let(:bond0_conn) do
Y2Network::ConnectionConfig::Bonding.new.tap do |conn|
conn.interface = "bond0"
conn.name = "bond0"
conn.slaves = ["eth0"]
end
end
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0, bond0]) }
let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn, bond0_conn]) }
it "updates interface name in its slaves" do
expect { config.rename_interface("eth0", "eth1", :mac) }.to change { bond0_conn.slaves }
.from(["eth0"]).to(["eth1"])
end
end
context "when interface is used in vlan" do
let(:vlan0) { Y2Network::VirtualInterface.new("vlan0") }
let(:vlan0_conn) do
Y2Network::ConnectionConfig::Vlan.new.tap do |conn|
conn.interface = "vlan0"
conn.name = "vlan0"
conn.parent_device = "eth0"
end
end
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0, vlan0]) }
let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn, vlan0_conn]) }
it "updates interface name in its parent device" do
expect { config.rename_interface("eth0", "eth1", :mac) }.to(
change { vlan0_conn.parent_device }.from("eth0").to("eth1")
)
end
end
end
describe "#add_or_update_connection_config" do
let(:new_conn) do
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = "eth2"
end
end
it "adds the connection config" do
config.add_or_update_connection_config(new_conn)
expect(config.connections.by_name(new_conn.name)).to eq(new_conn)
end
context "when a connection config with the same name exists" do
let(:other_conn) do
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = "eth2"
end
end
before do
config.add_or_update_connection_config(new_conn)
end
it "updates the connection config" do
config.add_or_update_connection_config(other_conn)
expect(config.connections.by_name(new_conn.name)).to eq(other_conn)
end
end
context "when the interface is missing" do
let(:new_conn) do
Y2Network::ConnectionConfig::Bridge.new.tap do |conn|
conn.interface = "br0"
end
end
it "adds the corresponding interface" do
config.add_or_update_connection_config(new_conn)
expect(config.interfaces.by_name("br0")).to be_a(Y2Network::VirtualInterface)
end
context "and the interface already exists" do
before do
config.interfaces << Y2Network::VirtualInterface.new("br0")
end
it "does not add any interface" do
expect { config.add_or_update_connection_config(new_conn) }
.to_not(change { config.interfaces.size })
end
end
end
end
describe "#delete_interface" do
let(:br0) { Y2Network::VirtualInterface.new("br0") }
let(:br0_conn) do
Y2Network::ConnectionConfig::Bridge.new.tap do |conn|
conn.interface = "br0"
conn.name = "br0"
end
end
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0, br0]) }
let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn, br0_conn]) }
context "when it is not a physical interface" do
it "removes the connection config" do
expect { config.delete_interface(br0.name) }.to change { config.connections.to_a }
.from([eth0_conn, br0_conn]).to([eth0_conn])
end
it "removes the interface" do
expect { config.delete_interface(br0.name) }.to change { config.interfaces.to_a }
.from([eth0, br0]).to([eth0])
end
end
context "when interface is used in bridge" do
let(:br0) { Y2Network::VirtualInterface.new("br0") }
let(:br0_conn) do
Y2Network::ConnectionConfig::Bridge.new.tap do |conn|
conn.interface = "br0"
conn.name = "br0"
conn.ports = ["eth0"]
end
end
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0, br0]) }
let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn, br0_conn]) }
it "removes interface from its ports" do
expect { config.delete_interface(eth0.name) }.to change { br0_conn.ports }
.from(["eth0"]).to([])
end
end
context "when interface is used in bonding" do
let(:bond0) { Y2Network::VirtualInterface.new("bond0") }
let(:bond0_conn) do
Y2Network::ConnectionConfig::Bonding.new.tap do |conn|
conn.interface = "bond0"
conn.name = "bond0"
conn.slaves = ["eth0"]
end
end
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0, bond0]) }
let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn, bond0_conn]) }
it "removes interface from its slaves" do
expect { config.delete_interface(eth0.name) }.to change { bond0_conn.slaves }
.from(["eth0"]).to([])
end
end
context "when interface is used in vlan" do
let(:vlan0) { Y2Network::VirtualInterface.new("vlan0") }
let(:vlan0_conn) do
Y2Network::ConnectionConfig::Vlan.new.tap do |conn|
conn.interface = "vlan0"
conn.name = "vlan0"
conn.parent_device = "eth0"
end
end
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0, vlan0]) }
let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn, vlan0_conn]) }
it "removes that vlan" do
expect(config).to receive(:delete_interface).with("vlan0")
# allow initial call to delete eth
allow(config).to receive(:delete_interface).and_call_original
config.delete_interface(eth0.name)
end
end
context "when interface is used for configuring hostname via dhcp" do
before do
config.hostname.dhcp_hostname = eth0.name
end
it "sets dns dhcp hostname to :none" do
expect { config.delete_interface(eth0.name) }.to change { config.hostname.dhcp_hostname }
.from("eth0").to(:none)
end
end
context "when it is a physical interface" do
let(:present?) { true }
before do
allow(eth0).to receive(:present?).and_return(present?)
end
it "removes the connection config" do
expect { config.delete_interface(eth0.name) }.to change { config.connections.to_a }
.from([eth0_conn, br0_conn]).to([br0_conn])
end
it "does not remove the interface" do
expect { config.delete_interface(eth0.name) }.to_not(
change { config.interfaces.to_a }
)
end
context "when the interface is not present" do
let(:present?) { false }
it "removes the interface" do
expect { config.delete_interface(eth0.name) }.to change { config.interfaces.to_a }
.from([eth0, br0]).to([br0])
end
end
end
end
describe "#drivers_for_interface" do
let(:e1000) { Y2Network::Driver.new("e1000", "") }
let(:custom) { Y2Network::Driver.new("custom", "") }
let(:drivers) { [virtio_net, e1000, custom] }
before do
allow(eth0).to receive(:drivers).and_return([Y2Network::Driver.new("virtio_net")])
end
it "returns the driver for a given interface" do
drivers = config.drivers_for_interface("eth0")
expect(drivers).to eq([virtio_net])
end
context "when a custom driver is set" do
before do
eth0.custom_driver = custom.name
end
it "includes the custom driver" do
expect(config.drivers_for_interface("eth0"))
.to include(custom)
end
end
end
describe "#add_or_update_driver" do
let(:new_driver) { Y2Network::Driver.new("e1000", "") }
it "adds the driver" do
config.add_or_update_driver(new_driver)
expect(config.drivers).to eq([virtio_net, new_driver])
end
context "when a driver with the same name already exists" do
let(:new_driver) { Y2Network::Driver.new("virtio_net", "csum=0") }
it "replaces the driver with the given one" do
config.add_or_update_driver(new_driver)
expect(config.drivers).to eq([new_driver])
end
end
end
describe "#configured_interface?" do
context "when a connection for the given interface exists" do
it "reeturns true" do
expect(config.configured_interface?("eth0")).to eq(true)
end
end
context "when no connection for the given interface exists" do
it "reeturns false" do
expect(config.configured_interface?("eth9")).to eq(false)
end
end
context "when interface name is nil" do
it "returns false" do
expect(config.configured_interface?(nil)).to eq(false)
end
end
context "when interface name is empty" do
it "returns false" do
expect(config.configured_interface?("")).to eq(false)
end
end
end
end