sshkit-custom-dsl 0.0.6 → 0.0.7

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.
@@ -0,0 +1,147 @@
1
+ require 'unit_spec_helper'
2
+
3
+ module SSHKit
4
+ module Custom
5
+ module DSL
6
+ describe ConfigStatements do
7
+ subject do
8
+ Class.new do
9
+ include ConfigStatements
10
+ include Helper
11
+
12
+ def _guard_sudo_user!(*)
13
+ end
14
+
15
+ def _guard_dir!(*)
16
+ end
17
+
18
+ def _guard_sudo_group!(*)
19
+ end
20
+ end.new
21
+ end
22
+
23
+ let(:mock_bck) do
24
+ SSHKit.config.backend.new(SSHKit::Host.new('localhost'))
25
+ end
26
+
27
+ let(:hosts) { ['localhost', '127.0.0.1'] }
28
+
29
+ before :each do
30
+ SSHKit::Custom::Runner::Abstract.active_backend = mock_bck
31
+ end
32
+
33
+ describe '.on' do
34
+ it 'sets the backends' do
35
+ subject.on hosts
36
+ expect(Config::Store.backends.count).to eq(2)
37
+ end
38
+
39
+ it 'creates a runner' do
40
+ subject.on hosts
41
+ expect(Config::Store.runner).to be_kind_of SSHKit::Custom::Runner::Parallel
42
+ end
43
+
44
+ it 'applies a block to the runner' do
45
+ block = ->(_host) {}
46
+ runner_double = double(:runner)
47
+
48
+ expect(subject).to receive(:_runner).and_return(runner_double)
49
+
50
+ expect(runner_double).to receive(:apply_block_to_bcks)
51
+
52
+ subject.on hosts, &block
53
+ end
54
+ end
55
+
56
+ describe '.within' do
57
+ it 'changes the directory' do
58
+ dir = '1234'
59
+ expect(subject._config_store).to receive(:add_pwd).with(dir)
60
+
61
+ subject.within(dir)
62
+ end
63
+
64
+ it 'ensures the directory is set back' do
65
+ dir = '1234'
66
+ expect(subject._config_store).to receive(:pop_pwd)
67
+
68
+ expect do
69
+ subject.within(dir) do
70
+ fail
71
+ end
72
+ end.to raise_error
73
+ end
74
+
75
+ it 'guards the block with a dir existing check' do
76
+ dir = '1234'
77
+ expect(subject).to receive(:_guard_dir!).with(dir)
78
+
79
+ subject.within(dir)
80
+ end
81
+ end
82
+
83
+ describe '.with' do
84
+ it 'changes the environment' do
85
+ env = { a: 2 }
86
+ expect(subject._config_store).to receive(:add_env).with(env)
87
+
88
+ subject.with(env)
89
+ end
90
+
91
+ it 'ensures the environment is set back' do
92
+ env = { a: 2 }
93
+ expect(subject._config_store).to receive(:pop_env)
94
+
95
+ expect do
96
+ subject.with(env) do
97
+ fail
98
+ end
99
+ end.to raise_error
100
+ end
101
+ end
102
+
103
+ describe '.as' do
104
+ it 'changes the user and group' do
105
+ user = 'u'
106
+ group = 'g'
107
+
108
+ expect(subject._config_store).to receive(:add_user_group).with(user, group)
109
+
110
+ subject.as(user: user, group: group)
111
+ end
112
+
113
+ it 'ensures the user and group is set back' do
114
+ user = 'u'
115
+ group = 'g'
116
+ expect(subject._config_store).to receive(:pop_user_group)
117
+
118
+ expect do
119
+ subject.as(user: user, group: group) do
120
+ fail
121
+ end
122
+ end.to raise_error
123
+ end
124
+
125
+ it 'guards the block execution with a user and group existing check' do
126
+ user = 'u'
127
+ group = 'g'
128
+
129
+ expect(subject).to receive(:_guard_sudo_user!).with(user)
130
+ expect(subject).to receive(:_guard_sudo_group!).with(user, group)
131
+
132
+ subject.as(user: user, group: group)
133
+ end
134
+
135
+ it 'uses the username when given' do
136
+ user = 'u'
137
+
138
+ expect(subject._config_store).to receive(:add_user_group).with(user, nil)
139
+
140
+ subject.as(user)
141
+ end
142
+ end
143
+
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,74 @@
1
+ require 'unit_spec_helper'
2
+
3
+ module SSHKit
4
+ module Custom
5
+ module DSL
6
+ describe ExecStatements do
7
+ subject do
8
+ Class.new do
9
+ include ExecStatements
10
+ include ConfigStatements
11
+ include Helper
12
+
13
+ def _config_store
14
+ self
15
+ end
16
+
17
+ attr_accessor :runner
18
+
19
+ end.new
20
+ end
21
+
22
+ let(:mock_bck) do
23
+ SSHKit.config.backend.new(SSHKit::Host.new('localhost'))
24
+ end
25
+
26
+ let(:hosts) { ['localhost', '127.0.0.1'] }
27
+
28
+ before :each do
29
+ SSHKit::Custom::Runner::Abstract.active_backend = mock_bck
30
+ end
31
+
32
+ describe '#EXEC_STATEMENTS' do
33
+ it 'creates a method for each exec statement' do
34
+ EXEC_STATEMENTS.each do |meth|
35
+ expect(subject).to respond_to meth
36
+ end
37
+ end
38
+
39
+ it 'delegates all calls to the runner' do
40
+ subject.runner = double(:runner)
41
+ args = [1, 2, 3, 4]
42
+
43
+ EXEC_STATEMENTS.each do |meth|
44
+ expect(subject.runner).to receive(:send_cmd).with(meth, *args)
45
+ subject.send(meth, *args)
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '._guard_sudo_user!' do
51
+ it 'checks if a user can sudo with a specific user' do
52
+ expect(subject).to receive(:execute).with(/if.*sudo.*-u.*deploy/m, verbosity: 0)
53
+ subject._guard_sudo_user!('deploy')
54
+ end
55
+ end
56
+
57
+ describe '._guard_sudo_group!' do
58
+ it 'checks if a user can sudo with a specific group' do
59
+ expect(subject).to receive(:execute).with(/if.*sudo.*-u.*deploy.*-g.*dgroup/m, verbosity: 0)
60
+ subject._guard_sudo_group!('deploy', 'dgroup')
61
+ end
62
+ end
63
+
64
+ describe '._guard_dir!' do
65
+ it 'checks if a user can access a specific dir' do
66
+ expect(subject).to receive(:execute).with(/if.*-d.*\/tmp/m, verbosity: 0)
67
+ subject._guard_dir!('/tmp')
68
+ end
69
+ end
70
+
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,57 @@
1
+ require 'unit_spec_helper'
2
+
3
+ module SSHKit
4
+ module Custom
5
+ module DSL
6
+ describe Helper do
7
+ subject do
8
+ Class.new do
9
+ include Helper
10
+ end.new
11
+ end
12
+
13
+ let(:mock_bck) do
14
+ SSHKit.config.backend.new(SSHKit::Host.new('localhost'))
15
+ end
16
+
17
+ before :each do
18
+ SSHKit::Custom::Runner::Abstract.active_backend = mock_bck
19
+ end
20
+
21
+ describe '#LOGGING_METHODS' do
22
+ it 'delegates all logging methods to active backend' do
23
+ LOGGING_METHODS.each do |meth|
24
+ expect(mock_bck).to receive(meth)
25
+ subject.send(meth, 'hello world')
26
+ end
27
+ end
28
+ end
29
+
30
+ describe '.active_backend' do
31
+ it 'returns the actual active backend' do
32
+ expect(subject.active_backend).to eq(mock_bck)
33
+ end
34
+ end
35
+
36
+ describe '.host' do
37
+ it 'return the actual host' do
38
+ expect(subject.host).to eq(mock_bck.host)
39
+ end
40
+ end
41
+
42
+ describe '.Host' do
43
+ it 'converts to a host object' do
44
+ expect(subject.Host('localhost')).to be_kind_of SSHKit::Host
45
+ end
46
+
47
+ it 'does nothing with a host object' do
48
+ lch = SSHKit::Host.new('localhost')
49
+ expect(subject.Host(lch)).to eq(lch)
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,123 @@
1
+ require 'unit_spec_helper'
2
+
3
+ module SSHKit
4
+ module Custom
5
+ module Runner
6
+ describe Abstract do
7
+
8
+ subject do
9
+ Abstract.new(wait: 1)
10
+ end
11
+
12
+ describe '#create_runner' do
13
+ it 'creates a group runner' do
14
+ expect(Abstract.create_runner(in: :groups)).to be_kind_of Group
15
+ end
16
+
17
+ it 'creates a parallel runner' do
18
+ expect(Abstract.create_runner(in: :parallel)).to be_kind_of Parallel
19
+ end
20
+
21
+ it 'creates a parallel runner as deafult' do
22
+ expect(Abstract.create_runner({})).to be_kind_of Parallel
23
+ end
24
+
25
+ it 'creates a sequential runner' do
26
+ expect(Abstract.create_runner(in: :sequence)).to be_kind_of Sequential
27
+ end
28
+
29
+ it 'raises an error for unknown runners' do
30
+ expect { Abstract.create_runner(in: :abc) }.to raise_error
31
+ end
32
+
33
+ describe '.active_backend' do
34
+ it 'sets the active backend' do
35
+ subject.active_backend = :some_thing
36
+ expect(subject.active_backend).to eq :some_thing
37
+ end
38
+
39
+ it 'delegates to the class method' do
40
+ subject.active_backend = :some_thing
41
+ expect(subject.class.active_backend).to eq :some_thing
42
+ end
43
+ end
44
+
45
+ describe '.snd_cmd' do
46
+ let(:mock_bck) do
47
+ SSHKit.config.backend.new(SSHKit::Host.new('localhost'))
48
+ end
49
+
50
+ before :each do
51
+ SSHKit::Custom::Runner::Abstract.active_backend = mock_bck
52
+ end
53
+
54
+ it 'sends the cmd to the active backend' do
55
+ args = [1, 2, 3]
56
+ expect(mock_bck).to receive(:execute).with(*args)
57
+ subject.send_cmd(:execute, *args)
58
+ end
59
+
60
+ it 'executes a block to get the args' do
61
+ args = [1, 2, 3]
62
+ expect(mock_bck).to receive(:execute).with(*args)
63
+
64
+ subject.send_cmd(:execute) do
65
+ args
66
+ end
67
+
68
+ end
69
+
70
+ it 'reraises an exception' do
71
+ expect do
72
+ subject.send_cmd(:execute) do
73
+ fail
74
+ end
75
+ end.to raise_error
76
+ end
77
+
78
+ end
79
+
80
+ describe '.apply_to_bck' do
81
+ let(:mock_bck) do
82
+ SSHKit.config.backend.new(SSHKit::Host.new('localhost'))
83
+ end
84
+
85
+ it 'executes the block and yields the host' do
86
+ block = ->(_host) {}
87
+ expect(block).to receive(:call)
88
+ subject.apply_to_bck(mock_bck, &block)
89
+ end
90
+
91
+ it 'reraises an exception' do
92
+ block = ->(_host) { fail }
93
+
94
+ expect { subject.apply_to_bck(mock_bck, &block) }.to raise_error
95
+ end
96
+
97
+ end
98
+
99
+ describe '.do_wait' do
100
+ it 'sleeps [wait] seconds' do
101
+
102
+ expect do
103
+ subject.send(:do_wait)
104
+ end.to change { Time.now.utc }.by_at_least(1)
105
+ end
106
+
107
+ end
108
+
109
+ describe '.apply_block_to_bcks' do
110
+ it 'should be implemented by sub classes' do
111
+ expect do
112
+ subject.apply_block_to_bcks do
113
+
114
+ end
115
+ end.to raise_error
116
+ end
117
+ end
118
+
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,48 @@
1
+ require 'unit_spec_helper'
2
+
3
+ module SSHKit
4
+ module Custom
5
+ module Runner
6
+ describe Group do
7
+ subject do
8
+ Group.new(wait: 0, limit: 1)
9
+ end
10
+
11
+ describe '.apply_block_to_bcks' do
12
+
13
+ let(:block) { ->(_) {} }
14
+ let(:bck1) { SSHKit.config.backend.new(SSHKit::Host.new('localhost')) }
15
+ let(:bck2) { SSHKit.config.backend.new(SSHKit::Host.new('127.0.0.1')) }
16
+
17
+ before :each do
18
+ allow(subject).to receive(:use_runner).and_return(->(options) {Sequential.new(options) })
19
+ end
20
+
21
+ it 'calls apply_to_bck for every backend' do
22
+ subject.backends = [bck1, bck2]
23
+
24
+ expect(block).to receive(:call).with(bck1.host)
25
+ expect(block).to receive(:call).with(bck2.host)
26
+
27
+ subject.apply_block_to_bcks(&block)
28
+ end
29
+
30
+ it 'groups the backends into batches' do
31
+ subject.backends = [bck1, bck2]
32
+
33
+ expect(subject).to receive(:exec_parallel).with([bck1])
34
+ expect(subject).to receive(:exec_parallel).with([bck2])
35
+
36
+ subject.apply_block_to_bcks(&block)
37
+ end
38
+ end
39
+
40
+ describe '.use_runner' do
41
+ it 'creates a parallel runner' do
42
+ expect(subject.use_runner.call({})).to be_kind_of Parallel
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,55 @@
1
+ require 'unit_spec_helper'
2
+
3
+ module SSHKit
4
+ module Custom
5
+ module Runner
6
+ class DummyFuture
7
+ def initialize(host, &block)
8
+ @host = host
9
+ @block = block
10
+ end
11
+
12
+ def value
13
+ @block.call @host
14
+ end
15
+ end
16
+
17
+ describe Parallel do
18
+ let(:thread_pool)do
19
+ double(:thread_pool).tap do |t|
20
+ allow(t).to receive(:future) do |host, &block|
21
+ DummyFuture.new(host, &block)
22
+ end
23
+ end
24
+ end
25
+
26
+ subject do
27
+ Parallel.new(wait: 0)
28
+ end
29
+
30
+ describe '.apply_block_to_bcks' do
31
+ it 'calls apply_to_bck for every backend' do
32
+ allow(subject).to receive(:thread_pool).and_return(thread_pool)
33
+
34
+ block = ->(_) {}
35
+
36
+ bck1 = SSHKit.config.backend.new(SSHKit::Host.new('localhost'))
37
+ bck2 = SSHKit.config.backend.new(SSHKit::Host.new('127.0.0.1'))
38
+ subject.backends = [bck1, bck2]
39
+
40
+ expect(block).to receive(:call).with(bck1.host)
41
+ expect(block).to receive(:call).with(bck2.host)
42
+
43
+ subject.apply_block_to_bcks(&block)
44
+ end
45
+ end
46
+
47
+ describe '.thread_pool' do
48
+ it 'creates a rake thread pool' do
49
+ expect(subject.thread_pool).to be_kind_of Rake::ThreadPool
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,27 @@
1
+ require 'unit_spec_helper'
2
+
3
+ module SSHKit
4
+ module Custom
5
+ module Runner
6
+ describe Sequential do
7
+ subject do
8
+ Sequential.new(wait: 0)
9
+ end
10
+
11
+ describe '.apply_block_to_bcks' do
12
+ it 'calls apply_to_bck for every backend' do
13
+ block = ->(_) {}
14
+ bck1 = SSHKit.config.backend.new(SSHKit::Host.new('localhost'))
15
+ bck2 = SSHKit.config.backend.new(SSHKit::Host.new('127.0.0.1'))
16
+ subject.backends = [bck1, bck2]
17
+
18
+ expect(block).to receive(:call).with(bck1.host)
19
+ expect(block).to receive(:call).with(bck2.host)
20
+
21
+ subject.apply_block_to_bcks(&block)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -4,26 +4,26 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'sshkit/custom/dsl/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "sshkit-custom-dsl"
7
+ spec.name = 'sshkit-custom-dsl'
8
8
  spec.version = SSHKit::Custom::DSL::VERSION
