kanrisuru 0.14.0 → 0.16.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -0
  3. data/README.md +5 -5
  4. data/kanrisuru.gemspec +7 -3
  5. data/lib/kanrisuru/command.rb +16 -3
  6. data/lib/kanrisuru/core/apt/parsers/base.rb +1 -1
  7. data/lib/kanrisuru/core/disk/commands/lsblk.rb +6 -11
  8. data/lib/kanrisuru/core/disk/constants.rb +9 -0
  9. data/lib/kanrisuru/core/disk/parser.rb +1 -0
  10. data/lib/kanrisuru/core/disk/parsers/lsblk_version.rb +21 -0
  11. data/lib/kanrisuru/core/disk.rb +1 -0
  12. data/lib/kanrisuru/core/dmi/commands/dmi.rb +1 -1
  13. data/lib/kanrisuru/core/file/commands/chmod.rb +1 -1
  14. data/lib/kanrisuru/core/file/commands/copy.rb +1 -3
  15. data/lib/kanrisuru/core/file/commands/mkdir.rb +11 -6
  16. data/lib/kanrisuru/core/file/commands/rm.rb +4 -1
  17. data/lib/kanrisuru/core/file/commands/touch.rb +2 -1
  18. data/lib/kanrisuru/core/ip/commands/address.rb +64 -47
  19. data/lib/kanrisuru/core/ip/commands/address_label.rb +32 -16
  20. data/lib/kanrisuru/core/ip/commands/link.rb +96 -54
  21. data/lib/kanrisuru/core/ip/commands/link_set_opts.rb +61 -0
  22. data/lib/kanrisuru/core/ip/commands/link_type_opts.rb +313 -0
  23. data/lib/kanrisuru/core/ip/commands/maddress.rb +22 -13
  24. data/lib/kanrisuru/core/ip/commands/neighbour.rb +49 -32
  25. data/lib/kanrisuru/core/ip/commands/route.rb +130 -93
  26. data/lib/kanrisuru/core/ip/commands/rule.rb +37 -22
  27. data/lib/kanrisuru/core/ip/commands.rb +5 -3
  28. data/lib/kanrisuru/core/ip/constants.rb +12 -0
  29. data/lib/kanrisuru/core/ip/parser.rb +1 -0
  30. data/lib/kanrisuru/core/ip/parsers/version.rb +15 -0
  31. data/lib/kanrisuru/core/ip.rb +10 -7
  32. data/lib/kanrisuru/core/system/commands/kill.rb +1 -1
  33. data/lib/kanrisuru/core/user/commands/create_user.rb +9 -17
  34. data/lib/kanrisuru/core/user/commands/delete_user.rb +1 -1
  35. data/lib/kanrisuru/core/user/commands/update_user.rb +14 -23
  36. data/lib/kanrisuru/core/zypper/commands/add_repo.rb +1 -8
  37. data/lib/kanrisuru/core/zypper/commands/add_service.rb +4 -2
  38. data/lib/kanrisuru/core/zypper/commands/info.rb +1 -2
  39. data/lib/kanrisuru/core/zypper/commands/install.rb +2 -3
  40. data/lib/kanrisuru/core/zypper/commands/modify_repo.rb +1 -7
  41. data/lib/kanrisuru/core/zypper/commands/modify_service.rb +3 -1
  42. data/lib/kanrisuru/core/zypper/commands/remove.rb +1 -2
  43. data/lib/kanrisuru/core/zypper/commands/remove_repo.rb +3 -3
  44. data/lib/kanrisuru/core/zypper/commands/remove_service.rb +6 -1
  45. data/lib/kanrisuru/core/zypper/commands/search.rb +1 -3
  46. data/lib/kanrisuru/core/zypper/commands/source_install.rb +2 -0
  47. data/lib/kanrisuru/core/zypper/commands.rb +10 -1
  48. data/lib/kanrisuru/os_package.rb +2 -0
  49. data/lib/kanrisuru/remote/host.rb +1 -3
  50. data/lib/kanrisuru/result.rb +15 -0
  51. data/lib/kanrisuru/version.rb +1 -1
  52. data/spec/functional/core/archive_spec.rb +1 -1
  53. data/spec/functional/core/disk_spec.rb +77 -0
  54. data/spec/functional/core/dmi_spec.rb +78 -0
  55. data/spec/functional/core/file_spec.rb +284 -0
  56. data/spec/functional/core/group_spec.rb +62 -0
  57. data/spec/functional/core/ip_address_label_spec.rb +81 -0
  58. data/spec/functional/core/ip_address_spec.rb +95 -0
  59. data/spec/functional/core/ip_link_spec.rb +814 -0
  60. data/spec/functional/core/ip_maddress_spec.rb +78 -0
  61. data/spec/functional/core/ip_neighbour_spec.rb +119 -0
  62. data/spec/functional/core/ip_route_spec.rb +174 -0
  63. data/spec/functional/core/ip_rule_spec.rb +75 -0
  64. data/spec/functional/core/ip_spec.rb +27 -0
  65. data/spec/functional/core/system_spec.rb +135 -0
  66. data/spec/functional/core/user_spec.rb +97 -0
  67. data/spec/functional/core/zypper_spec.rb +708 -0
  68. data/spec/functional/result_spec.rb +91 -44
  69. data/spec/helper/stub_network.rb +7 -3
  70. data/spec/support/shared_examples/integration/core/transfer.rb +1 -1
  71. data/spec/unit/command_spec.rb +2 -0
  72. data/spec/unit/core/ip_spec.rb +12 -0
  73. metadata +25 -4
