kanrisuru 0.8.15 → 0.8.19

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 522ae1903fe51ff350abc0fafa252d1b66f970710d4bed41748be03cc25ead3d
4
- data.tar.gz: 501c4b568aeed1bca4e22658291ebab44e9cb9ebb84c23f5154dc4c97e5daf08
3
+ metadata.gz: 90fafd703817d1e16744262817dd0b5e5a51ea942f2853b154f4bd2746295fd7
4
+ data.tar.gz: 0e0b35e325d3237faacbb71f22873b1c661495af0d93830e7ad91babbb060b3c
5
5
  SHA512:
6
- metadata.gz: 59fbae207747c7b7c93461a48f12cd959d8dcd2b2853619cb15624627534bca2a84fdf88c068285ef2cbb7a1ac48c74d592f784374847e3f89b35faa85de5366
7
- data.tar.gz: 8981e523f2373b7eb4740e1c6b1af2318cb45ba960a92e620f368d502a5359f68402fa23c2607f449e563c451b6b5d0efce9cba8ef45ffa4252b4158bd9a09a8
6
+ metadata.gz: 76e116b83a5c119c0672b8ec5f1bac8f23307e1e06d3b393804df912f6110faf63527ca126ed4eb164d6ba58ef4ba92f3d1c3e15b252dc703058e39fc3ab4ba3
7
+ data.tar.gz: 8e80e6457fd848d4dee5a31e33881c91458402df16cc82845b6300afc2543fe583fe48aeb2914e4329de9d9a668b6afbc2dd533d56886bd6fe0656ede58e5c95
data/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
1
+ ## Kanrisuru 0.8.19 (October 31, 2021)
2
+ * Add functional test cases for `ss` command.
3
+ * Enforce contraints on `family` parameter for `ss` command.
4
+ * Deprecating `string_join_array` in favor of `array_join_string`. Both methods do the same thing, and the `array_join_string` has a better nameing interface; will be removed in the next major release.
5
+ * Replace `string_join_array` method calls in `apt`, `transfer`, `yum`, and `zypper` with `array_join_string`.
6
+
7
+ ## Kanrisuru 0.8.18 (October 19, 2021)
8
+ * Add functional test cases for `find` commmand.
9
+ * Add `regex_type` option for `find` command.
10
+ * Fix bug with `size` option when using number in a string format, regex testing has been simplified on matching correctness for size with options like `100`, `+100`, `-100M` for comparitive fields.
11
+
12
+ ## Kanrisuru 0.8.17 (October 16, 2021)
13
+ * Add functional test cases for `transfer` module
14
+ * Update wget command to accept hash for `headers` opt.
15
+
16
+ ## Kanrisuru 0.8.16 (October 14, 2021)
17
+ * Add functional test cases for `stream` and `path` modules
18
+ * Create `expect_command` helper for quick testing on raw command result
19
+
1
20
  ## Kanrisuru 0.8.15 (October 12, 20201)
2
21
  * Move functional specs to integration. Anything that performs an actual network request will be under the integrations test.
3
22
  * 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.
@@ -142,7 +142,7 @@ module Kanrisuru
142
142
  command = Kanrisuru::Command.new('apt-get purge')
143
143
  command.append_flag('-y')
144
144
 
145
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
145
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
146
146
  command << packages
147
147
 
148
148
  execute_shell(command)
@@ -153,7 +153,7 @@ module Kanrisuru
153
153
  command = Kanrisuru::Command.new('apt-get remove')
154
154
  command.append_flag('-y')
155
155
 
156
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
156
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
157
157
  command << packages
158
158
 
159
159
  execute_shell(command)
@@ -168,7 +168,7 @@ module Kanrisuru
168
168
  command.append_flag('--only-upgrade', opts[:only_upgrade])
169
169
  command.append_flag('--reinstall', opts[:reinstall])
170
170
 
171
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
171
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
172
172
  command << packages
173
173
 
174
174
  execute_shell(command)
@@ -200,7 +200,7 @@ module Kanrisuru
200
200
  command = Kanrisuru::Command.new('apt show')
201
201
  command.append_flag('-a')
202
202
 
203
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
203
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
204
204
  command << packages
205
205
 
206
206
  execute_shell(command)
@@ -23,7 +23,7 @@ module Kanrisuru
23
23
  :name, :label, :uuid, :type, :uuid_sub, :label_fatboot, :version, :usage,
24
24
  :part_uuid, :part_entry_scheme, :part_entry_uuid, :part_entry_type,
25
25
  :part_entry_number, :part_entry_offset, :part_entry_size, :part_entry_disk,
