train 0.29.2 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -2
  3. data/lib/train.rb +1 -0
  4. data/lib/train/errors.rb +6 -0
  5. data/lib/train/extras.rb +0 -1
  6. data/lib/train/platforms.rb +82 -0
  7. data/lib/train/platforms/common.rb +34 -0
  8. data/lib/train/platforms/detect.rb +12 -0
  9. data/lib/train/platforms/detect/helpers/os_common.rb +56 -0
  10. data/lib/train/platforms/detect/helpers/os_linux.rb +75 -0
  11. data/lib/train/{extras/os_detect_windows.rb → platforms/detect/helpers/os_windows.rb} +3 -10
  12. data/lib/train/platforms/detect/scanner.rb +84 -0
  13. data/lib/train/platforms/detect/specifications/os.rb +480 -0
  14. data/lib/train/platforms/family.rb +26 -0
  15. data/lib/train/platforms/platform.rb +80 -0
  16. data/lib/train/plugins/base_connection.rb +75 -27
  17. data/lib/train/transports/docker.rb +17 -28
  18. data/lib/train/transports/local.rb +21 -22
  19. data/lib/train/transports/mock.rb +44 -30
  20. data/lib/train/transports/ssh_connection.rb +55 -67
  21. data/lib/train/transports/winrm_connection.rb +16 -26
  22. data/lib/train/version.rb +1 -1
  23. data/test/unit/file/remote/linux_test.rb +2 -2
  24. data/test/unit/platforms/detect/os_common_test.rb +85 -0
  25. data/test/unit/platforms/detect/os_linux_test.rb +124 -0
  26. data/test/unit/{extras/os_detect_windows_test.rb → platforms/detect/os_windows_test.rb} +5 -2
  27. data/test/unit/platforms/detect/scanner_test.rb +61 -0
  28. data/test/unit/platforms/family_test.rb +32 -0
  29. data/test/unit/platforms/os_detect_test.rb +175 -0
  30. data/test/unit/{extras/os_common_test.rb → platforms/platform_test.rb} +103 -18
  31. data/test/unit/platforms/platforms_test.rb +42 -0
  32. data/test/unit/plugins/connection_test.rb +106 -8
  33. data/test/unit/transports/local_test.rb +20 -15
  34. data/test/unit/transports/mock_test.rb +16 -6
  35. data/test/unit/transports/ssh_test.rb +17 -15
  36. metadata +28 -19
  37. data/lib/train/extras/linux_lsb.rb +0 -60
  38. data/lib/train/extras/os_common.rb +0 -151
  39. data/lib/train/extras/os_detect_arista_eos.rb +0 -34
  40. data/lib/train/extras/os_detect_darwin.rb +0 -40
  41. data/lib/train/extras/os_detect_esx.rb +0 -22
  42. data/lib/train/extras/os_detect_linux.rb +0 -164
  43. data/lib/train/extras/os_detect_openvms.rb +0 -29
  44. data/lib/train/extras/os_detect_unix.rb +0 -106
  45. data/lib/train/extras/uname.rb +0 -28
  46. data/lib/train/transports/local_os.rb +0 -51
  47. data/test/unit/extras/os_detect_linux_test.rb +0 -230
@@ -10,20 +10,28 @@ describe 'v1 Connection Plugin' do
10
10
  connection.close # wont raise
11
11
  end
12
12
 
13
- it 'provides a run_command method' do
14
- proc { connection.run_command('') }.must_raise Train::ClientError
13
+ it 'raises an exception for run_command' do
14
+ proc { connection.run_command('') }.must_raise NotImplementedError
15
15
  end
16
16
 
17
- it 'provides an os method' do
18
- proc { connection.os }.must_raise Train::ClientError
17
+ it 'raises an exception for run_command_via_connection' do
18
+ proc { connection.send(:run_command_via_connection, '') }.must_raise NotImplementedError
19
19
  end
20
20
 
21
- it 'provides a file method' do
22
- proc { connection.file('') }.must_raise Train::ClientError
21
+ it 'raises an exception for os method' do
22
+ proc { connection.os }.must_raise NotImplementedError
23
23
  end
24
24
 
25
- it 'provides a login command method' do
26
- proc { connection.login_command }.must_raise Train::ClientError
25
+ it 'raises an exception for file method' do
26
+ proc { connection.file('') }.must_raise NotImplementedError
27
+ end
28
+
29
+ it 'raises an exception for file_via_connection method' do
30
+ proc { connection.send(:file_via_connection, '') }.must_raise NotImplementedError
31
+ end
32
+
33
+ it 'raises an exception for login command method' do
34
+ proc { connection.login_command }.must_raise NotImplementedError
27
35
  end
