cf_script 0.0.1.beta.1

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.
Files changed (205) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/CHANGELOG.md +0 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE +22 -0
  6. data/README.md +84 -0
  7. data/Rakefile +33 -0
  8. data/cf_script.gemspec +34 -0
  9. data/lib/cf_script/callbacks.rb +43 -0
  10. data/lib/cf_script/command/base.rb +57 -0
  11. data/lib/cf_script/command/cf/apps/app.rb +19 -0
  12. data/lib/cf_script/command/cf/apps/apps.rb +44 -0
  13. data/lib/cf_script/command/cf/apps/env.rb +20 -0
  14. data/lib/cf_script/command/cf/apps/push.rb +47 -0
  15. data/lib/cf_script/command/cf/apps/restage.rb +19 -0
  16. data/lib/cf_script/command/cf/apps/restart.rb +19 -0
  17. data/lib/cf_script/command/cf/apps/set_env.rb +18 -0
  18. data/lib/cf_script/command/cf/apps/start.rb +19 -0
  19. data/lib/cf_script/command/cf/apps/stop.rb +19 -0
  20. data/lib/cf_script/command/cf/apps/unset_env.rb +18 -0
  21. data/lib/cf_script/command/cf/apps.rb +55 -0
  22. data/lib/cf_script/command/cf/general/api.rb +36 -0
  23. data/lib/cf_script/command/cf/general/auth.rb +28 -0
  24. data/lib/cf_script/command/cf/general/login.rb +42 -0
  25. data/lib/cf_script/command/cf/general/logout.rb +15 -0
  26. data/lib/cf_script/command/cf/general/target.rb +64 -0
  27. data/lib/cf_script/command/cf/general.rb +15 -0
  28. data/lib/cf_script/command/cf/routes/check_route.rb +17 -0
  29. data/lib/cf_script/command/cf/routes/create_route.rb +19 -0
  30. data/lib/cf_script/command/cf/routes/delete_route.rb +19 -0
  31. data/lib/cf_script/command/cf/routes/map_route.rb +30 -0
  32. data/lib/cf_script/command/cf/routes/routes.rb +40 -0
  33. data/lib/cf_script/command/cf/routes/unmap_route.rb +30 -0
  34. data/lib/cf_script/command/cf/routes.rb +17 -0
  35. data/lib/cf_script/command/cf/spaces/space.rb +36 -0
  36. data/lib/cf_script/command/cf/spaces/spaces.rb +23 -0
  37. data/lib/cf_script/command/cf/spaces.rb +9 -0
  38. data/lib/cf_script/command/error.rb +5 -0
  39. data/lib/cf_script/command/line.rb +105 -0
  40. data/lib/cf_script/command/registry.rb +50 -0
  41. data/lib/cf_script/command/runner.rb +97 -0
  42. data/lib/cf_script/command.rb +44 -0
  43. data/lib/cf_script/config.rb +60 -0
  44. data/lib/cf_script/executor/non_blocking.rb +107 -0
  45. data/lib/cf_script/executor/recorder.rb +16 -0
  46. data/lib/cf_script/executor/simple.rb +17 -0
  47. data/lib/cf_script/executor.rb +5 -0
  48. data/lib/cf_script/manifest.rb +38 -0
  49. data/lib/cf_script/object/api_endpoint.rb +10 -0
  50. data/lib/cf_script/object/app_info.rb +69 -0
  51. data/lib/cf_script/object/app_list.rb +24 -0
  52. data/lib/cf_script/object/app_spec.rb +55 -0
  53. data/lib/cf_script/object/attribute.rb +18 -0
  54. data/lib/cf_script/object/attribute_list.rb +40 -0
  55. data/lib/cf_script/object/instance_status.rb +29 -0
  56. data/lib/cf_script/object/route_info.rb +14 -0
  57. data/lib/cf_script/object/space.rb +17 -0
  58. data/lib/cf_script/object/target.rb +23 -0
  59. data/lib/cf_script/object.rb +24 -0
  60. data/lib/cf_script/output/buffer.rb +74 -0
  61. data/lib/cf_script/output/parser/attributes.rb +44 -0
  62. data/lib/cf_script/output/parser/section.rb +15 -0
  63. data/lib/cf_script/output/parser/table.rb +46 -0
  64. data/lib/cf_script/output/parser.rb +15 -0
  65. data/lib/cf_script/output/tests.rb +51 -0
  66. data/lib/cf_script/output/utils.rb +13 -0
  67. data/lib/cf_script/output.rb +62 -0
  68. data/lib/cf_script/scope/app/env.rb +15 -0
  69. data/lib/cf_script/scope/app/routes.rb +21 -0
  70. data/lib/cf_script/scope/app/state.rb +33 -0
  71. data/lib/cf_script/scope/app/utils.rb +32 -0
  72. data/lib/cf_script/scope/app.rb +94 -0
  73. data/lib/cf_script/scope/proxy.rb +36 -0
  74. data/lib/cf_script/scope/script.rb +59 -0
  75. data/lib/cf_script/scope/space.rb +9 -0
  76. data/lib/cf_script/scope/target.rb +90 -0
  77. data/lib/cf_script/scope.rb +68 -0
  78. data/lib/cf_script/ui.rb +261 -0
  79. data/lib/cf_script/utils.rb +19 -0
  80. data/lib/cf_script/version.rb +3 -0
  81. data/lib/cf_script.rb +105 -0
  82. data/test/fixtures/commands/apps/app/good.yml +18 -0
  83. data/test/fixtures/commands/apps/app/not_found.yml +8 -0
  84. data/test/fixtures/commands/apps/app/running.yml +19 -0
  85. data/test/fixtures/commands/apps/apps/good.yml +14 -0
  86. data/test/fixtures/commands/apps/apps/no_apps.yml +10 -0
  87. data/test/fixtures/commands/apps/env/empty.yml +37 -0
  88. data/test/fixtures/commands/apps/env/good.yml +41 -0
  89. data/test/fixtures/commands/apps/push/good.yml +60 -0
  90. data/test/fixtures/commands/apps/restage/good.yml +48 -0
  91. data/test/fixtures/commands/apps/restage/not_found.yml +8 -0
  92. data/test/fixtures/commands/apps/restart/good.yml +35 -0
  93. data/test/fixtures/commands/apps/restart/not_found.yml +8 -0
  94. data/test/fixtures/commands/apps/set_env/empty.yml +5 -0
  95. data/test/fixtures/commands/apps/set_env/good.yml +9 -0
  96. data/test/fixtures/commands/apps/set_env/not_found.yml +8 -0
  97. data/test/fixtures/commands/apps/start/good.yml +32 -0
  98. data/test/fixtures/commands/apps/start/not_found.yml +8 -0
  99. data/test/fixtures/commands/apps/start/with_container.yml +98 -0
  100. data/test/fixtures/commands/apps/stop/good.yml +8 -0
  101. data/test/fixtures/commands/apps/stop/not_found.yml +8 -0
  102. data/test/fixtures/commands/apps/unset_env/empty.yml +5 -0
  103. data/test/fixtures/commands/apps/unset_env/good.yml +9 -0
  104. data/test/fixtures/commands/apps/unset_env/not_found.yml +8 -0
  105. data/test/fixtures/commands/common/fail.yml +5 -0
  106. data/test/fixtures/commands/common/no_endpoint.yml +11 -0
  107. data/test/fixtures/commands/common/no_login.yml +8 -0
  108. data/test/fixtures/commands/general/api/bad_argument.yml +9 -0
  109. data/test/fixtures/commands/general/api/bad_endpoint.yml +9 -0
  110. data/test/fixtures/commands/general/api/good.yml +7 -0
  111. data/test/fixtures/commands/general/api/not_set.yml +7 -0
  112. data/test/fixtures/commands/general/api/set.yml +7 -0
  113. data/test/fixtures/commands/general/auth/empty.yml +5 -0
  114. data/test/fixtures/commands/general/auth/good.yml +10 -0
  115. data/test/fixtures/commands/general/auth/rejected.yml +10 -0
  116. data/test/fixtures/commands/general/login/good.yml +8 -0
  117. data/test/fixtures/commands/general/login/rejected.yml +23 -0
  118. data/test/fixtures/commands/general/logout/good.yml +8 -0
  119. data/test/fixtures/commands/general/target/empty.yml +5 -0
  120. data/test/fixtures/commands/general/target/good.yml +11 -0
  121. data/test/fixtures/commands/general/target/no_space.yml +10 -0
  122. data/test/fixtures/commands/general/target/not_found.yml +9 -0
  123. data/test/fixtures/commands/general/target/production.yml +11 -0
  124. data/test/fixtures/commands/general/target/staging.yml +11 -0
  125. data/test/fixtures/commands/general/target/staging_org.yml +11 -0
  126. data/test/fixtures/commands/general/target/test.yml +11 -0
  127. data/test/fixtures/commands/routes/check_route/good.yml +9 -0
  128. data/test/fixtures/commands/routes/check_route/no_domain.yml +9 -0
  129. data/test/fixtures/commands/routes/check_route/no_route.yml +9 -0
  130. data/test/fixtures/commands/routes/create_route/exists.yml +9 -0
  131. data/test/fixtures/commands/routes/create_route/good.yml +8 -0
  132. data/test/fixtures/commands/routes/delete_route/good.yml +8 -0
  133. data/test/fixtures/commands/routes/delete_route/not_exist.yml +8 -0
  134. data/test/fixtures/commands/routes/map_route/good.yml +10 -0
  135. data/test/fixtures/commands/routes/map_route/host_taken.yml +10 -0
  136. data/test/fixtures/commands/routes/map_route/invalid.yml +9 -0
  137. data/test/fixtures/commands/routes/map_route/no_app.yml +8 -0
  138. data/test/fixtures/commands/routes/map_route/no_domain.yml +9 -0
  139. data/test/fixtures/commands/routes/routes/good.yml +14 -0
  140. data/test/fixtures/commands/routes/unmap_route/good.yml +8 -0
  141. data/test/fixtures/commands/routes/unmap_route/no_app.yml +8 -0
  142. data/test/fixtures/commands/routes/unmap_route/no_domain.yml +9 -0
  143. data/test/fixtures/commands/spaces/space/empty.yml +5 -0
  144. data/test/fixtures/commands/spaces/space/good.yml +16 -0
  145. data/test/fixtures/commands/spaces/space/staging.yml +16 -0
  146. data/test/fixtures/commands/spaces/spaces/empty.yml +5 -0
  147. data/test/fixtures/commands/spaces/spaces/good.yml +12 -0
  148. data/test/lib/cf_script/command/base_test.rb +21 -0
  149. data/test/lib/cf_script/command/cf/apps/app_test.rb +56 -0
  150. data/test/lib/cf_script/command/cf/apps/apps_test.rb +81 -0
  151. data/test/lib/cf_script/command/cf/apps/env_test.rb +55 -0
  152. data/test/lib/cf_script/command/cf/apps/push_test.rb +46 -0
  153. data/test/lib/cf_script/command/cf/apps/restage_test.rb +47 -0
  154. data/test/lib/cf_script/command/cf/apps/restart_test.rb +47 -0
  155. data/test/lib/cf_script/command/cf/apps/set_env_test.rb +38 -0
  156. data/test/lib/cf_script/command/cf/apps/start_test.rb +55 -0
  157. data/test/lib/cf_script/command/cf/apps/stop_test.rb +35 -0
  158. data/test/lib/cf_script/command/cf/apps/unset_env_test.rb +38 -0
  159. data/test/lib/cf_script/command/cf/general/api_test.rb +55 -0
  160. data/test/lib/cf_script/command/cf/general/auth_test.rb +76 -0
  161. data/test/lib/cf_script/command/cf/general/login_test.rb +80 -0
  162. data/test/lib/cf_script/command/cf/general/logout_test.rb +34 -0
  163. data/test/lib/cf_script/command/cf/general/target_test.rb +110 -0
  164. data/test/lib/cf_script/command/cf/routes/check_route_test.rb +31 -0
  165. data/test/lib/cf_script/command/cf/routes/create_route_test.rb +19 -0
  166. data/test/lib/cf_script/command/cf/routes/delete_route_test.rb +19 -0
  167. data/test/lib/cf_script/command/cf/routes/map_route_test.rb +56 -0
  168. data/test/lib/cf_script/command/cf/routes/routes_test.rb +24 -0
  169. data/test/lib/cf_script/command/cf/routes/unmap_route_test.rb +36 -0
  170. data/test/lib/cf_script/command/cf/spaces/space_test.rb +55 -0
  171. data/test/lib/cf_script/command/cf/spaces/spaces_test.rb +47 -0
  172. data/test/lib/cf_script/command/line_test.rb +59 -0
  173. data/test/lib/cf_script/object/api_endpoint_test.rb +19 -0
  174. data/test/lib/cf_script/object/app_info_test.rb +127 -0
  175. data/test/lib/cf_script/object/app_list_test.rb +51 -0
  176. data/test/lib/cf_script/object/attribute_list_test.rb +42 -0
  177. data/test/lib/cf_script/object/attribute_test.rb +29 -0
  178. data/test/lib/cf_script/object/instance_status_test.rb +37 -0
  179. data/test/lib/cf_script/object/space_test.rb +31 -0
  180. data/test/lib/cf_script/object/target_test.rb +41 -0
  181. data/test/lib/cf_script/output/buffer_test.rb +73 -0
  182. data/test/lib/cf_script/output/parser/attributes_test.rb +102 -0
  183. data/test/lib/cf_script/output/parser/section_test.rb +60 -0
  184. data/test/lib/cf_script/output/parser/table_test.rb +108 -0
  185. data/test/lib/cf_script/output/tests_test.rb +91 -0
  186. data/test/lib/cf_script/output_test.rb +134 -0
  187. data/test/lib/cf_script/scope/app/state_test.rb +61 -0
  188. data/test/lib/cf_script/scope/app_test.rb +101 -0
  189. data/test/lib/cf_script/scope/script_test.rb +88 -0
  190. data/test/lib/cf_script/scope/space_test.rb +27 -0
  191. data/test/lib/cf_script/scope/target_test.rb +158 -0
  192. data/test/lib/cf_script/scope_test.rb +41 -0
  193. data/test/lib/cf_script/utils_test.rb +20 -0
  194. data/test/lib/cf_script_test.rb +73 -0
  195. data/test/support/assertions.rb +14 -0
  196. data/test/support/helpers/config_context.rb +19 -0
  197. data/test/support/helpers/fixture_executor.rb +79 -0
  198. data/test/support/helpers/mock_execution.rb +58 -0
  199. data/test/support/helpers/object_helpers.rb +29 -0
  200. data/test/support/helpers/output_sink.rb +25 -0
  201. data/test/support/helpers.rb +3 -0
  202. data/test/support/shared_examples/command_object.rb +38 -0
  203. data/test/support/shared_examples.rb +30 -0
  204. data/test/test_helper.rb +34 -0
  205. metadata +321 -0