26
- :minimum_io_size, :physical_sector_size, :logical_sector_size,
26
+ :minimum_io_size, :physical_sector_size, :logical_sector_size
27
27
  )
28
28
 
29
29
  def du(opts = {})
@@ -10,6 +10,7 @@ module Kanrisuru
10
10
  os_define :linux, :find
11
11
 
12
12
  FilePath = Struct.new(:path)
13
+ REGEX_TYPES = ['emacs', 'posix-awk', 'posix-basic', 'posix-egrep', 'posix-extended']
13
14
 
14
15
  def find(opts = {})
15
16
  paths = opts[:paths]
@@ -32,7 +33,7 @@ module Kanrisuru
32
33
  if paths.instance_of?(Array)
33
34
  paths = paths.join(' ')
34
35
  elsif paths.class != String
35
- raise 'Invalid paths type'
36
+ raise ArgumentError, 'Invalid paths type'
36
37
  end
37
38
 
38
39
  command << paths
@@ -64,13 +65,19 @@ module Kanrisuru
64
65
  command.append_arg('-cmin', opts[:cmin])
65
66
  command.append_arg('-mmin', opts[:mmin])
66
67
 
67
- command.append_arg('-regex', "'#{regex}'") if Kanrisuru::Util.present?(regex)
68
+ if Kanrisuru::Util.present?(opts[:regex_type])
69
+ unless REGEX_TYPES.include?(opts[:regex_type])
70
+ raise ArgumentError, 'invalid regex type'
71
+ end
68
72
 
69
- if size.instance_of?(String)
70
- match = Regexp.new(/^([-+]+)\s*?(\d+)([bcwkMG])$/).match(size)
73
+ command.append_arg('-regextype', opts[:regex_type])
74
+ end
71
75
 
72
- raise ArgumentError, "invalid size string: '#{@size}'" if match.nil? || match.captures.include?(nil)
76
+ command.append_arg('-regex', "'#{regex}'") if Kanrisuru::Util.present?(regex)
73
77
 
78
+ if size.instance_of?(String)
79
+ regex = Regexp.new(/^([-+])?(\d+)([bcwkMG])*$/)
80
+ raise ArgumentError, "invalid size string: '#{@size}'" unless regex.match?(size)
74
81
  command.append_arg('-size', size)
75
82
  elsif size.instance_of?(Integer)
76
83
  command.append_arg('-size', size)
@@ -38,9 +38,14 @@ module Kanrisuru
38
38
  'LAST-ACK' => 'last-ack', 'CLOSING' => 'closing'
39
39
  }.freeze
40
40
 
41
+ NETWORK_FAMILIES = %w[
42
+ unix inet inet6 link netlink
43
+ ].freeze
44
+
41
45
  def ss(opts = {})
42
46
  state = opts[:state]
43
47
  expression = opts[:expression]
48
+ family = opts[:family]
44
49
 
45
50
  command = Kanrisuru::Command.new('ss')
46
51
 
@@ -53,7 +58,10 @@ module Kanrisuru
53
58
  command.append_flag('-x', opts[:unix])
54
59
  command.append_flag('-w', opts[:raw])
55
60
 
56
- command.append_arg('-f', opts[:family])
61
+ if Kanrisuru::Util.present?(family)
62
+ raise ArgumentError, 'invalid family type' if !NETWORK_FAMILIES.include?(family)
63
+ command.append_arg('-f', family)
64
+ end
57
65
 
58
66
  if Kanrisuru::Util.present?(state)
59
67
  raise ArgumentError, 'invalid filter state' if !TCP_STATES.include?(state) && !OTHER_STATES.include?(state)
@@ -87,7 +87,7 @@ module Kanrisuru
87
87
  command.append_arg('--quota', opts[:quota])
88
88
 
89
89
  if Kanrisuru::Util.present?(opts[:restrict_file_names])
90
- command.append_arg('--restrict-file-names', Kanrisuru::Util.string_join_array(opts[:restrict_file_names]))
90
+ command.append_arg('--restrict-file-names', Kanrisuru::Util.array_join_string(opts[:restrict_file_names]))
91
91
  end
92
92
 
93
93
  case opts[:family]
@@ -124,7 +124,6 @@ module Kanrisuru
124
124
  command.append_flag('--no-cookies', opts[:no_cookies])
125
125
  command.append_flag('--keep-session-cookies', opts[:keep_session_cookies])
126
126
  command.append_flag('--ignore-length', opts[:ignore_length])
127
- command.append_arg('--header', opts[:header])
128
127
  command.append_arg('--max-redirect', opts[:max_redirect])
129
128
  command.append_arg('--proxy-user', opts[:proxy_user])
130
129
  command.append_arg('--proxy-password', opts[:proxy_password])