28
36
 
29
37
  it 'can wait until ready' do
@@ -40,5 +48,95 @@ describe 'v1 Connection Plugin' do
40
48
  cls.new({logger: l})
41
49
  .method(:logger).call.must_equal(l)
42
50
  end
51
+
52
+ describe 'create cache connection' do
53
+ it 'default connection cache settings' do
54
+ connection.cache_enabled?(:file).must_equal true
55
+ connection.cache_enabled?(:command).must_equal false
56
+ end
57
+ end
58
+
59
+ describe 'disable/enable caching' do
60
+ it 'disable file cache via connection' do
61
+ connection.disable_cache(:file)
62
+ connection.cache_enabled?(:file).must_equal false
63
+ end
64
+
65
+ it 'enable command cache via cache_connection' do
66
+ connection.enable_cache(:command)
67
+ connection.cache_enabled?(:command).must_equal true
68
+ end
69
+
70
+ it 'raises an exception for unknown cache type' do
71
+ proc { connection.enable_cache(:fake) }.must_raise Train::UnknownCacheType
72
+ proc { connection.disable_cache(:fake) }.must_raise Train::UnknownCacheType
73
+ end
74
+ end
75
+
76
+ describe 'cache enable check' do
77
+ it 'returns true when cache is enabled' do
78
+ cache_enabled = connection.instance_variable_get(:@cache_enabled)
79
+ cache_enabled[:test] = true
80
+ connection.cache_enabled?(:test).must_equal true
81
+ end
82
+
83
+ it 'returns false when cache is disabled' do
84
+ cache_enabled = connection.instance_variable_get(:@cache_enabled)
85
+ cache_enabled[:test] = false
86
+ connection.cache_enabled?(:test).must_equal false
87
+ end
88
+ end
89
+
90
+ describe 'clear cache' do
91
+ it 'clear file cache' do
92
+ cache = connection.instance_variable_get(:@cache)
93
+ cache[:file]['/tmp'] = 'test'
94
+ connection.send(:clear_cache, :file)
95
+ cache = connection.instance_variable_get(:@cache)
96
+ cache[:file].must_equal({})
97
+ end
98
+ end
99
+
100
+ describe 'load file' do
101
+ it 'with caching' do
102
+ connection.enable_cache(:file)
103
+ connection.expects(:file_via_connection).once.returns('test_file')
104
+ connection.file('/tmp/test').must_equal('test_file')
105
+ connection.file('/tmp/test').must_equal('test_file')
106
+ assert = { '/tmp/test' => 'test_file' }
107
+ cache = connection.instance_variable_get(:@cache)
108
+ cache[:file].must_equal(assert)
109
+ end
110
+
111
+ it 'without caching' do
112
+ connection.disable_cache(:file)
113
+ connection.expects(:file_via_connection).twice.returns('test_file')
114
+ connection.file('/tmp/test').must_equal('test_file')
115
+ connection.file('/tmp/test').must_equal('test_file')
116
+ cache = connection.instance_variable_get(:@cache)
117
+ cache[:file].must_equal({})
118
+ end
119
+ end
120
+
121
+ describe 'run command' do
122
+ it 'with caching' do
123
+ connection.enable_cache(:command)
124
+ connection.expects(:run_command_via_connection).once.returns('test_user')
125
+ connection.run_command('whoami').must_equal('test_user')
126
+ connection.run_command('whoami').must_equal('test_user')
127
+ assert = { 'whoami' => 'test_user' }
128
+ cache = connection.instance_variable_get(:@cache)
129
+ cache[:command].must_equal(assert)
130
+ end
131
+
132
+ it 'without caching' do
133
+ connection.disable_cache(:command)
134
+ connection.expects(:run_command_via_connection).twice.returns('test_user')
135
+ connection.run_command('whoami').must_equal('test_user')
136
+ connection.run_command('whoami').must_equal('test_user')
137
+ cache = connection.instance_variable_get(:@cache)
138
+ cache[:command].must_equal({})
139
+ end
140
+ end
43
141
  end
44
142
  end
@@ -1,27 +1,22 @@
1
1
  # encoding: utf-8
2
- #
3
- # author: Dominik Richter
4
- # author: Christoph Hartmann
5
2
 