9
- spec.authors = ["Dieter Späth"]
10
- spec.email = ["d.spaeth@faber.de"]
11
- spec.summary = %q{Exchanges original sshkit dsl against a custom dsl}
12
- spec.description = %q{Exchanges original sshkit dsl against a custom dsl. This DSL does not change the scope of the blocks.}
13
- spec.homepage = ""
14
- spec.license = "MIT"
9
+ spec.authors = ['Dieter Späth']
10
+ spec.email = ['d.spaeth@faber.de']
11
+ spec.summary = %q(Exchanges original sshkit dsl against a custom dsl)
12
+ spec.description = %q(Exchanges original sshkit dsl against a custom dsl. This DSL does not change the scope of the blocks.)
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'sshkit', '~> 1.5.1'
22
22
  spec.add_dependency 'scoped_storage'
23
23
  spec.add_dependency 'rake'
24
24
 
25
- spec.add_development_dependency "bundler"
26
- spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'rspec'
27
27
 
28
28
  spec.add_development_dependency 'rspec', '2.99.0.rc1'
29
29
  # show nicely how many specs have to be run
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sshkit-custom-dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dieter Späth
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-06 00:00:00.000000000 Z
11
+ date: 2014-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sshkit
@@ -157,16 +157,16 @@ files:
157
157
  - Rakefile