@@ -132,6 +131,18 @@ module Kanrisuru
132
131
  command.append_flag('--save-headers', opts[:save_headers])
133
132
  command.append_arg('--user-agent', opts[:user_agent])
134
133
 
134
+ headers = opts[:headers]
135
+ if Kanrisuru::Util.present?(headers)
136
+ if headers.instance_of?(Hash)
137
+ headers.each do |key, value|
138
+ header = "'#{key}: #{value}'"
139
+ command.append_arg('--header', header)
140
+ end
141
+ elsif headers.instance_of?(String)
142
+ command.append_arg('--header', headers)
143
+ end
144
+ end
145
+
135
146
  post_data = opts[:post_data]
136
147
 
137
148
  if Kanrisuru::Util.present?(post_data)
@@ -78,7 +78,7 @@ module Kanrisuru
78
78
  command = Kanrisuru::Command.new('yum install')
79
79
  command.append_flag('-y')
80
80
 
81
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
81
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
82
82
  command << packages
83
83
 
84
84
  execute_shell(command)
@@ -91,7 +91,7 @@ module Kanrisuru
91
91
  command.append_flag('-y')
92
92
 
93
93
  if Kanrisuru::Util.present?(opts[:repos])
94
- repos = Kanrisuru::Util.string_join_array(opts[:repos], ' ')
94
+ repos = Kanrisuru::Util.array_join_string(opts[:repos], ' ')
95
95
  command << repos
96
96
  end
97
97
 
@@ -136,7 +136,7 @@ module Kanrisuru
136
136
  command = Kanrisuru::Command.new('yum remove')
137
137
  command.append_flag('-y')
138
138
 
139
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
139
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
140
140
  raise ArugmentError, "can't remove yum" if packages.include?('yum')
141
141
 
142
142
  command << packages
@@ -172,7 +172,7 @@ module Kanrisuru
172
172
  command = Kanrisuru::Command.new('yum erase')
173
173
  command.append_flag('-y')
174
174
 
175
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
175
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
176
176
  raise ArugmentError, "can't erase yum" if packages.include?('yum')
177
177
 
178
178
  command << packages
@@ -198,7 +198,7 @@ module Kanrisuru
198
198
  def yum_search(opts)
199
199
  command = Kanrisuru::Command.new('yum search')
200
200
  command.append_flag('all', opts[:all])
201
- command << Kanrisuru::Util.string_join_array(opts[:packages], ' ')
201
+ command << Kanrisuru::Util.array_join_string(opts[:packages], ' ')
202
202
 
203
203
  pipe_output_newline(command)
204
204
 
@@ -228,7 +228,7 @@ module Kanrisuru
228
228
  command = Kanrisuru::Command.new('yum repolist')
229
229
  command.append_flag('--verbose')
230
230
 
231
- command << Kanrisuru::Util.string_join_array(opts[:repos], ' ') if opts[:repos]
231
+ command << Kanrisuru::Util.array_join_string(opts[:repos], ' ') if opts[:repos]
232
232
 
233
233
  execute_shell(command)
234
234
 
@@ -287,7 +287,7 @@ module Kanrisuru
287
287
  command.append_flag('--quiet')
288
288
  command.append_flag('installed', opts[:installed])
289
289
 
290
- command << Kanrisuru::Util.string_join_array(opts[:packages], ' ') if opts[:packages]
290
+ command << Kanrisuru::Util.array_join_string(opts[:packages], ' ') if opts[:packages]
291
291
 
292
292
  execute_shell(command)
293
293
 
@@ -567,7 +567,7 @@ module Kanrisuru
567
567
  zypper_repos_opt(command, opts)
568
568
  zypper_package_type_opt(command, opts)
569
569
 
570
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
570
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
571
571
  command << packages
572
572
 
573
573
  execute_shell(command)
@@ -663,7 +663,7 @@ module Kanrisuru
663
663
  zypper_download_and_install_opts(command, opts)
664
664
  zypper_expert_opts(command, opts)
665
665
 
666
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
666
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
667
667
  command << packages
668
668
 
669
669
  execute_shell(command)
@@ -714,7 +714,7 @@ module Kanrisuru
714
714
  zypper_package_type_opt(command, opts)
715
715
  zypper_solver_opts(command, opts)
716
716
 
717
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
717
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
718
718
  command << packages
719
719
 
720
720
  execute_shell(command)
@@ -783,7 +783,7 @@ module Kanrisuru
783
783
  zypper_repos_opt(command, opts)
784
784
  zypper_package_type_opt(command, opts)
785
785
 
786
- packages = Kanrisuru::Util.string_join_array(opts[:packages], ' ')
786
+ packages = Kanrisuru::Util.array_join_string(opts[:packages], ' ')
787
787
  command << packages
