kanrisuru 0.8.12 → 0.8.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -1
  3. data/README.md +2 -2
  4. data/lib/kanrisuru/core/file.rb +1 -1
  5. data/lib/kanrisuru/core/stat.rb +1 -3
  6. data/lib/kanrisuru/remote/cluster.rb +1 -1
  7. data/lib/kanrisuru/version.rb +1 -1
  8. data/spec/functional/core/path_spec.rb +33 -80
  9. data/spec/functional/core/stat_spec.rb +17 -89
  10. data/spec/functional/core/stream_spec.rb +104 -112
  11. data/spec/functional/os_package_spec.rb +3 -3
  12. data/spec/helper/expect_helpers.rb +7 -0
  13. data/spec/helper/stub_network.rb +45 -0
  14. data/spec/{functional → integration}/core/apt_spec.rb +0 -0
  15. data/spec/{functional → integration}/core/archive_spec.rb +0 -0
  16. data/spec/{functional → integration}/core/disk_spec.rb +0 -0
  17. data/spec/{functional → integration}/core/dmi_spec.rb +0 -0
  18. data/spec/{functional → integration}/core/file_spec.rb +10 -3
  19. data/spec/{functional → integration}/core/find_spec.rb +0 -0
  20. data/spec/{functional → integration}/core/group_spec.rb +0 -0
  21. data/spec/{functional → integration}/core/ip_spec.rb +0 -0
  22. data/spec/integration/core/path_spec.rb +93 -0
  23. data/spec/{functional → integration}/core/socket_spec.rb +0 -0
  24. data/spec/integration/core/stat_spec.rb +98 -0
  25. data/spec/integration/core/stream_spec.rb +117 -0
  26. data/spec/{functional → integration}/core/system_spec.rb +0 -0
  27. data/spec/{functional → integration}/core/transfer_spec.rb +0 -0
  28. data/spec/{functional → integration}/core/user_spec.rb +0 -0
  29. data/spec/{functional → integration}/core/yum_spec.rb +0 -0
  30. data/spec/{functional → integration}/core/zypper_spec.rb +0 -0
  31. data/spec/{functional → integration}/remote/cluster_spec.rb +4 -4
  32. data/spec/{functional → integration}/remote/cpu_spec.rb +0 -0
  33. data/spec/{functional → integration}/remote/env_spec.rb +0 -0
  34. data/spec/{functional → integration}/remote/fstab_spec.rb +0 -0
  35. data/spec/{functional → integration}/remote/host_spec.rb +0 -0
  36. data/spec/{functional → integration}/remote/memory_spec.rb +0 -0
  37. data/spec/{functional → integration}/remote/os_spec.rb +0 -0
  38. data/spec/{functional → integration}/remote/remote_file_spec.rb +0 -0
  39. data/spec/spec_helper.rb +4 -1
  40. metadata +29 -24
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96b168459e76c64aa511a58fc43d0d777d5d783874e47a24c373dd6da2ef3ff4
4
- data.tar.gz: 461c0236448253694f369cfc1b7757f61aeb0a4e0af3841cea62b6297d862c14
3
+ metadata.gz: 6c2110c3bfef603ff4d09ade3a4fabf81b34e2f5ca51ff9f120d382963542f7f
4
+ data.tar.gz: 6ef4e848168e93b37a06e0d0cbfffd9138d979e125d7a151e77c594539aba85d
5
5
  SHA512:
