kanrisuru 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CONTRIBUTING.md +9 -9
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +7 -8
  4. data/CHANGELOG.md +110 -101
  5. data/CODE_OF_CONDUCT.md +10 -10
  6. data/README.md +1 -0
  7. data/kanrisuru.gemspec +1 -1
  8. data/lib/kanrisuru/core/disk.rb +0 -3
  9. data/lib/kanrisuru/core/find.rb +4 -5
  10. data/lib/kanrisuru/core/socket.rb +2 -1
  11. data/lib/kanrisuru/remote/cpu.rb +5 -1
  12. data/lib/kanrisuru/remote/fstab.rb +4 -4
  13. data/lib/kanrisuru/util.rb +1 -1
  14. data/lib/kanrisuru/version.rb +1 -1
  15. data/spec/functional/core/apt_spec.rb +22 -30
  16. data/spec/functional/core/archive_spec.rb +94 -119
  17. data/spec/functional/core/find_spec.rb +94 -113
  18. data/spec/functional/core/socket_spec.rb +23 -28
  19. data/spec/functional/core/stream_spec.rb +12 -12
  20. data/spec/functional/core/transfer_spec.rb +108 -131
  21. data/spec/functional/core/yum_spec.rb +58 -83
  22. data/spec/functional/remote/cluster_spec.rb +11 -2
  23. data/spec/functional/remote/cpu_spec.rb +104 -0
  24. data/spec/functional/remote/env_spec.rb +3 -5
  25. data/spec/helper/stub_network.rb +30 -11
  26. data/spec/integration/core/file_spec.rb +0 -1
  27. data/spec/integration/core/find_spec.rb +1 -0
  28. data/spec/integration/core/system_spec.rb +0 -1
  29. data/spec/integration/core/zypper_spec.rb +3 -3
  30. data/spec/unit/command_spec.rb +1 -1
  31. data/spec/unit/core/find_spec.rb +1 -1
  32. data/spec/unit/core/yum_spec.rb +1 -1
  33. data/spec/unit/mode_spec.rb +2 -2
  34. data/spec/unit/remote/cluster_spec.rb +3 -1
  35. data/spec/unit/remote/cpu_spec.rb +1 -2
  36. data/spec/unit/remote/env_spec.rb +1 -3
  37. data/spec/unit/util_spec.rb +13 -0
  38. metadata +4 -3
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Remote::Cpu do
6
+ before(:all) do
7
+ StubNetwork.stub!
8
+ end
9
+
10
+ after(:all) do
11
+ StubNetwork.unstub!
12
+ end
13
+
14
+ context 'metal' do
15
+ let(:host) do
16
+ Kanrisuru::Remote::Host.new(
17
+ host: 'metal-host',
18
+ username: 'ubuntu',
19
+ keys: ['id_rsa']
20
+ )
21
+ end
22
+
23
+ before do
24
+ StubNetwork.stub_command!(:lscpu) do |_args|
25
+ struct = Kanrisuru::Core::System::CPUArchitecture.new
26
+ struct.architecture = 'x86_64'
27
+ struct.cores = 48
28
+ struct.byte_order = 'Little Endian'
29
+ struct.address_sizes = ['46 bits physical', '48 bits virtual']
30
+ struct.operation_modes = %w[32-bit 64-bit]
31
+ struct.online_cpus = 0
32
+ struct.threads_per_core = 2
33
+ struct.cores_per_socket = 12
34
+ struct.sockets = 2
35
+ struct.numa_mode = nil
36
+ struct.vendor_id = 'GenuineIntel'
37
+ struct.cpu_family = 6
38
+ struct.model = 63
39
+ struct.model_name = 'Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz'
40
+ struct.stepping = 2
41
+ struct.cpu_mhz = 1200.16
42
+ struct.cpu_max_mhz = 3300.0
43
+ struct.cpu_min_mhz = 1200.0
44
+ struct.bogo_mips = nil
45
+ struct.virtualization = 'VT-x'
46
+ struct.hypervisor_vendor = nil
47
+ struct.virtualization_type = nil
48
+ struct.l1d_cache = '768 KiB'
49
+ struct.l1i_cache = '768 KiB'
50
+ struct.l2_cache = '6 MiB'
51
+ struct.l3_cache = '60 MiB'
52
+ struct.numa_nodes = 2
53
+ struct.vulnerabilities = [
54
+ Kanrisuru::Core::System::CPUArchitectureVulnerability.new('Itlb multihit',
55
+ 'KVM: Mitigation: Split huge pages'),
56
+ Kanrisuru::Core::System::CPUArchitectureVulnerability.new('L1tf',
57
+ 'Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable'),
58
+ Kanrisuru::Core::System::CPUArchitectureVulnerability.new('Mds',
59
+ 'Mitigation; Clear CPU buffers; SMT vulnerable'),
60
+ Kanrisuru::Core::System::CPUArchitectureVulnerability.new('Meltdown', 'Mitigation; PTI'),
61
+ Kanrisuru::Core::System::CPUArchitectureVulnerability.new('Spec store bypass',
62
+ 'Mitigation; Speculative Store Bypass disabled via prctl and seccomp'),
63
+ Kanrisuru::Core::System::CPUArchitectureVulnerability.new('Spectre v1',
64
+ 'Mitigation; usercopy/swapgs barriers and __user pointer sanitization'),
65
+ Kanrisuru::Core::System::CPUArchitectureVulnerability.new('Spectre v2',
66
+ 'Mitigation; Full generic retpoline, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling'),
67
+ Kanrisuru::Core::System::CPUArchitectureVulnerability.new('Srbds', 'Not affected'),
68
+ Kanrisuru::Core::System::CPUArchitectureVulnerability.new('Tsx async abort', 'Not affected')
69
+ ]
70
+ struct.flags = %w[fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge
71
+ mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti intel_ppin ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc dtherm ida arat pln pts md_clear flush_l1d]
72
+ struct
73
+ end
74
+ end
75
+
76
+ after do
77
+ StubNetwork.unstub_command!(:lscpu)
78
+ end
79
+
80
+ it 'gets host cpu attributes' do
81
+ expect(host.cpu.architecture).to eq('x86_64')
82
+ expect(host.cpu.cores).to eq(48)
83
+ expect(host.cpu.byte_order).to eq('Little Endian')
84
+ expect(host.cpu.address_sizes).to eq(['46 bits physical', '48 bits virtual'])
85
+ expect(host.cpu.threads_per_core).to eq(2)
86
+ expect(host.cpu.cores_per_socket).to eq(12)
87
+ expect(host.cpu.sockets).to eq(2)
88
+ expect(host.cpu.vendor_id).to eq('GenuineIntel')
89
+ expect(host.cpu.cpu_family).to eq(6)
90
+ expect(host.cpu.model).to eq(63)
91
+ expect(host.cpu.model_name).to eq('Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz')
92
+ expect(host.cpu.cpu_mhz).to eq(1200.16)
93
+ expect(host.cpu.cpu_max_mhz).to eq(3300.0)
94
+ expect(host.cpu.cpu_min_mhz).to eq(1200.0)
95
+ expect(host.cpu.hypervisor).to be_nil
96
+ expect(host.cpu.virtualization_type).to be_nil
97
+ # expect(host.cpu.l1d_cache).to eq("768 KiB")
98
+ # expect(host.cpu.l1i_cache).to eq("768 KiB")
99
+ # expect(host.cpu.l2_cache).to eq("6 MiB")
100
+ # expect(host.cpu.l3_cache).to eq("60 MiB")
101
+ expect(host.cpu.numa_nodes).to eq(2)
102
+ end
103
+ end
104
+ end
@@ -3,13 +3,12 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe Kanrisuru::Remote::Env do
6
-
7
- let (:env) { Kanrisuru::Remote::Env.new }
6
+ let(:env) { described_class.new }
8
7
 