788
788
 
789
789
  execute_shell(command)
@@ -21,6 +21,10 @@ module Kanrisuru
21
21
  end
22
22
 
23
23
  def self.string_join_array(arg, field = ',')
24
+ Kanrisuru.logger.info do
25
+ 'DEPRECATION WARNING: string_join_array will be removed in the upcoming major release. Use array_join_string instead.'
26
+ end
27
+
24
28
  array = arg.instance_of?(String) ? [arg] : arg
25
29
  array.join(field)
26
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanrisuru
4
- VERSION = '0.8.15'
4
+ VERSION = '0.8.19'
5
5
  end
@@ -0,0 +1,163 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ StubNetwork.stub!
6
+
7
+ RSpec.describe Kanrisuru::Core::Find do
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 find command' do
17
+ expect_command(host.find, "find")
18
+
19
+ expect_command(host.find(follow: 'never'), "find -P")
20
+ expect_command(host.find(follow: 'always'), "find -L")
21
+ expect_command(host.find(follow: 'command'), "find -H")
22
+
23
+ expect_command(host.find(paths: '/etc'), "find /etc")
24
+ expect_command(host.find(paths: ['/etc', '/var', '/home']), "find /etc /var /home")
25
+
26
+ expect {
27
+ host.find(paths: 1)
28
+ }.to raise_error(ArgumentError)
29
+
30
+ expect_command(host.find(paths: '/',
31
+ executable: true,
32
+ empty: true,
33
+ readable: true,
34
+ writeable: true,
35
+ nogroup: true,
36
+ nouser: true,
37
+ mount: true
38
+ ),
39
+ "find / -executable -empty -readable -nogroup -nouser -mount"
40
+ )
41
+
42
+ expect_command(host.find(paths: '/',
43
+ path: "/lib",
44
+ name: '*.so',
45
+ gid: 0,
46
+ uid: 0,
47
+ user: 'root',
48
+ group: 'root',
49
+ links: 2,
50
+ mindepth: 0,
51
+ maxdepth: 100
52
+ ),
53
+ "find / -path /lib -name *.so -gid 0 -uid 0 -user root -group root -links 2 -maxdepth 100 -mindepth 0"
54
+ )
55
+
56
+ expect_command(host.find(paths: '/var/log',
57
+ atime: '+1',
58
+ ctime: '+2',
59
+ mtime: '+3',
60
+ amin: '100',
61
+ cmin: '200',
62
+ mmin: '300'
63
+ ),
64
+ "find /var/log -atime +1 -ctime +2 -mtime +3 -amin 100 -cmin 200 -mmin 300"
65
+ )
66
+
67
+ expect_command(host.find(
68
+ paths: '/dev',
69
+ regex: '/dev/tty[0-9]*'
70
+ ),
71
+ "find /dev -regex '/dev/tty[0-9]*'"
72
+ )
73
+
74
+ expect_command(host.find(
75
+ paths: '/dev',
76
+ regex_type: 'posix-egrep',
77
+ regex: '/dev/tty[0-9]*'
78
+ ),
79
+ "find /dev -regextype posix-egrep -regex '/dev/tty[0-9]*'"
80
+ )
81
+
82
+ expect_command(host.find(
83
+ paths: '/var/log',
84
+ size: 100
85
+ ),
86
+ "find /var/log -size 100"
87
+ )
88
+
89
+ expect_command(host.find(
90
+ paths: '/var/log',
91
+ size: "100"
92
+ ),
93
+ "find /var/log -size 100"
94
+ )
95
+
96
+ expect {
97
+ host.find(size: "100n")
98
+ }.to raise_error(ArgumentError)
99
+
100
+ expect_command(host.find(
101
+ paths: '/var/log',
102
+ size: "-100k"
103
+ ),
104
+ "find /var/log -size -100k"
105
+ )
106
+
107
+ expect_command(host.find(
108
+ paths: '/var/log',
109
+ size: "+10M"
110
+ ),
111
+ "find /var/log -size +10M"
112
+ )
113
+
114
+ expect_command(host.find(
115
+ paths: "/dev",
116
+ type: 'directory'
117
+ ),
118
+ "find /dev -type d"
119
+ )
120
+
121
+ expect_command(host.find(
122
+ paths: "/dev",
123
+ type: 'file'
124
+ ),
125
+ "find /dev -type f"
126
+ )
127
+
128
+ expect_command(host.find(
129
+ paths: "/dev",
130
+ type: 'symlinks'
131
+ ),
132
+ "find /dev -type l"
133
+ )
134
+
135
+ expect_command(host.find(
136
+ paths: "/dev",
137
+ type: 'block'
138
+ ),
139
+ "find /dev -type b"
140
+ )
141
+
142
+ expect_command(host.find(
143
+ paths: "/dev",
144
+ type: 'character'
145
+ ),
146
+ "find /dev -type c"
147
+ )
148
+
149
+ expect_command(host.find(
150
+ paths: "/dev",
151
+ type: 'pipe'
152
+ ),
153
+ "find /dev -type p"
154
+ )
155
+
156
+ expect_command(host.find(
157
+ paths: "/dev",
158
+ type: 'socket'
159
+ ),
160
+ "find /dev -type s"
161
+ )
162
+ end
163
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ StubNetwork.stub!
6
+
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
15
+
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
20
+
21
+ it 'prepares pwd command' do
22
+ expect_command(host.pwd, 'pwd')
23
+ end
24
+
25
+ it 'prepares whoami command' do
26
+ expect_command(host.whoami, 'whoami')
27
+ end
28
+
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
33
+
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
38
+
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')
44
+ end
45
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ StubNetwork.stub!
6
+
7
+ RSpec.describe Kanrisuru::Core::Socket do
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 ss command' do
17
+ expect_command(host.ss, 'ss -a -m')
18
+
19
+ expect_command(host.ss(
20
+ numeric: true,
21
+ tcp: true,
22
+ udp: true,
23
+ unix: true,
24
+ raw: true
25
+ ),
26
+ 'ss -a -m -n -t -u -x -w'
27
+ )
28
+
29
+ expect_command(host.ss(
30
+ family: 'inet'
31
+ ),
32
+ 'ss -a -m -f inet'
33
+ )
34
+
35
+ expect {
36
+ host.ss(family: 'inet5')
37
+ }.to raise_error(ArgumentError)
38
+
39
+ expect_command(host.ss(
40
+ state: 'established'
41
+ ),
42
+ 'ss -a -m state established'
43
+ )
44
+
45
+ expect_command(host.ss(
46
+ state: 'connected'
47
+ ),
48
+ 'ss -a -m state connected'
49
+ )
50
+
51
+ expect {
52
+ host.ss(state: 'test')
53
+ }.to raise_error(ArgumentError)
54
+
55
+ expect_command(host.ss(
56
+ expression: "'( dport = :ssh or sport = :ssh )'"
57
+ ),
58
+ "ss -a -m '( dport = :ssh or sport = :ssh )'"
59
+ )
60
+ end
61
+ end
@@ -8,21 +8,16 @@ RSpec.describe Kanrisuru::Core::Stat do
8
8
  let(:host) do