6
3
  require 'helper'
7
4
  require 'train/transports/local'
8
5
 
9
- # overwrite os detection to simplify mock tests, otherwise run_command tries to
10
- # determine the OS first and fails the tests
11
- class Train::Transports::Local::Connection
12
- class OS < OSCommon
13
- def initialize(backend)
14
- super(backend, { family: 'train_mock_os' })
15
- end
6
+ class TransportHelper
7
+ attr_accessor :transport
16
8
 
17
- def detect_family
18
- # no op, we do not need to detect the os
19
- end
9
+ def initialize
10
+ Train::Platforms::Detect::Specifications::OS.load
11
+ plat = Train::Platforms.name('mock').in_family('linux')
12
+ plat.add_platform_methods
13
+ Train::Platforms::Detect.stubs(:scan).returns(plat)
14
+ @transport = Train::Transports::Local.new
20
15
  end
21
16
  end
22
17
 
23
18
  describe 'local transport' do
24
- let(:transport) { Train::Transports::Local.new }
19
+ let(:transport) { TransportHelper.new.transport }
25
20
  let(:connection) { transport.connection }
26
21
 
27
22
  it 'can be instantiated' do
@@ -33,7 +28,7 @@ describe 'local transport' do
33
28
  end
34
29
 
35
30
  it 'provides a uri' do
36
- connection.uri.must_equal "local://"
31
+ connection.uri.must_equal 'local://'
37
32
  end
38
33
 
39
34
  it 'doesnt wait to be read' do
@@ -48,6 +43,16 @@ describe 'local transport' do
48
43
  connection.login_command.must_be_nil
49
44
  end
50
45
 
46
+ it 'provides a run_command_via_connection method' do
47
+ methods = connection.class.private_instance_methods(false)
48
+ methods.include?(:run_command_via_connection).must_equal true
49
+ end
50
+
51
+ it 'provides a file_via_connection method' do
52
+ methods = connection.class.private_instance_methods(false)
53
+ methods.include?(:file_via_connection).must_equal true
54
+ end
55
+
51
56
  describe 'when running a local command' do
52
57
  let(:cmd_runner) { Minitest::Mock.new }
53
58
 
@@ -19,6 +19,16 @@ describe 'mock transport' do
19
19
  connection.uri.must_equal 'mock://'
20
20
  end
21
21
 
22
+ it 'provides a run_command_via_connection method' do
23
+ methods = connection.class.private_instance_methods(false)
24
+ methods.include?(:run_command_via_connection).must_equal true
25
+ end
26
+
27
+ it 'provides a file_via_connection method' do
28
+ methods = connection.class.private_instance_methods(false)
29
+ methods.include?(:file_via_connection).must_equal true
30
+ end
31
+
22
32
  describe 'when running a mocked command' do
23
33
  let(:mock_cmd) { }
24
34
 
@@ -71,15 +81,15 @@ describe 'mock transport' do
71
81
  end
72
82
 
73
83
  describe 'when accessing a mocked os' do
74
- it 'has the default mock os faily set to unknown' do
75
- connection.os[:family].must_equal 'unknown'
84
+ it 'has the default mock os faily set to mock' do
85
+ connection.os[:name].must_equal 'mock'
76
86
  end
77
87
 
78
88
  it 'sets the OS to the mocked value' do
79
- connection.mock_os({ family: 'centos' })
89
+ connection.mock_os({ name: 'centos', family: 'redhat' })
80
90
  connection.os.linux?.must_equal true
81
91
  connection.os.redhat?.must_equal true
82
- connection.os[:family].must_equal 'centos'
92
+ connection.os[:family].must_equal 'redhat'
83
93
  end
84
94
 
85
95
  it 'allows the setting of the name' do
@@ -112,8 +122,8 @@ describe 'mock transport' do
112
122
 
113
123
  it 'properly handles a nil value' do
114
124
  connection.mock_os(nil)
115
- connection.os[:name].must_equal 'unknown'
116
- connection.os[:family].must_equal 'unknown'
125
+ connection.os[:name].must_equal 'mock'
126
+ connection.os[:family].must_equal 'mock'
117
127
  end
118
128
  end
119
129
 
@@ -1,23 +1,15 @@
1
1
  # encoding: utf-8
2
+
2
3
  require 'helper'
3
4
  require 'train/transports/ssh'
4
5
 
