kanrisuru 0.8.14 → 0.8.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/lib/kanrisuru/core/disk.rb +1 -1
  4. data/lib/kanrisuru/core/find.rb +12 -5
  5. data/lib/kanrisuru/core/stat.rb +1 -3
  6. data/lib/kanrisuru/core/transfer.rb +12 -1
  7. data/lib/kanrisuru/version.rb +1 -1
  8. data/spec/functional/core/find_spec.rb +156 -45
  9. data/spec/functional/core/path_spec.rb +32 -80
  10. data/spec/functional/core/stat_spec.rb +14 -89
  11. data/spec/functional/core/stream_spec.rb +103 -112
  12. data/spec/functional/core/transfer_spec.rb +175 -102
  13. data/spec/helper/expect_helpers.rb +8 -0
  14. data/spec/helper/stub_network.rb +43 -0
  15. data/spec/{functional → integration}/core/apt_spec.rb +0 -0
  16. data/spec/{functional → integration}/core/archive_spec.rb +0 -0
  17. data/spec/{functional → integration}/core/disk_spec.rb +1 -1
  18. data/spec/{functional → integration}/core/dmi_spec.rb +0 -0
  19. data/spec/{functional → integration}/core/file_spec.rb +8 -5
  20. data/spec/integration/core/find_spec.rb +52 -0
  21. data/spec/{functional → integration}/core/group_spec.rb +0 -0
  22. data/spec/{functional → integration}/core/ip_spec.rb +0 -0
  23. data/spec/integration/core/path_spec.rb +93 -0
  24. data/spec/{functional → integration}/core/socket_spec.rb +0 -0
  25. data/spec/integration/core/stat_spec.rb +98 -0
  26. data/spec/integration/core/stream_spec.rb +117 -0
  27. data/spec/{functional → integration}/core/system_spec.rb +0 -0
  28. data/spec/integration/core/transfer_spec.rb +108 -0
  29. data/spec/{functional → integration}/core/user_spec.rb +0 -0
  30. data/spec/{functional → integration}/core/yum_spec.rb +0 -0
  31. data/spec/{functional → integration}/core/zypper_spec.rb +0 -0
  32. data/spec/{functional → integration}/remote/cluster_spec.rb +0 -0
  33. data/spec/{functional → integration}/remote/cpu_spec.rb +0 -0
  34. data/spec/{functional → integration}/remote/env_spec.rb +0 -0
  35. data/spec/{functional → integration}/remote/fstab_spec.rb +0 -0
  36. data/spec/{functional → integration}/remote/host_spec.rb +0 -0
  37. data/spec/{functional → integration}/remote/memory_spec.rb +0 -0
  38. data/spec/{functional → integration}/remote/os_spec.rb +0 -0
  39. data/spec/{functional → integration}/remote/remote_file_spec.rb +0 -0
  40. data/spec/spec_helper.rb +3 -0
  41. data/spec/unit/core/find_spec.rb +3 -0
  42. metadata +29 -22
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Find do
6
+ TestHosts.each_os do |os_name|
7
+ context "with #{os_name}" do
8
+ let(:host_json) { TestHosts.host(os_name) }
9
+ let(:host) do
10
+ Kanrisuru::Remote::Host.new(
11
+ host: host_json['hostname'],
12
+ username: host_json['username'],
13
+ keys: [host_json['ssh_key']]
14
+ )
15
+ end
16
+
17
+ after do
18
+ host.disconnect
19
+ end
20
+
21
+ it 'finds home directory' do
22
+ result = host.find(paths: '/home')
23
+ home = result.find { |item| item.path == host_json['home'] }
24
+ expect(home.path).to eq(host_json['home'])
25
+
26
+ result = host.find(paths: ['/home'])
27
+ home = result.find { |item| item.path == host_json['home'] }
28
+ expect(home.path).to eq(host_json['home'])
29
+ end
30
+
31
+ it 'finds /etc/hosts file' do
32
+ host.su('root')
33
+ result = host.find(paths: '/etc', type: 'file', name: 'hosts', uid: 0, gid: 0, writeable: true, maxdepth: 1)
34
+ expect(result[0].path).to eq('/etc/hosts')
35
+
36
+ file = host.stat(result[0].path)
37
+ expect(file.uid).to eq(0)
38
+ expect(file.gid).to eq(0)
39
+ end
40
+
41
+ it 'finds files with regex' do
42
+ result = host.find(paths: '~', regex: '.*.\\(js\\|rb\\)')
43
+ expect(result.data).to be_instance_of(Array)
44
+ end
45
+
46
+ it 'finds files by size' do
47
+ result = host.find(paths: '~', size: '+2M', type: 'file')
48
+ expect(result.data).to be_instance_of(Array)
49
+ end
50
+ end
51
+ end
52
+ end
File without changes
File without changes
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Path do
6
+ TestHosts.each_os do |os_name|
7
+ context "with #{os_name}" do
8
+ let(:host_json) { TestHosts.host(os_name) }
9
+ let(:host) do
10
+ Kanrisuru::Remote::Host.new(
11
+ host: host_json['hostname'],
12
+ username: host_json['username'],
13
+ keys: [host_json['ssh_key']]
14
+ )
15
+ end
16
+
17
+ after do
18
+ host.disconnect
19
+ end
20
+
21
+ it 'lists files and directories' do
22
+ result = host.ls(all: true)
23
+ expect(result.data).to be_instance_of(Array)
24
+
25
+ dir = result.find { |file| file.path == '.' }
26
+ expect(dir.path).to eq('.')
27
+
28
+ result = host.ls(path: host_json['home'], id: true, all: true)
29
+ expect(result.data).to be_instance_of(Array)
30
+
31
+ file = result.find { |f| f.path == '.bashrc' }
32
+ expect(file.owner).to eq(1000)
33
+
34
+ case os_name
35
+ when 'opensuse', 'sles'
36
+ expect(file.group).to eq(100)
37
+ else
38
+ expect(file.group).to eq(1000)
39
+ end
40
+ end
41
+
42
+ it 'gets whoami' do
43
+ expect(host.whoami.user).to eq(host_json['username'])
44
+ end
45
+
46
+ it 'gets pwd' do
47
+ expect(host.pwd.path).to eq(host_json['home'])
48
+ end
49
+
50
+ it 'uses which to get path for bash' do
51
+ result = host.which('bash', all: true)
52
+ paths = result.map(&:path)
53
+
54
+ case os_name
55
+ when 'ubuntu'
56
+ if host.os.version <= 18.04
57
+ expect(paths).to include('/bin/bash')
58
+ else
59
+ expect(paths).to include('/usr/bin/bash', '/bin/bash')
60
+ end
61
+ when 'opensuse'
62
+ ## Ignore for local testing
63
+ when 'sles'
64
+ expect(paths).to include('/bin/bash')
65
+ else
66
+ expect(paths).to include('/usr/bin/bash', '/bin/bash')
67
+ end
68
+ end
69
+
70
+ it 'gets realpath for dir' do
71
+ case os_name
72
+ when 'sles'
73
+ expect(host.realpath('/var/run').path).to eq('/run')
74
+ when 'rhel'
75
+ expect(host.realpath('/bin').path).to eq('/usr/bin')
76
+ else
77
+ expect(host.realpath('/etc/os-release').path).to eq('/usr/lib/os-release')
78
+ end
79
+ end
80
+
81
+ it 'gets full path for dir with readlink' do
82
+ case os_name
83
+ when 'sles'
84
+ expect(host.readlink('/var/run', canonicalize: true).path).to eq('/run')
85
+ when 'rhel'
86
+ expect(host.readlink('/bin', canonicalize: true).path).to eq('/usr/bin')
87
+ else
88
+ expect(host.readlink('/etc/os-release', canonicalize: true).path).to eq('/usr/lib/os-release')
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Stat do
6
+ TestHosts.each_os do |os_name|
7
+ context "with #{os_name}" do
8
+ let(:host_json) { TestHosts.host(os_name) }
9
+ let(:host) do
10
+ Kanrisuru::Remote::Host.new(
11
+ host: host_json['hostname'],
12
+ username: host_json['username'],
13
+ keys: [host_json['ssh_key']]
14
+ )
15
+ end
16
+
17
+ after do
18
+ host.disconnect
19
+ end
20
+
21
+ it 'checks file type correctly' do
22
+ expect(host.dir?('/')).to eq(true)
23
+ expect(host.file?('/etc/hosts')).to eq(true)
24
+
25
+ case os_name
26
+ when 'sles'
27
+ expect(host.block_device?('/dev/xvda')).to eq(true)
28
+ else
29
+ expect(host.block_device?('/dev/vda')).to eq(true)
30
+ end
31
+
32
+ expect(host.char_device?('/dev/tty')).to eq(true)
33
+ expect(host.symlink?('/etc/mtab')).to eq(true)
34
+ expect(host.inode?('/proc/uptime')).to eq(true)
35
+ end
36
+
37
+ it 'gets file stat' do
38
+ result = host.stat(host_json['home'])
39
+
40
+ expect(result.mode).to be_instance_of(Kanrisuru::Mode)
41
+ expect(result.mode.directory?).to eq(true)
42
+
43
+ case os_name
44
+ when 'centos', 'rhel', 'fedora'
45
+ expect(result.mode.numeric).to eq('700')
46
+ expect(result.mode.to_i).to eq(0o700)
47
+
48
+ expect(result.mode.group.read?).to eq(false)
49
+ expect(result.mode.group.write?).to eq(false)
50
+ expect(result.mode.group.execute?).to eq(false)
51
+
52
+ expect(result.mode.other.read?).to eq(false)
53
+ expect(result.mode.other.write?).to eq(false)
54
+ expect(result.mode.other.execute?).to eq(false)
55
+
56
+ expect(result.mode.owner.read?).to eq(true)
57
+ expect(result.mode.owner.write?).to eq(true)
58
+ expect(result.mode.owner.execute?).to eq(true)
59
+
60
+ expect(result.mode.symbolic).to eq('drwx------')
61
+ else
62
+ expect(result.mode.numeric).to eq('755')
63
+ expect(result.mode.to_i).to eq(0o755)
64
+
65
+ expect(result.mode.group.read?).to eq(true)
66
+ expect(result.mode.group.write?).to eq(false)
67
+ expect(result.mode.group.execute?).to eq(true)
68
+
69
+ expect(result.mode.other.read?).to eq(true)
70
+ expect(result.mode.other.write?).to eq(false)
71
+ expect(result.mode.other.execute?).to eq(true)
72
+
73
+ expect(result.mode.owner.read?).to eq(true)
74
+ expect(result.mode.owner.write?).to eq(true)
75
+ expect(result.mode.owner.execute?).to eq(true)
76
+
77
+ expect(result.mode.symbolic).to eq('drwxr-xr-x')
78
+ end
79
+
80
+ expect(result.file_type).to eq('directory')
81
+
82
+ case os_name
83
+ when 'opensuse', 'sles'
84
+ expect(result.gid).to eq(100)
85
+ expect(result.group).to eq('users')
86
+ else
87
+ expect(result.gid).to eq(1000)
88
+ expect(result.group).to eq(host_json['username'])
89
+ end
90
+
91
+ expect(result.uid).to eq(1000)
92
+ expect(result.user).to eq(host_json['username'])
93
+
94
+ expect(result.fsize).to be >= 0
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Kanrisuru::Core::Stream do
4
+ TestHosts.each_os do |os_name|
5
+ context "with #{os_name}" do
6
+ before(:all) do
7
+ host_json = TestHosts.host(os_name)
8
+ host = Kanrisuru::Remote::Host.new(
9
+ host: host_json['hostname'],
10
+ username: host_json['username'],
11
+ keys: [host_json['ssh_key']]
12
+ )
13
+
14
+ host.mkdir("#{host_json['home']}/.kanrisuru_spec_files", silent: true)
15
+ host.disconnect
16
+ end
17
+
18
+ let(:host_json) { TestHosts.host(os_name) }
19
+ let(:host) do
20
+ Kanrisuru::Remote::Host.new(
21
+ host: host_json['hostname'],
22
+ username: host_json['username'],
23
+ keys: [host_json['ssh_key']]
24
+ )
25
+ end
26
+
27
+ let(:spec_dir) { "#{host_json['home']}/.kanrisuru_spec_files" }
28
+
29
+ after do
30
+ host.disconnect
31
+ end
32
+
33
+ after(:all) do
34
+ host_json = TestHosts.host(os_name)
35
+ host = Kanrisuru::Remote::Host.new(
36
+ host: host_json['hostname'],
37
+ username: host_json['username'],
38
+ keys: [host_json['ssh_key']]
39
+ )
40
+
41
+ host.rmdir("#{host_json['home']}/.kanrisuru_spec_files")
42
+ host.disconnect
43
+ end
44
+
45
+ it 'outputs beginning of a file' do
46
+ file = host.file("#{spec_dir}/test-file.txt")
47
+ file.touch
48
+ file.append do |f|
49
+ f << 'This'
50
+ f << 'is'
51
+ f << 'a'
52
+ f << 'file!'
53
+ end
54
+
55
+ result = host.head("#{spec_dir}/test-file.txt", lines: 2)
56
+ expect(result).to be_success
57
+ expect(result.data.length).to eq(2)
58
+ expect(result.data).to eq(%w[This is])
59
+ end
60
+
61
+ it 'outputs end of a file' do
62
+ file = host.file("#{spec_dir}/test-file.txt")
63
+ file.touch
64
+ file.append do |f|
65
+ f << 'This'
66
+ f << 'is'
67
+ f << 'a'
68
+ f << 'file!'
69
+ end
70
+
71
+ result = host.tail("#{spec_dir}/test-file.txt", lines: 2)
72
+ expect(result).to be_success
73
+ expect(result.data.length).to eq(2)
74
+ expect(result.data).to eq(['a', 'file!'])
75
+ end
76
+
77
+ it 'reads a chunk of text from a file' do
78
+ file = host.file("#{spec_dir}/test-file-chunk.txt")
79
+ file.touch
80
+ file.append do |f|
81
+ f << 'This'
82
+ f << 'is'
83
+ f << 'is'
84
+ f << 'a'
85
+ f << 'file'
86
+ f << 'forever...'
87
+ end
88
+
89
+ result = host.read_file_chunk("#{spec_dir}/test-file-chunk.txt", 2, 4)
90
+ expect(result).to be_success
91
+ expect(result.data.length).to eq(3)
92
+ expect(result.data).to eq(%w[is is a])
93
+ end
94
+
95
+ it 'cats a file' do
96
+ result = host.cat('/etc/group')
97
+ expect(result.success?).to eq(true)
98
+ expect(result.data.include?('root:x:0:')).to eq(true)
99
+ end
100
+
101
+ it 'echoes to stdout' do
102
+ result = host.echo('Hello world')
103
+ expect(result.data).to eq('Hello world')
104
+ end
105
+
106
+ it 'seds file to stdout' do
107
+ path = "#{spec_dir}/test-file.txt"
108
+ result = host.echo("Hello world, this is a Cat test file.\nCat\nCat\nDog", new_file: path, mode: 'write')
109
+ expect(result).to be_success
110
+
111
+ result = host.sed(path, 'Cat', 'Dog')
112
+ expect(result).to be_success
113
+ expect(result.data).to eq("Hello world, this is a Dog test file.\nDog\nDog\nDog")
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::File do
6
+ TestHosts.each_os do |os_name|
7
+ context "with #{os_name}" do
8
+ before(:all) do
9
+ host_json = TestHosts.host(os_name)
10
+ host = Kanrisuru::Remote::Host.new(
11
+ host: host_json['hostname'],
12
+ username: host_json['username'],
13
+ keys: [host_json['ssh_key']]
14
+ )
15
+
16
+ host.mkdir("#{host_json['home']}/.kanrisuru_spec_files", silent: true)
17
+ host.disconnect
18
+ end
19
+
20
+ let(:host_json) { TestHosts.host(os_name) }
21
+ let(:host) do
22
+ Kanrisuru::Remote::Host.new(
23
+ host: host_json['hostname'],
24
+ username: host_json['username'],
25
+ keys: [host_json['ssh_key']]
26
+ )
27
+ end
28
+
29
+ let(:spec_dir) { "#{host_json['home']}/.kanrisuru_spec_files" }
30
+
31
+ after do
32
+ host.disconnect
33
+ end
34
+
35
+ after(:all) do
36
+ host_json = TestHosts.host(os_name)
37
+ host = Kanrisuru::Remote::Host.new(
38
+ host: host_json['hostname'],
39
+ username: host_json['username'],
40
+ keys: [host_json['ssh_key']]
41
+ )
42
+
43
+ host.rmdir("#{host_json['home']}/.kanrisuru_spec_files")
44
+ host.disconnect
45
+ end
46
+
47
+ it 'uploads a template file' do
48
+ path = '../templates/test.conf.erb'
49
+ dest_path = "#{spec_dir}/test.conf"
50
+
51
+ template = Kanrisuru::Template.new(path, array: %w[this is an array])
52
+ result = host.upload(template.read, dest_path)
53
+
54
+ expect(result).to be_success
55
+ expect(result.mode.numeric).to eq('640')
56
+ expect(result.user).to eq(host_json['username'])
57
+
58
+ case os_name
59
+ when 'sles', 'opensuse'
60
+ expect(result.gid).to eq(100)
61
+ expect(result.group).to eq('users')
62
+ else
63
+ expect(result.gid).to eq(1000)
64
+ expect(result.group).to eq(host_json['username'])
65
+ end
66
+
67
+ expect(host.cat(dest_path).to_a).to eq([
68
+ '<h1>Hello World</h1>',
69
+ 'this',
70
+ 'is',
71
+ 'an',
72
+ 'array'
73
+ ])
74
+ end
75
+
76
+ it 'uploads a dir' do
77
+ path = '../meta/'
78
+ dest_path = "#{spec_dir}/meta"
79
+
80
+ result = host.upload(path, dest_path, recursive: true)
81
+ expect(result).to be_success
82
+ end
83
+
84
+ it 'downloads a file to local fs' do
85
+ path = '../hosts-file'
86
+ src_path = '/etc/hosts'
87
+
88
+ result = host.download(src_path, path)
89
+ expect(result).to eq(path)
90
+ FileUtils.rm(path)
91
+ end
92
+
93
+ it 'downloads a file directly' do
94
+ src_path = '/etc/hosts'
95
+
96
+ result = host.download(src_path)
97
+ expect(result).to be_instance_of(String)
98
+ lines = result.split("\n")
99
+ expect(lines.length).to be >= 1
100
+ end
101
+
102
+ it 'wgets url' do
103
+ result = host.wget('https://example.com', directory_prefix: spec_dir)
104
+ expect(result).to be_success
105
+ end
106
+ end
107
+ end
108
+ end
File without changes
File without changes
File without changes
File without changes
File without changes
data/spec/spec_helper.rb CHANGED
@@ -4,7 +4,10 @@ require 'simplecov'
4
4
  SimpleCov.start
