cf_script 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +5 -6
  3. data/CHANGELOG.md +11 -0
  4. data/README.md +9 -7
  5. data/lib/cf_script/command/base.rb +8 -4
  6. data/lib/cf_script/command/cf/apps.rb +4 -0
  7. data/lib/cf_script/command/cf/apps/restart_app_instance.rb +20 -0
  8. data/lib/cf_script/command/cf/apps/scale.rb +39 -0
  9. data/lib/cf_script/command/cf/routes/check_route.rb +1 -1
  10. data/lib/cf_script/command/cf/spaces/space.rb +1 -1
  11. data/lib/cf_script/command/registry.rb +1 -1
  12. data/lib/cf_script/command/runner.rb +6 -4
  13. data/lib/cf_script/executor/simple.rb +1 -1
  14. data/lib/cf_script/object/app_info.rb +8 -8
  15. data/lib/cf_script/output/buffer.rb +1 -1
  16. data/lib/cf_script/output/parser/attributes.rb +1 -1
  17. data/lib/cf_script/output/tests.rb +6 -6
  18. data/lib/cf_script/scope/app/state.rb +12 -0
  19. data/lib/cf_script/scope/script.rb +1 -1
  20. data/lib/cf_script/version.rb +1 -1
  21. data/test/fixtures/commands/apps/restart_app_instance/good.yml +8 -0
  22. data/test/fixtures/commands/apps/restart_app_instance/not_found.yml +8 -0
  23. data/test/fixtures/commands/apps/scale/good.yml +8 -0
  24. data/test/lib/cf_script/command/base_test.rb +127 -2
  25. data/test/lib/cf_script/command/cf/apps/app_test.rb +2 -2
  26. data/test/lib/cf_script/command/cf/apps/delete_test.rb +1 -1
  27. data/test/lib/cf_script/command/cf/apps/push_test.rb +1 -1
  28. data/test/lib/cf_script/command/cf/apps/rename_test.rb +2 -2
  29. data/test/lib/cf_script/command/cf/apps/restage_test.rb +2 -2
  30. data/test/lib/cf_script/command/cf/apps/restart_app_instance_test.rb +41 -0
  31. data/test/lib/cf_script/command/cf/apps/restart_test.rb +2 -2
  32. data/test/lib/cf_script/command/cf/apps/scale_test.rb +51 -0
  33. data/test/lib/cf_script/command/cf/apps/set_env_test.rb +1 -1
  34. data/test/lib/cf_script/command/cf/apps/start_test.rb +2 -2
  35. data/test/lib/cf_script/command/cf/apps/stop_test.rb +2 -2
  36. data/test/lib/cf_script/command/cf/apps/unset_env_test.rb +1 -1
  37. data/test/lib/cf_script/command/cf/general/api_test.rb +3 -3
  38. data/test/lib/cf_script/command/cf/general/target_test.rb +2 -2
  39. data/test/lib/cf_script/command/cf/routes/map_route_test.rb +6 -6
  40. data/test/lib/cf_script/command/cf/routes/routes_test.rb +1 -1
  41. data/test/lib/cf_script/command/cf/spaces/space_test.rb +1 -1
  42. data/test/lib/cf_script/command/cf/spaces/spaces_test.rb +1 -1
  43. data/test/lib/cf_script/command/line_test.rb +16 -8
  44. data/test/lib/cf_script/command/registry_test.rb +53 -0
  45. data/test/lib/cf_script/object/api_endpoint_test.rb +2 -2
  46. data/test/lib/cf_script/object/app_info_test.rb +13 -13
  47. data/test/lib/cf_script/object/instance_status_test.rb +7 -7
  48. data/test/lib/cf_script/object/space_test.rb +1 -1
  49. data/test/lib/cf_script/object/target_test.rb +1 -1
  50. data/test/lib/cf_script/output/buffer_test.rb +1 -1
  51. data/test/lib/cf_script/output/parser/section_test.rb +1 -1
  52. data/test/lib/cf_script/output/parser/table_test.rb +1 -1
  53. data/test/lib/cf_script/scope/app/state_test.rb +34 -0
  54. data/test/lib/cf_script/scope/app_test.rb +5 -1
  55. data/test/lib/cf_script/ui_test.rb +3 -3
  56. data/test/support/helpers/object_helpers.rb +4 -4
  57. data/test/support/shared_examples/command_object.rb +17 -3
  58. metadata +14 -3
