rake-terraform 0.0.8 → 0.2.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 (40) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +19 -0
  3. data/.rspec +5 -0
  4. data/.rubocop.yml +25 -0
  5. data/.travis.yml +1 -0
  6. data/README.md +93 -1
  7. data/Rakefile +22 -3
  8. data/lib/{rake_terraform.rb → rake-terraform.rb} +1 -0
  9. data/lib/rake-terraform/apply_task/config.rb +47 -0
  10. data/lib/rake-terraform/{tasks/applytask.rb → apply_task/task.rb} +21 -33
  11. data/lib/rake-terraform/applytask.rb +11 -0
  12. data/lib/rake-terraform/{tasks/basetask.rb → basetask.rb} +2 -1
  13. data/lib/rake-terraform/default_tasks.rb +5 -2
  14. data/lib/rake-terraform/dsl.rb +2 -2
  15. data/lib/rake-terraform/env_process.rb +142 -0
  16. data/lib/rake-terraform/errors.rb +4 -0
  17. data/lib/rake-terraform/plan_task/config.rb +61 -0
  18. data/lib/rake-terraform/plan_task/task.rb +51 -0
  19. data/lib/rake-terraform/plantask.rb +13 -0
  20. data/lib/rake-terraform/terraformcmd.rb +56 -0
  21. data/lib/rake-terraform/version.rb +1 -1
  22. data/rake-terraform.gemspec +3 -0
  23. data/spec/fixtures/envprocess_uniq_state_dir_var_invalid.env +3 -0
  24. data/spec/fixtures/envprocess_uniq_state_dir_var_valid.env +4 -0
  25. data/spec/fixtures/envprocess_uniq_state_false.env +2 -0
  26. data/spec/fixtures/envprocess_uniq_state_false_file_valid.env +3 -0
  27. data/spec/fixtures/envprocess_uniq_state_invalid_state_file_str.env +3 -0
  28. data/spec/fixtures/envprocess_uniq_state_missing_dir_var.env +4 -0
  29. data/spec/fixtures/envprocess_uniq_state_true.env +5 -0
  30. data/spec/fixtures/envprocess_uniq_state_true_both.env +6 -0
  31. data/spec/fixtures/envprocess_uniq_state_true_file_valid.env +3 -0
  32. data/spec/fixtures/set_all_variables_nil.env +8 -0
  33. data/spec/spec_helper.rb +86 -0
  34. data/spec/unit/applytask_spec.rb +112 -0
  35. data/spec/unit/basetask_spec.rb +10 -0
  36. data/spec/unit/envprocess_spec.rb +263 -0
  37. data/spec/unit/plantask_spec.rb +118 -0
  38. data/spec/unit/terraformcmd_spec.rb +174 -0
  39. metadata +90 -7
  40. data/lib/rake-terraform/tasks/plantask.rb +0 -87
