linux_admin 1.2.1 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/linux_admin.rb +0 -8
- data/lib/linux_admin/chrony.rb +1 -0
- data/lib/linux_admin/disk.rb +31 -7
- data/lib/linux_admin/fstab.rb +29 -14
- data/lib/linux_admin/partition.rb +1 -1
- data/lib/linux_admin/registration_system.rb +8 -4
- data/lib/linux_admin/scap.rb +1 -1
- data/lib/linux_admin/service/sys_v_init_service.rb +2 -1
- data/lib/linux_admin/service/systemd_service.rb +6 -2
- data/lib/linux_admin/version.rb +1 -1
- metadata +36 -120
- data/lib/linux_admin/registration_system/rhn.rb +0 -111
- data/spec/chrony_spec.rb +0 -64
- data/spec/common_spec.rb +0 -37
- data/spec/data/rhn/output_rhn-channel_list +0 -2
- data/spec/data/rhn/output_rhn-channel_list_available +0 -4
- data/spec/data/rhn/systemid +0 -57
- data/spec/data/rhn/systemid.missing_system_id +0 -57
- data/spec/data/rpm/cmd_output_for_list_installed +0 -20
- data/spec/data/subscription_manager/output_list_all_available +0 -42
- data/spec/data/subscription_manager/output_list_installed_not_subscribed +0 -19
- data/spec/data/subscription_manager/output_list_installed_subscribed +0 -19
- data/spec/data/subscription_manager/output_orgs +0 -6
- data/spec/data/subscription_manager/output_repos +0 -18
- data/spec/data/time_date/timedatectl_output +0 -14
- data/spec/data/yum/first.repo +0 -19
- data/spec/data/yum/output_repo_list +0 -13
- data/spec/data/yum/output_repoquery_multiple +0 -3
- data/spec/data/yum/output_repoquery_single +0 -1
- data/spec/data/yum/second.repo +0 -9
- data/spec/deb_spec.rb +0 -52
- data/spec/disk_spec.rb +0 -244
- data/spec/distro_spec.rb +0 -77
- data/spec/dns_spec.rb +0 -105
- data/spec/etc_issue_spec.rb +0 -37
- data/spec/fstab_spec.rb +0 -66
- data/spec/hardware_spec.rb +0 -46
- data/spec/hosts_spec.rb +0 -150
- data/spec/ip_address_spec.rb +0 -148
- data/spec/logical_volume_spec.rb +0 -166
- data/spec/mountable_spec.rb +0 -182
- data/spec/network_interface/network_interface_rh_spec.rb +0 -291
- data/spec/network_interface_spec.rb +0 -284
- data/spec/partition_spec.rb +0 -24
- data/spec/physical_volume_spec.rb +0 -101
- data/spec/registration_system_spec.rb +0 -85
- data/spec/rhn_spec.rb +0 -144
- data/spec/rpm_spec.rb +0 -85
- data/spec/scap_spec.rb +0 -48
- data/spec/service/sys_v_init_service_spec.rb +0 -127
- data/spec/service/systemd_service_spec.rb +0 -133
- data/spec/service_spec.rb +0 -54
- data/spec/spec_helper.rb +0 -116
- data/spec/ssh_spec.rb +0 -53
- data/spec/subscription_manager_spec.rb +0 -228
- data/spec/system_spec.rb +0 -15
- data/spec/time_date_spec.rb +0 -106
- data/spec/volume_group_spec.rb +0 -99
- data/spec/yum_spec.rb +0 -155
data/spec/logical_volume_spec.rb
DELETED
@@ -1,166 +0,0 @@
|
|
1
|
-
describe LinuxAdmin::LogicalVolume do
|
2
|
-
before(:each) do
|
3
|
-
@logical_volumes = <<eos
|
4
|
-
/dev/vg_foobar/lv_swap:vg_foobar:3:1:-1:2:4128768:63:-1:0:-1:253:0
|
5
|
-
/dev/vg_foobar/lv_root:vg_foobar:3:1:-1:1:19988480:305:-1:0:-1:253:1
|
6
|
-
eos
|
7
|
-
|
8
|
-
@groups = <<eos
|
9
|
-
vg_foobar:r/w:772:-1:0:2:2:-1:0:1:1:12058624:32768:368:368:0:tILZUF-IspH-H90I-pT5j-vVFl-b76L-zWx3CW
|
10
|
-
eos
|
11
|
-
end
|
12
|
-
|
13
|
-
after(:each) do
|
14
|
-
# reset local copies of volumes / groups
|
15
|
-
described_class.instance_variable_set(:@lvs, nil)
|
16
|
-
LinuxAdmin::PhysicalVolume.instance_variable_set(:@pvs, nil)
|
17
|
-
LinuxAdmin::VolumeGroup.instance_variable_set(:@vgs, nil)
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "#extend_with" do
|
21
|
-
it "uses lvextend" do
|
22
|
-
vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
|
23
|
-
lv = described_class.new :name => 'lv', :volume_group => vg
|
24
|
-
expect(LinuxAdmin::Common).to receive(:run!)
|
25
|
-
.with(LinuxAdmin::Common.cmd(:lvextend),
|
26
|
-
:params => %w(lv vg))
|
27
|
-
lv.extend_with(vg)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "returns self" do
|
31
|
-
vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
|
32
|
-
lv = described_class.new :name => 'lv', :volume_group => vg
|
33
|
-
allow(LinuxAdmin::Common).to receive(:run!)
|
34
|
-
expect(lv.extend_with(vg)).to eq(lv)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe "#path" do
|
39
|
-
it "returns /dev/vgname/lvname" do
|
40
|
-
vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
|
41
|
-
lv = described_class.new :name => 'lv', :volume_group => vg
|
42
|
-
expect(lv.path).to eq('/dev/vg/lv')
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
describe "#create" do
|
47
|
-
before(:each) do
|
48
|
-
@vg = LinuxAdmin::VolumeGroup.new :name => 'vg'
|
49
|
-
end
|
50
|
-
|
51
|
-
it "uses lvcreate" do
|
52
|
-
described_class.instance_variable_set(:@lvs, [])
|
53
|
-
expect(LinuxAdmin::Common).to receive(:run!)
|
54
|
-
.with(LinuxAdmin::Common.cmd(:lvcreate),
|
55
|
-
:params => {'-n' => 'lv',
|
56
|
-
nil => 'vg',
|
57
|
-
'-L' => '256G'})
|
58
|
-
described_class.create 'lv', @vg, 274_877_906_944 # 256.gigabytes
|
59
|
-
end
|
60
|
-
|
61
|
-
context "size is specified" do
|
62
|
-
it "passes -L option to lvcreate" do
|
63
|
-
described_class.instance_variable_set(:@lvs, [])
|
64
|
-
expect(LinuxAdmin::Common).to receive(:run!)
|
65
|
-
.with(LinuxAdmin::Common.cmd(:lvcreate),
|
66
|
-
:params => {'-n' => 'lv',
|
67
|
-
nil => 'vg',
|
68
|
-
'-L' => '256G'})
|
69
|
-
described_class.create 'lv', @vg, 274_877_906_944 # 256.gigabytes
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
context "extents is specified" do
|
74
|
-
it "passes -l option to lvcreate" do
|
75
|
-
described_class.instance_variable_set(:@lvs, [])
|
76
|
-
expect(LinuxAdmin::Common).to receive(:run!)
|
77
|
-
.with(LinuxAdmin::Common.cmd(:lvcreate),
|
78
|
-
:params => {'-n' => 'lv',
|
79
|
-
nil => 'vg',
|
80
|
-
'-l' => '100%FREE'})
|
81
|
-
described_class.create 'lv', @vg, 100
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
it "returns new logical volume" do
|
86
|
-
allow(LinuxAdmin::Common).to receive_messages(:run! => double(:output => ""))
|
87
|
-
allow(LinuxAdmin::Common).to receive_messages(:run! => double(:output => ""))
|
88
|
-
lv = described_class.create 'lv', @vg, 274_877_906_944 # 256.gigabytes
|
89
|
-
expect(lv).to be_an_instance_of(described_class)
|
90
|
-
expect(lv.name).to eq('lv')
|
91
|
-
end
|
92
|
-
|
93
|
-
context "name is specified" do
|
94
|
-
it "sets path under volume group" do
|
95
|
-
allow(LinuxAdmin::Common).to receive_messages(:run! => double(:output => ""))
|
96
|
-
allow(LinuxAdmin::Common).to receive_messages(:run! => double(:output => ""))
|
97
|
-
lv = described_class.create 'lv', @vg, 274_877_906_944 # 256.gigabytes
|
98
|
-
expect(lv.path.to_s).to eq("#{described_class::DEVICE_PATH}#{@vg.name}/lv")
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
context "path is specified" do
|
103
|
-
it "sets name" do
|
104
|
-
allow(LinuxAdmin::Common).to receive_messages(:run! => double(:output => ""))
|
105
|
-
allow(LinuxAdmin::Common).to receive_messages(:run! => double(:output => ""))
|
106
|
-
lv = described_class.create '/dev/lv', @vg, 274_877_906_944 # 256.gigabytes
|
107
|
-
expect(lv.name).to eq("lv")
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
context "path is specified as Pathname" do
|
112
|
-
it "sets name" do
|
113
|
-
require 'pathname'
|
114
|
-
allow(LinuxAdmin::Common).to receive_messages(:run! => double(:output => ""))
|
115
|
-
allow(LinuxAdmin::Common).to receive_messages(:run! => double(:output => ""))
|
116
|
-
lv = described_class.create Pathname.new("/dev/#{@vg.name}/lv"), @vg, 274_877_906_944 # 256.gigabytes
|
117
|
-
expect(lv.name).to eq("lv")
|
118
|
-
expect(lv.path).to eq("/dev/vg/lv")
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
it "adds logical volume to local registry" do
|
123
|
-
allow(LinuxAdmin::Common).to receive_messages(:run! => double(:output => ""))
|
124
|
-
allow(LinuxAdmin::Common).to receive_messages(:run! => double(:output => ""))
|
125
|
-
lv = described_class.create 'lv', @vg, 274_877_906_944 # 256.gigabytes
|
126
|
-
expect(described_class.scan).to include(lv)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
describe "#scan" do
|
131
|
-
it "uses lvdisplay" do
|
132
|
-
expect(LinuxAdmin::Common).to receive(:run!)
|
133
|
-
.with(LinuxAdmin::Common.cmd(:lvdisplay),
|
134
|
-
:params => {'-c' => nil})
|
135
|
-
.and_return(double(:output => @logical_volumes))
|
136
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @groups)) # stub out call to vgdisplay
|
137
|
-
described_class.scan
|
138
|
-
end
|
139
|
-
|
140
|
-
it "returns local logical volumes" do
|
141
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @logical_volumes))
|
142
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @groups))
|
143
|
-
lvs = described_class.scan
|
144
|
-
|
145
|
-
expect(lvs[0]).to be_an_instance_of(described_class)
|
146
|
-
expect(lvs[0].path).to eq('/dev/vg_foobar/lv_swap')
|
147
|
-
expect(lvs[0].name).to eq('lv_swap')
|
148
|
-
expect(lvs[0].sectors).to eq(4128768)
|
149
|
-
|
150
|
-
expect(lvs[1]).to be_an_instance_of(described_class)
|
151
|
-
expect(lvs[1].path).to eq('/dev/vg_foobar/lv_root')
|
152
|
-
expect(lvs[1].name).to eq('lv_root')
|
153
|
-
expect(lvs[1].sectors).to eq(19988480)
|
154
|
-
end
|
155
|
-
|
156
|
-
it "resolves volume group references" do
|
157
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @logical_volumes))
|
158
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @groups))
|
159
|
-
lvs = described_class.scan
|
160
|
-
expect(lvs[0].volume_group).to be_an_instance_of(LinuxAdmin::VolumeGroup)
|
161
|
-
expect(lvs[0].volume_group.name).to eq('vg_foobar')
|
162
|
-
expect(lvs[1].volume_group).to be_an_instance_of(LinuxAdmin::VolumeGroup)
|
163
|
-
expect(lvs[1].volume_group.name).to eq('vg_foobar')
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|
data/spec/mountable_spec.rb
DELETED
@@ -1,182 +0,0 @@
|
|
1
|
-
class TestMountable
|
2
|
-
include LinuxAdmin::Mountable
|
3
|
-
|
4
|
-
def path
|
5
|
-
"/dev/foo"
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
describe LinuxAdmin::Mountable do
|
10
|
-
before(:each) do
|
11
|
-
@mountable = TestMountable.new
|
12
|
-
|
13
|
-
# stub out calls that modify system
|
14
|
-
allow(FileUtils).to receive(:mkdir)
|
15
|
-
allow(LinuxAdmin::Common).to receive(:run!)
|
16
|
-
|
17
|
-
@mount_out1 = <<eos
|
18
|
-
/dev/sda on /mnt/usb type vfat (rw)
|
19
|
-
eos
|
20
|
-
@mount_out2 = <<eos
|
21
|
-
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
|
22
|
-
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=26,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
|
23
|
-
eos
|
24
|
-
|
25
|
-
@mount_out3 = <<eos
|
26
|
-
/dev/mapper/vg_data-lv_pg on /var/opt/rh/rh-postgresql95/lib/pgsql type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
|
27
|
-
/dev/foo on /tmp type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
|
28
|
-
/dev/foo on /home type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
|
29
|
-
eos
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "#mount_point_exists?" do
|
33
|
-
it "uses mount" do
|
34
|
-
expect(LinuxAdmin::Common).to receive(:run!).with(LinuxAdmin::Common.cmd(:mount))
|
35
|
-
.and_return(double(:output => ""))
|
36
|
-
TestMountable.mount_point_exists?('/mnt/usb')
|
37
|
-
end
|
38
|
-
|
39
|
-
context "disk mounted at specified location" do
|
40
|
-
before do
|
41
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @mount_out1))
|
42
|
-
end
|
43
|
-
|
44
|
-
it "returns true" do
|
45
|
-
expect(TestMountable.mount_point_exists?('/mnt/usb')).to be_truthy
|
46
|
-
end
|
47
|
-
|
48
|
-
it "returns true when using a pathname" do
|
49
|
-
path = Pathname.new("/mnt/usb")
|
50
|
-
expect(TestMountable.mount_point_exists?(path)).to be_truthy
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
context "no disk mounted at specified location" do
|
55
|
-
before do
|
56
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @mount_out2))
|
57
|
-
end
|
58
|
-
|
59
|
-
it "returns false" do
|
60
|
-
expect(TestMountable.mount_point_exists?('/mnt/usb')).to be_falsey
|
61
|
-
end
|
62
|
-
|
63
|
-
it "returns false when using a pathname" do
|
64
|
-
path = Pathname.new("/mnt/usb")
|
65
|
-
expect(TestMountable.mount_point_exists?(path)).to be_falsey
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe "#mount_point_available?" do
|
71
|
-
it "uses mount" do
|
72
|
-
expect(LinuxAdmin::Common).to receive(:run!).with(LinuxAdmin::Common.cmd(:mount))
|
73
|
-
.and_return(double(:output => ""))
|
74
|
-
TestMountable.mount_point_available?('/mnt/usb')
|
75
|
-
end
|
76
|
-
|
77
|
-
context "disk mounted at specified location" do
|
78
|
-
before do
|
79
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @mount_out1))
|
80
|
-
end
|
81
|
-
|
82
|
-
it "returns false" do
|
83
|
-
expect(TestMountable.mount_point_available?('/mnt/usb')).to be_falsey
|
84
|
-
end
|
85
|
-
|
86
|
-
it "returns false when using a pathname" do
|
87
|
-
path = Pathname.new("/mnt/usb")
|
88
|
-
expect(TestMountable.mount_point_available?(path)).to be_falsey
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context "no disk mounted at specified location" do
|
93
|
-
before do
|
94
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @mount_out2))
|
95
|
-
end
|
96
|
-
|
97
|
-
it "returns true" do
|
98
|
-
expect(TestMountable.mount_point_available?('/mnt/usb')).to be_truthy
|
99
|
-
end
|
100
|
-
|
101
|
-
it "returns true when using a pathname" do
|
102
|
-
path = Pathname.new("/mnt/usb")
|
103
|
-
expect(TestMountable.mount_point_available?(path)).to be_truthy
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
describe "#discover_mount_point" do
|
109
|
-
it "sets the correct mountpoint when the path is mounted" do
|
110
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @mount_out3))
|
111
|
-
@mountable.discover_mount_point
|
112
|
-
expect(@mountable.mount_point).to eq("/tmp")
|
113
|
-
end
|
114
|
-
|
115
|
-
it "sets mount_point to nil when the path is not mounted" do
|
116
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => @mount_out1))
|
117
|
-
@mountable.discover_mount_point
|
118
|
-
expect(@mountable.mount_point).to be_nil
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
describe "#format_to" do
|
123
|
-
it "uses mke2fs" do
|
124
|
-
expect(LinuxAdmin::Common).to receive(:run!)
|
125
|
-
.with(LinuxAdmin::Common.cmd(:mke2fs),
|
126
|
-
:params => {'-t' => 'ext4', nil => '/dev/foo'})
|
127
|
-
@mountable.format_to('ext4')
|
128
|
-
end
|
129
|
-
|
130
|
-
it "sets fs type" do
|
131
|
-
expect(LinuxAdmin::Common).to receive(:run!) # ignore actual formatting cmd
|
132
|
-
@mountable.format_to('ext4')
|
133
|
-
expect(@mountable.fs_type).to eq('ext4')
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
describe "#mount" do
|
138
|
-
it "sets mount point" do
|
139
|
-
# ignore actual mount cmds
|
140
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => ""))
|
141
|
-
expect(LinuxAdmin::Common).to receive(:run!).and_return(double(:output => ""))
|
142
|
-
|
143
|
-
expect(@mountable.mount('/mnt/sda2')).to eq('/mnt/sda2')
|
144
|
-
expect(@mountable.mount_point).to eq('/mnt/sda2')
|
145
|
-
end
|
146
|
-
|
147
|
-
context "mountpoint does not exist" do
|
148
|
-
it "creates mountpoint" do
|
149
|
-
expect(TestMountable).to receive(:mount_point_exists?).and_return(false)
|
150
|
-
expect(File).to receive(:directory?).with('/mnt/sda2').and_return(false)
|
151
|
-
expect(FileUtils).to receive(:mkdir).with('/mnt/sda2')
|
152
|
-
expect(LinuxAdmin::Common).to receive(:run!) # ignore actual mount cmd
|
153
|
-
@mountable.mount '/mnt/sda2'
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
context "disk mounted at mountpoint" do
|
158
|
-
it "raises argument error" do
|
159
|
-
expect(TestMountable).to receive(:mount_point_exists?).and_return(true)
|
160
|
-
expect(File).to receive(:directory?).with('/mnt/sda2').and_return(true)
|
161
|
-
expect { @mountable.mount '/mnt/sda2' }.to raise_error(ArgumentError, "disk already mounted at /mnt/sda2")
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
it "mounts partition" do
|
166
|
-
expect(TestMountable).to receive(:mount_point_exists?).and_return(false)
|
167
|
-
expect(LinuxAdmin::Common).to receive(:run!)
|
168
|
-
.with(LinuxAdmin::Common.cmd(:mount),
|
169
|
-
:params => {nil => ['/dev/foo', '/mnt/sda2']})
|
170
|
-
@mountable.mount '/mnt/sda2'
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
|
-
describe "#umount" do
|
175
|
-
it "unmounts partition" do
|
176
|
-
@mountable.mount_point = '/mnt/sda2'
|
177
|
-
expect(LinuxAdmin::Common).to receive(:run!).with(LinuxAdmin::Common.cmd(:umount),
|
178
|
-
:params => {nil => ['/mnt/sda2']})
|
179
|
-
@mountable.umount
|
180
|
-
end
|
181
|
-
end
|
182
|
-
end
|
@@ -1,291 +0,0 @@
|
|
1
|
-
describe LinuxAdmin::NetworkInterfaceRH do
|
2
|
-
let(:device_name) { "eth0" }
|
3
|
-
let(:ifcfg_file_dhcp) do
|
4
|
-
<<-EOF
|
5
|
-
#A comment is here
|
6
|
-
DEVICE=eth0
|
7
|
-
BOOTPROTO=dhcp
|
8
|
-
UUID=3a48a5b5-b80b-4712-82f7-e517e4088999
|
9
|
-
ONBOOT=yes
|
10
|
-
TYPE=Ethernet
|
11
|
-
NAME="System eth0"
|
12
|
-
EOF
|
13
|
-
end
|
14
|
-
|
15
|
-
let(:ifcfg_file_static) do
|
16
|
-
<<-EOF
|
17
|
-
#A comment is here
|
18
|
-
DEVICE=eth0
|
19
|
-
BOOTPROTO=static
|
20
|
-
UUID=3a48a5b5-b80b-4712-82f7-e517e4088999
|
21
|
-
ONBOOT=yes
|
22
|
-
TYPE=Ethernet
|
23
|
-
NAME="System eth0"
|
24
|
-
IPADDR=192.168.1.100
|
25
|
-
NETMASK=255.255.255.0
|
26
|
-
GATEWAY=192.168.1.1
|
27
|
-
EOF
|
28
|
-
end
|
29
|
-
|
30
|
-
def stub_foreach_to_string(string)
|
31
|
-
allow(File).to receive(:foreach) do |&block|
|
32
|
-
string.each_line { |l| block.call(l) }
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def result(output, exit_status)
|
37
|
-
AwesomeSpawn::CommandResult.new("", output, "", exit_status)
|
38
|
-
end
|
39
|
-
|
40
|
-
subject(:dhcp_interface) do
|
41
|
-
allow(File).to receive(:exist?).and_return(true)
|
42
|
-
stub_path = described_class.path_to_interface_config_file(device_name)
|
43
|
-
allow(Pathname).to receive(:new).and_return(stub_path)
|
44
|
-
allow(stub_path).to receive(:file?).and_return(true)
|
45
|
-
stub_foreach_to_string(ifcfg_file_dhcp)
|
46
|
-
allow(AwesomeSpawn).to receive(:run!).exactly(6).times.and_return(result("", 0))
|
47
|
-
described_class.new(device_name)
|
48
|
-
end
|
49
|
-
|
50
|
-
subject(:static_interface) do
|
51
|
-
allow(File).to receive(:exist?).and_return(true)
|
52
|
-
stub_foreach_to_string(ifcfg_file_static)
|
53
|
-
allow(AwesomeSpawn).to receive(:run!).exactly(4).times.and_return(result("", 0))
|
54
|
-
described_class.new(device_name)
|
55
|
-
end
|
56
|
-
|
57
|
-
describe ".new" do
|
58
|
-
it "loads the configuration" do
|
59
|
-
conf = dhcp_interface.interface_config
|
60
|
-
expect(conf["NM_CONTROLLED"]).to eq("no")
|
61
|
-
expect(conf["DEVICE"]).to eq("eth0")
|
62
|
-
expect(conf["BOOTPROTO"]).to eq("dhcp")
|
63
|
-
expect(conf["UUID"]).to eq("3a48a5b5-b80b-4712-82f7-e517e4088999")
|
64
|
-
expect(conf["ONBOOT"]).to eq("yes")
|
65
|
-
expect(conf["TYPE"]).to eq("Ethernet")
|
66
|
-
expect(conf["NAME"]).to eq('"System eth0"')
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe "#parse_conf" do
|
71
|
-
it "reloads the interface configuration" do
|
72
|
-
interface = dhcp_interface
|
73
|
-
stub_foreach_to_string(ifcfg_file_static)
|
74
|
-
interface.parse_conf
|
75
|
-
|
76
|
-
conf = interface.interface_config
|
77
|
-
expect(conf["NM_CONTROLLED"]).to eq("no")
|
78
|
-
expect(conf["DEVICE"]).to eq("eth0")
|
79
|
-
expect(conf["BOOTPROTO"]).to eq("static")
|
80
|
-
expect(conf["UUID"]).to eq("3a48a5b5-b80b-4712-82f7-e517e4088999")
|
81
|
-
expect(conf["ONBOOT"]).to eq("yes")
|
82
|
-
expect(conf["TYPE"]).to eq("Ethernet")
|
83
|
-
expect(conf["NAME"]).to eq('"System eth0"')
|
84
|
-
expect(conf["IPADDR"]).to eq("192.168.1.100")
|
85
|
-
expect(conf["NETMASK"]).to eq("255.255.255.0")
|
86
|
-
expect(conf["GATEWAY"]).to eq("192.168.1.1")
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe "#address=" do
|
91
|
-
it "sets the address" do
|
92
|
-
address = "192.168.1.100"
|
93
|
-
|
94
|
-
dhcp_interface.address = address
|
95
|
-
|
96
|
-
conf = dhcp_interface.interface_config
|
97
|
-
expect(conf["IPADDR"]).to eq(address)
|
98
|
-
expect(conf["BOOTPROTO"]).to eq("static")
|
99
|
-
end
|
100
|
-
|
101
|
-
it "raises argument error when given a bad address" do
|
102
|
-
expect { dhcp_interface.address = "garbage" }.to raise_error(ArgumentError)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
describe '#address6=' do
|
107
|
-
it 'sets the ipv6 address' do
|
108
|
-
address = 'fe80::1/64'
|
109
|
-
dhcp_interface.address6 = address
|
110
|
-
conf = dhcp_interface.interface_config
|
111
|
-
expect(conf['IPV6ADDR']).to eq(address)
|
112
|
-
expect(conf['IPV6INIT']).to eq('yes')
|
113
|
-
expect(conf['DHCPV6C']).to eq('no')
|
114
|
-
end
|
115
|
-
|
116
|
-
it 'raises error when given a bad address' do
|
117
|
-
expect { dhcp_interface.address6 = '1::1::1' }.to raise_error(ArgumentError)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
describe "#gateway=" do
|
122
|
-
it "sets the gateway address" do
|
123
|
-
address = "192.168.1.1"
|
124
|
-
dhcp_interface.gateway = address
|
125
|
-
expect(dhcp_interface.interface_config["GATEWAY"]).to eq(address)
|
126
|
-
end
|
127
|
-
|
128
|
-
it "raises argument error when given a bad address" do
|
129
|
-
expect { dhcp_interface.gateway = "garbage" }.to raise_error(ArgumentError)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
describe '#gateway6=' do
|
134
|
-
it 'sets the default gateway for IPv6' do
|
135
|
-
address = 'fe80::1/64'
|
136
|
-
dhcp_interface.gateway6 = address
|
137
|
-
expect(dhcp_interface.interface_config['IPV6_DEFAULTGW']).to eq(address)
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
describe "#netmask=" do
|
142
|
-
it "sets the sub-net mask" do
|
143
|
-
mask = "255.255.255.0"
|
144
|
-
dhcp_interface.netmask = mask
|
145
|
-
expect(dhcp_interface.interface_config["NETMASK"]).to eq(mask)
|
146
|
-
end
|
147
|
-
|
148
|
-
it "raises argument error when given a bad address" do
|
149
|
-
expect { dhcp_interface.netmask = "garbage" }.to raise_error(ArgumentError)
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
describe "#dns=" do
|
154
|
-
it "sets the correct configuration" do
|
155
|
-
dns1 = "192.168.1.1"
|
156
|
-
dns2 = "192.168.1.10"
|
157
|
-
|
158
|
-
static_interface.dns = dns1, dns2
|
159
|
-
|
160
|
-
conf = static_interface.interface_config
|
161
|
-
expect(conf["DNS1"]).to eq(dns1)
|
162
|
-
expect(conf["DNS2"]).to eq(dns2)
|
163
|
-
end
|
164
|
-
|
165
|
-
it "sets the correct configuration when given an array" do
|
166
|
-
dns = %w(192.168.1.1 192.168.1.10)
|
167
|
-
|
168
|
-
static_interface.dns = dns
|
169
|
-
|
170
|
-
conf = static_interface.interface_config
|
171
|
-
expect(conf["DNS1"]).to eq(dns[0])
|
172
|
-
expect(conf["DNS2"]).to eq(dns[1])
|
173
|
-
end
|
174
|
-
|
175
|
-
it "sets only DNS1 if given one value" do
|
176
|
-
dns = "192.168.1.1"
|
177
|
-
|
178
|
-
static_interface.dns = dns
|
179
|
-
|
180
|
-
conf = static_interface.interface_config
|
181
|
-
expect(conf["DNS1"]).to eq(dns)
|
182
|
-
expect(conf["DNS2"]).to be_nil
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
describe "#search_order=" do
|
187
|
-
it "sets the search domain list" do
|
188
|
-
search1 = "localhost"
|
189
|
-
search2 = "test.example.com"
|
190
|
-
search3 = "example.com"
|
191
|
-
static_interface.search_order = search1, search2, search3
|
192
|
-
expect(static_interface.interface_config["DOMAIN"]).to eq("\"#{search1} #{search2} #{search3}\"")
|
193
|
-
end
|
194
|
-
|
195
|
-
it "sets the search domain list when given an array" do
|
196
|
-
search_list = %w(localhost test.example.com example.com)
|
197
|
-
static_interface.search_order = search_list
|
198
|
-
expect(static_interface.interface_config["DOMAIN"]).to eq("\"#{search_list.join(' ')}\"")
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
describe "#enable_dhcp" do
|
203
|
-
it "sets the correct configuration" do
|
204
|
-
static_interface.enable_dhcp
|
205
|
-
conf = static_interface.interface_config
|
206
|
-
expect(conf["BOOTPROTO"]).to eq("dhcp")
|
207
|
-
expect(conf["IPADDR"]).to be_nil
|
208
|
-
expect(conf["NETMASK"]).to be_nil
|
209
|
-
expect(conf["GATEWAY"]).to be_nil
|
210
|
-
expect(conf["PREFIX"]).to be_nil
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
describe '#enable_dhcp6' do
|
215
|
-
it 'sets the correct configuration' do
|
216
|
-
[static_interface, dhcp_interface].each do |interface|
|
217
|
-
interface.enable_dhcp6
|
218
|
-
conf = interface.interface_config
|
219
|
-
expect(conf).to include('IPV6INIT' => 'yes', 'DHCPV6C' => 'yes')
|
220
|
-
expect(conf.keys).not_to include('IPV6ADDR', 'IPV6_DEFAULTGW')
|
221
|
-
end
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
|
-
describe "#apply_static" do
|
226
|
-
it "sets the correct configuration" do
|
227
|
-
expect(dhcp_interface).to receive(:save)
|
228
|
-
dhcp_interface.apply_static("192.168.1.12", "255.255.255.0", "192.168.1.1", ["192.168.1.1", nil], ["localhost"])
|
229
|
-
|
230
|
-
conf = dhcp_interface.interface_config
|
231
|
-
expect(conf["BOOTPROTO"]).to eq("static")
|
232
|
-
expect(conf["IPADDR"]).to eq("192.168.1.12")
|
233
|
-
expect(conf["NETMASK"]).to eq("255.255.255.0")
|
234
|
-
expect(conf["GATEWAY"]).to eq("192.168.1.1")
|
235
|
-
expect(conf["DNS1"]).to eq("192.168.1.1")
|
236
|
-
expect(conf["DNS2"]).to be_nil
|
237
|
-
expect(conf["DOMAIN"]).to eq("\"localhost\"")
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
describe '#apply_static6' do
|
242
|
-
it 'sets the static IPv6 configuration' do
|
243
|
-
expect(dhcp_interface).to receive(:save)
|
244
|
-
dhcp_interface.apply_static6('d:e:a:d:b:e:e:f', 127, 'd:e:a:d::/64', ['d:e:a:d::'])
|
245
|
-
conf = dhcp_interface.interface_config
|
246
|
-
expect(conf).to include('IPV6INIT' => 'yes', 'DHCPV6C' => 'no', 'IPV6ADDR' => 'd:e:a:d:b:e:e:f/127', 'IPV6_DEFAULTGW' => 'd:e:a:d::/64')
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
describe "#save" do
|
251
|
-
let(:iface_file) { Pathname.new("/etc/sysconfig/network-scripts/ifcfg-#{device_name}") }
|
252
|
-
|
253
|
-
def expect_old_contents
|
254
|
-
expect(File).to receive(:write) do |file, contents|
|
255
|
-
expect(file).to eq(iface_file)
|
256
|
-
expect(contents).to include("DEVICE=eth0")
|
257
|
-
expect(contents).to include("BOOTPROTO=dhcp")
|
258
|
-
expect(contents).to include("UUID=3a48a5b5-b80b-4712-82f7-e517e4088999")
|
259
|
-
expect(contents).to include("ONBOOT=yes")
|
260
|
-
expect(contents).to include("TYPE=Ethernet")
|
261
|
-
expect(contents).to include('NAME="System eth0"')
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
it "writes the configuration" do
|
266
|
-
expect(File).to receive(:read).with(iface_file)
|
267
|
-
expect(dhcp_interface).to receive(:stop).and_return(true)
|
268
|
-
expect(dhcp_interface).to receive(:start).and_return(true)
|
269
|
-
expect_old_contents
|
270
|
-
expect(dhcp_interface.save).to be true
|
271
|
-
end
|
272
|
-
|
273
|
-
it "returns false when the interface cannot be brought down" do
|
274
|
-
expect(File).to receive(:read).with(iface_file)
|
275
|
-
expect(dhcp_interface).to receive(:stop).twice.and_return(false)
|
276
|
-
expect(File).not_to receive(:write)
|
277
|
-
expect(dhcp_interface.save).to be false
|
278
|
-
end
|
279
|
-
|
280
|
-
it "returns false and writes the old contents when the interface fails to come back up" do
|
281
|
-
dhcp_interface # evaluate the subject first so the expectations stub the right calls
|
282
|
-
expect(File).to receive(:read).with(iface_file).and_return("old stuff")
|
283
|
-
expect(dhcp_interface).to receive(:stop).and_return(true)
|
284
|
-
expect_old_contents
|
285
|
-
expect(dhcp_interface).to receive(:start).and_return(false)
|
286
|
-
expect(File).to receive(:write).with(iface_file, "old stuff")
|
287
|
-
expect(dhcp_interface).to receive(:start)
|
288
|
-
expect(dhcp_interface.save).to be false
|
289
|
-
end
|
290
|
-
end
|
291
|
-
end
|