9
8
  it 'adds a environment variable' do
10
9
  env['VAR1'] = 'hello'
11
10
  expect(env.count).to eq(1)
12
- expect(env.to_h).to eq({'VAR1' => 'hello'})
11
+ expect(env.to_h).to eq({ 'VAR1' => 'hello' })
13
12
  expect(env.to_s).to eq('export VAR1=hello;')
14
13
  expect(env['VAR1']).to eq('hello')
15
14
  end
@@ -19,7 +18,7 @@ RSpec.describe Kanrisuru::Remote::Env do
19
18
  env['var2'] = 'world'
20
19
 
21
20
  expect(env.count).to eq(2)
22
- expect(env.to_h).to eq({'VAR1' => 'hello', 'VAR2' => 'world'})
21
+ expect(env.to_h).to eq({ 'VAR1' => 'hello', 'VAR2' => 'world' })
23
22
  expect(env.to_s).to eq('export VAR1=hello; export VAR2=world;')
24
23
  expect(env['VAR1']).to eq('hello')
25
24
  expect(env['VAR2']).to eq('world')
@@ -46,5 +45,4 @@ RSpec.describe Kanrisuru::Remote::Env do
46
45
 
47
46
  expect(env.count).to eq(0)
48
47
  end
49
-
50
48
  end