5
5
 
6
6
  require 'kanrisuru'
7
+
7
8
  require_relative 'helper/test_hosts'
9
+ require_relative 'helper/stub_network'
10
+ require_relative 'helper/expect_helpers'
8
11
 
9
12
  Kanrisuru.logger.level = Logger::WARN
10
13
 
@@ -5,5 +5,8 @@ require 'spec_helper'
5
5
  RSpec.describe Kanrisuru::Core::Find do
6
6
  it 'responds to find fields' do
7
7
  expect(Kanrisuru::Core::Find::FilePath.new).to respond_to(:path)
8
+ expect(Kanrisuru::Core::Find::REGEX_TYPES).to(
9
+ eq(['emacs', 'posix-awk', 'posix-basic', 'posix-egrep', 'posix-extended'])
10
+ )
8
11
  end
9
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanrisuru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.14
4
+ version: 0.8.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Mammina
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-09 00:00:00.000000000 Z
11
+ date: 2021-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -173,34 +173,41 @@ files:
173
173
  - lib/kanrisuru/util/os_family.rb
174
174
  - lib/kanrisuru/util/signal.rb
175
175
  - lib/kanrisuru/version.rb
176
- - spec/functional/core/apt_spec.rb
177
- - spec/functional/core/archive_spec.rb
178
- - spec/functional/core/disk_spec.rb
179
- - spec/functional/core/dmi_spec.rb
180
- - spec/functional/core/file_spec.rb
181
176
  - spec/functional/core/find_spec.rb
