Class: RightScaleSelfService::Test::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/rightscale_selfservice/test/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ Template

Returns a new instance of Template.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rightscale_selfservice/test/template.rb', line 33

def initialize(filepath)
  @name = File.basename(filepath)
  self.api_responses = {}
  self.state = "initialized"
  self.errors = []
  self.cases = []
  self.template_string = template_str = RightScaleSelfService::Utilities::Template.preprocess(filepath)
  test_config = {}
  if template_str.include?('#test:compile_only=true')
    self.cases = [RightScaleSelfService::Test::Case.new(:compile_only)]
    self.state = "running"
  else
    execution_state_matches = template_str.match(/^#test:execution_state=(?<state>[0-9a-zA-Z ]*)/)
    execution_state = 'running'
    if execution_state_matches && execution_state_matches.names.include?('state')
      execution_state = execution_state_matches['state']
    end
    options = {:state => execution_state}
    alt_state_matches = template_str.match(/^#test:execution_alternate_state=(?<state>[0-9a-zA-Z ]*)/)
    if alt_state_matches && alt_state_matches.size > 0
      options[:alternate_state] = alt_state_matches['state']
    end
    self.cases << RightScaleSelfService::Test::Case.new(:execution, options)

    template_str.scan(/(#test_operation:.*?)\noperation ["'](.*?)["'] do/m).each do |operation|
      tags = operation[0]
      operation_name = operation[1]

      options = {:operation_name => operation_name}

      execution_state_matches = tags.match(/^#test_operation:execution_state=(?<state>[0-9a-zA-Z ]*)/)
      if execution_state_matches && execution_state_matches.names.include?('state')
        options[:state] = execution_state_matches['state']
      end

      alt_state_matches = tags.match(/^#test_operation:execution_alternate_state=(?<state>[0-9a-zA-Z ]*)/)
      if alt_state_matches && alt_state_matches.names.include?('state')
        options[:alternate_state] = alt_state_matches['state']
      end

      tags.scan(/#test_operation_param:(?<key>.*?)=(?<val>.*?)$/).each do |param_pair|
        options[:params] = {} unless options.has_key?(:params)
        options[:params][param_pair[0]] = param_pair[1]
      end

      self.cases << RightScaleSelfService::Test::Case.new(:operation, options)
    end
  end
end

Instance Attribute Details

#api_responsesObject

Returns the value of attribute api_responses.



29
30
31
# File 'lib/rightscale_selfservice/test/template.rb', line 29

def api_responses
  @api_responses
end

#casesObject

Returns the value of attribute cases.



23
24
25
# File 'lib/rightscale_selfservice/test/template.rb', line 23

def cases
  @cases
end

#errorsObject

Returns the value of attribute errors.



28
29
30
# File 'lib/rightscale_selfservice/test/template.rb', line 28

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/rightscale_selfservice/test/template.rb', line 31

def name
  @name
end

#stateObject

intialized -> launching -> running | failed -> terminating -> finished



26
27
28
# File 'lib/rightscale_selfservice/test/template.rb', line 26

def state
  @state
end

#template_stringObject

Returns the value of attribute template_string.



24
25
26
# File 'lib/rightscale_selfservice/test/template.rb', line 24

def template_string
  @template_string
end

Instance Method Details

#execution_idObject



130
131
132
133
134
135
136
137
138
139
# File 'lib/rightscale_selfservice/test/template.rb', line 130

def execution_id
  unless self.api_responses.has_key?(:execution_create)
    nil
  else
    execution_id = RightScaleSelfService::Api::Client
      .get_resource_id_from_href(
        self.api_responses[:execution_create].headers[:location]
      )
  end
end

#pump(suite) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rightscale_selfservice/test/template.rb', line 83

def pump(suite)
  case self.state
  when 'initialized'
    begin
      self.api_responses[:execution_create] = suite.api_client.manager.execution.create(:source => self.template_string)
      self.state = 'launching'
    rescue RestClient::ExceptionWithResponse => e
      self.errors << "Failed to create execution from template\n\n#{RightScaleSelfService::Api::Client.format_error(e)}"
      self.state = 'failed'
    end
  when 'launching'
    get_execution_status_and_set_as_state(suite)
  when 'running','failed'
    # TODO: Any error handling here?
    unfinished_cases = cases.select {|c| c.pump(suite, self)}
    if unfinished_cases.size == 0
      if self.api_responses.has_key?(:execution_create)
        exec_id = execution_id()
        self.api_responses[:terminate_operation_create] =
          suite.api_client.manager.operation.create(
            :name => 'terminate', :execution_id => exec_id
          )
        self.state = 'terminating'
      else
        self.state = 'terminated'
      end
    end
  when 'terminating'
    get_execution_status_and_set_as_state(suite)
  when 'terminated'
    begin
      exec_id = execution_id()
      if exec_id
        suite.api_client.manager.execution.delete(:id => exec_id)
      end
    rescue RestClient::ExceptionWithResponse => e
      self.errors << "Failed to delete execution #{self.api_response[:execution_create][:headers][:location]}\n\n#{RightScaleSelfService::Api::Client.format_error(e)}"
    end
    self.state = 'finished'
  when 'finished'
    # Do nothing
  else
    self.errors << "unknown template state #{self.state}"
    self.state = 'finished'
  end
end