@@ -37,13 +37,13 @@ describe 'AppCommand' do
37
37
  fake_cf app: :empty do
38
38
  result = command.run(:worker)
39
39
 
40
- assert_equal nil, result
40
+ assert_nil result
41
41
  end
42
42
  end
43
43
 
44
44
  it "returns nil when the app is not found" do
45
45
  fake_cf app: :not_found do
46
- assert_equal nil, command.run(:api)
46
+ assert_nil command.run(:api)
47
47
  end
48
48
  end
49
49
 
@@ -21,7 +21,7 @@ describe 'DeleteCommand' do
21
21
  fake_cf delete: :not_exist do |stdout, stderr|
22
22
  command.run(:name)
23
23
 
24
- assert_match /\{delete\} failed to delete app/, stderr.lines.first
24
+ assert_match(/\{delete\} failed to delete app/, stderr.lines.first)
25
25
  end
26
26
  end
27
27
 
@@ -23,7 +23,7 @@ describe 'PushCommand' do
23
23
  fake_cf push: :empty do
24
24
  result = command.run(:api)
25
25
 
26
- assert_equal nil, result
26
+ assert_nil result
27
27
  end
28
28
  end
29
29
 
@@ -21,7 +21,7 @@ describe 'RenameCommand' do
21
21
  fake_cf rename: :not_found do |stdout, stderr|
22
22
  command.run(:old, :new)
23
23
 
24
- assert_match /\{rename\} failed to rename/, stderr.lines.first
24
+ assert_match(/\{rename\} failed to rename/, stderr.lines.first)
25
25
  end
26
26
  end
27
27
 
@@ -29,7 +29,7 @@ describe 'RenameCommand' do
29
29
  fake_cf rename: :not_found do |stdout, stderr|
30
30
  assert_equal false, command.run(:old, :new)
31
31
 
32
- assert_match /\{rename\} failed to rename/, stderr.lines.first
32
+ assert_match(/\{rename\} failed to rename/, stderr.lines.first)
33
33
  end
34
34
  end
35
35
  end
@@ -43,13 +43,13 @@ describe 'RestageCommand' do
43
43
  fake_cf restage: :empty do
44
44
  result = command.run(:api)
45
45
 
46
- assert_equal nil, result
46
+ assert_nil result
47
47
  end
48
48
  end
49
49
 
50
50
  it "returns nil on failure" do
51
51
  fake_cf restage: :not_found do
52
- assert_equal nil, command.run(:some_app)
52
+ assert_nil command.run(:some_app)
53
53
  end
54
54
  end
55
55
  end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ describe 'RestartAppInstanceCommand' do
4
+ include MockExecution
5
+
6
+ let(:command) { CfScript::Command::Apps::RestartAppInstanceCommand.instance }
7
+
8
+ it_behaves_like 'a command object that', {
9
+ has_type: :apps,
10
+ has_name: :restart_app_instance,
11
+ fails_with: false
12
+ }
13
+
14
+ it "returns true on success" do
15
+ fake_cf do
16
+ assert_equal true, command.run(:worker, 0)
17
+ end
18
+ end
19
+
20
+ it "prints an error on failure" do
21
+ fake_cf restart_app_instance: :not_found do |stdout, stderr|
22
+ command.run(:bogus, 0)
23
+
24
+ assert_match(
25
+ /\{restart_app_instance\} failed to restart app instance 0/,
26
+ stderr.lines.first
27
+ )
28
+ end
29
+ end
30
+
31
+ it "returns false when the app is not found" do
32
+ fake_cf restart_app_instance: :not_found do |stdout, stderr|
33
+ assert_equal false, command.run(:bogus, 0)
34
+
35
+ assert_match(
36
+ /\{restart_app_instance\} failed to restart app instance 0/,
37
+ stderr.lines.first
38
+ )
39
+ end
40
+ end
41
+ end
@@ -43,13 +43,13 @@ describe 'RestartCommand' do
43
43
  fake_cf restart: :empty do
44
44
  result = command.run(:worker)
45
45
 
46
- assert_equal nil, result
46
+ assert_nil result
47
47
  end
48
48
  end
49
49
 
50
50
  it "returns nil on failure" do
51
51
  fake_cf restart: :not_found do
