summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/parsers/terraform/tfplan_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/ci/parsers/terraform/tfplan_spec.rb')
-rw-r--r--spec/lib/gitlab/ci/parsers/terraform/tfplan_spec.rb72
1 files changed, 54 insertions, 18 deletions
diff --git a/spec/lib/gitlab/ci/parsers/terraform/tfplan_spec.rb b/spec/lib/gitlab/ci/parsers/terraform/tfplan_spec.rb
index 19cd75e586c..fec27c0f31a 100644
--- a/spec/lib/gitlab/ci/parsers/terraform/tfplan_spec.rb
+++ b/spec/lib/gitlab/ci/parsers/terraform/tfplan_spec.rb
@@ -8,7 +8,7 @@ describe Gitlab::Ci::Parsers::Terraform::Tfplan do
let(:reports) { Gitlab::Ci::Reports::TerraformReports.new }
- context 'when data is tfplan.json' do
+ context 'when data is invalid' do
context 'when there is no data' do
it 'raises an error' do
plan = '{}'
@@ -19,31 +19,67 @@ describe Gitlab::Ci::Parsers::Terraform::Tfplan do
end
end
- context 'when there is data' do
- it 'parses JSON and returns a report' do
- plan = '{ "create": 0, "update": 1, "delete": 0 }'
+ context 'when data is not a JSON file' do
+ it 'raises an error' do
+ plan = { 'create' => 0, 'update' => 1, 'delete' => 0 }.to_s
- expect { subject.parse!(plan, reports, artifact: artifact) }.not_to raise_error
+ expect { subject.parse!(plan, reports, artifact: artifact) }.to raise_error(
+ described_class::TfplanParserError
+ )
+ end
+ end
- expect(reports.plans).to match(
- a_hash_including(
- 'tfplan.json' => a_hash_including(
- 'create' => 0,
- 'update' => 1,
- 'delete' => 0
- )
- )
+ context 'when JSON is missing a required key' do
+ it 'raises an error' do
+ plan = '{ "wrong_key": 1 }'
+
+ expect { subject.parse!(plan, reports, artifact: artifact) }.to raise_error(
+ described_class::TfplanParserError
)
end
end
end
- context 'when data is not tfplan.json' do
- it 'raises an error' do
- plan = { 'create' => 0, 'update' => 1, 'delete' => 0 }.to_s
+ context 'when data is valid' do
+ it 'parses JSON and returns a report' do
+ plan = '{ "create": 0, "update": 1, "delete": 0 }'
+
+ expect { subject.parse!(plan, reports, artifact: artifact) }.not_to raise_error
- expect { subject.parse!(plan, reports, artifact: artifact) }.to raise_error(
- described_class::TfplanParserError
+ reports.plans.each do |key, hash_value|
+ expect(hash_value.keys).to match_array(%w[create delete job_name job_path update])
+ end
+
+ expect(reports.plans).to match(
+ a_hash_including(
+ artifact.job.id.to_s => a_hash_including(
+ 'create' => 0,
+ 'update' => 1,
+ 'delete' => 0,
+ 'job_name' => artifact.job.options.dig(:artifacts, :name).to_s
+ )
+ )
+ )
+ end
+
+ it 'parses JSON when extra keys are present' do
+ plan = '{ "create": 0, "update": 1, "delete": 0, "extra_key": 4 }'
+
+ expect { subject.parse!(plan, reports, artifact: artifact) }.not_to raise_error
+
+ reports.plans.each do |key, hash_value|
+ expect(hash_value.keys).to match_array(%w[create delete job_name job_path update])
+ end
+
+ expect(reports.plans).to match(
+ a_hash_including(
+ artifact.job.id.to_s => a_hash_including(
+ 'create' => 0,
+ 'update' => 1,
+ 'delete' => 0,
+ 'job_name' => artifact.job.options.dig(:artifacts, :name).to_s
+ )
+ )
)
end
end