@@ -17,7 +17,7 @@ class StubNetwork
17
17
 
18
18
  unless Kanrisuru::Remote::Os.instance_methods(false).include?(:initialize_alias)
19
19
  Kanrisuru::Remote::Os.class_eval do
20
- alias_method :initialize_alias, :initialize
20
+ alias_method :initialize_alias, :initialize
21
21
  define_method :initialize do |host|
22
22
  @host = host
23
23
 
@@ -34,23 +34,44 @@ class StubNetwork
34
34
 
35
35
  unless Kanrisuru::Result.instance_methods(false).include?(:initialize_alias)
36
36
  Kanrisuru::Result.class_eval do
37
- alias_method :initialize_alias, :initialize
38
- def initialize(command)
37
+ alias_method :initialize_alias, :initialize
38
+ def initialize(command, parse_result = false, &block)
39
39
  @command = command
40
40
  @data = nil
41
41
 
42
+ @data = block.call(@command) if @command.success? && block_given? && parse_result
42
43
  @error = @command.to_a if @command.failure?
44
+
45
+ ## Define getter methods on result that maps to
46
+ ## the same methods of a data struct.
47
+ return unless @command.success? && Kanrisuru::Util.present?(@data) && @data.class.ancestors.include?(Struct)
48
+
49
+ method_names = @data.members
50
+ self.class.class_eval do
51
+ method_names.each do |method_name|
52
+ define_method method_name do
53
+ @data[method_name]
54
+ end
55
+ end
56
+ end
43
57
  end
44
58
  end
45
59
  end
46
60
  end
47
61
 
48
- def stub_command!(method, &block)
62
+ def stub_command!(method, opts = {}, &block)
49
63
  Kanrisuru::Remote::Host.class_eval do
50
64
  alias_method "#{method}_alias", method
51
65
 
52
66
  define_method(method) do |*args|
53
- block.call(args)
67
+ command = Kanrisuru::Command.new(method.to_s)
68
+
69
+ status = opts[:status] || 0
70
+ command.handle_status(status)
71
+
72
+ Kanrisuru::Result.new(command, true) do |_cmd|
73
+ block.call(args)
74
+ end
54
75
  end
55
76
  end
56
77
  end
@@ -67,11 +88,11 @@ class StubNetwork
67
88
  end
68
89
 
69
90
  Kanrisuru::Remote::Os.class_eval do
70
- alias_method :initialize, :initialize_alias
91
+ alias_method :initialize, :initialize_alias
71
92
  end
72
93
 
73
94
  Kanrisuru::Result.class_eval do
74
- alias_method :initialize, :initialize_alias
95
+ alias_method :initialize, :initialize_alias
75
96
  end
76
97
  end
77
98
 
@@ -106,13 +127,11 @@ class StubNetwork
106
127
  hardware_platform: 'x86_64',
107
128
  processor: 'x86_64',
108
129
  release: 'opensuse-leap',
109
- version:15.2
130
+ version: 15.2
110
131
  }
111
132
  }
112
133
 
113
- defaults[name].key?(property) ?
114
- defaults[name][property] : nil
134
+ defaults[name][property] if defaults[name].key?(property)
115
135
  end
116
-
117
136
  end
118
137
  end
@@ -356,7 +356,6 @@ RSpec.describe Kanrisuru::Core::File do
356
356
  data = host.cat('/etc/hosts').command.raw_result
357
357
  doc = data.map(&:lines).flatten
358
358
 
359
- lines = doc.length
360
359
  words = doc.map(&:split).flatten