6
- metadata.gz: 4d8b47306256c698e30c7950fc18a59551fb156b14bc6e077df323a27832af8125f567213ec066ec1c8e5f7295411a0a113823ef4a6c97df0bbf5a6d753fc851
7
- data.tar.gz: 5152a07e2d3580c4de9fb477d36c8de6f1d3fae3a6e234a6c3a80318524cb8ae6fefaebd984501f63b3b5de79350c2d9b53eac92b133453aebd1c47ba4323619
6
+ metadata.gz: 3444e7a640af911e1f0eb67181503e0b87677847adacd6c0bf3a45c13ce8c8287b2b17ad56ba36d4fe301fe3a19aaf5b17856d3f9bceb1175002ed199d1eb774
7
+ data.tar.gz: d5a76df5b4ffd4f7483c6953d5c36479755f30efb3e19c822f1df41e459e6213c555c64324f6412df2a9b1ac5a4f003d5e525110c9e6f8e92389d655666fd5d7
data/CHANGELOG.md CHANGED
@@ -1,4 +1,19 @@
1
- ## Kanrisuru 0.8.12 (October 3, 20201)
1
+ ## Kanrisuru 0.8.16 (October 14, 20201)
2
+ * Add functional test cases for `stream` and `path` modules
3
+ * Create `expect_command` helper for quick testing on raw command result
4
+
5
+ ## Kanrisuru 0.8.15 (October 12, 20201)
6
+ * Move functional specs to integration. Anything that performs an actual network request will be under the integrations test.
7
+ * Create a `StubNetwork` to quickly monkey patch the `Kanrisuru::Remote::Host` to simulate a `Net::SSH` channel request. Will add additional functionality for different simulations later on.
8
+ * Start with testing the `stat` command as a functional test.
9
+
10
+ ## Kanrisuru 0.8.14 (October 8, 20201)
11
+ * Update `Kanrisuru::Remote::Cluster` instantiation method to use array splat instead of passing array directly.
12
+
13
+ ## Kanrisuru 0.8.13 (October 4, 20201)
14
+ * Fix `wc` command. Ensure result parsing is cast to integer values.
15
+
16
+ ## Kanrisuru 0.8.12 (October 4, 20201)
2
17
  * Refactor `rmdir` command to only work on empty directories.
3
18
 
4
19
  ## Kanrisuru 0.8.11 (October 1, 20201)
data/README.md CHANGED
@@ -48,7 +48,7 @@ result.path # => /home/ubuntu
48
48
 
49
49
  ### Cluster
50
50
  ```ruby
51
- cluster = Kanrisuru::Remote::Cluster.new([{
51
+ cluster = Kanrisuru::Remote::Cluster.new({
52
52
  host: 'host1', username: 'ubuntu', keys: ['~/.ssh/id_rsa']
53
53
  }, {
54
54
  host: 'host2', username: 'alice', keys: ['~/.ssh/id_rsa']
@@ -72,7 +72,7 @@ host.execute(command)
72
72
  command.success? #=> true
73
73
  command.to_s #=> Linux
74
74
 
75
- cluster = Kanrisuru::Remote::Cluster.new([host, {host: 'host2', username: 'alice', keys: ['~/.ssh/id_rsa']}])
75
+ cluster = Kanrisuru::Remote::Cluster.new(host, {host: 'host2', username: 'alice', keys: ['~/.ssh/id_rsa']})
76
76
 
77
77
  cluster.execute('uname') #=> {host: 'host1', result: 'Linux'}, {host: 'host2', result: 'Linux'}
78
78
  ```
@@ -332,7 +332,7 @@ module Kanrisuru
332
332
  execute_shell(command)
333
333
 
334
334
  Kanrisuru::Result.new(command) do |cmd|
335
- items = cmd.to_a
335
+ items = cmd.to_s.split
336
336
  FileCount.new(items[0].to_i, items[1].to_i, items[2].to_i)
337
337
  end
338
338
  end
@@ -66,10 +66,8 @@ module Kanrisuru
66
66
  end
67
67
 
68
68
  def stat(path, opts = {})
69
- follow = opts[:follow]
70
-
71
69
  command = Kanrisuru::Command.new('stat')
72
- command.append_flag('-L', follow)
70
+ command.append_flag('-L', opts[:follow])
73
71
  command.append_arg('-c', '%A,%b,%D,%F,%g,%G,%h,%i,%n,%s,%u,%U,%x,%y,%z')
74
72
  command << path
75
73
 
@@ -6,7 +6,7 @@ module Kanrisuru
6
6
  extend OsPackage::Collection
7
7
  include Enumerable
8
8
 
9
- def initialize(hosts)
9
+ def initialize(*hosts)
10
10
  @hosts = {}
11
11
  hosts.each do |host_opts|
12
12
  add_host(host_opts)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanrisuru
4
- VERSION = '0.8.12'
4
+ VERSION = '0.8.16'
5
5
  end
@@ -2,92 +2,45 @@
2
2
 
3
3
  require 'spec_helper'