9
9
  Kanrisuru::Remote::Host.new(
10
10
  host: 'localhost',
11
- username: 'ubuntu',
11
+ username: 'ubuntu',
12
12
  keys: ['id_rsa']
13
13
  )
14
14
  end
15
15
 
16
16
  it 'prepares stat command' do
17
- result = host.stat('~/file1.txt')
18
- expect(result.command.raw_command).to eq(
19
- 'stat -c %A,%b,%D,%F,%g,%G,%h,%i,%n,%s,%u,%U,%x,%y,%z ~/file1.txt'
20
- )
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')
21
19
 
22
- result = host.stat('~/file2.txt', follow: true)
23
- expect(result.command.raw_command).to eq(
24
- 'stat -L -c %A,%b,%D,%F,%g,%G,%h,%i,%n,%s,%u,%U,%x,%y,%z ~/file1.txt'
25
- )
20
+ expect_command(host.stat('~/file2.txt', follow: true),
21
+ 'stat -L -c %A,%b,%D,%F,%g,%G,%h,%i,%n,%s,%u,%U,%x,%y,%z ~/file2.txt')
26
22
  end
27
-
28
23
  end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ StubNetwork.stub!
6
+
7
+ RSpec.describe Kanrisuru::Core::Stream do
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 do
46
+ host.read_file_chunk('file.log', 10, '20')
47
+ end.to raise_error(ArgumentError)
48
+
49
+ expect do
50
+ host.read_file_chunk('file.log', '10', 20)
51
+ end.to raise_error(ArgumentError)
52
+
53
+ expect do
54
+ host.read_file_chunk('file.log', 10, 9)
55
+ end.to raise_error(ArgumentError)
56
+
57
+ expect do
58
+ host.read_file_chunk('file.log', -1, 1)
59
+ end.to raise_error(ArgumentError)
60
+
61
+ expect do
62
+ host.read_file_chunk('file.log', 10, -2)
63
+ end.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
+
84
+ expect_command(host.echo('Goodbye', new_file: '~/file1.txt', mode: 'append'),
85
+ "echo 'Goodbye' >> ~/file1.txt")
86
+ end
87
+
88
+ it 'prepares cat command' do
89
+ expect_command(host.cat('/etc/group'), 'cat /etc/group')
90
+ expect_command(host.cat('/etc/group', show_all: true), 'cat -A /etc/group')
91
+ expect_command(host.cat('/etc/group',
92
+ show_tabs: true,
93
+ number: true,
94
+ squeeze_blank: true,
95
+ show_nonprinting: true,
96
+ show_ends: true,
97
+ number_nonblank: true), '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
+ end
@@ -0,0 +1,181 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ StubNetwork.stub!
6
+
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
15
+
16
+ it 'prepares wget command' do
17
+ url = 'https://rubygems.org'
18
+
19
+ expect_command(host.wget(url),
20
+ "wget #{url}"
21
+ )
22
+
23
+ ## Output options
24
+ expect_command(host.wget(url,
25
+ quiet: true, verbose: false, log_file: '/var/log/wget.log'),
26
+ "wget --quiet --no-verbose --output-file /var/log/wget.log #{url}"
27
+ )
28
+
29
+ expect_command(host.wget(url,
30
+ verbose: true, append_log_file: '/var/log/wget.log'),
31
+ "wget --verbose --append-output /var/log/wget.log #{url}"
32
+ )
33
+
34
+ ## Download options
35
+ expect_command(host.wget(url,
36
+ bind_address: '0.0.0.0',
37
+ retries: 3,
38
+ output_document: '/home/ubuntu/index.html',
39
+ no_clobber: true,
40
+ timeout: 30,
41
+ dns_timeout: 60,
42
+ connect_timeout: 60,
43
+ read_timeout: 15,
44
+ limit_rate: '120k',
45
+ wait: 5,
46
+ waitretry: 15,
47
+ random_wait: true,
48
+ no_proxy: true,
49
+ no_dns_cache: true
50
+ ),
51
+ "wget --bind-address 0.0.0.0 --tries 3 --output-document /home/ubuntu/index.html --no-clobber --timeout 30 --dns-timeout 60 --connect-timeout 60 --read-timeout 15 --limit-rate 120k --wait 5 --waitretry 15 --random-wait --no-proxy --no-dns-cache https://rubygems.org"
52
+ )
53
+
54
+ ## Other Options
55
+ expect_command(host.wget(url,
56
+ quota: 'inf',
57
+ family: 'inet',
58
+ restrict_file_names: ['unix', 'ascii', 'lowercase', 'uppercase'],
59
+ retry_connrefused: true,
60
+ user: 'admin',
61
+ password: 'admin',
62
+ no_iri: true,
63
+ ),
64
+ "wget --quota inf --restrict-file-names unix,ascii,lowercase,uppercase --inet4-only --retry-connrefused --user admin --password admin --no-iri #{url}"
65
+ )
66
+
67
+ ## Directories
68
+ expect_command(host.wget(url,
69
+ no_directories: true,
70
+ no_host_directories: true,
71
+ cut_dirs: 3,
72
+ directory_prefix: '~/downloads/'
73
+ ),
74
+ "wget --no-directories --no-host-directories --cut-dirs 3 --directory-prefix ~/downloads/ #{url}"
75
+ )
76
+
77
+ ## HTTP
78
+ expect_command(host.wget(url,
79
+ default_page: 'index.html',
80
+ adjust_extension: true,
81
+ http_user: 'admin',
82
+ http_password: 'admin',
83
+ load_cookies: '~/cookies.txt',
84
+ save_cookies: '~/cookies.txt',
85
+ no_cache: true,
86
+ keep_session_cookies: true,
87
+ ignore_length: true,
88
+ headers: {
89
+ 'Accept-Language' => 'hr',
90
+ 'Authorization' => 'Bearer 1234'
91
+ },
92
+ max_redirect: 5,
93
+ proxy_user: 'admin',
94
+ proxy_password: '12345678'
95
+ ),
96
+ "wget --default-page index.html --adjust-extension --http-user admin --http-password admin --load-cookies ~/cookies.txt --save-cookies ~/cookies.txt --no-cache --keep-session-cookies --ignore-length --max-redirect 5 --proxy-user admin --proxy-password 12345678 --header 'Accept-Language: hr' --header 'Authorization: Bearer 1234' #{url}"
97
+ )
98
+
99
+ expect_command(host.wget(url,
100
+ post_data: {
101
+ url: "https://example.com?param=123"
102
+ },
103
+ content_disposition: true,
104
+ secure_protocol: 'SSLv3',
105
+ no_check_certificate: true,
106
+ ),
107
+ "wget --post-data url=https%3A%2F%2Fexample.com%3Fparam%3D123 --content-disposition --secure-protocol SSLv3 --no-check-certificate #{url}"
108
+ )
109
+
110
+ expect {
111
+ host.wget(url, secure_protocol: 'SSL')
112
+ }.to raise_error(ArgumentError)
113
+
114
+ expect_command(host.wget(url,
115
+ certificate: '~/cert.pem',
116
+ certificate_type: 'PEM',
117
+ private_key: '~/key.pem',
118
+ private_key_type: 'PEM',
119
+ ca_certificate: '~/ca.pem',
120
+ random_file: '~/random'
121
+ ),
122
+ "wget --certificate ~/cert.pem --certificate-type PEM --private-key ~/key.pem --private-key-type PEM --ca-certificate ~/ca.pem --random-file ~/random #{url}"
123
+ )
124
+
125
+ expect_command(host.wget(url,
126
+ certificate: '~/cert.pem',
127
+ certificate_type: 'PEM',
128
+ private_key: '~/key.pem',
129
+ private_key_type: 'PEM',
130
+ ca_certificate: '~/ca.pem',
131
+ random_file: '~/random'
132
+ ),
133
+ "wget --certificate ~/cert.pem --certificate-type PEM --private-key ~/key.pem --private-key-type PEM --ca-certificate ~/ca.pem --random-file ~/random #{url}"
134
+ )
135
+
136
+ ## FTP
137
+ expect_command(host.wget(url,
138
+ ftp_user: 'admin',
139
+ ftp_password: '12345678',
140
+ no_remove_listing: true,
141
+ no_glob: true,
142
+ no_passive_ftp: true,
143
+ retr_symlinks: true
144
+ ),
145
+ "wget --ftp-user admin --ftp-password 12345678 --no-remove-listing --no-glob --no-passive-ftp --retr-symlinks #{url}"
146
+ )
147
+
148
+ ## Recursive Retrieval
149
+ expect_command(host.wget(url,
150
+ recursive: true,
151
+ depth: 10,
152
+ delete_after: true,
153
+ convert_links: true,
154
+ backup_converted: true,
155
+ mirror: true,
156
+ page_requisites: true,
157
+ strict_comments: true
158
+ ),
159
+ "wget --recursive --level 10 --delete-after --convert-links --backup-converted --mirror --page-requisites --strict-comments #{url}"
160
+ )
161
+
162
+ ## Recursive Accept/Reject
163
+ expect_command(host.wget(url,
164
+ accept_list: ['.txt', '.html'],
165
+ reject_list: ['.csv'],
166
+ domain_list: ['example.com'],
167
+ exclude_domain_list: ['hackernews.com'],
168
+ follow_tags: ['a', 'div', 'span'],
169
+ ignore_tags: ['area', 'link'],
170
+ include_directories: ['/gems'],
171
+ exclude_directories: ['/releases'],
172
+ follow_ftp: true,
173
+ ignore_case: true,
174
+ span_hosts: true,
175
+ relative: true,
176
+ no_parent: true
177
+ ),
178
+ "wget --accept .txt,.html --reject .csv --domains example.com --exclude-domains hackernews.com --follow-tags a,div,span --ignore-tags area,link --include-directories /gems --exclude-directories /releases --follow-ftp --ignore-case --span-hosts --relative --no-parent #{url}"
179
+ )
180
+ end
181
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ def expect_command(result, string)
4
+ puts result.command.raw_command
5
+ expect(result.command.raw_command).to eq(
6
+ string
7
+ )
8
+ end
@@ -2,15 +2,14 @@
2
2
 
3
3
  class StubNetwork
4
4
  class << self
5
-
6
5
  def stub!
7
- ## Stub out the execute_with_retries method to
8
- ## test functionality of host commands
6
+ ## Stub out the execute_with_retries method to
7
+ ## test functionality of host commands
9
8
  Kanrisuru::Remote::Host.class_eval do
10
9
  private
11
10
 
12
11
  def execute_with_retries(command)
13
- command.handle_status(0)
12
+ command.handle_status(0)
14
13
  command.handle_data(nil)
15
14
 
16
15
  command
@@ -24,22 +23,21 @@ class StubNetwork
24
23
  @kernel_name = 'Linux'
25
24
  @kernel_version = '#91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021'
26
25
  @operating_system = 'GNU/Linux'
27
- @hardware_platform = "x86_64"
28
- @processor = "x86_64"
29
- @release = "ubuntu"
26
+ @hardware_platform = 'x86_64'
27
+ @processor = 'x86_64'
28
+ @release = 'ubuntu'
30
29
  @version = 20.0
31
30
  end
32
31
  end
33
32
 
34
33
  Kanrisuru::Result.class_eval do
35
- def initialize(command, &block)
34
+ def initialize(command)
36
35
  @command = command
37
36
  @data = nil
38
37
 
39
38
  @error = @command.to_a if @command.failure?
40
39
  end
41
40
  end
42
- end
43
-
41
+ end
44
42
  end
45
43
  end
@@ -41,7 +41,7 @@ RSpec.describe Kanrisuru::Core::Disk do
41
41
  :name, :label, :uuid, :type, :uuid_sub, :label_fatboot, :version, :usage,
42
42
  :part_uuid, :part_entry_scheme, :part_entry_uuid, :part_entry_type,
43
43
  :part_entry_number, :part_entry_offset, :part_entry_size, :part_entry_disk,
44
- :minimum_io_size, :physical_sector_size, :logical_sector_size,
44
+ :minimum_io_size, :physical_sector_size, :logical_sector_size
45
45
  )
