masamune 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +54 -0
  4. data/Rakefile +15 -0
  5. data/bin/masamune-elastic-mapreduce +4 -0
  6. data/bin/masamune-hive +4 -0
  7. data/bin/masamune-psql +4 -0
  8. data/bin/masamune-shell +4 -0
  9. data/lib/masamune.rb +56 -0
  10. data/lib/masamune/accumulate.rb +60 -0
  11. data/lib/masamune/actions.rb +38 -0
  12. data/lib/masamune/actions/data_flow.rb +131 -0
  13. data/lib/masamune/actions/date_parse.rb +75 -0
  14. data/lib/masamune/actions/elastic_mapreduce.rb +68 -0
  15. data/lib/masamune/actions/execute.rb +52 -0
  16. data/lib/masamune/actions/filesystem.rb +37 -0
  17. data/lib/masamune/actions/hadoop_filesystem.rb +40 -0
  18. data/lib/masamune/actions/hadoop_streaming.rb +41 -0
  19. data/lib/masamune/actions/hive.rb +74 -0
  20. data/lib/masamune/actions/postgres.rb +76 -0
  21. data/lib/masamune/actions/postgres_admin.rb +34 -0
  22. data/lib/masamune/actions/s3cmd.rb +44 -0
  23. data/lib/masamune/actions/transform.rb +89 -0
  24. data/lib/masamune/after_initialize_callbacks.rb +55 -0
  25. data/lib/masamune/cached_filesystem.rb +110 -0
  26. data/lib/masamune/commands.rb +37 -0
  27. data/lib/masamune/commands/elastic_mapreduce.rb +119 -0
  28. data/lib/masamune/commands/hadoop_filesystem.rb +57 -0
  29. data/lib/masamune/commands/hadoop_streaming.rb +116 -0
  30. data/lib/masamune/commands/hive.rb +178 -0
  31. data/lib/masamune/commands/interactive.rb +37 -0
  32. data/lib/masamune/commands/postgres.rb +128 -0
  33. data/lib/masamune/commands/postgres_admin.rb +72 -0
  34. data/lib/masamune/commands/postgres_common.rb +33 -0
  35. data/lib/masamune/commands/retry_with_backoff.rb +60 -0
  36. data/lib/masamune/commands/s3cmd.rb +70 -0
  37. data/lib/masamune/commands/shell.rb +202 -0
  38. data/lib/masamune/configuration.rb +195 -0
  39. data/lib/masamune/data_plan.rb +31 -0
  40. data/lib/masamune/data_plan/builder.rb +66 -0
  41. data/lib/masamune/data_plan/elem.rb +190 -0
  42. data/lib/masamune/data_plan/engine.rb +162 -0
  43. data/lib/masamune/data_plan/rule.rb +292 -0
  44. data/lib/masamune/data_plan/set.rb +176 -0
  45. data/lib/masamune/environment.rb +164 -0
  46. data/lib/masamune/filesystem.rb +567 -0
  47. data/lib/masamune/has_environment.rb +40 -0
  48. data/lib/masamune/helpers.rb +27 -0
  49. data/lib/masamune/helpers/postgres.rb +84 -0
  50. data/lib/masamune/io.rb +33 -0
  51. data/lib/masamune/last_element.rb +53 -0
  52. data/lib/masamune/method_logger.rb +41 -0
  53. data/lib/masamune/multi_io.rb +39 -0
  54. data/lib/masamune/schema.rb +36 -0
  55. data/lib/masamune/schema/catalog.rb +233 -0
  56. data/lib/masamune/schema/column.rb +527 -0
  57. data/lib/masamune/schema/dimension.rb +133 -0
  58. data/lib/masamune/schema/event.rb +121 -0
  59. data/lib/masamune/schema/fact.rb +133 -0
  60. data/lib/masamune/schema/map.rb +265 -0
  61. data/lib/masamune/schema/row.rb +133 -0
  62. data/lib/masamune/schema/store.rb +115 -0
  63. data/lib/masamune/schema/table.rb +308 -0
  64. data/lib/masamune/schema/table_reference.rb +76 -0
  65. data/lib/masamune/spec_helper.rb +23 -0
  66. data/lib/masamune/string_format.rb +34 -0
  67. data/lib/masamune/tasks/elastic_mapreduce_thor.rb +60 -0
  68. data/lib/masamune/tasks/hive_thor.rb +55 -0
  69. data/lib/masamune/tasks/postgres_thor.rb +47 -0
  70. data/lib/masamune/tasks/shell_thor.rb +63 -0
  71. data/lib/masamune/template.rb +77 -0
  72. data/lib/masamune/thor.rb +186 -0
  73. data/lib/masamune/thor_loader.rb +38 -0
  74. data/lib/masamune/topological_hash.rb +34 -0
  75. data/lib/masamune/transform.rb +47 -0
  76. data/lib/masamune/transform/bulk_upsert.psql.erb +64 -0
  77. data/lib/masamune/transform/bulk_upsert.rb +52 -0
  78. data/lib/masamune/transform/consolidate_dimension.rb +54 -0
  79. data/lib/masamune/transform/deduplicate_dimension.psql.erb +52 -0
  80. data/lib/masamune/transform/deduplicate_dimension.rb +53 -0
  81. data/lib/masamune/transform/define_event_view.hql.erb +51 -0
  82. data/lib/masamune/transform/define_event_view.rb +60 -0
  83. data/lib/masamune/transform/define_index.psql.erb +34 -0
  84. data/lib/masamune/transform/define_schema.hql.erb +23 -0
  85. data/lib/masamune/transform/define_schema.psql.erb +79 -0
  86. data/lib/masamune/transform/define_schema.rb +56 -0
  87. data/lib/masamune/transform/define_table.hql.erb +34 -0
  88. data/lib/masamune/transform/define_table.psql.erb +95 -0
  89. data/lib/masamune/transform/define_table.rb +40 -0
  90. data/lib/masamune/transform/define_unique.psql.erb +30 -0
  91. data/lib/masamune/transform/insert_reference_values.psql.erb +43 -0
  92. data/lib/masamune/transform/insert_reference_values.rb +64 -0
  93. data/lib/masamune/transform/load_dimension.rb +47 -0
  94. data/lib/masamune/transform/load_fact.rb +45 -0
  95. data/lib/masamune/transform/operator.rb +96 -0
  96. data/lib/masamune/transform/relabel_dimension.psql.erb +76 -0
  97. data/lib/masamune/transform/relabel_dimension.rb +39 -0
  98. data/lib/masamune/transform/rollup_fact.psql.erb +79 -0
  99. data/lib/masamune/transform/rollup_fact.rb +149 -0
  100. data/lib/masamune/transform/snapshot_dimension.psql.erb +75 -0
  101. data/lib/masamune/transform/snapshot_dimension.rb +74 -0
  102. data/lib/masamune/transform/stage_dimension.psql.erb +39 -0
  103. data/lib/masamune/transform/stage_dimension.rb +83 -0
  104. data/lib/masamune/transform/stage_fact.psql.erb +80 -0
  105. data/lib/masamune/transform/stage_fact.rb +111 -0
  106. data/lib/masamune/version.rb +25 -0
  107. data/spec/fixtures/aggregate.sql.erb +25 -0
  108. data/spec/fixtures/comment.sql.erb +27 -0
  109. data/spec/fixtures/invalid.sql.erb +23 -0
  110. data/spec/fixtures/relative.sql.erb +23 -0
  111. data/spec/fixtures/simple.sql.erb +28 -0
  112. data/spec/fixtures/whitespace.sql.erb +30 -0
  113. data/spec/masamune/actions/elastic_mapreduce_spec.rb +108 -0
  114. data/spec/masamune/actions/execute_spec.rb +50 -0
  115. data/spec/masamune/actions/hadoop_filesystem_spec.rb +44 -0
  116. data/spec/masamune/actions/hadoop_streaming_spec.rb +74 -0
  117. data/spec/masamune/actions/hive_spec.rb +117 -0
  118. data/spec/masamune/actions/postgres_admin_spec.rb +58 -0
  119. data/spec/masamune/actions/postgres_spec.rb +134 -0
  120. data/spec/masamune/actions/s3cmd_spec.rb +44 -0
  121. data/spec/masamune/actions/transform_spec.rb +144 -0
  122. data/spec/masamune/after_initialization_callbacks_spec.rb +61 -0
  123. data/spec/masamune/cached_filesystem_spec.rb +167 -0
  124. data/spec/masamune/commands/hadoop_filesystem_spec.rb +50 -0
  125. data/spec/masamune/commands/hadoop_streaming_spec.rb +106 -0
  126. data/spec/masamune/commands/hive_spec.rb +117 -0
  127. data/spec/masamune/commands/postgres_admin_spec.rb +69 -0
  128. data/spec/masamune/commands/postgres_spec.rb +100 -0
  129. data/spec/masamune/commands/retry_with_backoff_spec.rb +116 -0
  130. data/spec/masamune/commands/s3cmd_spec.rb +50 -0
  131. data/spec/masamune/commands/shell_spec.rb +101 -0
  132. data/spec/masamune/configuration_spec.rb +102 -0
  133. data/spec/masamune/data_plan/builder_spec.rb +91 -0
  134. data/spec/masamune/data_plan/elem_spec.rb +102 -0
  135. data/spec/masamune/data_plan/engine_spec.rb +356 -0
  136. data/spec/masamune/data_plan/rule_spec.rb +407 -0
  137. data/spec/masamune/data_plan/set_spec.rb +517 -0
  138. data/spec/masamune/environment_spec.rb +65 -0
  139. data/spec/masamune/filesystem_spec.rb +1421 -0
  140. data/spec/masamune/helpers/postgres_spec.rb +95 -0
  141. data/spec/masamune/schema/catalog_spec.rb +613 -0
  142. data/spec/masamune/schema/column_spec.rb +696 -0
  143. data/spec/masamune/schema/dimension_spec.rb +137 -0
  144. data/spec/masamune/schema/event_spec.rb +75 -0
  145. data/spec/masamune/schema/fact_spec.rb +117 -0
  146. data/spec/masamune/schema/map_spec.rb +593 -0
  147. data/spec/masamune/schema/row_spec.rb +28 -0
  148. data/spec/masamune/schema/store_spec.rb +49 -0
  149. data/spec/masamune/schema/table_spec.rb +395 -0
  150. data/spec/masamune/string_format_spec.rb +60 -0
  151. data/spec/masamune/tasks/elastic_mapreduce_thor_spec.rb +57 -0
  152. data/spec/masamune/tasks/hive_thor_spec.rb +75 -0
  153. data/spec/masamune/tasks/postgres_thor_spec.rb +42 -0
  154. data/spec/masamune/tasks/shell_thor_spec.rb +51 -0
  155. data/spec/masamune/template_spec.rb +77 -0
  156. data/spec/masamune/thor_spec.rb +238 -0
  157. data/spec/masamune/transform/bulk_upsert.dimension_spec.rb +200 -0
  158. data/spec/masamune/transform/consolidate_dimension_spec.rb +62 -0
  159. data/spec/masamune/transform/deduplicate_dimension_spec.rb +84 -0
  160. data/spec/masamune/transform/define_event_view_spec.rb +84 -0
  161. data/spec/masamune/transform/define_schema_spec.rb +83 -0
  162. data/spec/masamune/transform/define_table.dimension_spec.rb +306 -0
  163. data/spec/masamune/transform/define_table.fact_spec.rb +291 -0
  164. data/spec/masamune/transform/define_table.table_spec.rb +525 -0
  165. data/spec/masamune/transform/insert_reference_values.dimension_spec.rb +111 -0
  166. data/spec/masamune/transform/insert_reference_values.fact_spec.rb +149 -0
  167. data/spec/masamune/transform/load_dimension_spec.rb +76 -0
  168. data/spec/masamune/transform/load_fact_spec.rb +89 -0
  169. data/spec/masamune/transform/relabel_dimension_spec.rb +102 -0
  170. data/spec/masamune/transform/rollup_fact_spec.rb +333 -0
  171. data/spec/masamune/transform/snapshot_dimension_spec.rb +103 -0
  172. data/spec/masamune/transform/stage_dimension_spec.rb +115 -0
  173. data/spec/masamune/transform/stage_fact_spec.rb +204 -0
  174. data/spec/masamune_spec.rb +32 -0
  175. data/spec/spec_helper.rb +41 -0
  176. data/spec/support/masamune/example_group.rb +36 -0
  177. data/spec/support/masamune/mock_command.rb +99 -0
  178. data/spec/support/masamune/mock_delegate.rb +51 -0
  179. data/spec/support/masamune/mock_filesystem.rb +96 -0
  180. data/spec/support/masamune/thor_mute.rb +35 -0
  181. data/spec/support/rspec/example/action_example_group.rb +34 -0
  182. data/spec/support/rspec/example/task_example_group.rb +80 -0
  183. data/spec/support/rspec/example/transform_example_group.rb +36 -0
  184. data/spec/support/shared_examples/postgres_common_examples.rb +53 -0
  185. metadata +462 -0