4
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('.')
5
+ StubNetwork.stub!
27
6
 
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
7
+ RSpec.describe Kanrisuru::Core::Path do
8
+ let(:host) do
9
+ Kanrisuru::Remote::Host.new(
10
+ host: 'localhost',
11
+ username: 'ubuntu',
12
+ keys: ['id_rsa']
13
+ )
14
+ end
41
15
 
42
- it 'gets whoami' do
43
- expect(host.whoami.user).to eq(host_json['username'])
44
- end
16
+ it 'prepares ls command' do
17
+ expect_command(host.ls(path: '/etc'), 'ls -i -l /etc')
18
+ expect_command(host.ls(path: '/var/log', all: true, id: true), 'ls -i -l -a -n /var/log')
19
+ end
45
20
 
46
- it 'gets pwd' do
47
- expect(host.pwd.path).to eq(host_json['home'])
48
- end
21
+ it 'prepares pwd command' do
22
+ expect_command(host.pwd, 'pwd')
23
+ end
49
24
 
50
- it 'uses which to get path for bash' do
51
- result = host.which('bash', all: true)
52
- paths = result.map(&:path)
25
+ it 'prepares whoami command' do
26
+ expect_command(host.whoami, 'whoami')
27
+ end
53
28
 
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
29
+ it 'prepares which command' do
30
+ expect_command(host.which('which'), 'which which')
31
+ expect_command(host.which('pwd', all: true), 'which -a pwd')
32
+ end
69
33
 
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
34
+ it 'prepares realpath command' do
35
+ expect_command(host.realpath('/etc/os-release'), 'realpath /etc/os-release')
36
+ expect_command(host.realpath('/etc/os-release', strip: true), 'realpath /etc/os-release -s')
37
+ end
80
38
 
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
39
+ it 'prepares readlink command' do
40
+ expect_command(host.readlink('/etc/os-release'), 'readlink /etc/os-release')
41
+ expect_command(host.readlink('/etc/os-release', canonicalize: true), 'readlink -f /etc/os-release')
42
+ expect_command(host.readlink('/etc/os-release', canonicalize_existing: true), 'readlink -e /etc/os-release')
43
+ expect_command(host.readlink('/etc/os-release', canonicalize_missing: true), 'readlink -m /etc/os-release')
92
44
  end
45
+
93
46
  end
@@ -2,97 +2,25 @@
2
2
 
3
3
  require 'spec_helper'
4
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)
5
+ StubNetwork.stub!
55
6
 
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
7
+ RSpec.describe Kanrisuru::Core::Stat do
8
+ let(:host) do
9
+ Kanrisuru::Remote::Host.new(
10
+ host: 'localhost',
11
+ username: 'ubuntu',
12
+ keys: ['id_rsa']
13
+ )
14
+ end
90
15
 
91
- expect(result.uid).to eq(1000)
92
- expect(result.user).to eq(host_json['username'])
16
+ it 'prepares stat command' do
17
+ expect_command(host.stat('~/file1.txt'),
18
+ 'stat -c %A,%b,%D,%F,%g,%G,%h,%i,%n,%s,%u,%U,%x,%y,%z ~/file1.txt'
19
+ )
93
20
 
94
- expect(result.fsize).to be >= 0
95
- end
96
- end
21
+ expect_command(host.stat('~/file2.txt', follow: true),
22
+ 'stat -L -c %A,%b,%D,%F,%g,%G,%h,%i,%n,%s,%u,%U,%x,%y,%z ~/file2.txt'
23
+ )
97
24
  end
25
+
98
26
  end
@@ -1,117 +1,109 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'spec_helper'
4
+
5
+ StubNetwork.stub!
6
+
3
7
  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