5
- # overwrite os detection to simplify mock tests, otherwise run_command tries to
6
- # determine the OS first and fails the tests
7
- class Train::Transports::SSH::Connection
8
- class OS < OSCommon
9
- def initialize(backend)
10
- super(backend, { family: 'train_mock_os' })
11
- end
12
-
13
- def detect_family
14
- # no op, we do not need to detect the os
15
- end
16
- end
17
- end
18
-
19
6
  describe 'ssh transport' do
20
- let(:cls) { Train::Transports::SSH }
7
+ let(:cls) do
8
+ plat = Train::Platforms.name('mock').in_family('linux')
9
+ plat.add_platform_methods
10
+ Train::Platforms::Detect.stubs(:scan).returns(plat)
11
+ Train::Transports::SSH
12
+ end
21
13
  let(:conf) {{
22
14
  host: rand.to_s,
23
15
  password: rand.to_s,
@@ -66,6 +58,16 @@ describe 'ssh transport' do
66
58
  let(:ssh) { cls.new(conf) }
67
59
  let(:connection) { ssh.connection }
68
60
 
61
+ it 'provides a run_command_via_connection method' do
62
+ methods = connection.class.private_instance_methods(false)
63
+ methods.include?(:run_command_via_connection).must_equal true
64
+ end
65
+
66
+ it 'provides a file_via_connection method' do
67
+ methods = connection.class.private_instance_methods(false)
68
+ methods.include?(:file_via_connection).must_equal true
69
+ end
70
+
69
71
  it 'gets the connection' do
70
72
  connection.must_be_kind_of Train::Transports::SSH::Connection
71
73
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: train
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.29.2
4
+ version: 0.30.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominik Richter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-21 00:00:00.000000000 Z
11
+ date: 2017-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -151,17 +151,7 @@ files:
151
151
  - lib/train/errors.rb
152
152
  - lib/train/extras.rb
153
153
  - lib/train/extras/command_wrapper.rb
154
- - lib/train/extras/linux_lsb.rb
155
- - lib/train/extras/os_common.rb
156
- - lib/train/extras/os_detect_arista_eos.rb
157
- - lib/train/extras/os_detect_darwin.rb
158
- - lib/train/extras/os_detect_esx.rb
159
- - lib/train/extras/os_detect_linux.rb
160
- - lib/train/extras/os_detect_openvms.rb
161
- - lib/train/extras/os_detect_unix.rb
162
- - lib/train/extras/os_detect_windows.rb
163
154
  - lib/train/extras/stat.rb
164
- - lib/train/extras/uname.rb
165
155
  - lib/train/file.rb
166
156
  - lib/train/file/local.rb
167
157
  - lib/train/file/local/unix.rb
@@ -173,12 +163,21 @@ files:
173
163
  - lib/train/file/remote/unix.rb
174
164
  - lib/train/file/remote/windows.rb
175
165
  - lib/train/options.rb
166
+ - lib/train/platforms.rb
167
+ - lib/train/platforms/common.rb
168
+ - lib/train/platforms/detect.rb
169
+ - lib/train/platforms/detect/helpers/os_common.rb
170
+ - lib/train/platforms/detect/helpers/os_linux.rb
171
+ - lib/train/platforms/detect/helpers/os_windows.rb
172
+ - lib/train/platforms/detect/scanner.rb
173
+ - lib/train/platforms/detect/specifications/os.rb
174
+ - lib/train/platforms/family.rb
175
+ - lib/train/platforms/platform.rb
176
176
  - lib/train/plugins.rb
177
177
  - lib/train/plugins/base_connection.rb
178
178
  - lib/train/plugins/transport.rb
179
179
  - lib/train/transports/docker.rb
180
180
  - lib/train/transports/local.rb
181
- - lib/train/transports/local_os.rb
182
181
  - lib/train/transports/mock.rb
183
182
  - lib/train/transports/ssh.rb
184
183
  - lib/train/transports/ssh_connection.rb
@@ -217,9 +216,6 @@ files:
217
216
  - test/integration/tests/path_symlink_test.rb
218
217
  - test/integration/tests/run_command_test.rb
219
218
  - test/unit/extras/command_wrapper_test.rb
220
- - test/unit/extras/os_common_test.rb
221
- - test/unit/extras/os_detect_linux_test.rb
222
- - test/unit/extras/os_detect_windows_test.rb
223
219
  - test/unit/extras/stat_test.rb
224
220
  - test/unit/file/local/unix_test.rb
225
221
  - test/unit/file/local/windows_test.rb
@@ -229,6 +225,14 @@ files:
229
225
  - test/unit/file/remote_test.rb
230
226
  - test/unit/file_test.rb
231
227
  - test/unit/helper.rb
228
+ - test/unit/platforms/detect/os_common_test.rb
229
+ - test/unit/platforms/detect/os_linux_test.rb
230
+ - test/unit/platforms/detect/os_windows_test.rb
231
+ - test/unit/platforms/detect/scanner_test.rb
232
+ - test/unit/platforms/family_test.rb
233
+ - test/unit/platforms/os_detect_test.rb
234
+ - test/unit/platforms/platform_test.rb
235
+ - test/unit/platforms/platforms_test.rb
232
236
  - test/unit/plugins/connection_test.rb
233
237
  - test/unit/plugins/transport_test.rb
234
238
  - test/unit/plugins_test.rb
@@ -297,9 +301,6 @@ test_files:
297
301
  - test/integration/tests/path_symlink_test.rb
298
302
  - test/integration/tests/run_command_test.rb
299
303
  - test/unit/extras/command_wrapper_test.rb
300
- - test/unit/extras/os_common_test.rb
301
- - test/unit/extras/os_detect_linux_test.rb
302
- - test/unit/extras/os_detect_windows_test.rb
303
304
  - test/unit/extras/stat_test.rb
304
305
  - test/unit/file/local/unix_test.rb
305
306
  - test/unit/file/local/windows_test.rb
@@ -309,6 +310,14 @@ test_files:
309
310
  - test/unit/file/remote_test.rb
310
311
  - test/unit/file_test.rb
311
312
  - test/unit/helper.rb
313
+ - test/unit/platforms/detect/os_common_test.rb
314
+ - test/unit/platforms/detect/os_linux_test.rb
315
+ - test/unit/platforms/detect/os_windows_test.rb
316
+ - test/unit/platforms/detect/scanner_test.rb
317
+ - test/unit/platforms/family_test.rb
318
+ - test/unit/platforms/os_detect_test.rb
319
+ - test/unit/platforms/platform_test.rb
320
+ - test/unit/platforms/platforms_test.rb
312
321
  - test/unit/plugins/connection_test.rb
313
322
  - test/unit/plugins/transport_test.rb
314
323
  - test/unit/plugins_test.rb
@@ -1,60 +0,0 @@
1
- # encoding: utf-8
2
- # author: Dominik Richter
3
- # author: Christoph Hartmann
4
- #
5
- # This is heavily based on:
6
- #
7
- # OHAI https://github.com/chef/ohai
8
- # by Adam Jacob, Chef Software Inc
9
- #
10
-
11
- module Train::Extras
12
- module LinuxLSB
13
- def lsb_config(content)
14
- {
15
- id: content[/^DISTRIB_ID=["']?(.+?)["']?$/, 1],
16
- release: content[/^DISTRIB_RELEASE=["']?(.+?)["']?$/, 1],
17
- codename: content[/^DISTRIB_CODENAME=["']?(.+?)["']?$/, 1],
18
- }
19
- end
20
-
21
- def lsb_release
22
- raw = @backend.run_command('lsb_release -a').stdout
23
- {
24
- id: raw[/^Distributor ID:\s+(.+)$/, 1],
25
- release: raw[/^Release:\s+(.+)$/, 1],
26
- codename: raw[/^Codename:\s+(.+)$/, 1],
27
- }
28
- end
29
-
30
- def lsb
31
- return @lsb if defined?(@lsb)
32
- @lsb = {}
33
- if !(raw = get_config('/etc/lsb-release')).nil?
34
- @lsb = lsb_config(raw)
35
- elsif unix_file?('/usr/bin/lsb_release')
36
- @lsb = lsb_release
37
- end
38
- @lsb
39
- end
40
-
41
- def detect_linux_via_lsb
42
- return false if lsb[:id].nil?
43
- id = lsb[:id].downcase
44
- case id
45
- when /redhat/
46
- @platform[:family] = 'redhat'
47
- when /amazon/
48
- @platform[:family] = 'amazon'
49
- when /scientificsl/
50
- @platform[:family] = 'scientific'
51
- when /xenserver/
52
- @platform[:family] = 'xenserver'
53
- else
54
- @platform[:family] = id
55
- end
56
- @platform[:release] = lsb[:release]
57
- true
58
- end
59
- end
60
- end