@@ -0,0 +1,158 @@
1
+ require 'test_helper'
2
+
3
+ describe CfScript::Scope::Target do
4
+ include MockExecution
5
+
6
+ subject {
7
+ target = nil
8
+
9
+ fake_cf target: :staging do
10
+ target = CfScript::Scope::Target.new
11
+ end
12
+
13
+ target
14
+ }
15
+
16
+ it "sets initial_target and current_target on initialize" do
17
+ initial_target = subject.instance_variable_get('@initial_target')
18
+ current_target = subject.instance_variable_get('@current_target')
19
+
20
+ assert initial_target
21
+ assert current_target
22
+
23
+ assert_instance_of CfScript::Target, initial_target
24
+ assert_instance_of CfScript::Target, current_target
25
+
26
+ assert_equal 'staging', initial_target.space
27
+ assert_equal 'ACME', initial_target.org
28
+
29
+ assert_equal 'staging', current_target.space
30
+ assert_equal 'ACME', current_target.org
31
+ end
32
+
33
+ it "updates current_target when target is called" do
34
+ fake_cf target: :production do
35
+ assert_equal 'staging', subject.current_space
36
+ assert_equal 'ACME', subject.current_org
37
+
38
+ subject.target(:production)
39
+ assert_equal 'production', subject.current_space
40
+ assert_equal 'ACME', subject.current_org
41
+ end
42
+ end
43
+
44
+ it "resets target to initial_target if the space has changed" do
45
+ fake_cf target: :production do
46
+ subject.target(:production)
47
+ assert_equal 'production', subject.current_space
48
+ assert_equal 'ACME', subject.current_org
49
+
50
+ subject.finalize
51
+ assert_equal 'staging', subject.current_space
52
+ assert_equal 'ACME', subject.current_org
53
+ end
54
+ end
55
+
56
+ it "resets target to initial_target if the org has changed" do
57
+ fake_cf do
58
+ subject.target(:staging_org)
59
+ assert_equal 'staging', subject.current_space
60
+ assert_equal 'ORG', subject.current_org
61
+
62
+ subject.finalize
63
+ assert_equal 'staging', subject.current_space
64
+ assert_equal 'ACME', subject.current_org
65
+ end
66
+ end
67
+
68
+ it "responds to target without args and calls Command.run" do
69
+ assert subject.respond_to?(:target)
70
+
71
+ CfScript::Command.stub :run, :called do
72
+ assert_equal :called, subject.target
73
+ end
74
+ end
75
+
76
+ it "responds to target with a space name and calls Command.run if not == current" do
77
+ assert subject.respond_to?(:target)
78
+
79
+ assert_equal 'staging', subject.current_space
80
+
81
+ CfScript::Command.stub :run, :called do
82
+ assert_equal :called, subject.target(:production)
83
+ end
84
+ end
85
+
86
+ it "returns current target if it's the same as the requested target" do
87
+ current_target = subject.instance_variable_get('@current_target')
88
+
89
+ CfScript::Command.stub :run, :called do
90
+ assert_same current_target, subject.target(:staging)
91
+ end
92
+ end
93
+
94
+ it "responds to space and calls Command.run" do
95
+ assert subject.respond_to?(:space)
96
+
97
+ CfScript::Command.stub :run, :called do
98
+ assert_equal :called, subject.space(:foo)
99
+ end
100
+ end
101
+
102
+ it "responds to space with a block and calls exec_in with a Space scope" do
103
+ assert subject.respond_to?(:space)
104
+
105
+ arg_catcher = lambda do |scope, args, &block|
106
+ assert_instance_of CfScript::Scope::Space, scope
107
+ end
108
+
109
+ fake_cf do
110
+ subject.stub :exec_in, arg_catcher do
111
+ assert subject.space(:staging) { nil }
112
+ end
113
+ end
114
+ end
115
+
116
+ it "responds to app and calls Command.run" do
117
+ assert subject.respond_to?(:app)
118
+
119
+ CfScript::Command.stub :run, :called do
120
+ assert_equal :called, subject.app(:foo)
121
+ end
122
+ end
123
+
124
+ it "responds to app with a block and calls exec_in with an App scope" do
125
+ assert subject.respond_to?(:app)
126
+
127
+ arg_catcher = lambda do |scope, args, &block|
128
+ assert_instance_of CfScript::Scope::App, scope
129
+ end
130
+
131
+ fake_cf do
132
+ subject.stub :exec_in, arg_catcher do
133
+ assert subject.app(:worker) { nil }
134
+ end
135
+ end
136
+ end
137
+
138
+ it "responds to apps and calls Command.run" do
139
+ assert subject.respond_to?(:apps)
140
+
141
+ CfScript::Command.stub :run, :called do
142
+ assert_equal :called, subject.apps
143
+ end
144
+ end
145
+
146
+ it "calls apps.select! when options is not empty" do
147
+ assert subject.respond_to?(:apps)
148
+
149
+ apps = MiniTest::Mock.new
150
+ apps.expect(:select!, apps, [{ ending_with: :foo }])
151
+
152
+ CfScript::Command.stub :run, apps do
153
+ subject.apps(ending_with: :foo)
154
+ end
155
+
156
+ apps.verify
157
+ end
158
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ describe CfScript::Scope do
4
+ subject { CfScript::Scope::Base.new }
5
+
6
+ class ScopeTest < CfScript::Scope::Base
7
+ attr_reader :called_enter_scope
8
+
9
+ def enter_scope
10
+ @called_enter_scope = true
11
+ end
12
+ end
13
+
14
+ it "calls enter_scope on initialize" do
15
+ assert ScopeTest.new.called_enter_scope
16
+ end
17
+
18
+ it "responds to finalize which calls exit_scope" do
19
+ assert subject.respond_to?(:finalize)
20
+
21
+ subject.stub :exit_scope, 'foo' do
22
+ assert_equal 'foo', subject.finalize
23
+ end
24
+ end
25
+
26
+ it "responds to spec_for and calls CfScript.spec_for" do
27
+ assert subject.respond_to?(:spec_for)
28
+
29
+ CfScript.stub :spec_for, 'called' do
30
+ assert_equal 'called', subject.spec_for(:app)
31
+ end
32
+ end
33
+
34
+ it "responds to method_missing and calls Command.run" do
35
+ assert subject.respond_to?(:method_missing)
36
+
37
+ CfScript::Command.stub :run, 'pong' do
38
+ assert_equal 'pong', subject.ping
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ describe CfScript::Utils do
4
+ subject { CfScript::Utils }
5
+
6
+ it "defines a symbolize method that symbolizes a string" do
7
+ assert_equal :foo_bar, subject.symbolize('foo bar')
8
+ assert_equal :foo_bar, subject.symbolize('foo-bar')
9
+ assert_equal :foo_bar, subject.symbolize('foo.bar')
10
+ end
11
+
12
+ it "defines a symbolize_keys method that symbolizes the keys of a Hash" do
13
+ input = { 'foo-bar' => 'baz' }
14
+ output = subject.symbolize_keys(input)
15
+
16
+ expected = { foo_bar: 'baz' }
17
+
18
+ assert_equal expected, output
19
+ end
20
+ end
@@ -0,0 +1,73 @@
1
+ require 'test_helper'
2
+
3
+ describe CfScript do
4
+ include MockExecution
5
+
6
+ it "defines a config method that returns the Config object" do
7
+ assert CfScript.respond_to? :config
8
+
9
+ config = CfScript.config
10
+
11
+ assert_instance_of CfScript::Config, config
12
+ assert_same CfScript.config, config
13
+ end
14
+
15
+ it "defines a configure method that yields the Config object" do
16
+ assert CfScript.respond_to? :configure
17
+
18
+ called = false
19
+
20
+ CfScript.configure do |config|
21
+ called = true
22
+
23
+ assert_instance_of CfScript::Config, config
24
+ assert_same CfScript.config, config
25
+ end
26
+
27
+ assert called, 'Expected configure to yield to the block'
28
+ end
29
+
30
+ it "defines stdout/stderr methods that return the configured IO objects" do
31
+ assert CfScript.respond_to? :stdout
32
+ assert_same CfScript.config.io.stdout, CfScript.stdout
33
+
34
+ assert CfScript.respond_to? :stderr
35
+ assert_same CfScript.config.io.stderr, CfScript.stderr
36
+ end
37
+
38
+ it "works when cf calls are nested" do
39
+ space_sequence = []
40
+
41
+ fake_cf do
42
+ cf space: :development do
43
+ space_sequence << current_space
44
+
45
+ cf space: :staging do
46
+ space_sequence << current_space
47
+
48
+ cf space: :production do
49
+ space_sequence << current_space
50
+ end
51
+
52
+ space_sequence << current_space
53
+ end
54
+
55
+ space_sequence << current_space
56
+ end
57
+ end
58
+
59
+ assert_equal 'development', space_sequence[0]
60
+ assert_equal 'staging', space_sequence[1]
61
+ assert_equal 'production', space_sequence[2]
62
+ assert_equal 'staging', space_sequence[3]
63
+ assert_equal 'development', space_sequence[4]
64
+ end
65
+
66
+ it "doesn't expose private methods" do
67
+ refute CfScript.respond_to?(:stack_level)
68
+ assert CfScript.singleton_class.private_method_defined?(:stack_level)
69
+
70
+ refute CfScript.respond_to?(:print_error)
71
+ assert CfScript.singleton_class.private_method_defined?(:print_error)
72
+ end
73
+ end
@@ -0,0 +1,14 @@
1
+ def assert_app_info(app_info, expected_instances)
2
+ assert_instance_of CfScript::AppInfo, app_info
3
+
4
+ assert_instance_of Array, app_info.instance_status
5
+ assert_equal expected_instances.length, app_info.instance_status.length
6
+
7
+ app_info.instance_status.each_with_index do |instance_status, index|
8
+ assert_instance_of CfScript::InstanceStatus, instance_status
9
+
10
+ expected_instances[index].each do |attr, value|
11
+ assert_equal value, instance_status.send(attr)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ class ConfigContext
2
+ def initialize
3
+ @stdout = CfScript.config.io.stdout
4
+ @stderr = CfScript.config.io.stderr
5
+
6
+ @color = CfScript.config.ui.color
7
+
8
+ @executor = CfScript.config.runtime.executor
9
+ end
10
+
11
+ def reset
12
+ CfScript.config.io.stdout = @stdout
13
+ CfScript.config.io.stderr = @stderr
14
+
15
+ CfScript.config.ui.color = @color
16
+
17
+ CfScript.config.runtime.executor = @executor
18
+ end
19
+ end
@@ -0,0 +1,79 @@
1
+ class FixtureExecutor
2
+ COMMON_FIXTURE_PATH = "fixtures/common"
3
+ COMMAND_FIXTURE_PATH = "fixtures/commands"
4
+
5
+ def initialize(command_fixtures)
6
+ @@fixture_cache ||= {}
7
+
8
+ @command_fixtures = command_fixtures
9
+ end
10
+
11
+ def execute(env, command_line)
12
+ data = fixture_for(command_line)
13
+
14
+ CfScript.stdout.puts if CfScript.config.runtime.trace
15
+
16
+ [
17
+ data['stdout'],
18
+ data['stderr'],
19
+ OpenStruct.new(
20
+ exitstatus: data['status']['exitstatus'],
21
+ success?: data['status']['exitstatus'] == 0
22
+ )
23
+ ]
24
+ end
25
+
26
+ private
27
+
28
+ def fixture_for(command_line)
29
+ case @command_fixtures[command_line.name]
30
+ when :fail, :no_endpoint, :no_login
31
+ dir = File.join('commands', 'common')
32
+ key = @command_fixtures[command_line.name]
33
+ else
34
+ dir = 'commands'
35
+ key = fixture_key(command_line)
36
+ end
37
+
38
+ unless @@fixture_cache.has_key?(key)
39
+ @@fixture_cache[key] = load_fixture(dir, key)
40
+ end
41
+
42
+ @@fixture_cache[key]
43
+ end
44
+
45
+ def fixture_key(command_line)
46
+ File.join(
47
+ command_line.type.to_s,
48
+ command_line.name.to_s,
49
+ command_fixture(command_line).to_s
50
+ )
51
+ end
52
+
53
+ def command_fixture(command_line)
54
+ case command_line.name
55
+ when :target
56
+ if command_line.options[:s]
57
+ command_line.options[:s]
58
+ else
59
+ @command_fixtures[command_line.name] || :good
60
+ end
61
+ else
62
+ @command_fixtures[command_line.name] || :good
63
+ end
64
+ end
65
+
66
+ def fixtures_path
67
+ File.expand_path("../../../fixtures", __FILE__)
68
+ end
69
+
70
+ def load_fixture(dir, key)
71
+ file = File.join(fixtures_path, dir, "#{key}.yml")
72
+
73
+ if File.exist?(file)
74
+ YAML.load(File.new(file, 'r'))
75
+ else
76
+ raise "Missing fixture file `#{file}`"
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,58 @@
1
+ module MockExecution
2
+ module_function
3
+
4
+ def test_env
5
+ ENV['CF_BINARY'] = 'cf'
6
+ end
7
+
8
+ def test_config(config = {})
9
+ CfScript.config.io.stdout = config[:stderr] || StringIO.new
10
+ CfScript.config.io.stderr = config[:stderr] || StringIO.new
11
+
12
+ CfScript.config.ui.color = config[:color] || false
13
+
14
+ CfScript.config.runtime.trace = config[:trace] || true
15
+ end
16
+
17
+ def test_executor(executor)
18
+ CfScript.config.runtime.executor = executor
19
+ end
20
+
21
+ def fake_process_status(exitstatus = 0)
22
+ OpenStruct.new(
23
+ exitstatus: exitstatus,
24
+ success?: exitstatus == 0
25
+ )
26
+ end
27
+
28
+ def fake_output(out = '', err = '', exitstatus = 0)
29
+ command = CfScript::Command::General::TargetCommand.instance
30
+ stdout = out
31
+ stderr = err
32
+ status = fake_process_status(exitstatus)
33
+
34
+ CfScript::Output.new(command, stdout, stderr, status)
35
+ end
36
+
37
+ def fake_command_args(command)
38
+ arity = command.method(:run).arity
39
+
40
+ if arity == -1
41
+ []
42
+ else
43
+ ([:arg] * (arity.abs - 1)) + [{}]
44
+ end
45
+ end
46
+
47
+ def fake_cf(command_fixtures = {}, &block)
48
+ config = ConfigContext.new
49
+
50
+ test_env
51
+ test_config
52
+ test_executor FixtureExecutor.new(command_fixtures)
53
+
54
+ yield(CfScript.stdout.string, CfScript.stderr.string)
55
+ ensure
56
+ config.reset
57
+ end
58
+ end
@@ -0,0 +1,29 @@
1
+ module ObjectHelpers
2
+ def build_attribute_list(attrs = {})
3
+ list = CfScript::AttributeList.new()
4
+
5
+ attrs.each do |name, value|
6
+ list << CfScript::Attribute.new(name, value)
7
+ end
8
+
9
+ list
10
+ end
11
+
12
+ def build_instance_status_attrs(options = {})
13
+ attrs = build_attribute_list({
14
+ index: '0',
15
+ state: '1',
16
+ since: '2',
17
+ cpu: '3',
18
+ memory: '4',
19
+ disk: '5',
20
+ details: '6'
21
+ }.merge(options))
22
+ end
23
+
24
+ def build_instance_status(options = {})
25
+ attrs = build_instance_status_attrs(options)
26
+
27
+ CfScript::InstanceStatus.new(attrs)
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module OutputSink
2
+ module_function
3
+
4
+ def start
5
+ CfScript.config.color = false
6
+ CfScript.config.stdout = StringIO.new
7
+ CfScript.config.stderr = StringIO.new
8
+ end
9
+
10
+ def stdout
11
+ CfScript.stdout.string
12
+ end
13
+
14
+ def stdout_lines
15
+ stdout.lines.map(&:strip)
16
+ end
17
+
18
+ def stderr
19
+ CfScript.stderr.string
20
+ end
21
+
22
+ def stderr_lines
23
+ stderr.lines.map(&:strip)
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ # Load all helper files
2
+ helpers_dir = File.expand_path('../helpers', __FILE__)
3
+ Dir["#{helpers_dir}/*.rb"].each {|f| require f}
@@ -0,0 +1,38 @@
1
+ shared_examples_for 'a command object that' do |attrs|
2
+ it "behaves like a subclass of Command::Base" do
3
+ assert_kind_of CfScript::Command::Base, command
4
+
5
+ assert command.respond_to?(:type)
6
+ assert command.respond_to?(:name)
7
+ assert command.respond_to?(:run)
8
+
9
+ assert_equal attrs[:has_type], command.type
10
+ assert_equal attrs[:has_name], command.name
11
+ end
12
+
13
+ it "returns fail value when run_cf fails (cf's exit status != 0)" do
14
+ fake_cf attrs[:has_name] => :fail do
15
+ args = fake_command_args(command)
16
+
17
+ assert_equal attrs[:fails_with], command.run(*args)
18
+ end
19
+ end
20
+
21
+ it "prints an error message and returns fail value when an API endpoint is not set" do
22
+ fake_cf attrs[:has_name] => :no_endpoint do |stdout, stderr|
23
+ args = fake_command_args(command)
24
+
25
+ assert_equal attrs[:fails_with], command.run(*args)
26
+ assert_includes stderr.lines, "{#{attrs[:has_name]}} No API endpoint set\n"
27
+ end
28
+ end
29
+
30
+ it "prints an error message and returns fail value when not logged in" do
31
+ fake_cf attrs[:has_name] => :no_login do |stdout, stderr|
32
+ args = fake_command_args(command)
33
+
34
+ assert_equal attrs[:fails_with], command.run(*args)
35
+ assert_includes stderr.lines, "{#{attrs[:has_name]}} Not logged in\n"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ MiniTest::Test.class_eval do
2
+ def self.shared_examples
3
+ @shared_examples ||= {}
4
+ end
5
+ end
6
+
7
+ module MiniTest::Test::SharedExamples
8
+ def shared_examples_for(desc, &block)
9
+ MiniTest::Test.shared_examples[desc] = block
10
+ end
11
+
12
+ def it_behaves_like(desc, *args)
13
+ raise "Unknown shared example '#{desc}' in it_behaves_like" unless
14
+ MiniTest::Test.shared_examples[desc]
15
+
16
+ if args.empty?
17
+ self.instance_eval(&MiniTest::Test.shared_examples[desc])
18
+ else
19
+ self.instance_exec(*args, &MiniTest::Test.shared_examples[desc])
20
+ end
21
+ end
22
+
23
+ alias :it_is :it_behaves_like
24
+ end
25
+
26
+ Object.class_eval { include(MiniTest::Test::SharedExamples) }
27
+
28
+ # Load all shared example files
29
+ shared_examples_dir = File.expand_path('../shared_examples', __FILE__)
30
+ Dir["#{shared_examples_dir}/**/*.rb"].each {|f| require f}
@@ -0,0 +1,34 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "/test/"
5
+ end
6
+ end
7
+
8
+ gem 'minitest'
9
+
10
+ require 'minitest/autorun'
11
+ require "minitest/focus"
12
+ require 'minitest/pride'
13
+
14
+ if ENV['REPORTER']
15
+ require 'minitest/reporters'
16
+
17
+ case ENV['REPORTER']
18
+ when 'spec'
19
+ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
20
+ when 'progress'
21
+ Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new
22
+ when 'time'
23
+ Minitest::Reporters.use! Minitest::Reporters::MeanTimeReporter.new
24
+ else
25
+ Minitest::Reporters.use! Minitest::Reporters::DefaultReporter.new
26
+ end
27
+ end
28
+
29
+ require 'pry'
30
+ require_relative './support/assertions'
31
+ require_relative './support/helpers'
32
+ require_relative './support/shared_examples'
33
+
34
+ require File.expand_path("../../lib/cf_script", __FILE__)