8
+ let(:host) do
9
+ Kanrisuru::Remote::Host.new(
10
+ host: 'localhost',
11
+ username: 'ubuntu',
12
+ keys: ['id_rsa']
13
+ )
14
+ end
15
+
16
+ it 'prepares head command' do
17
+ result = host.head('/var/log/syslog')
18
+ expect_command(result, 'head /var/log/syslog')
19
+
20
+ result = host.head('/var/log/syslog', bytes: 1024)
21
+ expect_command(result, 'head -c 1024 /var/log/syslog')
22
+
23
+ result = host.head('/var/log/syslog', lines: 50)
24
+ expect_command(result, 'head -n 50 /var/log/syslog')
25
+ end
26
+
27
+ it 'prepares tail command' do
28
+ result = host.tail('/var/log/syslog')
29
+ expect_command(result, 'tail /var/log/syslog' )
30
+
31
+ result = host.tail('/var/log/syslog', bytes: 1024)
32
+ expect_command(result, 'tail -c 1024 /var/log/syslog')
33
+
34
+ result = host.tail('/var/log/syslog', lines: 50)
35
+ expect_command(result, 'tail -n 50 /var/log/syslog' )
36
+ end
37
+
38
+ it 'prepares read_file_chunk command' do
39
+ result = host.read_file_chunk('/var/log/apache2/access.log', 10, 20)
40
+ expect_command(result, 'tail -n +10 /var/log/apache2/access.log | head -n 11')
41
+
42
+ result = host.read_file_chunk('/var/log/apache2/access.log', 0, 0)
43
+ expect_command(result, 'tail -n +0 /var/log/apache2/access.log | head -n 1')
44
+
45
+ expect {
46
+ host.read_file_chunk('file.log', 10, '20')
47
+ }.to raise_error(ArgumentError)
48
+
49
+ expect {
50
+ host.read_file_chunk('file.log', '10', 20)
51
+ }.to raise_error(ArgumentError)
52
+
53
+ expect {
54
+ host.read_file_chunk('file.log', 10, 9)
55
+ }.to raise_error(ArgumentError)
56
+
57
+ expect {
58
+ host.read_file_chunk('file.log', -1, 1)
59
+ }.to raise_error(ArgumentError)
60
+
61
+ expect {
62
+ host.read_file_chunk('file.log', 10, -2)
63
+ }.to raise_error(ArgumentError)
64
+ end
65
+
66
+ it 'prepares sed command' do
67
+ result = host.sed("~/file.txt", 'Cat', 'Dog')
68
+ expect_command(result, "sed 's/Cat/Dog/g' '~/file.txt'")
69
+
70
+ result = host.sed("~/file.txt", 'Cat', 'Doggz', in_place: true, new_file: '~/file2.txt', mode: 'write')
71
+ expect_command(result, "sed -i 's/Cat/Doggz/g' '~/file.txt' > ~/file2.txt")
72
+
73
+ result = host.sed("~/file.txt", 'Cat', 'Dogo', regexp_extended: true, new_file: '~/file2.txt', mode: 'append')
74
+ expect_command(result, "sed -r 's/Cat/Dogo/g' '~/file.txt' >> ~/file2.txt")
75
+ end
76
+
77
+ it 'prepares echo command' do
78
+ expect_command(host.echo('Hello world') , "echo 'Hello world'")
79
+ expect_command(host.echo("Hello\\n world", backslash: true), "echo -e 'Hello\\n world'")
80
+
81
+ expect_command(host.echo('Hello world', new_file: '~/file1.txt', mode: 'write'),
82
+ "echo 'Hello world' > ~/file1.txt")
83
+ expect_command(host.echo("Goodbye", new_file: '~/file1.txt', mode: 'append'),
84
+ "echo 'Goodbye' >> ~/file1.txt")
116
85
  end
86
+
87
+ it 'prepares cat command' do
88
+ expect_command(host.cat('/etc/group'), 'cat /etc/group')
89
+ expect_command(host.cat('/etc/group', show_all: true), 'cat -A /etc/group')
90
+ expect_command(host.cat('/etc/group',
91
+ show_tabs: true,
92
+ number: true,
93
+ squeeze_blank: true,
94
+ show_nonprinting: true,
95
+ show_ends: true,
96
+ number_nonblank: true
97
+ ), 'cat -T -n -s -v -E -b /etc/group')
98
+
99
+ expect_command(host.cat(['~/file1.txt', '~/file2.txt', '~/file3.txt']),
100
+ 'cat ~/file1.txt ~/file2.txt ~/file3.txt')
101
+
102
+ expect_command(host.cat(['~/file1.txt', '~/file2.txt', '~/file3.txt'], mode: 'write', new_file: 'combined.txt'),
103
+ 'cat ~/file1.txt ~/file2.txt ~/file3.txt > combined.txt')
104
+
105
+ expect_command(host.cat(['~/file1.txt', '~/file2.txt', '~/file3.txt'], mode: 'append', new_file: 'combined.txt'),
106
+ 'cat ~/file1.txt ~/file2.txt ~/file3.txt >> combined.txt')
107
+ end
108
+
117
109
  end