158
158
  - lib/core_ext/sshkit.rb
159
159
  - lib/core_ext/sshkit/backend/abstract.rb
160
- - lib/sshkit/custom/config/runner/abstract.rb
161
- - lib/sshkit/custom/config/runner/group.rb
162
- - lib/sshkit/custom/config/runner/parallel.rb
163
- - lib/sshkit/custom/config/runner/sequential.rb
164
160
  - lib/sshkit/custom/config/store.rb
165
161
  - lib/sshkit/custom/dsl.rb
166
162
  - lib/sshkit/custom/dsl/config_statements.rb
167
163
  - lib/sshkit/custom/dsl/exec_statements.rb
168
164
  - lib/sshkit/custom/dsl/helper.rb
169
165
  - lib/sshkit/custom/dsl/version.rb
166
+ - lib/sshkit/custom/runner/abstract.rb
167
+ - lib/sshkit/custom/runner/group.rb
168
+ - lib/sshkit/custom/runner/parallel.rb
169
+ - lib/sshkit/custom/runner/sequential.rb
170
170
  - spec/acceptance/.gitkeep
171
171
  - spec/acceptance_spec_helper.rb
172
172
  - spec/integration/.gitkeep
@@ -174,6 +174,14 @@ files:
174
174
  - spec/spec_helper.rb
