Class: Pangea::Magma::Matchers::PlanCleanlyUnderMagma

Inherits:
Object
  • Object
show all
Defined in:
lib/pangea/magma/matchers.rb

Instance Method Summary collapse

Constructor Details

#initializePlanCleanlyUnderMagma

Returns a new instance of PlanCleanlyUnderMagma.



37
38
39
40
41
# File 'lib/pangea/magma/matchers.rb', line 37

def initialize
  @required_providers = []
  @min_resource_changes = nil
  @resource_types = []
end

Instance Method Details

#descriptionObject



114
115
116
117
118
119
120
# File 'lib/pangea/magma/matchers.rb', line 114

def description
  desc = +"plan cleanly under magma"
  desc << " with providers #{@required_providers.inspect}" unless @required_providers.empty?
  desc << " emitting ≥#{@min_resource_changes} resource_changes" if @min_resource_changes
  desc << " declaring resource types #{@resource_types.inspect}" unless @resource_types.empty?
  desc
end

#failure_messageObject



106
107
108
# File 'lib/pangea/magma/matchers.rb', line 106

def failure_message
  @failure_message || "expected workspace to plan cleanly under magma"
end

#failure_message_when_negatedObject



110
111
112
# File 'lib/pangea/magma/matchers.rb', line 110

def failure_message_when_negated
  "expected workspace not to plan cleanly under magma, but it did:\n#{@report.inspect}"
end

#matches?(workspace_path) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pangea/magma/matchers.rb', line 67

def matches?(workspace_path)
  unless Pangea::Magma.installed?
    @failure_message = "magma binary not installed (set MAGMA_BINARY or install)"
    return false
  end
  @report = Pangea::Magma.verify_workspace(workspace_path)

  unless @report.dig('compatibility', 'plans_cleanly')
    @failure_message = "workspace #{workspace_path} did not plan cleanly: #{@report.inspect}"
    return false
  end

  @required_providers.each do |source|
    unless Array(@report['providers']).include?(source)
      @failure_message = "expected provider #{source.inspect}; got #{@report['providers']}"
      return false
    end
  end

  if @min_resource_changes
    actual = @report['resource_change_count'].to_i
    if actual < @min_resource_changes
      @failure_message = "expected ≥#{@min_resource_changes} resource changes; got #{actual}"
      return false
    end
  end

  @resource_types.each do |type|
    unless Array(@report['resource_types']).include?(type)
      @failure_message = "expected resource type #{type.inspect}; got #{@report['resource_types']}"
      return false
    end
  end
  true
rescue Pangea::Magma::VerificationFailed => e
  @failure_message = "magma fixture verify raised: #{e.message}"
  false
end

#resource_changesObject

No-op terminator — lets the chain read ‘.with_at_least(3).resource_changes` naturally.



57
58
59
# File 'lib/pangea/magma/matchers.rb', line 57

def resource_changes
  self
end

#with_at_least(n) ⇒ Object

Alias to read more naturally before ‘.resource_changes`



50
51
52
53
# File 'lib/pangea/magma/matchers.rb', line 50

def with_at_least(n)
  @min_resource_changes = n
  self
end

#with_provider(source) ⇒ Object

Returns self — chains: ‘with_provider(’hashicorp/aws’)‘.

Returns:

  • self — chains: ‘with_provider(’hashicorp/aws’)‘



44
45
46
47
# File 'lib/pangea/magma/matchers.rb', line 44

def with_provider(source)
  @required_providers << source
  self
end

#with_resource_type(type) ⇒ Object

Returns self — chains: ‘with_resource_type(’aws_vpc’)‘.

Returns:

  • self — chains: ‘with_resource_type(’aws_vpc’)‘



62
63
64
65
# File 'lib/pangea/magma/matchers.rb', line 62

def with_resource_type(type)
  @resource_types << type
  self
end