@@ -111,7 +111,7 @@ RSpec.describe Kanrisuru::OsPackage do
111
111
  host = Kanrisuru::Remote::Host.new(host: '127.0.0.1', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])
112
112
  host2 = Kanrisuru::Remote::Host.new(host: 'localhost', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])
113
113
 
114
- cluster = Kanrisuru::Remote::Cluster.new([host, host2])
114
+ cluster = Kanrisuru::Remote::Cluster.new(host, host2)
115
115
 
116
116
  expect(host).to respond_to(:tester)
117
117
  expect(host.tester).to eq('hello ubuntu')
@@ -125,7 +125,7 @@ RSpec.describe Kanrisuru::OsPackage do
125
125
  host = Kanrisuru::Remote::Host.new(host: '127.0.0.1', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])
126
126
  host2 = Kanrisuru::Remote::Host.new(host: 'localhost', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])
127
127
 
128
- cluster = Kanrisuru::Remote::Cluster.new([host, host2])
128
+ cluster = Kanrisuru::Remote::Cluster.new(host, host2)
129
129
 
130
130
  expect(host).to respond_to(:asdf)
131
131
  expect(host.asdf).to respond_to(:tester)
@@ -151,7 +151,7 @@ RSpec.describe Kanrisuru::OsPackage do
151
151
  host2 = Kanrisuru::Remote::Host.new(host: 'centos-host', username: 'centos', keys: ['~/.ssh/id_rsa'])
152
152
  host3 = Kanrisuru::Remote::Host.new(host: 'opensuse-host', username: 'opensuse', keys: ['~/.ssh/id_rsa'])
153
153
 
154
- cluster = Kanrisuru::Remote::Cluster.new([host1, host2, host3])
154
+ cluster = Kanrisuru::Remote::Cluster.new(host1, host2, host3)
155
155
 
156
156
  expect(host1).to respond_to(:output)
157
157
  expect(host2).to respond_to(:output)
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ def expect_command(result, string)
4
+ expect(result.command.raw_command).to eq(
5
+ string
6
+ )
7
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ class StubNetwork
4
+ class << self
5
+
6
+ def stub!
7
+ ## Stub out the execute_with_retries method to
8
+ ## test functionality of host commands
9
+ Kanrisuru::Remote::Host.class_eval do
10
+ private
11
+
12
+ def execute_with_retries(command)
13
+ command.handle_status(0)
14
+ command.handle_data(nil)
15
+
16
+ command
17
+ end
18
+ end
19
+
20
+ Kanrisuru::Remote::Os.class_eval do
21
+ def initialize(host)
22
+ @host = host
23
+
24
+ @kernel_name = 'Linux'
25
+ @kernel_version = '#91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021'
26
+ @operating_system = 'GNU/Linux'
27
+ @hardware_platform = "x86_64"
28
+ @processor = "x86_64"
29
+ @release = "ubuntu"
30
+ @version = 20.0
31
+ end
32
+ end
33
+
34
+ Kanrisuru::Result.class_eval do
35
+ def initialize(command, &block)
36
+ @command = command
37
+ @data = nil
38
+
39
+ @error = @command.to_a if @command.failure?
40
+ end
41
+ end
42
+ end
43
+
44
+ end
45
+ end
File without changes
File without changes
File without changes
@@ -349,10 +349,17 @@ RSpec.describe Kanrisuru::Core::File do
349
349
  expect(result.success?).to eq(true)
350
350
  expect(result).to respond_to(:lines, :words, :characters)
351
351
 
352
- lines = result.lines
353
- result = host.cat('/etc/hosts')
352
+ ## Need to remap data including newline chars to get byte size
353
+ data = host.cat('/etc/hosts').command.raw_result
354
+ doc = data.map(&:lines).flatten
354
355
 
355
- expect(lines).to eq(result.to_a.length)
356
+ lines = doc.length
357
+ words = doc.map(&:split).flatten
358
+ chars = doc.map(&:length).flatten
359
+
360
+ expect(result.lines).to eq(doc.length)
361
+ expect(result.words).to eq(words.length)
362
+ expect(result.characters).to eq(chars.sum)
356
363
  end
