kanrisuru 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/kanrisuru.gemspec +7 -3
  4. data/lib/kanrisuru/command.rb +5 -1
  5. data/lib/kanrisuru/core/disk/commands/lsblk.rb +6 -11
  6. data/lib/kanrisuru/core/disk/constants.rb +9 -0
  7. data/lib/kanrisuru/core/disk/parser.rb +1 -0
  8. data/lib/kanrisuru/core/disk/parsers/lsblk_version.rb +21 -0
  9. data/lib/kanrisuru/core/disk.rb +1 -0
  10. data/lib/kanrisuru/core/dmi/commands/dmi.rb +1 -1
  11. data/lib/kanrisuru/core/file/commands/chmod.rb +1 -1
  12. data/lib/kanrisuru/core/file/commands/copy.rb +1 -3
  13. data/lib/kanrisuru/core/file/commands/mkdir.rb +11 -6
  14. data/lib/kanrisuru/core/file/commands/rm.rb +4 -1
  15. data/lib/kanrisuru/core/file/commands/touch.rb +2 -1
  16. data/lib/kanrisuru/core/system/commands/kill.rb +1 -1
  17. data/lib/kanrisuru/core/user/commands/create_user.rb +9 -17
  18. data/lib/kanrisuru/core/user/commands/delete_user.rb +1 -1
  19. data/lib/kanrisuru/core/user/commands/update_user.rb +14 -23
  20. data/lib/kanrisuru/result.rb +15 -0
  21. data/lib/kanrisuru/version.rb +1 -1
  22. data/spec/functional/core/archive_spec.rb +1 -1
  23. data/spec/functional/core/disk_spec.rb +77 -0
  24. data/spec/functional/core/dmi_spec.rb +78 -0
  25. data/spec/functional/core/file_spec.rb +284 -0
  26. data/spec/functional/core/group_spec.rb +62 -0
  27. data/spec/functional/core/system_spec.rb +135 -0
  28. data/spec/functional/core/user_spec.rb +97 -0
  29. data/spec/functional/result_spec.rb +91 -44
  30. data/spec/helper/stub_network.rb +6 -2
  31. data/spec/unit/command_spec.rb +2 -0
  32. data/spec/unit/core/ip_spec.rb +12 -0
  33. metadata +13 -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,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::System 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 cpu_info command' do