182
- - spec/functional/core/group_spec.rb
183
- - spec/functional/core/ip_spec.rb
184
177
  - spec/functional/core/path_spec.rb
185
- - spec/functional/core/socket_spec.rb
186
178
  - spec/functional/core/stat_spec.rb
187
179
  - spec/functional/core/stream_spec.rb
188
- - spec/functional/core/system_spec.rb
189
180
  - spec/functional/core/transfer_spec.rb
190
- - spec/functional/core/user_spec.rb
191
- - spec/functional/core/yum_spec.rb
192
- - spec/functional/core/zypper_spec.rb
193
181
  - spec/functional/os_package_spec.rb
194
- - spec/functional/remote/cluster_spec.rb
195
- - spec/functional/remote/cpu_spec.rb
196
- - spec/functional/remote/env_spec.rb
197
- - spec/functional/remote/fstab_spec.rb
198
- - spec/functional/remote/host_spec.rb
199
- - spec/functional/remote/memory_spec.rb
200
- - spec/functional/remote/os_spec.rb
201
- - spec/functional/remote/remote_file_spec.rb
182
+ - spec/helper/expect_helpers.rb
183
+ - spec/helper/stub_network.rb
202
184
  - spec/helper/test_hosts.rb
203
185
  - spec/hosts.json