@@ -0,0 +1,10 @@
1
+ require 'rake-terraform/basetask'
2
+
3
+ module RakeTerraform
4
+ describe BaseTask do
5
+ it 'should raise an ArgumentError with no arguments' do
6
+ expect { RakeTerraform::BaseTask.new }
7
+ .to raise_error(ArgumentError)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,263 @@
1
+ require 'spec_helper'
2
+ require 'rake-terraform/env_process'
3
+
4
+ module RakeTerraform
5
+ # envprocess unit tests
6
+ module EnvProcess
7
+ describe 'EnvProcess' do
8
+ before(:all) do
9
+ Dotenv.overload(
10
+ 'spec/fixtures/set_all_variables_nil.env'
11
+ )
12
+ end
13
+ # load the environment into instance variables
14
+ describe 'initialize' do
15
+ # wrapper class for envprocess methods
16
+ let(:test_class) do
17
+ Class.new do
18
+ include RakeTerraform::EnvProcess
19
+ def initialize
20
+ super
21
+ end
22
+ end
23
+ end
24
+ # instance of test class
25
+ let(:test_class_inst) { test_class.new }
26
+ context 'When an instance var is set through an environment var' do
27
+ it 'should not change if the environment variable changes' do
28
+ Dotenv.overload(
29
+ 'spec/fixtures/envprocess_uniq_state_false_file_valid.env'
30
+ ) do
31
+ expect(test_class_inst.unique_state).to eq(false)
32
+ end
33
+ Dotenv.overload(
34
+ 'spec/fixtures/envprocess_uniq_state_true_file_valid.env'
35
+ ) do
36
+ expect(test_class_inst.unique_state).to eq(false)
37
+ end
38
+ end
39
+ end
40
+ context 'When unique state and TERRAFORM_STATE_FILE are provided ' \
41
+ 'and valid' do
42
+ let(:state_file_str) { '/tmp/some_state.tfstate' }
43
+ Dotenv.overload(
44
+ 'spec/fixtures/envprocess_uniq_state_true_file_valid.env'
45
+ ) do
46
+ it 'should set state_file to the expected string' do
47
+ expect(test_class_inst.state_file).to eq(state_file_str)
48
+ end
49
+ it 'should set state_dir_var to nil' do
50
+ expect(test_class_inst.state_dir_var).to eq(nil)
51
+ end
52
+ it 'should set state_dir to nil' do
53
+ expect(test_class_inst.state_dir).to eq(nil)
54
+ end
55
+ end
56
+ end
57
+
58
+ context 'When unique state and TERRAFORM_STATE_DIR_VAR are provided ' \
59
+ 'and valid' do
60
+ let(:tf_state_dir_value) { 'terraform/state/staging' }
61
+ let(:tf_state_dir_var) { 'SOMETHING' }
62
+ let(:tf_state_file_path) do
63
+ "#{PROJECT_ROOT}/terraform/#{tf_state_dir_value}/terraform.tfstate"
64
+ end
65
+ Dotenv.overload(
66
+ 'spec/fixtures/envprocess_uniq_state_dir_var_valid.env'
67
+ ) do
68
+ it 'should set state_file to the expected string' do
69
+ expect(test_class_inst.state_file).to eq(tf_state_file_path)
70
+ end
71
+ it 'should set state_dir_var to "SOMETHING"' do
72
+ expect(test_class_inst.state_dir_var).to eq(tf_state_dir_var)
73
+ end
74
+ it 'should set state_dir to state/staging' do
75
+ expect(test_class_inst.state_dir).to eq(tf_state_dir_value)
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ # this parent method calls a bunch of the other ones - certain amount of
82
+ # coverage from those methods contained here
83
+ describe 'tf_unique_state' do
84
+ # wrapper class for envprocess methods
85
+ let(:test_class) { Class.new { include RakeTerraform::EnvProcess } }
86
+ # instance of test class
87
+ let(:test_class_inst) { test_class.new }
88
+ context 'when TERRAFORM_UNIQUE_STATE var is false' do
89
+ it 'should return false' do
90
+ Dotenv.overload('spec/fixtures/envprocess_uniq_state_false.env')
91
+ expect(test_class_inst.tf_unique_state).to eq(false)
92
+ end
93
+ end
94
+ context 'when TERRAFORM_UNIQUE_STATE is true but dependent ' \
95
+ 'variables are missing' do
96
+ it 'should raise ArgumentError when no other variables are given' do
97
+ Dotenv.overload('spec/fixtures/envprocess_uniq_state_true.env')
98
+ expect { test_class_inst.tf_unique_state }
99
+ .to raise_error(
100
+ ArgumentError,
101
+ /^Both or neither of TERRAFORM_STATE_FILE/
102
+ )
103
+ end
104
+ it 'should return an ArgumentError where the target for ' \
105
+ 'TERRAFORM_STATE_DIR_VAR is missing' do
106
+ Dotenv.overload(
107
+ 'spec/fixtures/envprocess_uniq_state_missing_dir_var.env'
108
+ )
109
+ expect { test_class_inst.tf_unique_state }
110
+ .to raise_error(
111
+ ArgumentError,
112
+ /^Both or neither of TERRAFORM_STATE_FILE/
113
+ )
114
+ end
115
+ end
116
+ context 'when TERRAFORM_UNIQUE_STATE is true but more than one' \
117
+ 'optional variable is given' do
118
+ it 'should return an ArgumentError' do
119
+ Dotenv.overload('spec/fixtures/envprocess_uniq_state_true_both.env')
120
+ expect { test_class_inst.tf_unique_state }
121
+ .to raise_error(
122
+ ArgumentError,
123
+ /^Both or neither of TERRAFORM_STATE_FILE/
124
+ )
125
+ end
126
+ end
127
+ context 'when TERRAFORM_UNIQUE_STATE is true but dependent ' \
128
+ 'variables are missing' do
129
+ it 'should return an ArgumentError' do
130
+ Dotenv.load('spec/fixtures/envprocess_uniq_state_true.env')
131
+ expect { test_class_inst.tf_unique_state }
132
+ .to raise_error(
133
+ ArgumentError,
134
+ /^Both or neither of TERRAFORM_STATE_FILE/
135
+ )
136
+ end
137
+ end
138
+ end
139
+
140
+ describe 'tf_state_file' do
141
+ # wrapper class for envprocess methods
142
+ let(:test_class) { Class.new { include RakeTerraform::EnvProcess } }
143
+ # instance of test class
144
+ let(:test_class_inst) { test_class.new }
145
+ let(:state_file_str) { '/tmp/some_state.tfstate' }
146
+ context 'when TERRAFORM_UNIQUE_STATE var is false but ' \
147
+ 'TERRAFORM_STATE_FILE is given' do
148
+ it 'should still return the content of a valid var' do
149
+ Dotenv.overload(
150
+ 'spec/fixtures/envprocess_uniq_state_false_file_valid.env'
151
+ )
152
+ expect(test_class_inst.tf_unique_state).to eq(false)
153
+ expect(test_class_inst.tf_state_file).to eq(state_file_str)
154
+ end
155
+ end
156
+ context 'when TERRAFORM_UNIQUE_STATE var is true and ' \
157
+ 'TERRAFORM_STATE_FILE is given' do
158
+ it 'should return the content of a valid var' do
159
+ Dotenv.overload(
160
+ 'spec/fixtures/envprocess_uniq_state_true_file_valid.env'
161
+ )
162
+ expect(test_class_inst.tf_unique_state).to eq(true)
163
+ expect(test_class_inst.tf_state_file).to eq(state_file_str)
164
+ end
165
+ end
166
+ context 'when TERRAFORM_STATE_FILE is _not_ given but ' \
167
+ 'TERRAFORM_STATE_DIR_VAR and target variable are valid' do
168
+ let(:tf_state_dir_value) { 'terraform/state/staging' }
169
+ let(:tf_state_file_path) do
170
+ "#{PROJECT_ROOT}/#{tf_state_dir_value}/terraform.tfstate"
171
+ end
172
+ it 'should return the full path to the calculated state file' do
173
+ Dotenv.overload(
174
+ 'spec/fixtures/envprocess_uniq_state_dir_var_valid.env'
175
+ )
176
+ expect(test_class_inst.tf_state_file).to eq(tf_state_file_path)
177
+ end
178
+ end
179
+ context 'when TERRAFORM_STATE_FILE is an invalid string' do
180
+ it 'should raise an ArgumentError' do
181
+ Dotenv.overload(
182
+ 'spec/fixtures/envprocess_uniq_state_invalid_state_file_str.env'
183
+ )
184
+ expect { test_class_inst.tf_state_file }
185
+ .to raise_error(
186
+ ArgumentError,
187
+ /^Argument for TERRAFORM_STATE_FILE is invalid/
188
+ )
189
+ end
190
+ end
191
+ end
192
+
193
+ # see also: tf_state_dir for the content of the target var
194
+ describe 'tf_state_dir_var' do
195
+ # wrapper class for envprocess methods
196
+ let(:test_class) { Class.new { include RakeTerraform::EnvProcess } }
197
+ # instance of test class
198
+ let(:test_class_inst) { test_class.new }
199
+ context 'when TERRAFORM_STATE_DIR_VAR is an invalid string' do
200
+ it 'should raise an ArgumentError' do
201
+ Dotenv.overload(
202
+ 'spec/fixtures/envprocess_uniq_state_dir_var_invalid.env'
203
+ )
204
+ expect { test_class_inst.tf_state_dir_var }
205
+ .to raise_error(
206
+ ArgumentError,
207
+ /^Argument for TERRAFORM_STATE_DIR_VAR is invalid/
208
+ )
209
+ end
210
+ end
211
+ end
212
+
213
+ describe 'tf_state_dir' do
214
+ # wrapper class for envprocess methods
215
+ let(:test_class) { Class.new { include RakeTerraform::EnvProcess } }
216
+ # instance of test class
217
+ let(:test_class_inst) { test_class.new }
218
+ let(:tf_state_dir_value) { 'terraform/state/staging' }
219
+ context 'when TERRAFORM_STATE_DIR_VAR points to a valid value' do
220
+ it 'should return the value ' do
221
+ Dotenv.overload(
222
+ 'spec/fixtures/envprocess_uniq_state_dir_var_valid.env'
223
+ )
224
+ expect(test_class_inst.tf_state_dir).to eq(tf_state_dir_value)
225
+ end
226
+ end
227
+ end
228
+
229
+ describe 'state_dir_full_path' do
230
+ # wrapper class for envprocess methods
231
+ let(:test_class) { Class.new { prepend RakeTerraform::EnvProcess } }
232
+ let(:test_class_inst) { test_class.new }
233
+ # instance of test class
234
+ let(:tf_environment) { 'terraform/eu-west-1' }
235
+ let(:tf_state_dir_value) { 'state/staging' }
236
+ let(:tf_state_file_path) do
237
+ "#{PROJECT_ROOT}/terraform/#{tf_state_dir_value}/terraform.tfstate"
238
+ end
239
+ context 'when tf_environment is "state/staging"' do
240
+ it 'should return the full path to the state file' do
241
+ Dotenv.overload(
242
+ 'spec/fixtures/envprocess_uniq_state_dir_var_valid.env'
243
+ )
244
+ expect(test_class_inst.state_dir_full_path)
245
+ .to eq(tf_state_file_path)
246
+ end
247
+ end
248
+ end
249
+
250
+ describe 'default_state_file_name' do
251
+ # wrapper class for envprocess methods
252
+ let(:test_class) { Class.new { include RakeTerraform::EnvProcess } }
253
+ # instance of test class
254
+ let(:test_class_inst) { test_class.new }
255
+ let(:default_state_file_name) { 'terraform.tfstate' }
256
+ it 'should return the value of the default state file name' do
257
+ expect(test_class_inst.default_state_file_name)
258
+ .to eq(default_state_file_name)
259
+ end
260
+ end
261
+ end
262
+ end
263
+ end
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+ require 'rake-terraform/plantask'
3
+
4
+ module RakeTerraform
5
+ module PlanTask
6
+ describe Config do
7
+ before(:all) do
8
+ Dotenv.overload(
9
+ 'spec/fixtures/set_all_variables_nil.env'
10
+ )
11
+ end
12
+ let(:non_existent_input_dir) { "#{PROJECT_ROOT}/non/existent/input/dir" }
13
+ let(:non_existent_output_file) do
14
+ "#{PROJECT_ROOT}/non/existent/output/file.tf"
15
+ end
16
+ let(:default_input_dir_str) { "#{PROJECT_ROOT}/terraform" }
17
+ let(:default_aws_project_str) { 'default' }
18
+ let(:default_output_file_str) do
19
+ "#{PROJECT_ROOT}/output/terraform/plan.tf"
20
+ end
21
+ let(:default_credentials_str) { File.expand_path('~/.aws/credentials') }
22
+ let(:default_opts_hash) do
23
+ {
24
+ input_dir: default_input_dir_str,
25
+ output_file: default_output_file_str,
26
+ credentials: default_credentials_str,
27
+ aws_project: default_aws_project_str,
28
+ unique_state: false,
29
+ state_file: nil
30
+ }
31
+ end
32
+
33
+ before(:each) do
34
+ # reset default config object before each test
35
+ @default_config = RakeTerraform::PlanTask::Config.new
36
+ end
37
+
38
+ it 'should initialize successfully with no arguments' do
39
+ expect { RakeTerraform::PlanTask::Config.new }.to_not raise_error
40
+ end
41
+
42
+ describe 'opts' do
43
+ Dotenv.overload(
44
+ 'spec/fixtures/set_all_variables_nil.env'
45
+ ) do
46
+ context 'with a default config object' do
47
+ it 'should return the expected default keys and values' do
48
+ default_opts_hash.keys.each do |key|
49
+ expect(@default_config.opts).to have_key(key)
50
+ expect(@default_config.opts[key]).to eq(default_opts_hash[key])
51
+ end
52
+ end
53
+
54
+ it 'should reflect new values when the config object is updated' do
55
+ @default_config.input_dir = non_existent_input_dir
56
+ @default_config.output_file = non_existent_output_file
57
+ expect(@default_config.opts[:input_dir])
58
+ .to eq(non_existent_input_dir)
59
+ expect(@default_config.opts[:output_file])
60
+ .to eq(non_existent_output_file)
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ describe 'input_dir' do
67
+ after(:all) do
68
+ Dotenv.overload(
69
+ 'spec/fixtures/set_all_variables_nil.env'
70
+ )
71
+ end
72
+ let(:tf_environment) { 'terraform/eu-west-1' }
73
+ let(:tf_state_dir_value) { "#{tf_environment}/state/staging" }
74
+ let(:tf_state_file_path) do
75
+ "#{PROJECT_ROOT}/#{tf_state_dir_value}/terraform.tfstate"
76
+ end
77
+ context 'with a default config object' do
78
+ it 'should set the input_dir to PROJECT_ROOT/terraform' do
79
+ expect(@default_config.input_dir).to eq(default_input_dir_str)
80
+ expect(@default_config.tf_environment).to eq(nil)
81
+ expect(@default_config.state_file).to eq(nil)
82
+ end
83
+ end
84
+ context 'when I set the input_dir to something else and state_dir ' \
85
+ 'is true' do
86
+ Dotenv.overload(
87
+ 'spec/fixtures/envprocess_uniq_state_dir_var_valid.env'
88
+ ) do
89
+ it 'should update input_dir, tf_environment and state_file' do
90
+ @default_config.input_dir = tf_environment
91
+ expect(@default_config.tf_environment).to eq(tf_environment)
92
+ expect(@default_config.state_file).to eq(tf_state_file_path)
93
+ end
94
+ end
95
+ end
96
+ context 'when I set the input_dir to something else and state_dir ' \
97
+ 'is false' do
98
+ Dotenv.overload(
99
+ 'spec/fixtures/envprocess_uniq_state_true_file_valid.env'
100
+ ) do
101
+ it 'should update input_dir, tf_environment but _not_ state_file' do
102
+ @default_config.input_dir = tf_environment
103
+ expect(@default_config.tf_environment).to eq(tf_environment)
104
+ expect(@default_config.state_file).to eq(nil)
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ describe Task do
112
+ it 'should raise an ArgumentError with no arguments' do
113
+ expect { RakeTerraform::PlanTask::Task.new }
114
+ .to raise_error(ArgumentError)
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,174 @@
1
+ require 'spec_helper'
2
+ require 'rake-terraform/terraformcmd'
3
+
4
+ module RakeTerraform
5
+ # mock kernel calls to terraform binary
6
+ module TerraformCmd
7
+ describe 'TerraformCmd' do
8
+ # wrapper class for terraformcmd methods
9
+ let(:test_class) { Class.new { include RakeTerraform::TerraformCmd } }
10
+ # instance of test class
11
+ let(:test_class_inst) { test_class.new }
12
+ before(:all) do
13
+ Dotenv.overload(
14
+ 'spec/fixtures/set_all_variables_nil.env'
15
+ )
16
+ end
17
+
18
+ describe 'tf_get' do
19
+ let(:get_cmd) { 'terraform get' }
20
+ let(:get_up_cmd) { 'terraform get -update' }
21
+ context 'with no arguments' do
22
+ it 'should call terraform get' do
23
+ expect(test_class_inst).to receive(:system)
24
+ .with(get_cmd)
25
+ test_class_inst.tf_get
26
+ end
27
+ end
28
+ context 'with update=true' do
29
+ it 'should call terraform get -update' do
30
+ expect(test_class_inst).to receive(:system)
31
+ .with(get_up_cmd)
32
+ test_class_inst.tf_get(true)
33
+ end
34
+ end
35
+ end
36
+
37
+ describe 'tf_plan' do
38
+ let(:default_plan_cmd) { 'terraform plan -module-depth 2' }
39
+ let(:default_output_file) { "#{PROJECT_ROOT}/output/terraform/plan.tf" }
40
+ let(:cred_plan_cmd) do
41
+ "terraform plan -module-depth 2 -var access_key=\"#{access_key}\"" \
42
+ " -var secret_key=\"#{secret_key}\""
43
+ end
44
+ let(:output_plan_cmd) do
45
+ "terraform plan -module-depth 2 -out #{default_output_file}"
46
+ end
47
+ let(:module_arg_cmd) { 'terraform plan -module-depth 56' }
48
+ let(:module_arg) { 56 }
49
+ let(:access_key) { 'BISFITPONHYWERBENTEIN' }
50
+ let(:secret_key) { 'trujRepGidjurivGomAyctyeOpVuWiuvafeeshjuo' }
51
+ let(:state_file) do
52
+ "#{PROJECT_ROOT}/terraform/test_env/state_1.tfstate"
53
+ end
54
+ let(:state_file_cmd) do
55
+ "terraform plan -module-depth 2 -state #{state_file}"
56
+ end
57
+ context 'with no arguments' do
58
+ it 'should call terraform plan' do
59
+ expect(test_class_inst).to receive(:system)
60
+ .with(default_plan_cmd)
61
+ test_class_inst.tf_plan
62
+ end
63
+ end
64
+ context 'with just an access_key' do
65
+ it 'should raise an ArgumentError' do
66
+ expect { test_class_inst.tf_plan(access_key) }
67
+ .to raise_error(ArgumentError,
68
+ 'Only one of access_key or secret_key given')
69
+ end
70
+ end
71
+ context 'with just a secret_key' do
72
+ it 'should raise an ArgumentError' do
73
+ expect { test_class_inst.tf_plan(nil, secret_key) }
74
+ .to raise_error(ArgumentError,
75
+ 'Only one of access_key or secret_key given')
76
+ end
77
+ end
78
+ context 'with both an access_key and a secret_key' do
79
+ it 'should call terraform plan with those vars configured' do
80
+ expect(test_class_inst).to receive(:system)
81
+ .with(cred_plan_cmd)
82
+ test_class_inst.tf_plan(access_key, secret_key)
83
+ end
84
+ end
85
+ context 'with an output file' do
86
+ it 'should call terraform plan with an output file' do
87
+ expect(test_class_inst).to receive(:system)
88
+ .with(output_plan_cmd)
89
+ test_class_inst.tf_plan(nil, nil, default_output_file)
90
+ end
91
+ end
92
+ context 'with an state file' do
93
+ it 'should call terraform plan with a state file' do
94
+ expect(test_class_inst).to receive(:system)
95
+ .with(state_file_cmd)
96
+ test_class_inst.tf_plan(nil, nil, nil, state_file)
97
+ end
98
+ end
99
+ context 'where module_depth is given as an argument' do
100
+ it 'should call terraform plan with updated module-depth argument' do
101
+ expect(test_class_inst).to receive(:system)
102
+ .with(module_arg_cmd)
103
+ test_class_inst.tf_plan(nil, nil, nil, nil, module_arg)
104
+ end
105
+ end
106
+ end
107
+
108
+ describe 'tf_show' do
109
+ let(:default_plan_file) { "#{PROJECT_ROOT}/output/terraform/plan.tf" }
110
+ let(:default_show_cmd) do
111
+ "terraform show -module-depth 2 #{default_plan_file}"
112
+ end
113
+ let(:module_arg_cmd) do
114
+ "terraform show -module-depth 56 #{default_plan_file}"
115
+ end
116
+ let(:module_arg) { 56 }
117
+ context 'with no arguments' do
118
+ it 'should raise an ArgumentError' do
119
+ expect { test_class_inst.tf_show }
120
+ .to raise_error(ArgumentError)
121
+ end
122
+ end
123
+ context 'with a plan file argument' do
124
+ it 'should call terraform show with the plan file' do
125
+ expect(test_class_inst).to receive(:system)
126
+ .with(default_show_cmd)
127
+ test_class_inst.tf_show(default_plan_file)
128
+ end
129
+ end
130
+ context 'where module_depth is given as an argument' do
131
+ it 'should call terraform show with updated module-depth argument' do
132
+ expect(test_class_inst).to receive(:system)
133
+ .with(module_arg_cmd)
134
+ test_class_inst.tf_show(default_plan_file, module_arg)
135
+ end
136
+ end
137
+ end
138
+
139
+ describe 'tf_apply' do
140
+ let(:default_plan_file) { "#{PROJECT_ROOT}/output/terraform/plan.tf" }
141
+ let(:default_apply_cmd) do
142
+ "terraform apply #{default_plan_file}"
143
+ end
144
+ let(:state_file) do
145
+ "#{PROJECT_ROOT}/terraform/test_env/state_1.tfstate"
146
+ end
147
+ let(:state_file_cmd) do
148
+ 'terraform apply -state ' \
149
+ "#{state_file} #{default_plan_file}"
150
+ end
151
+ context 'with no arguments' do
152
+ it 'should raise an ArgumentError' do
153
+ expect { test_class_inst.tf_apply }
154
+ .to raise_error(ArgumentError)
155
+ end
156
+ end
157
+ context 'with a plan file argument' do
158
+ it 'should call terraform apply with the plan file' do
159
+ expect(test_class_inst).to receive(:system)
160
+ .with(default_apply_cmd)
161
+ test_class_inst.tf_apply(default_plan_file)
162
+ end
163
+ end
164
+ context 'where state file is given as an argument' do
165
+ it 'should call terraform apply with a state file argument' do
166
+ expect(test_class_inst).to receive(:system)
167
+ .with(state_file_cmd)
168
+ test_class_inst.tf_apply(default_plan_file, state_file)
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end