@@ -0,0 +1,284 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::File do
6
+ before(:all) do
7
+ StubNetwork.stub!
8
+ end
9
+
10
+ after(:all) do
11
+ StubNetwork.unstub!
12
+ end
13
+
14
+ let(:host) do
15
+ Kanrisuru::Remote::Host.new(
16
+ host: 'localhost',
17
+ username: 'ubuntu',
18
+ keys: ['id_rsa']
19
+ )
20
+ end
21
+
22
+ it 'prepares chmod command' do
23
+ expect_command(host.chmod('/home/ubuntu/file1.txt', '-rwxrwxrwx'), 'chmod 777 /home/ubuntu/file1.txt')
24
+ expect_command(host.chmod('/home/ubuntu/dir', 'drwxrwxrwx', recursive: true), 'chmod 777 /home/ubuntu/dir -R')
25
+ expect_command(host.chmod('/home/ubuntu/dir', '600', recursive: true), 'chmod 600 /home/ubuntu/dir -R')
26
+ expect_command(host.chmod('/home/ubuntu/file2.conf', Kanrisuru::Mode.new('-rwxr--r--')),
27
+ 'chmod 744 /home/ubuntu/file2.conf')
28
+ expect_command(host.chmod('/home/ubuntu/file2.conf', 'g+x'), 'chmod g+x /home/ubuntu/file2.conf')
29
+ end
30
+
31
+ context 'with user and group stubs' do
32
+ before(:all) do
33
+ StubNetwork.stub_command!(:get_uid) do
34
+ 1000
35
+ end
36
+
37
+ StubNetwork.stub_command!(:get_gid) do
38
+ 1000
39
+ end
40
+ end
41
+
42
+ after(:all) do
43
+ StubNetwork.unstub_command!(:get_uid)
44
+ StubNetwork.unstub_command!(:get_gid)
45
+ end
46
+
47
+ it 'prepares chown command' do
48
+ expect_command(host.chown('/home/ubuntu/file1.text', owner: 'ubuntu', group: 'ubuntu'),
49
+ 'chown 1000:1000 /home/ubuntu/file1.text')
50
+ expect_command(host.chown('/home/ubuntu/file1.text', owner: 'ubuntu'), 'chown 1000 /home/ubuntu/file1.text')
51
+ expect_command(host.chown('/home/ubuntu/file1.text', group: 'ubuntu'), 'chown :1000 /home/ubuntu/file1.text')
52
+ expect(host.chown('/home/ubuntu/file1.txt')).to be_falsey
53
+ expect_command(host.chown('/home/ubuntu/dir', owner: 1000, group: 1000, recursive: true),
54
+ 'chown 1000:1000 -R /home/ubuntu/dir')
55
+ end
56
+ end
57
+
58
+ it 'prepares copy command' do
59
+ expect_command(host.copy('~/fileA', '~/fileB'), 'cp ~/fileA ~/fileB')
60
+ expect_command(host.copy('~/fileA', '~/fileB'), 'cp ~/fileA ~/fileB')
61
+ expect_command(host.copy('~/dir1', '~/dir2', {
62
+ recursive: true,
63
+ one_file_system: true,
64
+ update: true,
65
+ no_clobber: true,
66
+ strip_trailing_slashes: true
67
+ }), 'cp -R -x -u -n --strip-trailing-slashes ~/dir1 ~/dir2')
68
+
69
+ expect_command(host.copy('~/file1', '~/file1.copy', {
70
+ backup: true
71
+ }), 'cp -b ~/file1 ~/file1.copy')
72
+
73
+ expect_command(host.copy('~/file1', '~/file1.copy', {
74
+ backup: 'simple'
75
+ }), 'cp --backup=simple ~/file1 ~/file1.copy')
76
+
77
+ expect do
78
+ host.cp('~/file1', '~/file1.copy', { backup: 'forever' })
79
+ end.to raise_error(ArgumentError)
80
+
81
+ expect_command(host.cp('~/file1', '~/file2', { preserve: true }), 'cp -p ~/file1 ~/file2')
82
+ expect_command(host.cp('~/file1', '~/file2', { preserve: 'mode' }), 'cp --preserve=mode ~/file1 ~/file2')
83
+ expect_command(host.cp('~/file1', '~/file2', { preserve: %w[timestamps links] }),
84
+ 'cp --preserve=timestamps,links ~/file1 ~/file2')
85
+ expect_command(host.copy('~/fileA', '~/fileB', { no_target_directory: true }), 'cp -T ~/fileA ~/fileB')
86
+ expect_command(host.copy('~/fileA', '~/dirB', { target_directory: true }), 'cp -t ~/dirB ~/fileA')
87
+ end
88
+
89
+ context 'with dir stubs' do
90
+ before(:all) do
91
+ StubNetwork.stub_command!(:dir?, { return_value: true })
92
+ end
93
+
94
+ after(:all) do
95
+ StubNetwork.unstub_command!(:dir?)
96
+ end
97
+
98
+ it 'prepares link command' do
99
+ expect(host.ln('/home/ubuntu/dir', '/home/ubuntu/file1')).to be_falsey
100
+ end
101
+ end
102
+
103
+ context 'with dir stubs false' do
104
+ before(:all) do
105
+ StubNetwork.stub_command!(:dir?, { return_value: false })
106
+ end
107
+
108
+ after(:all) do
109
+ StubNetwork.unstub_command!(:dir?)
110
+ end
111
+
112
+ it 'prepares link command' do
113
+ expect_command(host.link('/home/ubuntu/file1', '/home/ubuntu/dir/file1'),
114
+ 'ln /home/ubuntu/file1 /home/ubuntu/dir/file1')
115
+
116
+ expect_command(host.ln('/home/ubuntu/file1', '/home/ubuntu/dir/file1', { force: true }),
117
+ 'ln /home/ubuntu/file1 /home/ubuntu/dir/file1 -f')
118
+ end
119
+ end
120
+
121
+ it 'prepares mkdir command' do
122
+ expect_command(host.mkdir('/home/ubuntu/dir1'), 'mkdir /home/ubuntu/dir1')
123
+ expect_command(host.mkdir(['~/dir1', '~/dir2', '~/dir3'], { silent: true }), 'mkdir ~/dir1 ~/dir2 ~/dir3 -p')
124
+ expect_command(host.mkdir('dir', { mode: '644' }), 'mkdir dir -m 644')
125
+ expect_command(host.mkdir(['dir2', '/etc/ddir'], { mode: Kanrisuru::Mode.new('dr--r--r--') }),
126
+ 'mkdir dir2 /etc/ddir -m 444')
127
+ expect_command(host.mkdir(['dir2', '/etc/ddir'], { mode: 'o+w' }), 'mkdir dir2 /etc/ddir -m o+w')
128
+ end
129
+
130
+ it 'prepares move command' do
131
+ expect_command(host.mv('~/fileA', '~/fileB'), 'mv ~/fileA ~/fileB')
132
+ expect_command(host.move('~/fileA', '~/fileB', { force: true }), 'mv -f ~/fileA ~/fileB')
133
+ expect_command(host.mv('~/fileA', '~/fileB', { no_clobber: true }), 'mv -n ~/fileA ~/fileB')
134
+ expect_command(host.mv('~/fileA', '~/fileB', { strip_trailing_slashes: true, force: true }),
135
+ 'mv --strip-trailing-slashes -f ~/fileA ~/fileB')
136
+ expect_command(host.mv('~/fileA', '~/fileB', { backup: true }), 'mv -b ~/fileA ~/fileB')
137
+ expect_command(host.mv('~/fileA', '~/fileB', { backup: 'numbered' }), 'mv --backup=numbered ~/fileA ~/fileB')
138
+
139
+ expect do
140
+ host.mv('~/fileA', '~/fileB', { backup: 'null' })
141
+ end.to raise_error(ArgumentError)
142
+
143
+ expect_command(host.mv('~/fileA', '~/fileB', { no_target_directory: true }), 'mv -T ~/fileA ~/fileB')
144
+ expect_command(host.mv(['~/fileA', '~/fileB'], '~/dir1', { target_directory: true }),
145
+ 'mv -t ~/dir1 ~/fileA ~/fileB')
146
+ expect_command(host.mv(['~/fileA', '~/fileB'], '~/dir1'), 'mv ~/fileA ~/fileB ~/dir1')
147
+ end
148
+
149
+ context 'with realpath non root' do
150
+ before(:all) do
151
+ StubNetwork.stub_command!(:realpath) do
152
+ Kanrisuru::Core::Path::FilePath.new('/home/ubuntu/fileA')
153
+ end
154
+ end
155
+
156
+ after(:all) do
157
+ StubNetwork.unstub_command!(:realpath)
158
+ end
159
+
160
+ it 'prepares rm command' do
161
+ expect_command(host.rm('~/fileA'), 'rm ~/fileA --preserve-root')
162
+ expect_command(host.rm('~/fileA', { force: true }), 'rm ~/fileA --preserve-root -f')
163
+ expect_command(host.rm('~/dirA', { force: true, recursive: true }), 'rm ~/dirA --preserve-root -f -r')
164
+ expect_command(host.rm(['~/dirA', '~/dirB'], { force: true, recursive: true }),
165
+ 'rm ~/dirA ~/dirB --preserve-root -f -r')
166
+ end
167
+
168
+ it 'prepares rmdir command' do
169
+ expect_command(host.rmdir('~/dir1'), 'rmdir ~/dir1')
170
+ expect_command(host.rmdir(['~/dir1', '~/dir2'], { silent: true, parents: true }),
171
+ 'rmdir ~/dir1 ~/dir2 --ignore-fail-on-non-empty --parents')
172
+ end
173
+ end
174
+
175
+ context 'with realpath root' do
176
+ before(:all) do
177
+ StubNetwork.stub_command!(:realpath) do
178
+ Kanrisuru::Core::Path::FilePath.new('/')
179
+ end
180
+ end
181
+
182
+ after(:all) do
183
+ StubNetwork.unstub_command!(:realpath)
184
+ end
185
+
186
+ it 'prepares rm command' do
187
+ expect do
188
+ host.rm('/', { force: true, recursive: true })
189
+ end.to raise_error(ArgumentError)
190
+
191
+ expect do
192
+ host.rm('/etc/..', { force: true, recursive: true })
193
+ end.to raise_error(ArgumentError)
194
+ end
195
+
196
+ it 'prepares rmdir command' do
197
+ expect do
198
+ host.rmdir('/etc/..', { silent: true })
199
+ end.to raise_error(ArgumentError)
200
+ end
201
+ end
202
+
203
+ context 'with stub' do
204
+ before(:all) do
205
+ StubNetwork.stub_command!(:realpath) do |cmd|
206
+ result = cmd[0].gsub('~', '/home/ubuntu')
207
+ Kanrisuru::Core::Path::FilePath.new(result)
208
+ end
209
+
210
+ StubNetwork.stub_command!(:inode?, { return_value: true })
211
+ StubNetwork.stub_command!(:symlink?, { return_value: false })
212
+ end
213
+
214
+ after(:all) do
215
+ StubNetwork.unstub_command!(:realpath)
216
+ StubNetwork.unstub_command!(:inode?)
217
+ StubNetwork.unstub_command!(:symlink?)
218
+ end
219
+
220
+ it 'prepares symlink command' do
221
+ StubNetwork.stub_command!(:dir?, { return_value: false })
222
+ expect_command(host.ln_s('~/fileA', '/etc/fileA'), 'ln -s /home/ubuntu/fileA /etc/fileA')
223
+ expect_command(host.ln_s('~/fileA', '/etc/fileA', { force: true }), 'ln -s /home/ubuntu/fileA /etc/fileA -f')
224
+ StubNetwork.unstub_command!(:dir?)
225
+
226
+ StubNetwork.stub_command!(:dir?, { return_value: true })
227
+ expect_command(host.symlink('~/fileA', '~/dirA', { force: true }),
228
+ 'ln -s /home/ubuntu/fileA /home/ubuntu/dirA -f')
229
+ StubNetwork.unstub_command!(:dir?)
230
+ end
231
+ end
232
+
233
+ context 'with broken stub' do
234
+ it 'prepares symlink command' do
235
+ StubNetwork.stub_command!(:inode?, { return_value: true })
236
+ StubNetwork.stub_command!(:realpath) do |_cmd|
237
+ Kanrisuru::Core::Path::FilePath.new('/')
238
+ end
239
+
240
+ expect do
241
+ host.symlink('/', '~/fileB')
242
+ end.to raise_error(ArgumentError)
243
+
244
+ expect do
245
+ host.symlink('/var/..', '~/fileB')
246
+ end.to raise_error(ArgumentError)
247
+
248
+ StubNetwork.unstub_command!(:inode?)
249
+ StubNetwork.unstub_command!(:realpath)
250
+
251
+ StubNetwork.stub_command!(:inode?, { return_value: false })
252
+ StubNetwork.stub_command!(:realpath) do |_cmd|
253
+ Kanrisuru::Core::Path::FilePath.new('')
254
+ end
255
+
256
+ expect do
257
+ host.symlink('', '~/fileB')
258
+ end.to raise_error(ArgumentError)
259
+
260
+ expect do
261
+ host.symlink('/home', '~/fileB')
262
+ end.to raise_error(ArgumentError)
263
+
264
+ StubNetwork.unstub_command!(:inode?)
265
+ StubNetwork.unstub_command!(:realpath)
266
+ end
267
+ end
268
+
269
+ it 'prepares touch command' do
270
+ expect_command(host.touch('file.conf'), 'touch file.conf')
271
+ expect_command(host.touch(['file1.conf', 'file2.conf']), 'touch file1.conf file2.conf')
272
+ expect_command(host.touch('file.conf', { atime: true, mtime: true, nofiles: true }), 'touch file.conf -a -m -c')
273
+ expect_command(host.touch('file.conf', { date: '2021-12-31' }), 'touch file.conf -d 2021-12-31')
274
+ expect_command(host.touch('file.conf', { reference: '~/otherfile.conf' }), 'touch file.conf -r ~/otherfile.conf')
275
+ end
276
+
277
+ it 'prepares unlink command' do
278
+ expect_command(host.unlink('/home/ubuntu/file1'), 'unlink /home/ubuntu/file1')
279
+ end
280
+
281
+ it 'prepares wc command' do
282
+ expect_command(host.wc('/var/log/syslog'), 'wc /var/log/syslog')
283
+ end
284
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Group do
6
+ before(:all) do
7
+ StubNetwork.stub!
8
+ end
9
+
10
+ after(:all) do
11
+ StubNetwork.unstub!
12
+ end
13
+
14
+ let(:host) do
15
+ Kanrisuru::Remote::Host.new(
16
+ host: 'localhost',
17
+ username: 'ubuntu',
18
+ keys: ['id_rsa']
19
+ )
20
+ end
21
+
22
+ it 'prepares create_group command' do
23
+ expect_command(host.create_group('admin'), 'groupadd admin')
24
+ expect_command(host.create_group('admin', { gid: 9000 }), 'groupadd admin -g 9000')
25
+ end
26
+
27
+ it 'prepares delete_group command' do
28
+ StubNetwork.stub_command!(:group?, { return_value: true })
29
+ expect_command(host.delete_group('admin'), 'groupdel admin')
30
+ StubNetwork.unstub_command!(:group?)
31
+ StubNetwork.stub_command!(:group?, { return_value: false })
32
+ expect(host.delete_group('admin')).to be_falsey
33
+ StubNetwork.unstub_command!(:group?)
34
+ end
35
+
36
+ it 'prepares get_gid command' do
37
+ expect_command(host.get_gid('ubuntu'), 'getent group ubuntu')
38
+ end
39
+
40
+ it 'prepares get_group command' do
41
+ expect_command(host.get_group('ubuntu'), 'getent group ubuntu | cut -d: -f4')
42
+ end
43
+
44
+ it 'prepares group? command' do
45
+ StubNetwork.stub_command!(:get_gid) do
46
+ 1000
47
+ end
48
+ expect(host).to be_group('ubuntu')
49
+ StubNetwork.unstub_command!(:get_gid)
50
+
51
+ StubNetwork.stub_command!(:get_gid, { status: 1 })
52
+ expect(host).not_to be_group('ubuntu')
53
+ StubNetwork.unstub_command!(:get_gid)
54
+ end
55
+
56
+ it 'prepares update_group command' do
57
+ expect_command(host.update_group('www-data', { gid: 34, new_name: 'web' }), 'groupmod www-data -g 34 -n web')
58
+ expect(host.update_group('www-data')).to be_nil
59
+ expect(host.update_group('www-data', { gid: 0, new_name: '' })).to be_nil
60
+ expect(host.update_group('www-data', { gid: '', new_name: 'test' })).to be_nil
61
+ end
62
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::IP do
6
+ before(:all) do
7
+ StubNetwork.stub!
8
+ end
9
+
10
+ after(:all) do
11
+ StubNetwork.unstub!
12
+ end
13
+
14
+ let(:host) do
15
+ Kanrisuru::Remote::Host.new(
16
+ host: 'localhost',
17
+ username: 'ubuntu',
18
+ keys: ['id_rsa']
19
+ )
20
+ end
21
+
22
+ %w[addrlabel addrl].each do |object_variant|
23
+ context "with ip #{object_variant}" do
24
+ context 'with json support' do
25
+ before(:all) do
26
+ StubNetwork.stub_command!(:ip_version) do
27
+ Kanrisuru::Core::IP::IPROUTE2_JSON_VERSION
28
+ end
29
+ end
30
+
31
+ after(:all) do
32
+ StubNetwork.unstub_command!(:ip_version)
33
+ end
34
+
35
+ %w[show list].each do |action_variant|
36
+ it "prepares #{action_variant} command" do
37
+ expect_command(host.ip(object_variant, action_variant), 'ip -json addrlabel list')
38
+ end
39
+ end
40
+ end
41
+
42
+ context 'without json support' do
43
+ before(:all) do
44
+ StubNetwork.stub_command!(:ip_version) do
45
+ Kanrisuru::Core::IP::IPROUTE2_JSON_VERSION - 10
46
+ end
47
+ end
48
+
49
+ after(:all) do
50
+ StubNetwork.unstub_command!(:ip_version)
51
+ end
52
+
53
+ %w[show list].each do |action_variant|
54
+ it "prepares #{action_variant} command" do
55
+ expect_command(host.ip(object_variant, action_variant), 'ip addrlabel list')
56
+ end
57
+ end
58
+ end
59
+
60
+ it 'prepares flush command' do
61
+ expect_command(host.ip(object_variant, 'flush'), 'ip addrlabel flush')
62
+ end
63
+
64
+ it 'prepares add command' do
65
+ expect_command(host.ip(object_variant, 'add', {
66
+ prefix: '0.0.0.0/96',
67
+ dev: 'eno2',
68
+ label: 'eno2'
69
+ }), 'ip addrlabel add prefix 0.0.0.0/96 dev eno2 label eno2')
70
+ end
71
+
72
+ it 'prepares del command' do
73
+ expect_command(host.ip(object_variant, 'del', {
74
+ prefix: '0.0.0.0/96',
75
+ dev: 'eno2',
76
+ label: 'eno2'
77
+ }), 'ip addrlabel del prefix 0.0.0.0/96 dev eno2 label eno2')
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::IP do
6
+ before(:all) do
7
+ StubNetwork.stub!
8
+ end
9
+
10
+ after(:all) do
11
+ StubNetwork.unstub!
12
+ end
13
+
14
+ let(:host) do
15
+ Kanrisuru::Remote::Host.new(
16
+ host: 'localhost',
17
+ username: 'ubuntu',
18
+ keys: ['id_rsa']
19
+ )
20
+ end
21
+
22
+ %w[address addr a].each do |object_variant|
23
+ context "with ip #{object_variant}" do
24
+ before(:all) do
25
+ StubNetwork.stub_command!(:ip_version) do
26
+ Kanrisuru::Core::IP::IPROUTE2_JSON_VERSION
27
+ end
28
+ end
29
+
30
+ after(:all) do
31
+ StubNetwork.unstub_command!(:ip_version)
32
+ end
33
+
34
+ %w[show list].each do |action_variant|
35
+ it "prepares #{action_variant} command" do
36
+ expect_command(host.ip(object_variant, action_variant), 'ip -json address show')
37
+ expect_command(host.ip(object_variant, action_variant, {
38
+ stats: true,
39
+ family: 'inet',
40
+ dev: 'lo',
41
+ scope: 'host',
42
+ prefix: '127.0.0.1',
43
+ label: 'lo'
44
+ }), 'ip -json -s -family inet address show dev lo scope host to 127.0.0.1 label lo')
45
+
46
+ expect_command(host.ip(object_variant, action_variant, {
47
+ dynamic: true,
48
+ permanent: true,
49
+ deprecated: true,
50
+ primary: true,
51
+ secondary: true,
52
+ up: true
53
+ }), 'ip -json address show dynamic permanent deprecated primary secondary up')
54
+ end
55
+ end
56
+
57
+ it 'prepares add command' do
58
+ expect_command(host.ip(object_variant, 'add', {
59
+ address: '10.0.0.1',
60
+ dev: 'eno1',
61
+ label: 'eno1',
62
+ scope: 'link'
63
+ }), 'ip address add 10.0.0.1 dev eno1 label eno1 scope link')
64
+ end
65
+
66
+ %w[del delete].each do |action_variant|
67
+ it "prepares #{action_variant} command" do
68
+ expect_command(host.ip(object_variant, action_variant, {
69
+ address: '10.0.0.1',
70
+ dev: 'eno1',
71
+ label: 'eno1',
72
+ scope: 'link'
73
+ }), 'ip address del 10.0.0.1 dev eno1 label eno1 scope link')
74
+ end
75
+ end
76
+
77
+ it 'prepares flush command' do
78
+ expect_command(host.ip(object_variant, 'flush', {
79
+ dev: 'eno1',
80
+ scope: 'link',
81
+ prefix: 'eno1',
82
+ label: 'eno1'
83
+ }), 'ip address flush dev eno1 scope link to eno1 label eno1')
84
+
85
+ expect_command(host.ip(object_variant, 'flush', {
86
+ dynamic: true,
87
+ permanent: true,
88
+ deprecated: true,
89
+ primary: true,
90
+ secondary: true
91
+ }), 'ip address flush dynamic permanent deprecated primary secondary')
92
+ end
93
+ end
94
+ end
95
+ end