357
364
  end
358
365
  end
File without changes
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
File without changes
File without changes
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  RSpec.describe Kanrisuru::Remote::Cluster do
6
6
  context 'with ubuntu' do
7
7
  it 'gets hostname for cluster' do
8
- cluster = described_class.new([{
8
+ cluster = described_class.new({
9
9
  host: 'localhost',
10
10
  username: 'ubuntu',
11
11
  keys: ['~/.ssh/id_rsa']
@@ -13,7 +13,7 @@ RSpec.describe Kanrisuru::Remote::Cluster do
13
13
  host: '127.0.0.1',
14
14
  username: 'ubuntu',
15
15
  keys: ['~/.ssh/id_rsa']
16
- }])
16
+ })
17
17
 
18
18
  expect(cluster.hostname).to match([
19
19
  { host: 'localhost', result: 'ubuntu' },
@@ -24,7 +24,7 @@ RSpec.describe Kanrisuru::Remote::Cluster do
24
24
  end
25
25
 
26
26
  it 'can ping host cluster' do
27
- cluster = described_class.new([{
27
+ cluster = described_class.new({
28
28
  host: 'localhost',
29
29
  username: 'ubuntu',
30
30
  keys: ['~/.ssh/id_rsa']
@@ -32,7 +32,7 @@ RSpec.describe Kanrisuru::Remote::Cluster do
32
32
  host: '127.0.0.1',
33
33
  username: 'ubuntu',
34
34
  keys: ['~/.ssh/id_rsa']
35
- }])
35
+ })
36
36
 
37
37
  expect(cluster.ping?).to match([
38
38
  { host: 'localhost', result: true },
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
 
@@ -18,4 +21,4 @@ RSpec.configure do |config|
18
21
  config.expect_with :rspec do |c|
19
22
  c.syntax = :expect
20
23
  end
21
- end
24
+ 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.12
4
+ version: 0.8.16
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-04 00:00:00.000000000 Z
11
+ date: 2021-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -173,34 +173,39 @@ 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
- - spec/functional/core/find_spec.rb
182
- - spec/functional/core/group_spec.rb
183
- - spec/functional/core/ip_spec.rb
184
176
  - spec/functional/core/path_spec.rb
185
- - spec/functional/core/socket_spec.rb
186
177
  - spec/functional/core/stat_spec.rb
187
178
  - spec/functional/core/stream_spec.rb
188
- - spec/functional/core/system_spec.rb
189
- - 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
179
  - 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
180
+ - spec/helper/expect_helpers.rb
181
+ - spec/helper/stub_network.rb
202
182
  - spec/helper/test_hosts.rb
203
183
  - spec/hosts.json
184
+ - spec/integration/core/apt_spec.rb
185
+ - spec/integration/core/archive_spec.rb
186
+ - spec/integration/core/disk_spec.rb
187
+ - spec/integration/core/dmi_spec.rb
188
+ - spec/integration/core/file_spec.rb
189
+ - spec/integration/core/find_spec.rb
190
+ - spec/integration/core/group_spec.rb
191
+ - spec/integration/core/ip_spec.rb
192
+ - spec/integration/core/path_spec.rb
193
+ - spec/integration/core/socket_spec.rb
194
+ - spec/integration/core/stat_spec.rb
195
+ - spec/integration/core/stream_spec.rb
196
+ - spec/integration/core/system_spec.rb
197
+ - spec/integration/core/transfer_spec.rb
198
+ - spec/integration/core/user_spec.rb
199
+ - spec/integration/core/yum_spec.rb
200
+ - spec/integration/core/zypper_spec.rb
201
+ - spec/integration/remote/cluster_spec.rb
202
+ - spec/integration/remote/cpu_spec.rb
203
+ - spec/integration/remote/env_spec.rb
204
+ - spec/integration/remote/fstab_spec.rb
205
+ - spec/integration/remote/host_spec.rb
206
+ - spec/integration/remote/memory_spec.rb
207
+ - spec/integration/remote/os_spec.rb
208
+ - spec/integration/remote/remote_file_spec.rb
204
209
  - spec/spec_helper.rb
205
210
  - spec/unit/core/apt_spec.rb
206
211
  - spec/unit/core/archive_spec.rb