Class: RightScaleSelfService::Test::Case

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, options = {}) ⇒ Case



33
34
35
36
37
38
39
40
41
# File 'lib/rightscale_selfservice/test/case.rb', line 33

def initialize(type, options = {})
  self.type = type
  self.options = options
  self.result = ""
  self.errors = []
  self.failures = []
  self.state = 'initialized'
  self.api_responses = {}
end

Instance Attribute Details

#api_responsesObject

Returns the value of attribute api_responses.



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

def api_responses
  @api_responses
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

#failuresObject

Returns the value of attribute failures.



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

def failures
  @failures
end

#finishedObject

Returns the value of attribute finished.



27
28
29
# File 'lib/rightscale_selfservice/test/case.rb', line 27

def finished
  @finished
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#resultObject

Returns the value of attribute result.



25
26
27
# File 'lib/rightscale_selfservice/test/case.rb', line 25

def result
  @result
end

#stateObject

initialized -> running -> completed | ?? -> finished



30
31
32
# File 'lib/rightscale_selfservice/test/case.rb', line 30

def state
  @state
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#pump(suite, template) ⇒ bool

Performs the test for this case. Returns a boolean indicating if the test is “done” or not.



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
82
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rightscale_selfservice/test/case.rb', line 53

def pump(suite, template)
  if result != ""
    false
  else
    case self.type
    when :compile_only
      begin
        suite.api_client.designer.template.compile(:source => template.template_string)
        self.result = 'SUCCESS'
      rescue RestClient::ExceptionWithResponse => e
        if e.http_code == 422
          self.result = 'FAILED'
          self.failures << "Failed to compile template\n\n#{RightScaleSelfService::Api::Client.format_error(e)}"
        else
          self.result = 'ERROR'
          self.errors << RightScaleSelfService::Api::Client.format_error(e)
        end
      end
      false
    when :execution
      if self.options[:state] == template.state
        self.result = 'SUCCESS'
        self.result = 'FIXED' if self.options.has_key?(:alternate_state)
      else
        if self.options.has_key?(:alternate_state) && self.options[:alternate_state] == template.state
          self.result = 'FAILED (EXPECTED)'
        else
          self.result = 'FAILED'
          self.failures << "Expected execution end state to be (#{self.options[:state]}) but got execution end state (#{template.state})"
        end
      end
      false
    when :operation
      if template.state == 'running'
        case self.state
        when 'initialized'
          execution_id = template.execution_id
          create_params = {
            :execution_id => execution_id,
            :name => self.options[:operation_name]
          }

          if self.options.has_key?(:params)
            create_params[:options] = self.options[:params]
          end

          begin
            self.api_responses[:operation_create] = suite.api_client.manager.operation.create(create_params)
            self.state = 'running'
          rescue RestClient::ExceptionWithResponse => e
            self.errors << "Failed to create operation #{self.options[:operation_name]} for execution\n\n#{RightScaleSelfService::Api::Client.format_error(e)}"
            self.result = 'ERROR'
            return false
          end
          true
        when 'running'
          begin
            operation_id = RightScaleSelfService::Api::Client.get_resource_id_from_href(self.api_responses[:operation_create].headers[:location])
            if operation_id
              self.api_responses[:operation_show] = suite.api_client.manager.operation.show(:id => operation_id)
              json_str = self.api_responses[:operation_show].body
              show = JSON.parse(json_str)
              self.state = show['status']['summary']
            end
          rescue RestClient::ExceptionWithResponse => e
            # TODO: Do I want to catch errors here, or let it fall through and
            # let the next pump retry?
            self.errors << "Failed to check operation status\n\n#{RightScaleSelfService::Api::Client.format_error(e)}"
          end
        when 'completed','failed'
          if self.options[:state] == self.state
            self.result = 'SUCCESS'
            self.result = 'FIXED' if self.options.has_key?(:alternate_state)
          else
            if self.options.has_key?(:alternate_state) && self.options[:alternate_state] == self.state
              self.result = 'FAILED (EXPECTED)'
            else
              self.result = 'FAILED'
              self.failures << "Expected operation end state to be (#{self.options[:state]}) but got execution end state (#{self.state})"
            end
          end
          false
        else
        end
      elsif template.state == 'failed'
        self.result = 'FAILED'
        self.failures << "Execution failed to start, could not start a new operation."
        false
      else
        self.result = 'ERROR'
        self.errors << "Unexpected execution state #{template.state} while processing operation test case."
        false
      end
    else
      self.result = 'ERROR'
      self.errors << "Unknown test case type (:#{self.type})"
      false
    end
  end
end