@@ -0,0 +1,50 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Commands::HadoopFilesystem do
26
+ let(:configuration) { {options: options} }
27
+ let(:options) { [] }
28
+ let(:attrs) { {} }
29
+
30
+ let(:delegate) { double }
31
+ let(:instance) { described_class.new(delegate, attrs) }
32
+
33
+ before do
34
+ allow(delegate).to receive_message_chain(:configuration, :hadoop_filesystem).and_return(configuration)
35
+ end
36
+
37
+ describe '#command_args' do
38
+ let(:attrs) { {extra: ['-ls', '/']} }
39
+
40
+ subject { instance.command_args }
41
+
42
+ it { is_expected.to eq(['hadoop', 'fs', '-ls', '/']) }
43
+
44
+ context 'with options' do
45
+ let(:options) { [{'--conf' => 'hadoop.conf'}] }
46
+
47
+ it { is_expected.to eq(['hadoop', 'fs', '--conf', 'hadoop.conf', '-ls', '/']) }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,106 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Commands::HadoopStreaming do
26
+ let(:filesystem) { Masamune::MockFilesystem.new }
27
+
28
+ let(:configuration) { {options: options, input: input_option, output: 'output_dir', mapper: 'mapper.rb', reducer: 'reducer.rb', extra: extra} }
29
+ let(:options) { [] }
30
+ let(:input_option) { 'input.txt' }
31
+ let(:extra) { ['-D', %q(map.output.key.field.separator='\t')] }
32
+ let(:attrs) { {} }
33
+
34
+ let(:delegate) { double }
35
+ let(:instance) { described_class.new(delegate, attrs) }
36
+
37
+ before do
38
+ allow(delegate).to receive(:filesystem).and_return(filesystem)
39
+ allow(delegate).to receive(:console).and_return(double)
40
+ allow(delegate).to receive(:logger).and_return(double)
41
+ allow(delegate).to receive_message_chain(:configuration, :hadoop_streaming).and_return(configuration)
42
+ end
43
+
44
+ describe '#before_execute' do
45
+ subject(:input) { instance.input }
46
+
47
+ context 'input path with suffix exists' do
48
+ let(:input_option) { 'dir/input.txt' }
49
+ before do
50
+ filesystem.touch!('dir/input.txt')
51
+ instance.before_execute
52
+ end
53
+ it { is_expected.to eq(['dir/input.txt']) }
54
+ end
55
+
56
+ context 'input path hadoop part' do
57
+ let(:input_option) { 'dir/part_0000' }
58
+ before do
59
+ filesystem.touch!('dir/part_0000')
60
+ instance.before_execute
61
+ end
62
+ it { is_expected.to eq(['dir/part_0000']) }
63
+ end
64
+
65
+ context 'input path directory' do
66
+ let(:input_option) { 'dir' }
67
+ before do
68
+ filesystem.touch!('dir')
69
+ instance.before_execute
70
+ end
71
+ it { is_expected.to eq(['dir/*']) }
72
+ end
73
+
74
+ context 'input path does not exist' do
75
+ before do
76
+ expect(instance.logger).to receive(:debug).with(/\ARemoving missing input/)
77
+ instance.before_execute
78
+ end
79
+ it { is_expected.to be_empty }
80
+ end
81
+ end
82
+
83
+ describe '#command_args' do
84
+ let(:pre_command_args) { ['hadoop', 'jar', described_class.default_hadoop_streaming_jar] }
85
+ let(:post_command_args) { ['-input', 'input.txt', '-mapper', 'mapper.rb', '-file', 'mapper.rb', '-reducer', 'reducer.rb', '-file', 'reducer.rb', '-output', 'output_dir'] }
86
+
87
+ subject { instance.command_args }
88
+
89
+ it { is_expected.to eq(pre_command_args + extra + post_command_args) }
90
+
91
+ context 'with options' do
92
+ let(:options) { [{'-cacheFile' => 'cache.rb'}] }
93
+
94
+ it { is_expected.to eq(pre_command_args + extra + options.map(&:to_a).flatten + post_command_args) }
95
+ end
96
+
97
+ context 'with quote' do
98
+ let(:attrs) { {quote: true} }
99
+ let(:quoted_extra) { ['-D', %q(map.output.key.field.separator='"'\\\\t'"')] }
100
+
101
+ subject { instance.command_args }
102
+
103
+ it { is_expected.to eq(pre_command_args + quoted_extra + post_command_args) }
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,117 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Commands::Hive do
26
+ let(:filesystem) { Masamune::MockFilesystem.new }
27
+ let(:configuration) { {:options => options} }
28
+ let(:options) { [] }
29
+ let(:attrs) { {} }
30
+
31
+ let(:delegate) { double }
32
+ let(:instance) { described_class.new(delegate, attrs) }
33
+
34
+ let(:local_file) { File.join(Dir.tmpdir, SecureRandom.hex + '.txt') }
35
+ let(:mock_tmpdir) { 'mock' }
36
+ let(:remote_file) { filesystem.path(:tmp_dir, mock_tmpdir, File.basename(local_file)) }
37
+
38
+ before do
39
+ FileUtils.touch(local_file)
40
+ filesystem.add_path(:tmp_dir, File.join(Dir.tmpdir, SecureRandom.hex))
41
+ allow(filesystem).to receive(:mktempdir!) { filesystem.path(:tmp_dir, mock_tmpdir) }
42
+
43
+ allow(delegate).to receive(:filesystem) { filesystem }
44
+ allow(delegate).to receive_message_chain(:configuration, :hive).and_return(configuration)
45
+ end
46
+
47
+ describe '#stdin' do
48
+ context 'with exec' do
49
+ let(:attrs) { {exec: %q(SELECT * FROM table;)} }
50
+ subject(:stdin) { instance.stdin }
51
+
52
+ it { is_expected.to be_a(StringIO) }
53
+
54
+ describe '#string' do
55
+ subject { stdin.string }
56
+ it { is_expected.to eq(%q(SELECT * FROM table;)) }
57
+ end
58
+ end
59
+ end
60
+
61
+ describe '#command_args' do
62
+ let(:default_command) { ['hive', '--database', 'default'] }
63
+
64
+ subject do
65
+ instance.command_args
66
+ end
67
+
68
+ it { is_expected.to eq(default_command) }
69
+
70
+ context 'with command attrs' do
71
+ let(:options) { [{'-d' => 'DATABASE=development'}] }
72
+ it { is_expected.to eq([*default_command, '-d', 'DATABASE=development']) }
73
+ end
74
+
75
+ context 'with file' do
76
+ let(:attrs) { {file: local_file} }
77
+ it { is_expected.to eq([*default_command, '-f', remote_file]) }
78
+ end
79
+
80
+ context 'with variables' do
81
+ let(:attrs) { {file: local_file, variables: {R: 'R2DO', C: 'C3PO'}} }
82
+ it { is_expected.to eq([*default_command, '-f', remote_file, '-d', 'R=R2DO', '-d', 'C=C3PO']) }
83
+ end
84
+
85
+ context 'with setup files' do
86
+ before do
87
+ filesystem.touch!('setup_a.hql', 'setup_b.hql')
88
+ end
89
+
90
+ let(:attrs) { {setup_files: ['setup_a.hql', 'setup_b.hql']} }
91
+ it { is_expected.to eq([*default_command, '-i', 'setup_a.hql', '-i', 'setup_b.hql']) }
92
+ end
93
+
94
+ context 'with template file' do
95
+ let(:attrs) { {file: 'zomg.hql.erb'} }
96
+ before do
97
+ expect(Masamune::Template).to receive(:render_to_file).with('zomg.hql.erb', {}).and_return('XXX')
98
+ expect_any_instance_of(Masamune::MockFilesystem).to receive(:copy_file_to_file)
99
+ end
100
+ it { is_expected.to eq([*default_command, '-f', filesystem.get_path(:tmp_dir, mock_tmpdir, 'zomg.hql')]) }
101
+ end
102
+ end
103
+
104
+ describe '#handle_stdout' do
105
+ let(:buffer) { StringIO.new }
106
+ let(:delimiter) { "\t" }
107
+ let(:attrs) { {buffer: buffer, delimiter: delimiter, csv: true} }
108
+ let(:input_row) { ['A', 'NULL', 'B', 'C', '', 'E'].join(delimiter) }
109
+ let(:output_row) { ['A', nil, 'B', 'C', nil, 'E'].join(',') }
110
+
111
+ before do
112
+ instance.handle_stdout(input_row, 0)
113
+ end
114
+
115
+ it { expect(buffer.string).to eq(output_row + "\n") }
116
+ end
117
+ end
@@ -0,0 +1,69 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Commands::PostgresAdmin do
26
+ let(:configuration) { {:create_db_path => 'createdb', :drop_db_path => 'dropdb', :hostname => 'localhost', :username => 'postgres'} }
27
+ let(:attrs) { {} }
28
+
29
+ let(:delegate) { double }
30
+ let(:instance) { described_class.new(delegate, attrs) }
31
+
32
+ before do
33
+ allow(delegate).to receive_message_chain(:configuration, :postgres).and_return({})
34
+ allow(delegate).to receive_message_chain(:configuration, :postgres_admin).and_return(configuration)
35
+ end
36
+
37
+ describe '#command_args' do
38
+ subject do
39
+ instance.command_args
40
+ end
41
+
42
+ context 'action :create with database' do
43
+ let(:attrs) { {action: :create, database: 'zombo'} }
44
+ it { is_expected.to eq(['createdb', '--host=localhost', '--username=postgres', '--no-password', 'zombo']) }
45
+ end
46
+
47
+ context 'action :create without database' do
48
+ let(:attrs) { {action: :create} }
49
+ it { expect { subject }.to raise_error ArgumentError, ':database must be given' }
50
+ end
51
+
52
+ context 'action :drop with database' do
53
+ let(:attrs) { {action: :drop, database: 'zombo'} }
54
+ it { is_expected.to eq(['dropdb', '--if-exists', '--host=localhost', '--username=postgres', '--no-password', 'zombo']) }
55
+ end
56
+
57
+ context 'action :drop without database' do
58
+ let(:attrs) { {action: :drop} }
59
+ it { expect { subject }.to raise_error ArgumentError, ':database must be given' }
60
+ end
61
+
62
+ context 'action unfuddle with database' do
63
+ let(:attrs) { {action: :unfuddle, database: 'zombo'} }
64
+ it { expect { subject }.to raise_error ArgumentError, ':action must be :create or :drop' }
65
+ end
66
+ end
67
+
68
+ it_should_behave_like Masamune::Commands::PostgresCommon
69
+ end
@@ -0,0 +1,100 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Commands::Postgres do
26
+ let(:configuration) { {:path => 'psql', :database => 'postgres', :options => options} }
27
+ let(:options) { [] }
28
+ let(:attrs) { {} }
29
+
30
+ let(:delegate) { double }
31
+ let(:instance) { described_class.new(delegate, attrs) }
32
+
33
+ before do
34
+ allow(delegate).to receive_message_chain(:configuration, :postgres).and_return(configuration)
35
+ end
36
+
37
+ describe '#stdin' do
38
+ context 'with input' do
39
+ let(:attrs) { {input: %q(SELECT * FROM table;)} }
40
+ subject(:stdin) { instance.stdin }
41
+
42
+ it { is_expected.to be_a(StringIO) }
43
+
44
+ describe '#string' do
45
+ subject { stdin.string }
46
+ it { is_expected.to eq(%q(SELECT * FROM table;)) }
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#command_args' do
52
+ let(:default_command) { ['psql', '--host=localhost', '--dbname=postgres', '--username=postgres', '--no-password'] }
53
+
54
+ subject do
55
+ instance.command_args
56
+ end
57
+
58
+ it { is_expected.to eq(default_command) }
59
+
60
+ context 'with options' do
61
+ let(:options) { [{'-A' => nil}] }
62
+ it { is_expected.to eq([*default_command, '-A']) }
63
+ end
64
+
65
+ context 'with file' do
66
+ let(:attrs) { {file: 'zomg.hql'} }
67
+ it { is_expected.to eq([*default_command, '--file=zomg.hql']) }
68
+ end
69
+
70
+ context 'with template file' do
71
+ let(:attrs) { {file: 'zomg.hql.erb'} }
72
+ before do
73
+ expect(Masamune::Template).to receive(:render_to_file).with('zomg.hql.erb', {}).and_return('zomg.hql')
74
+ end
75
+ it { is_expected.to eq([*default_command, '--file=zomg.hql']) }
76
+ end
77
+
78
+ context 'with variables and no file' do
79
+ let(:attrs) { {variables: {R: 'R2D2', C: 'C3PO'}} }
80
+ it { is_expected.to eq(default_command) }
81
+ end
82
+
83
+ context 'with variables and file' do
84
+ let(:attrs) { {file: 'zomg.hql', variables: {R: 'R2D2', C: 'C3PO'}} }
85
+ it { is_expected.to eq([*default_command, '--file=zomg.hql', %q(--set=R='R2D2'), %q(--set=C='C3PO')]) }
86
+ end
87
+
88
+ context 'with csv' do
89
+ let(:attrs) { {csv: true} }
90
+ it { is_expected.to eq([*default_command, '--no-align', '--field-separator=,', '--pset=footer']) }
91
+ end
92
+
93
+ context 'with tuple_output' do
94
+ let(:attrs) { {tuple_output: true} }
95
+ it { is_expected.to eq([*default_command, '--pset=tuples_only']) }
96
+ end
97
+ end
98
+
99
+ it_should_behave_like Masamune::Commands::PostgresCommon
100
+ end
@@ -0,0 +1,116 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Commands::RetryWithBackoff do
26
+ let(:options) { {retries: retries, backoff: 0} }
27
+ let(:delegate) { double }
28
+ let(:instance) { described_class.new(delegate, options) }
29
+
30
+ before do
31
+ allow(delegate).to receive(:logger).and_return(double)
32
+ allow(delegate).to receive(:configuration).and_return(double(retries: 0, backoff: 0))
33
+ end
34
+
35
+ describe '#around_execute' do
36
+ let(:retries) { 3 }
37
+
38
+ context 'when retry command fails with status but eventually succeeds' do
39
+ before do
40
+ expect(instance.logger).to receive(:error).with('exited with code: 42').exactly(retries - 1)
41
+ expect(instance.logger).to receive(:debug).with(/retrying.*/).exactly(retries - 1)
42
+ subject
43
+ end
44
+
45
+ subject do
46
+ @retry_count = 0
47
+ instance.around_execute do
48
+ @retry_count += 1
49
+ if @retry_count < retries
50
+ OpenStruct.new(:success? => false, :exitstatus => 42)
51
+ else
52
+ OpenStruct.new(:success? => true)
53
+ end
54
+ end
55
+ end
56
+
57
+ it 'logs useful debug and error messages' do; end
58
+ it 'attempts to retry the specified number of times' do
59
+ expect(@retry_count).to eq(retries)
60
+ end
61
+ it 'returns result status' do
62
+ is_expected.to be_success
63
+ end
64
+ end
65
+
66
+ context 'when retry command fails with exception but eventually succeeds' do
67
+ before do
68
+ expect(instance.logger).to receive(:error).with('wtf').exactly(retries - 1)
69
+ expect(instance.logger).to receive(:debug).with(/retrying.*/).exactly(retries - 1)
70
+ subject
71
+ end
72
+
73
+ subject do
74
+ @retry_count = 0
75
+ instance.around_execute do
76
+ @retry_count += 1
77
+ raise 'wtf' if @retry_count < retries
78
+ OpenStruct.new(:success? => true)
79
+ end
80
+ end
81
+
82
+ it 'logs useful debug and error messages' do; end
83
+ it 'attempts to retry the specified number of times' do
84
+ expect(@retry_count).to eq(retries)
85
+ end
86
+ it 'returns result status' do
87
+ is_expected.to be_success
88
+ end
89
+ end
90
+
91
+ context 'when retry command eventually fails' do
92
+ before do
93
+ expect(instance.logger).to receive(:error).with('wtf').exactly(retries + 1)
94
+ expect(instance.logger).to receive(:debug).with(/retrying.*/).exactly(retries)
95
+ expect(instance.logger).to receive(:debug).with(/max retries.*bailing/)
96
+ subject
97
+ end
98
+
99
+ subject do
100
+ @retry_count = 0
101
+ instance.around_execute do
102
+ @retry_count += 1
103
+ raise 'wtf'
104
+ end
105
+ end
106
+
107
+ it 'logs useful debug and error messages' do; end
108
+ it 'attempts to retry the specified number of times' do
109
+ expect(@retry_count).to eq(retries + 1)
110
+ end
111
+ it 'returns failure status' do
112
+ is_expected.not_to be_success
113
+ end
114
+ end
115
+ end
116
+ end