46
46
  end
47
47
 
@@ -41,7 +41,10 @@ RSpec.describe Kanrisuru::Core::File do
41
41
  )
42
42
 
43
43
  host.rm("#{host_json['home']}/.kanrisuru_spec_files", force: true, recursive: true)
44
- host.rm("#{host_json['home']}/extract-tar-files", force: true, recursive: true) if host.dir?("#{host_json['home']}/extract-tar-files")
44
+ if host.dir?("#{host_json['home']}/extract-tar-files")
45
+ host.rm("#{host_json['home']}/extract-tar-files", force: true,
46
+ recursive: true)
47
+ end
45
48
  host.disconnect
46
49
  end
47
50
 
@@ -68,9 +71,9 @@ RSpec.describe Kanrisuru::Core::File do
68
71
  expect(mode.symbolic).to eq('-rwxr--r--')
69
72
  expect(mode.to_i).to eq(0o744)
70
73
 
71
- expect {
74
+ expect do
72
75
  host.chmod(path, 600)
73
- }.to raise_error(ArgumentError)
76
+ end.to raise_error(ArgumentError)
74
77
  end
75
78
 
76
79
  it 'changes file owner and group' do
@@ -338,7 +341,7 @@ RSpec.describe Kanrisuru::Core::File do
338
341
  ## Can't delete non empty dir