52
- assert_equal nil, command.run(:some_app)
52
+ assert_nil command.run(:some_app)
53
53
  end
54
54
  end
55
55
  end
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+
3
+ describe 'ScaleCommand' do
4
+ include MockExecution
5
+
6
+ let(:command) { CfScript::Command::Apps::ScaleCommand.instance }
7
+
8
+ it_behaves_like 'a command object that', {
9
+ has_type: :apps,
10
+ has_name: :scale,
11
+ fails_with: nil
12
+ }
13
+
14
+ it "returns true on success" do
15
+ fake_cf do
16
+ result = command.run(:worker)
17
+
18
+ assert_instance_of TrueClass, result
19
+ end
20
+ end
21
+
22
+ it "returns false if no attributes were found" do
23
+ fake_cf scale: :empty do
24
+ result = command.run(:api)
25
+
26
+ assert_equal false, result
27
+ end
28
+ end
29
+
30
+ it "translates long option names to cf's short ones" do
31
+ fake_cf do
32
+ assert_equal({ f: 'a' }, command.translate_options({ force_restart: 'a' }))
33
+ assert_equal({ i: 'a' }, command.translate_options({ instances: 'a' }))
34
+ assert_equal({ m: 'a' }, command.translate_options({ memory: 'a' }))
35
+ assert_equal({ k: 'a' }, command.translate_options({ disk: 'a' }))
36
+
37
+ assert_equal(
38
+ { i: 'a', m: 'b', k: 'c' },
39
+ command.translate_options(
40
+ { instances: 'a', memory: 'b', disk: 'c' }
41
+ )
42
+ )
43
+ end
44
+ end
45
+
46
+ it "builds options and flag for run_cf" do
47
+ assert_command_args command,
48
+ [:worker, i: 2, memory: '123', disk: '345'],
49
+ [:worker, { i: 2, m: '123', k: '345' }]
50
+ end
51
+ end
@@ -32,7 +32,7 @@ describe 'SetEnvCommand' do
32
32
  result = command.run(:api, 'API_PASSWORD', 'password')
33
33
 
34
34
  assert_equal false, result
35
- assert_match /\{set_env\} FAILED/, stderr.lines.first
35
+ assert_match(/\{set_env\} FAILED/, stderr.lines.first)
36
36
  end
37
37
  end
38
38
  end
@@ -51,13 +51,13 @@ describe 'StartCommand' do
51
51
  fake_cf start: :empty do
52
52
  result = command.run(:worker)
53
53
 
54
- assert_equal nil, result
54
+ assert_nil result
55
55
  end
56
56
  end
57
57
 
58
58
  it "returns nil on failure" do
59
59
  fake_cf start: :not_found do
60
- assert_equal nil, command.run(:some_app)
60
+ assert_nil command.run(:some_app)
61
61
  end
62
62
  end
63
63
  end
@@ -21,7 +21,7 @@ describe 'StopCommand' do
21
21
  fake_cf stop: :not_found do |stdout, stderr|
22
22
  command.run(:bogus)
23
23
 
24
- assert_match /\{stop\} failed to stop/, stderr.lines.first
24
+ assert_match(/\{stop\} failed to stop/, stderr.lines.first)
25
25
  end
26
26
  end
27
27
 
@@ -29,7 +29,7 @@ describe 'StopCommand' do
29
29
  fake_cf stop: :not_found do |stdout, stderr|
30
30
  assert_equal false, command.run(:bogus)
31
31
 
32
- assert_match /\{stop\} failed to stop/, stderr.lines.first
32
+ assert_match(/\{stop\} failed to stop/, stderr.lines.first)
33
33
  end
34
34
  end
35
35
  end
@@ -32,7 +32,7 @@ describe 'UnsetEnvCommand' do
32
32
  result = command.run(:bogus, 'VAR_NAME')
33
33
 
34
34
  assert_equal false, result
35
- assert_match /\{unset_env\} FAILED/, stderr.lines.first
35
+ assert_match(/\{unset_env\} FAILED/, stderr.lines.first)
36
36
  end
37
37
  end
38
38
  end
@@ -24,7 +24,7 @@ describe 'ApiCommand' do
24
24
 
25
25
  it "returns nil when the api endpoint is not set" do
26
26
  fake_cf api: :not_set do |stdout, stderr|
27
- assert_equal nil, command.run
27
+ assert_nil command.run
28
28
  end