361
360
  chars = doc.map(&:length).flatten
362
361
 
@@ -19,6 +19,7 @@ RSpec.describe Kanrisuru::Core::Find do
19
19
  end
20
20
 
21
21
  it 'finds home directory' do
22
+ host.su('root')
22
23
  result = host.find(paths: '/home')
23
24
  home = result.find { |item| item.path == host_json['home'] }
24
25
  expect(home.path).to eq(host_json['home'])
@@ -107,7 +107,6 @@ RSpec.describe Kanrisuru::Core::System do
107
107
  host.su('root')
108
108
 
109
109
  result = host.last(failed_attempts: true)
110
- puts result.data
111
110
  expect(result).to be_success
112
111
  end
113
112
 
@@ -30,19 +30,19 @@ RSpec.describe Kanrisuru::Core::Zypper do
30
30
 
31
31
  it 'installs a package' do
32
32
  host.su('root')
33
- result = host.zypper('install', packages: 'ffmpeg')
33
+ result = host.zypper('install', packages: 'nginx')
34
34
  expect(result).to be_success
35
35
  end
36
36
 
37
37
  it 'installs multiple packages' do
38
38
  host.su('root')
39
- result = host.zypper('install', packages: %w[curl ffmpeg])
39
+ result = host.zypper('install', packages: %w[curl nginx])
40
40
  expect(result).to be_success
41
41
  end
42
42
 
43
43
  it 'removes a package' do
44
44
  host.su('root')
45
- result = host.zypper('remove', packages: ['ffmpeg'])
45
+ result = host.zypper('remove', packages: ['nginx'])
46
46
  expect(result).to be_success
47
47
  end
48
48
 
@@ -4,7 +4,7 @@ require 'spec_helper'
4
4
 
5
5
  RSpec.describe Kanrisuru::Command do
6
6
  it 'responds to methods' do
7
- command = Kanrisuru::Command.new('ls')
7
+ command = described_class.new('ls')
8
8
  expect(command).to respond_to(:exit_status)
9
9
  expect(command).to respond_to(:raw_result)
10
10
  expect(command).to respond_to(:program)
@@ -26,7 +26,7 @@ RSpec.describe Kanrisuru::Core::Find do
26
26
  it 'responds to find fields' do
27
27
  expect(Kanrisuru::Core::Find::FilePath.new).to respond_to(:path)
28
28
  expect(Kanrisuru::Core::Find::REGEX_TYPES).to(
29
- eq(['emacs', 'posix-awk', 'posix-basic', 'posix-egrep', 'posix-extended'])
29
+ eq(%w[emacs posix-awk posix-basic posix-egrep posix-extended])
30
30
  )
31
31
  end
32
32
  end
@@ -22,7 +22,7 @@ RSpec.describe Kanrisuru::Core::Yum do
22
22
  it 'responds to methods' do
23
23
  expect(host).to respond_to(:yum)
24
24
  end
25
-
25
+
26
26
  it 'responds to yum fields' do