339
342
  result = host.rmdir("#{spec_dir}/directory")
340
343
  expect(result).to be_failure
341
-
344
+
342
345
  result = host.rmdir("#{spec_dir}/directory/3")
343
346
  expect(result).to be_success
344
347
  end
@@ -355,7 +358,7 @@ RSpec.describe Kanrisuru::Core::File do
355
358
 
356
359
  lines = doc.length
357
360
  words = doc.map(&:split).flatten
358
- chars = doc.map(&:length).flatten
361
+ chars = doc.map(&:length).flatten
359
362
 
360
363
  expect(result.lines).to eq(doc.length)
361
364
  expect(result.words).to eq(words.length)
data/spec/spec_helper.rb CHANGED
@@ -4,8 +4,10 @@ require 'simplecov'
4
4
  SimpleCov.start
5
5
 
6
6
  require 'kanrisuru'
7
+
7
8
  require_relative 'helper/test_hosts'
8
9
  require_relative 'helper/stub_network'
10
+ require_relative 'helper/expect_helpers'
9
11
 
10
12
  Kanrisuru.logger.level = Logger::WARN
11
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
@@ -20,6 +20,11 @@ RSpec.describe Kanrisuru::Core::Socket do
20
20
  closed close-wait last-ack listening closing
21
21
  ]
22
22
  )
23
+ expect(Kanrisuru::Core::Socket::NETWORK_FAMILIES).to eq(
24
+ %w[
25
+ unix inet inet6 link netlink
26
+ ]
27
+ )
23
28
  expect(Kanrisuru::Core::Socket::OTHER_STATES).to eq(
24
29
  %w[
25
30
  all connected synchronized bucket syn-recv
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.15
4
+ version: 0.8.19
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-12 00:00:00.000000000 Z
11
+ date: 2021-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -173,8 +173,14 @@ 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/find_spec.rb
177
+ - spec/functional/core/path_spec.rb
178
+ - spec/functional/core/socket_spec.rb
176
179
  - spec/functional/core/stat_spec.rb
180
+ - spec/functional/core/stream_spec.rb
181
+ - spec/functional/core/transfer_spec.rb
177
182
  - spec/functional/os_package_spec.rb
183
+ - spec/helper/expect_helpers.rb
178
184
  - spec/helper/stub_network.rb
179
185
  - spec/helper/test_hosts.rb
180
186
  - spec/hosts.json