masamune 0.11.0

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 (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::S3Cmd 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, :s3cmd).and_return(configuration)
35
+ end
36
+
37
+ describe '#command_args' do
38
+ let(:attrs) { {extra: ['ls', 's3://fake']} }
39
+
40
+ subject { instance.command_args }
41
+
42
+ it { is_expected.to eq(['s3cmd', 'ls', 's3://fake']) }
43
+
44
+ context 'with options' do
45
+ let(:options) { [{'--config' => '/opt/etc/etl/s3cfg'}] }
46
+
47
+ it { is_expected.to eq(['s3cmd', '--config', '/opt/etc/etl/s3cfg', 'ls', 's3://fake']) }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,101 @@
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::Shell do
26
+ let(:input) { nil }
27
+ let(:options) { {fail_fast: false} }
28
+ let(:delegate) { Masamune::MockDelegate.new(command, input) }
29
+ let(:instance) { described_class.new(delegate, options) }
30
+
31
+ describe '#execute' do
32
+ subject do
33
+ instance.execute
34
+ end
35
+
36
+ context 'with simple command that succeeds' do
37
+ let(:command) { %Q{bash -c "echo 'stdout 1'; echo 'stderr 1' 1>&2; echo 'stdout 2'; echo 'stderr 2' 1>&2"} }
38
+
39
+ before do
40
+ subject
41
+ end
42
+
43
+ it { expect(delegate.status).to eq(0) }
44
+ it { expect(delegate.stdout).to eq(['stdout 1', 'stdout 2']) }
45
+ it { expect(delegate.stderr).to eq(['stderr 1', 'stderr 2']) }
46
+ end
47
+
48
+ context 'with simple command that fails' do
49
+ let(:command) { %Q{bash -c 'exit 1'} }
50
+
51
+ before do
52
+ subject
53
+ end
54
+
55
+ it { expect(delegate.status).to eq(1) }
56
+ it { expect(delegate.stdout).to eq([]) }
57
+ it { expect(delegate.stderr).to eq([]) }
58
+ end
59
+
60
+ context 'with fail_fast and simple command that fails' do
61
+ let(:command) { %Q{bash -c 'exit 1'} }
62
+ let(:options) { {fail_fast: true} }
63
+ it { expect { subject }.to raise_error RuntimeError, "fail_fast: #{command}" }
64
+ end
65
+
66
+ context 'when command is interrupted' do
67
+ let(:command) { %Q{bash -c "echo 'test'"} }
68
+
69
+ before do
70
+ expect(delegate).to receive(:after_execute) { raise Interrupt }
71
+ subject
72
+ end
73
+
74
+ it { expect(delegate.status).to eq(130) }
75
+ end
76
+
77
+ context 'with simple command with input' do
78
+ let(:command) { 'cat' }
79
+ let(:input) { "ping\npong" }
80
+
81
+ before do
82
+ subject
83
+ end
84
+
85
+ it { expect(delegate.status).to eq(0) }
86
+ it { expect(delegate.stdout).to eq(['ping', 'pong']) }
87
+ it { expect(delegate.stderr).to eq([]) }
88
+ end
89
+
90
+ context 'with simple command with not-ready input' do
91
+ let(:command) { 'cat' }
92
+ let(:input) { "ping\npong" }
93
+
94
+ before do
95
+ expect_any_instance_of(IO).to receive(:wait_writable).and_return(nil)
96
+ end
97
+
98
+ it { expect { subject }.to raise_error RuntimeError, /IO stdin not ready/ }
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,102 @@
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::Configuration do
26
+ let(:environment) { Masamune::Environment.new }
27
+ let(:instance) { described_class.new(environment) }
28
+
29
+ describe '.default_config_file' do
30
+ subject { described_class.default_config_file }
31
+ it { is_expected.to match(%r{config/masamune\.yml\.erb\Z}) }
32
+ end
33
+
34
+ describe '#default_config_file' do
35
+ subject { instance.default_config_file }
36
+ it { is_expected.to match(%r{config/masamune\.yml\.erb\Z}) }
37
+ end
38
+
39
+ describe '#bind_template' do
40
+ let(:section) { nil }
41
+ let(:template) { nil }
42
+ let(:params) { nil }
43
+
44
+ subject do
45
+ instance.bind_template(section, template, params)
46
+ end
47
+
48
+ context 'with invalid template section' do
49
+ let(:section) { :missing_section }
50
+ it { expect { subject }.to raise_error(ArgumentError) }
51
+ end
52
+
53
+ context 'when template section is missing' do
54
+ let(:section) { :elastic_mapreduce }
55
+ it { expect { subject }.to raise_error(ArgumentError) }
56
+ end
57
+
58
+ context 'with valid template section' do
59
+ let(:section) { :elastic_mapreduce }
60
+ before do
61
+ instance.elastic_mapreduce[:templates] = {
62
+ list_with_state: {
63
+ command: '--list --key-pair %key_pair --start %state --verbose=%verbose',
64
+ default: {
65
+ state: 'RUNNING',
66
+ verbose: true
67
+ }
68
+ },
69
+ broken_template: nil
70
+ }
71
+ end
72
+
73
+ context 'when template is missing' do
74
+ let(:template) { :missing_template }
75
+ it { expect { subject }.to raise_error(ArgumentError, 'no template for missing_template') }
76
+ end
77
+
78
+ context 'when template is broken' do
79
+ let(:template) { :broken_template }
80
+ it { expect { subject }.to raise_error(ArgumentError, 'no template for broken_template') }
81
+ end
82
+
83
+ context 'when params missing but default exists' do
84
+ let(:template) { :list_with_state }
85
+ let(:params) { {key_pair: 'emr-2013'} }
86
+ it { is_expected.to eq(['--list', '--key-pair', 'emr-2013', '--start', 'RUNNING', '--verbose=true']) }
87
+ end
88
+
89
+ context 'when params missing and default missing' do
90
+ let(:template) { :list_with_state }
91
+ let(:params) { {state: 'COMPLETED'} }
92
+ it { expect { subject }.to raise_error(ArgumentError, 'no param for %key_pair') }
93
+ end
94
+
95
+ context 'with params' do
96
+ let(:template) { :list_with_state }
97
+ let(:params) { {key_pair: 'emr-2013', state: 'COMPLETED'} }
98
+ it { is_expected.to eq(['--list', '--key-pair', 'emr-2013', '--start', 'COMPLETED', '--verbose=true']) }
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,91 @@
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::DataPlan::Builder do
26
+ describe '#build' do
27
+ subject(:engine) do
28
+ described_class.instance.build(namespaces, commands, sources, targets)
29
+ end
30
+
31
+ context 'with multiple namespaces' do
32
+ let(:namespaces) { ['a', 'b'] }
33
+ let(:commands) { ['load', 'store'] }
34
+ let(:sources) { [{ path: 'log/%Y%m%d.*.log' }, { path: 'table/y=%Y/m=%m/d=%d' }] }
35
+ let(:targets) { [{ path: 'table/y=%Y/m=%m/d=%d' }, { path: 'daily/%Y-%m-%d' }] }
36
+
37
+ before do
38
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_target_rule).with('a:load', { path: 'table/y=%Y/m=%m/d=%d'})
39
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_source_rule).with('a:load', { path: 'log/%Y%m%d.*.log' })
40
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_command_rule).with('a:load', an_instance_of(Proc))
41
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_target_rule).with('b:store', { path: 'daily/%Y-%m-%d' })
42
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_source_rule).with('b:store', { path: 'table/y=%Y/m=%m/d=%d' })
43
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_command_rule).with('b:store', an_instance_of(Proc))
44
+ subject
45
+ end
46
+
47
+ it 'should build a Masamune::DataPlan::Engine instance' do; end
48
+ end
49
+
50
+ context 'with :for option' do
51
+ let(:namespaces) { ['a', 'a', 'a'] }
52
+ let(:commands) { ['missing_before', 'override', 'missing_after'] }
53
+ let(:sources) { [{ path: 'log/%Y%m%d.*.log', for: 'override' }] }
54
+ let(:targets) { [{ path: 'table/y=%Y/m=%m/d=%d', for: 'override' }] }
55
+
56
+ before do
57
+ expect_any_instance_of(Masamune::DataPlan::Engine).not_to receive(:add_target_rule).with('a:missing_before', anything)
58
+ expect_any_instance_of(Masamune::DataPlan::Engine).not_to receive(:add_source_rule).with('a:missing_before', anything)
59
+ expect_any_instance_of(Masamune::DataPlan::Engine).not_to receive(:add_command_rule).with('a:missing_before', an_instance_of(Proc))
60
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_target_rule).with('a:override', { path: 'table/y=%Y/m=%m/d=%d' })
61
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_source_rule).with('a:override', { path: 'log/%Y%m%d.*.log' })
62
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_command_rule).with('a:override', an_instance_of(Proc))
63
+ expect_any_instance_of(Masamune::DataPlan::Engine).not_to receive(:add_target_rule).with('a:missing_after', anything)
64
+ expect_any_instance_of(Masamune::DataPlan::Engine).not_to receive(:add_source_rule).with('a:missing_after', anything)
65
+ expect_any_instance_of(Masamune::DataPlan::Engine).not_to receive(:add_command_rule).with('a:missing_after', an_instance_of(Proc))
66
+ subject
67
+ end
68
+
69
+ it 'should build a Masamune::DataPlan::Engine instance' do; end
70
+ end
71
+
72
+ context 'with :skip option' do
73
+ let(:namespaces) { ['a', 'a', 'a'] }
74
+ let(:commands) { ['missing_before', 'override'] }
75
+ let(:sources) { [{ skip: true}, { path: 'log/%Y%m%d.*.log' }] }
76
+ let(:targets) { [{ skip: true}, { path: 'table/y=%Y/m=%m/d=%d' }] }
77
+
78
+ before do
79
+ expect_any_instance_of(Masamune::DataPlan::Engine).not_to receive(:add_target_rule).with('a:missing_before', anything)
80
+ expect_any_instance_of(Masamune::DataPlan::Engine).not_to receive(:add_source_rule).with('a:missing_before', anything)
81
+ expect_any_instance_of(Masamune::DataPlan::Engine).not_to receive(:add_command_rule).with('a:missing_before', an_instance_of(Proc))
82
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_target_rule).with('a:override', { path: 'table/y=%Y/m=%m/d=%d' })
83
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_source_rule).with('a:override', { path: 'log/%Y%m%d.*.log' })
84
+ expect_any_instance_of(Masamune::DataPlan::Engine).to receive(:add_command_rule).with('a:override', an_instance_of(Proc))
85
+ subject
86
+ end
87
+
88
+ it 'should build a Masamune::DataPlan::Engine instance' do; end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,102 @@
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::DataPlan::Elem do
26
+ let(:engine) { Masamune::DataPlan::Engine.new }
27
+ let(:name) { 'primary' }
28
+ let(:type) { :target }
29
+ let(:rule) { Masamune::DataPlan::Rule.new(engine, name, type, {path: 'report/%Y-%m-%d/%H'}) }
30
+ let(:other_rule) { Masamune::DataPlan::Rule.new(engine, name, type, {path: 'log/%Y%m%d.*.log'}) }
31
+
32
+ let(:start_time) { DateTime.civil(2013,07,19,11,07) }
33
+ let(:other_start_time) { DateTime.civil(2013,07,20,0,0) }
34
+
35
+ let(:options) { {tz: 'EST'} }
36
+ let(:other_options) { {tz: 'PST'} }
37
+
38
+ let(:instance) { described_class.new(rule, start_time, options) }
39
+
40
+ describe '#path' do
41
+ subject do
42
+ instance.path
43
+ end
44
+ it { is_expected.to eq('report/2013-07-19/11') }
45
+ end
46
+
47
+ describe '#==' do
48
+ subject do
49
+ instance == other
50
+ end
51
+
52
+ context 'when rule, options, and start_time match' do
53
+ let(:other) { described_class.new(rule, start_time, options) }
54
+ it { is_expected.to eq(true) }
55
+ it 'should have same hash' do
56
+ expect(instance.hash).to eq(other.hash)
57
+ end
58
+ end
59
+
60
+ context 'when rules differ' do
61
+ let(:other) { described_class.new(other_rule, start_time) }
62
+ it { is_expected.to eq(false) }
63
+ end
64
+
65
+ context 'when options differ' do
66
+ let(:other) { described_class.new(rule, start_time, other_options) }
67
+ it { is_expected.to eq(false) }
68
+ end
69
+
70
+ context 'when start_times differ' do
71
+ let(:other) { described_class.new(rule, other_start_time) }
72
+ it { is_expected.to eq(false) }
73
+ end
74
+ end
75
+
76
+ describe '#last_modified_at' do
77
+ let(:early) { Time.parse("2014-05-01 00:00:00 +0000") }
78
+ let(:later) { Time.parse("2014-06-01 00:00:00 +0000") }
79
+
80
+ subject do
81
+ instance.last_modified_at.utc
82
+ end
83
+
84
+ context 'with missing mtime' do
85
+ before do
86
+ expect(rule.engine.filesystem).to receive(:stat).with(instance.path).
87
+ and_return(nil)
88
+ end
89
+
90
+ it { is_expected.to eq(Masamune::DataPlan::Elem::MISSING_MODIFIED_AT) }
91
+ end
92
+
93
+ context 'with single mtime' do
94
+ before do
95
+ expect(rule.engine.filesystem).to receive(:stat).with(instance.path).
96
+ and_return(OpenStruct.new(mtime: early))
97
+ end
98
+
99
+ it { is_expected.to eq(early) }
100
+ end
101
+ end
102
+ end