27
27
  expect(Kanrisuru::Core::Yum::PackageOverview.new).to respond_to(
28
28
  :package, :architecture, :version, :installed
@@ -92,9 +92,9 @@ RSpec.describe Kanrisuru::Mode do
92
92
  expect(mode.other.to_i).to eq(7)
93
93
  expect(mode.other.symbolic).to eq('rwx')
94
94
 
95
- mode = described_class.new("---x--x--x")
95
+ mode = described_class.new('---x--x--x')
96
96
  expect(mode.directory?).to eq(false)
97
- expect(mode.numeric).to eq("111")
97
+ expect(mode.numeric).to eq('111')
98
98
 
99
99
  expect(mode.owner.read?).to eq(false)
100
100
  expect(mode.owner.write?).to eq(false)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe Kanrisuru::Remote::Cluster do
2
4
  before(:all) do
3
5
  StubNetwork.stub!
@@ -16,7 +18,7 @@ RSpec.describe Kanrisuru::Remote::Cluster do
16
18
  end
17
19
 
18
20
  it 'responds to methods' do
19
- cluster = Kanrisuru::Remote::Cluster.new(host1)
21
+ cluster = described_class.new(host1)
20
22
  expect(cluster).to respond_to(:hosts)
21
23
  expect(cluster).to respond_to(:[])
22
24
  expect(cluster).to respond_to(:<<)
@@ -20,8 +20,7 @@ RSpec.describe Kanrisuru::Remote::Cpu do
20
20
  end
21
21
 
22
22
  it 'responds to methods' do
23
- cpu = Kanrisuru::Remote::Cpu.new(host)
24
-
23
+ cpu = described_class.new(host)
25
24
  expect(cpu).to respond_to(:load_average)
26
25
  expect(cpu).to respond_to(:load_average1)
27
26
  expect(cpu).to respond_to(:load_average5)
@@ -3,9 +3,8 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe Kanrisuru::Remote::Env do
6
-
7
6
  it 'responds to methods' do
8
- env = Kanrisuru::Remote::Env.new
7
+ env = described_class.new
9
8
  expect(env).to respond_to(:to_h)
10
9
  expect(env).to respond_to(:to_s)
11
10
  expect(env).to respond_to(:clear)
@@ -15,5 +14,4 @@ RSpec.describe Kanrisuru::Remote::Env do
15
14
  expect(env).to respond_to(:[])
16
15
  expect(env).to respond_to(:[]=)
17
16
  end
18
-
19
17
  end
@@ -84,6 +84,10 @@ RSpec.describe Kanrisuru::Util do
84
84
  expect { Kanrisuru::Util::Bits.convert_bytes(100, :kilobytee, :kilobit) }.to raise_error(ArgumentError)
85
85
  end
86
86
 
87
+ it 'gets invalid signal type' do
88
+ expect { Kanrisuru::Util::Signal[{ 'Hello' => 'World' }] }.to raise_error(ArgumentError)
89
+ end
90
+
87
91
  it 'translates signal from string to numeric' do
88
92
  expect(Kanrisuru::Util::Signal['HUP']).to eq(1)
89
93
  expect(Kanrisuru::Util::Signal['INT']).to eq(2)
@@ -214,6 +218,10 @@ RSpec.describe Kanrisuru::Util do
214
218
  expect(Kanrisuru::Util::Signal[64]).to eq('RTMAX')
215
219
  end
216
220
 
221
+ it 'gets invalid dmi type' do
222
+ expect { Kanrisuru::Util::DmiType[{ 'Hello' => 'World' }] }.to raise_error(ArgumentError)
223
+ end
224
+
217
225
  it 'translates dmi types from string to integer' do
218
226
  expect(Kanrisuru::Util::DmiType['BIOS']).to eq(0)
219
227
  expect(Kanrisuru::Util::DmiType['System']).to eq(1)
@@ -308,6 +316,11 @@ RSpec.describe Kanrisuru::Util do
308
316
  expect(Kanrisuru::Util::DmiType[43]).to eq('TPM Device')
309
317
  end
310
318
 
319
+ it 'gets fs_mount options' do
320
+ expect(Kanrisuru::Util::FsMountOpts.get_device('common')[:async]).to eq('boolean')
321
+ expect(Kanrisuru::Util::FsMountOpts.get_device_opt(:ext4, :acl)).to eq('boolean')
322
+ end
323
+
311
324
  it 'converts power' do
312
325
  expect(Kanrisuru::Util::Bits.convert_power(:deca, :deca)).to eq(0)
313
326
  expect(Kanrisuru::Util::Bits.convert_power(:deca, :kilo)).to eq(-1)
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.10.0
4
+ version: 0.11.0
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-12-03 00:00:00.000000000 Z
11
+ date: 2021-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -123,7 +123,7 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: '6.1'
125
125
  description: Kanrisuru helps manage remote servers with objected oriented ruby. Results
126
- come back as structured data, parsed, prepared and ready .
126
+ come back as structured data, parsed, prepared and ready.
127
127
  email: ryan@avamia.com
128
128
  executables: []
129
129
  extensions: []
@@ -198,6 +198,7 @@ files:
198
198
  - spec/functional/core/transfer_spec.rb
199
199
  - spec/functional/core/yum_spec.rb
200
200
  - spec/functional/remote/cluster_spec.rb
201
+ - spec/functional/remote/cpu_spec.rb
201
202
  - spec/functional/remote/env_spec.rb
202
203
  - spec/helper/expect_helpers.rb
203
204
  - spec/helper/stub_network.rb