186
+ - spec/integration/core/apt_spec.rb
187
+ - spec/integration/core/archive_spec.rb
188
+ - spec/integration/core/disk_spec.rb
189
+ - spec/integration/core/dmi_spec.rb
190
+ - spec/integration/core/file_spec.rb
191
+ - spec/integration/core/find_spec.rb
192
+ - spec/integration/core/group_spec.rb
193
+ - spec/integration/core/ip_spec.rb
194
+ - spec/integration/core/path_spec.rb
195
+ - spec/integration/core/socket_spec.rb
196
+ - spec/integration/core/stat_spec.rb
197
+ - spec/integration/core/stream_spec.rb
198
+ - spec/integration/core/system_spec.rb
199
+ - spec/integration/core/transfer_spec.rb
200
+ - spec/integration/core/user_spec.rb
201
+ - spec/integration/core/yum_spec.rb
202
+ - spec/integration/core/zypper_spec.rb
203
+ - spec/integration/remote/cluster_spec.rb
204
+ - spec/integration/remote/cpu_spec.rb
205
+ - spec/integration/remote/env_spec.rb
206
+ - spec/integration/remote/fstab_spec.rb
207
+ - spec/integration/remote/host_spec.rb
208
+ - spec/integration/remote/memory_spec.rb
209
+ - spec/integration/remote/os_spec.rb
210
+ - spec/integration/remote/remote_file_spec.rb
204
211
  - spec/spec_helper.rb
205
212
  - spec/unit/core/apt_spec.rb
206
213
  - spec/unit/core/archive_spec.rb