175
175
  - spec/support/.gitkeep
176
176
  - spec/unit/.gitkeep
177
+ - spec/unit/sshkit/custom/config/store_spec.rb
178
+ - spec/unit/sshkit/custom/dsl/config_statements_spec.rb
179
+ - spec/unit/sshkit/custom/dsl/exec_statements_spec.rb
180
+ - spec/unit/sshkit/custom/dsl/helper_spec.rb
181
+ - spec/unit/sshkit/custom/runner/abstract_spec.rb
182
+ - spec/unit/sshkit/custom/runner/group_spec.rb
183
+ - spec/unit/sshkit/custom/runner/parallel_spec.rb
184
+ - spec/unit/sshkit/custom/runner/sequential_spec.rb
177
185
  - spec/unit_spec_helper.rb
178
186
  - sshkit-custom-dsl.gemspec
179
187
  homepage: ''
@@ -208,4 +216,12 @@ test_files:
208
216
  - spec/spec_helper.rb
209
217
  - spec/support/.gitkeep
210
218
  - spec/unit/.gitkeep
219
+ - spec/unit/sshkit/custom/config/store_spec.rb
220
+ - spec/unit/sshkit/custom/dsl/config_statements_spec.rb
221
+ - spec/unit/sshkit/custom/dsl/exec_statements_spec.rb
222
+ - spec/unit/sshkit/custom/dsl/helper_spec.rb
223
+ - spec/unit/sshkit/custom/runner/abstract_spec.rb
224
+ - spec/unit/sshkit/custom/runner/group_spec.rb
225
+ - spec/unit/sshkit/custom/runner/parallel_spec.rb
226
+ - spec/unit/sshkit/custom/runner/sequential_spec.rb
211
227
  - spec/unit_spec_helper.rb