29
29
  end
30
30
  end
@@ -42,13 +42,13 @@ describe 'ApiCommand' do
42
42
 
43
43
  it "returns nil when the argument is invalid" do
44
44
  fake_cf api: :bad_argument do |stdout, stderr|
45
- assert_equal nil, command.run('')
45
+ assert_nil command.run('')
46
46
  end
47
47
  end
48
48
 
49
49
  it "returns nil when the endpoint is invalid" do
50
50
  fake_cf api: :bad_endpoint do |stdout, stderr|
51
- assert_equal nil, command.run('https://api.example.com')
51
+ assert_nil command.run('https://api.example.com')
52
52
  end
53
53
  end
54
54
  end
@@ -13,7 +13,7 @@ describe 'TargetCommand' do
13
13
 
14
14
  it "returns nil and prints an error on parsing failure" do
15
15
  fake_cf target: :empty do |stdout, stderr|
16
- assert_equal nil, command.run
16
+ assert_nil command.run
17
17
 
18
18
  assert_includes stderr.lines, "{target} object is nil\n"
19
19
  end
@@ -75,7 +75,7 @@ describe 'TargetCommand' do
75
75
 
76
76
  it "returns nil and prints an error when the space is not found" do
77
77
  fake_cf do |stdout, stderr|
78
- assert_equal nil, command.run(:not_found)
78
+ assert_nil command.run(:not_found)
79
79
 
80
80
  assert_includes stderr.lines, "{target} Space not_found not found\n"
81
81
  end
@@ -34,9 +34,9 @@ describe 'MapRouteCommand' do
34
34
  fake_cf map_route: :invalid do |stdout, stderr|
35
35
  assert_equal false, command.run(:api, 'example.com')
36
36
 
37
- assert_match /\{map_route\}/, stderr.lines.last
38
- assert_match /Server error/, stderr.lines.last
39
- assert_match /route is invalid/, stderr.lines.last
37
+ assert_match(/\{map_route\}/, stderr.lines.last)
38
+ assert_match(/Server error/, stderr.lines.last)
39
+ assert_match(/route is invalid/, stderr.lines.last)
40
40
  end
41
41
  end
42
42
 
@@ -60,9 +60,9 @@ describe 'MapRouteCommand' do
60
60
  fake_cf map_route: :host_taken do |stdout, stderr|
61
61
  assert_equal false, command.run(:api, 'example.com')
62
62
 
63
- assert_match /\{map_route\}/, stderr.lines.last
64
- assert_match /Server error/, stderr.lines.last
65
- assert_match /The host is taken: api/, stderr.lines.last
63
+ assert_match(/\{map_route\}/, stderr.lines.last)
64
+ assert_match(/Server error/, stderr.lines.last)
65
+ assert_match(/The host is taken: api/, stderr.lines.last)
66
66
  end
67
67
  end
68
68
  end
@@ -26,7 +26,7 @@ describe 'RoutesCommand' do
26
26
  fake_cf routes: :empty do
27
27
  routes = command.run
28
28
 
29
- assert_equal nil, routes
29
+ assert_nil routes
30
30
  end
31
31
  end
32
32
  end
@@ -34,7 +34,7 @@ describe 'SpaceCommand' do
34
34
  fake_cf space: :empty do
35
35
  space = command.run(:space)
36
36
 
37
- assert_equal nil, space
37
+ assert_nil space
38
38
  end
39
39
  end
40
40
 
@@ -24,7 +24,7 @@ describe 'SpacesCommand' do
24
24
  fake_cf spaces: :empty do
25
25
  spaces = command.run
26
26
 
27
- assert_equal nil, spaces
27
+ assert_nil spaces
28
28
  end
29
29
  end
30
30
 
@@ -4,7 +4,9 @@ describe CfScript::Command::Line do
4
4
  subject { CfScript::Command::Line }
5
5
 
6
6
  it "initializes bin, name, args, options, and flags" do
7
- line = subject.new({ foo: :bar}, 'bin', :test, :dummy, [:arg1, :arg2, { i: 1, o: 2, flags: [:s] }])
7
+ line = subject.new({ foo: :bar }, 'bin', :test, :dummy,
8
+ [:arg1, :arg2, { i: 1, o: 2, flags: [:s] }]
9
+ )
8
10
 
9
11
  assert_equal 'bin', line.bin
