Module: Pangea::Testing::SynthesisTestHelpers

Defined in:
lib/pangea/testing/synthesis_test_helpers.rb

Overview

Shared test helpers for synthesis validation across all Pangea provider gems. Include this module in your RSpec configuration to get access to synthesizer creation, Terraform structure validation, and resource assertion helpers.

Instance Method Summary collapse

Instance Method Details

#cleanup_test_resourcesObject



142
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 142

def cleanup_test_resources; end

#create_synthesizerObject

Create a new TerraformSynthesizer instance, falling back to mock



39
40
41
42
43
44
45
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 39

def create_synthesizer
  if defined?(TerraformSynthesizer)
    TerraformSynthesizer.new
  else
    MockTerraformSynthesizer.new
  end
end

#normalize_synthesis(result) ⇒ Object

Normalize synthesis result to string keys via JSON round-trip



48
49
50
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 48

def normalize_synthesis(result)
  JSON.parse(result.to_json)
end

#reset_terraform_synthesizer_stateObject



141
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 141

def reset_terraform_synthesizer_state; end

#synthesize_and_validate(entity_type = :resource, normalize: false, &block) ⇒ Object

Synthesize and validate Terraform configuration. Optionally normalizes symbol keys to strings (for TerraformSynthesizer compatibility).



28
29
30
31
32
33
34
35
36
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 28

def synthesize_and_validate(entity_type = :resource, normalize: false, &block)
  synthesizer = create_synthesizer
  synthesizer.instance_eval(&block)
  result = synthesizer.synthesis
  result = normalize_synthesis(result) if normalize

  validate_terraform_structure(result, entity_type)
  result
end

#validate_dependency_ordering(result) ⇒ Object

Validate Terraform dependencies and ordering



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 129

def validate_dependency_ordering(result)
  resources = result['resource'] || {}
  dependencies = extract_dependencies(resources)

  dependencies.each do |resource_id, deps|
    deps.each do |dep|
      expect(dependencies).to have_key(dep),
        "Dependency '#{dep}' referenced by '#{resource_id}' is not defined"
    end
  end
end

#validate_provider_configuration(result, provider_name) ⇒ Object

Validate Terraform provider configuration



95
96
97
98
99
100
101
102
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 95

def validate_provider_configuration(result, provider_name)
  return unless result.key?('provider')

  expect(result['provider']).to have_key(provider_name)
  provider_config = result['provider'][provider_name]
  expect(provider_config).to be_a(Hash)
  provider_config
end

#validate_required_attributes(resource_config, required_attributes) ⇒ Object

Validate that required attributes are present



121
122
123
124
125
126
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 121

def validate_required_attributes(resource_config, required_attributes)
  required_attributes.each do |attr_name|
    expect(resource_config).to have_key(attr_name.to_s),
      "Required attribute '#{attr_name}' is missing"
  end
end

#validate_resource_attributes(resource_config, expected_attributes) ⇒ Object

Validate that resource attributes match expected types



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 105

def validate_resource_attributes(resource_config, expected_attributes)
  expected_attributes.each do |attr_name, attr_type|
    next unless resource_config.key?(attr_name.to_s)

    value = resource_config[attr_name.to_s]
    case attr_type
    when String  then expect(value).to be_a(String)
    when Integer then expect(value).to be_a(Integer)
    when TrueClass, FalseClass then expect([true, false]).to include(value)
    when Array   then expect(value).to be_a(Array)
    when Hash    then expect(value).to be_a(Hash)
    end
  end
end

#validate_resource_references(result) ⇒ Object

Validate resource references in generated Terraform



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 70

def validate_resource_references(result)
  terraform_json = result.to_json
  reference_pattern = /\$\{[a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]*\}/

  references = terraform_json.scan(reference_pattern)
  references.each do |ref|
    expect(ref).to match(reference_pattern)
  end

  references
end

#validate_resource_structure(result, resource_type, resource_name) ⇒ Object

Validate a specific resource exists in synthesis output



83
84
85
86
87
88
89
90
91
92
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 83

def validate_resource_structure(result, resource_type, resource_name)
  expect(result).to have_key('resource')
  expect(result['resource']).to have_key(resource_type)
  expect(result['resource'][resource_type]).to have_key(resource_name)

  resource_config = result['resource'][resource_type][resource_name]
  expect(resource_config).to be_a(Hash)

  resource_config
end

#validate_terraform_structure(result, entity_type) ⇒ Object

Validate basic Terraform JSON structure



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pangea/testing/synthesis_test_helpers.rb', line 53

def validate_terraform_structure(result, entity_type)
  expect(result).to be_a(Hash)

  case entity_type
  when :resource
    expect(result).to have_key('resource')
    expect(result['resource']).to be_a(Hash)
  when :data_source
    expect(result).to have_key('data')
    expect(result['data']).to be_a(Hash)
  when :output
    expect(result).to have_key('output')
    expect(result['output']).to be_a(Hash)
  end
end