23
+ expect_command(host.cpu_info('sockets'), "lscpu | grep -i '^Socket' | awk '{print $NF}'")
24
+ expect_command(host.cpu_info('cores_per_socket'), "lscpu | grep -i '^Core' | awk '{print $NF}'")
25
+ expect_command(host.cpu_info('threads'), "lscpu | grep -i '^Thread' | awk '{print $NF}'")
26
+
27
+ allow(Kanrisuru.logger).to receive(:info)
28
+ expect(Kanrisuru.logger).to receive(:info) do |&block|
29
+ expect(block.call).to eq('DEPRECATION WARNING: cpu_info will be removed in the upcoming major release. Use lscpu instead.')
30
+ end
31
+
32
+ expect_command(host.cpu_info('cores'), "lscpu | grep -i '^CPU(' | awk '{print $NF}'")
33
+ end
34
+
35
+ it 'prepares free command' do
36
+ expect_command(host.free('total'), "cat /proc/meminfo | grep -i '^MemTotal' | awk '{print $2}'")
37
+ expect_command(host.free('free'), "cat /proc/meminfo | grep -i '^MemFree' | awk '{print $2}'")
38
+ expect_command(host.free('swap'), "cat /proc/meminfo | grep -i '^SwapTotal' | awk '{print $2}'")
39
+ expect_command(host.free('swap_free'), "cat /proc/meminfo | grep -i '^SwapFree' | awk '{print $2}'")
40
+
41
+ expect do
42
+ host.free('hello')
43
+ end.to raise_error(ArgumentError)
44
+ end
45
+
46
+ it 'prepares kernel_statistics command' do
47
+ expect_command(host.kernel_statistics, 'cat /proc/stat')
48
+ end
49
+
50
+ it 'prepares kill command' do
51
+ expect_command(host.kill('KILL', 1024), 'kill -KILL 1024')
52
+ expect_command(host.kill(1, 1024), 'kill -HUP 1024')
53
+ expect_command(host.kill('TERM', [1024, 1025, 1026]), 'kill -TERM 1024 1025 1026')
54
+
55
+ expect do
56
+ host.kill('all', 0)
57
+ end.to raise_error(ArgumentError)
58
+ end
59
+
60
+ it 'prepares last command' do
61
+ expect_command(host.last, "last -i -F | sed 's/ / /'")
62
+ expect_command(host.last({ file: '/var/log/login.log' }), "last -i -F -f /var/log/login.log | sed 's/ / /'")
63
+ expect_command(host.last({ failed_attempts: true }), "lastb -i -F | sed 's/ / /'")
64
+ end
65
+
66
+ it 'prepares load_average command' do
67
+ expect_command(host.load_average, "cat /proc/loadavg | awk '{print $1,$2,$3}'")
68
+ end
69
+
70
+ it 'prepares load_env command' do
71
+ expect_command(host.load_env, 'env')
72
+ end
73
+
74
+ it 'prepares lscpu command' do
75
+ expect_command(host.lscpu, 'lscpu')
76
+ end
77
+
78
+ it 'prepares lsof command' do
79
+ expect_command(host.lsof, 'lsof -F pcuftDsin')
80
+ end
81
+
82
+ it 'prepares poweroff command' do
83
+ expect_command(host.poweroff, 'shutdown')
84
+ expect_command(host.poweroff(cancel: true), 'shutdown -c')
85
+ expect_command(host.poweroff(cancel: true, message: 'canceling...'), 'shutdown -c canceling...')
86
+ expect_command(host.poweroff(no_wall: true), 'shutdown --no-wall')
87
+ expect_command(host.poweroff(time: 10), 'shutdown +10')
88
+ expect_command(host.poweroff(time: 'now'), 'shutdown now')
89
+ expect_command(host.poweroff(time: '11:11'), 'shutdown 11:11')
90
+ expect_command(host.poweroff(time: '11:11', message: 'turning off'), 'shutdown 11:11 turning off')
91
+
92
+ expect do
93
+ host.poweroff(time: 'hsadf')
94
+ end.to raise_error(ArgumentError)
95
+ end
96
+
97
+ it 'prepares ps command' do
98
+ expect_command(host.ps,
99
+ 'ps ww ax -o uid,user,gid,group,ppid,pid,pcpu,pmem,stat,pri,flags,policy,time,cmd --no-headers')
100
+ expect_command(host.ps(user: '1000', group: '1000'),
101
+ 'ps ww --user 1000 --group 1000 -o uid,user,gid,group,ppid,pid,pcpu,pmem,stat,pri,flags,policy,time,cmd --no-headers')
102
+ expect_command(host.ps(user: [0, 1000], group: [0, 1000]),
103
+ 'ps ww --user 0,1000 --group 0,1000 -o uid,user,gid,group,ppid,pid,pcpu,pmem,stat,pri,flags,policy,time,cmd --no-headers')
104
+ expect_command(host.ps(pid: 100),
105
+ 'ps ww --pid 100 -o uid,user,gid,group,ppid,pid,pcpu,pmem,stat,pri,flags,policy,time,cmd --no-headers')
106
+ expect_command(host.ps(pid: [100, 101, 102]),
107
+ 'ps ww --pid 100,101,102 -o uid,user,gid,group,ppid,pid,pcpu,pmem,stat,pri,flags,policy,time,cmd --no-headers')
108
+ expect_command(host.ps(pid: [100, 101, 102], ppid: [0, 3, 5]),
109
+ 'ps ww --pid 100,101,102 --ppid 0,3,5 -o uid,user,gid,group,ppid,pid,pcpu,pmem,stat,pri,flags,policy,time,cmd --no-headers')
110
+ end
111
+
112
+ it 'prepares reboot command' do
113
+ expect_command(host.reboot, 'shutdown -r')
114
+ expect_command(host.reboot(cancel: true), 'shutdown -c')
115
+ expect_command(host.reboot(cancel: true, message: 'canceling...'), 'shutdown -c canceling...')
116
+ expect_command(host.reboot(no_wall: true), 'shutdown -r --no-wall')
117
+ expect_command(host.reboot(time: 10), 'shutdown -r +10')
118
+ expect_command(host.reboot(time: 'now'), 'shutdown -r now')
119
+ expect_command(host.reboot(time: '11:11'), 'shutdown -r 11:11')
120
+ expect_command(host.reboot(time: '11:11', message: 'turning off'), 'shutdown -r 11:11 turning off')
121
+
122
+ expect do
123
+ host.reboot(time: 'hsadf')
124
+ end.to raise_error(ArgumentError)
125
+ end
126
+
127
+ it 'prepares uptime command' do
128
+ expect_command(host.uptime, 'cat /proc/uptime')
129
+ end
130
+
131
+ it 'prepares w command' do
132
+ expect_command(host.w, 'w -hi')
133
+ expect_command(host.who(users: 'centos'), 'w -hi centos')
134
+ end
135
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::User 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_user command' do
23
+ expect_command(host.create_user('bob'), 'useradd bob -s /bin/false')
24
+ expect_command(host.create_user('bob', uid: 5555), 'useradd bob -u 5555 -s /bin/false')
25
+ expect_command(host.create_user('bob', uid: 5555, non_unique: true), 'useradd bob -u 5555 -o -s /bin/false')
26
+ expect_command(host.create_user('bob', system: true), 'useradd bob -r -s /bin/false')
27
+ expect_command(host.create_user('bob', shell: '/bin/bash'), 'useradd bob -s /bin/bash')
28
+ expect_command(host.create_user('bob', home: '/home/bob1'), 'useradd bob -s /bin/false -d /home/bob1')
29
+ expect_command(host.create_user('bob', home: '/home/bob1', createhome: true),
30
+ 'useradd bob -s /bin/false -d /home/bob1 -m')
31
+ expect_command(host.create_user('bob', home: '/home/bob1', createhome: true, skeleton: '/home/bob1skele'),
32
+ 'useradd bob -s /bin/false -d /home/bob1 -m -k /home/bob1skele')
33
+ expect_command(host.create_user('bob', createhome: false), 'useradd bob -s /bin/false -M')
34
+ expect_command(host.create_user('bob', password: '12345678'), 'useradd bob -s /bin/false -p 12345678')
35
+ expect_command(host.create_user('bob', expires: '2021-12-31'), 'useradd bob -s /bin/false -e 2021-12-31')
36
+
37
+ expect_command(host.create_user('bob', groups: %w[www-data sudo admin]),
38
+ 'useradd bob -s /bin/false -G www-data,sudo,admin')
39
+
40
+ StubNetwork.stub_command!(:group?, { return_value: true })
41
+ expect_command(host.create_user('bob', group: 'www-data'), 'useradd bob -s /bin/false -g www-data')
42
+ expect_command(host.create_user('bob'), 'useradd bob -s /bin/false -N')
43
+ StubNetwork.unstub_command!(:group?)
44
+ end
45
+
46
+ it 'prepares delete_user command' do
47
+ StubNetwork.stub_command!(:get_uid) { 1000 }
48
+ expect_command(host.delete_user('ubuntu'), 'userdel ubuntu')
49
+ expect_command(host.delete_user('ubuntu', force: true), 'userdel ubuntu -f')
50
+ StubNetwork.unstub_command!(:get_uid)
51
+
52
+ StubNetwork.stub_command!(:get_uid, { status: 1 })
53
+ expect(host.delete_user('ubuntu')).to be_falsey
54
+ StubNetwork.unstub_command!(:get_uid)
55
+ end
56
+
57
+ it 'prepares get_uid command' do
58
+ expect_command(host.get_uid('ubuntu'), 'id -u ubuntu')
59
+ end
60
+
61
+ it 'prepares get_user command' do
62
+ expect_command(host.get_user('ubuntu'), 'id ubuntu')
63
+ end
64
+
65
+ it 'prepares user? command' do
66
+ StubNetwork.stub_command!(:get_uid) { 1000 }
67
+ expect(host).to be_user('ubuntu')
68
+ StubNetwork.unstub_command!(:get_uid)
69
+
70
+ StubNetwork.stub_command!(:get_uid, { status: 1 })
71
+ expect(host).not_to be_group('ubuntu')
72
+ StubNetwork.unstub_command!(:get_uid)
73
+ end
74
+
75
+ it 'prepares update_user command' do
76
+ expect_command(host.update_user('bob', home: '/home/bob'), 'usermod bob -d /home/bob')
77
+ expect_command(host.update_user('bob', home: '/home/bob', move_home: true), 'usermod bob -d /home/bob -m')
78
+ expect_command(host.update_user('bob', home: '/home/bob', move_home: true), 'usermod bob -d /home/bob -m')
79
+ expect_command(host.update_user('bob', shell: '/bin/zsh'), 'usermod bob -s /bin/zsh')
80
+ expect_command(host.update_user('bob', uid: 6431), 'usermod bob -u 6431')
81
+ expect_command(host.update_user('bob', uid: 1000, non_unique: true), 'usermod bob -u 1000 -o')
82
+
83
+ StubNetwork.stub_command!(:group?, { return_value: true })
84
+ expect_command(host.update_user('bob', group: 'backup'), 'usermod bob -g backup')
85
+ StubNetwork.unstub_command!(:group?)
86
+
87
+ expect_command(host.update_user('bob', groups: 'backup'), 'usermod bob -G backup')
88
+ expect_command(host.update_user('bob', groups: %w[backup mail]), 'usermod bob -G backup,mail')
89
+ expect_command(host.update_user('bob', groups: %w[backup mail], append: true), 'usermod bob -G backup,mail -a')
90
+
91
+ expect_command(host.update_user('bob', locked: true), 'usermod bob -L -e 1')
92
+ expect_command(host.update_user('bob', locked: false), 'usermod bob -U -e 99999')
93
+ expect_command(host.update_user('bob', locked: false, password: '123456'), 'usermod bob -U -e 99999')
94
+ expect_command(host.update_user('bob', password: '123456'), 'usermod bob -p 123456')
95
+ expect_command(host.update_user('bob', expires: '2022-01-01'), 'usermod bob -e 2022-01-01')
96
+ end
97
+ end