10
12
  assert_equal :dummy, line.name
@@ -15,19 +17,25 @@ describe CfScript::Command::Line do
15
17
 
16
18
  describe "line" do
17
19
  it "returns a formatted command line" do
18
- line = subject.new({}, 'cf', :test, :dummy, [:arg1, :arg2, { i: 1, o: 2, flags: [:s] }])
20
+ line = subject.new({}, 'cf', :test, :dummy,
21
+ [:arg1, :arg2, { i: 1, o: 2, flags: [:s] }]
22
+ )
19
23
 
20
24
  assert_equal 'cf dummy arg1 arg2 -i 1 -o 2 -s', line.line
21
25
  end
22
26
 
23
27
  it "formats long flag names" do
24
- line = subject.new({}, 'cf', :test, :dummy, [:arg1, { i: 1, flags: [:some_thing] }])
28
+ line = subject.new({}, 'cf', :test, :dummy,
29
+ [:arg1, { i: 1, flags: [:some_thing] }]
30
+ )
25
31
 
26
32
  assert_equal 'cf dummy arg1 -i 1 --some-thing', line.line
27
33
  end
28
34
 
29
35
  it "pass preformatted flags as is" do
30
- line = subject.new({}, 'cf', :test, :dummy, [:arg1, { i: 1, flags: ['--one', '--foo-bar'] }])
36
+ line = subject.new({}, 'cf', :test, :dummy,
37
+ [:arg1, { i: 1, flags: ['--one', '--foo-bar'] }]
38
+ )
31
39
 
32
40
  assert_equal 'cf dummy arg1 -i 1 --one --foo-bar', line.line
33
41
  end
@@ -50,10 +58,10 @@ describe CfScript::Command::Line do
50
58
  line = subject.new({}, 'cf', :test, :login, [test])
51
59
  text = line.hide_sensitive
52
60
 
53
- assert_match /cf login /, text
54
- assert_match /-p \[PASSWORD HIDDEN\]/, text
55
- assert_match /-u name/, text
56
- assert_match /-a work/, text
61
+ assert_match(/cf login /, text)
62
+ assert_match(/-p \[PASSWORD HIDDEN\]/, text)
63
+ assert_match(/-u name/, text)
64
+ assert_match(/-a work/, text)
57
65
  end
58
66
  end
59
67
  end
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ describe CfScript::Command::Registry do
4
+ subject { CfScript::Command::Registry.new }
5
+
6
+ class DummyCommand < CfScript::Command::Base
7
+ def initialize
8
+ super(:test, :dummy)
9
+ end
10
+ end
11
+
12
+ it "starts with zero commands" do
13
+ assert_equal({}, subject.commands)
14
+ end
15
+
16
+ describe "known?" do
17
+ it "returns false when given command is not registered" do
18
+ assert_equal false, subject.known?(:dummy)
19
+ end
20
+
21
+ it "returns true when given command is registered" do
22
+ subject.add! DummyCommand
23
+
24
+ assert_equal true, subject.known?(:dummy)
25
+ end
26
+ end
27
+
28
+ describe "check!" do
29
+ it "raises an exception for unknown commands" do
30
+ assert_raises(CfScript::Command::Registry::UnimplementedCommand) {
31
+ subject.check! :dummy
32
+ }
33
+ end
34
+ end
35
+
36
+ describe "add!" do
37
+ it "adds commands" do
38
+ assert_equal false, subject.known?(:dummy)
39
+
40
+ subject.add! DummyCommand
41
+
42
+ assert_equal true, subject.known?(:dummy)
43
+ end
44
+
45
+ it "raises an exception for duplicate commands" do
46
+ subject.add! DummyCommand
47
+
48
+ assert_raises(CfScript::Command::Registry::DuplicateCommand) {
49
+ subject.add! DummyCommand
50
+ }
51
+ end
52
+ end
53
+ end
@@ -6,8 +6,8 @@ describe CfScript::ApiEndpoint do
6
6
  it "initializes all attributes to sensible defaults" do
7
7
  api_endpoint = subject.new
8
8
 
9
- assert_equal nil, api_endpoint.url
10
- assert_equal nil, api_endpoint.version
9
+ assert_nil api_endpoint.url
10
+ assert_nil api_endpoint.version
11
11
  end
12
12
 
13
13
  it "sets all given attributes" do