tests/cimi/test_helper.rb
# Cleanup: stop/destroy the resources created for the tests
stop/destroy the resources created for the tests
stop / destroy the resources created for the tests
def teardown(created_resources, api_basic_auth)
      @@created_resources = created_resources
      puts "CLEANING UP... resources for deletion: #{@@created_resources.inspect}"
      if @@created_resources[:systems]
        @@created_resources[:systems].each do |sys_id|
          sys = get(sys_id, :accept=>:json)
          delete_op = sys.json["operations"].find { |op| op["rel"] =~ /delete$/ }
          if delete_op
              delete_res = RestClient.delete( delete_op["href"],
                  {'Authorization' => api_basic_auth, :accept => :json} )
              @@created_resources[:systems].delete(sys_id)  if (200..207).include? delete_res.code
              @@created_resources.delete(:systems) if @@created_resources[:systems].empty?
          end
        end
      end
      if not @@created_resources[:machines].nil?
        @@created_resources[:machines].each_index do |i|
          machine = get(@@created_resources[:machines][i], :accept => :json)
          unless machine.json["state"].upcase.eql?("STOPPED")
            stop_op = machine.json["operations"].find { |op| op["rel"] =~ /stop$/ }
            stop_res = post( stop_op["href"],
            "<Action xmlns=\"http://schemas.dmtf.org/cimi/1\">" +
            "<action>http://schemas.dmtf.org/cimi/1/action/stop</action>" +
            "</Action>",
            :accept => :xml, :content_type => :xml )
            machine = get(machine.json["id"], :accept => :json)
          end
          cep_json = cep(:accept => :json)
          while (get(cep_json.json["machines"]["href"], {:accept=>:json}).include?(machine.json["id"]) && (not machine.json["state"].upcase.eql?("STOPPED")))
            puts 'waiting for machine to be STOPPED'
            sleep(3)
            unless (not get(cep_json.json["machines"]["href"], {:accept=>:json}).include?(machine.json["id"]))
              machine = get(machine.json["id"], :accept => :json)
            end
          end
          if get(cep_json.json["machines"]["href"], {:accept=>:json}).include?(machine.json["id"])
            delete_op = machine.json["operations"].find { |op| op["rel"] =~ /delete$/ }
            if delete_op
              delete_res = RestClient.delete( delete_op["href"],
                  {'Authorization' => api_basic_auth, :accept => :json} )
              @@created_resources[:machines][i] = nil if (200..207).include? delete_res.code
            end
          else
            @@created_resources[:machines][i] = nil
          end
        end
        @@created_resources[:machines].compact!
        @@created_resources.delete(:machines) if @@created_resources[:machines].empty?
      end
      if (not @@created_resources[:machine_images].nil?) &&
      (not @@created_resources[:volumes].nil?)
        [:machine_images, :volumes, :machine_templates].each do |col|
          @@created_resources[col].each do |k|
            attempts = 0
            begin
              puts "#{k}"
              res = RestClient.delete( "#{k}",
              {'Authorization' => api_basic_auth, :accept => :json} )
              @@created_resources[col].delete(k) if res.code == 200
            rescue Exception => e
              sleep(10)
              attempts += 1
              retry if (attempts <= 5)
            end
          end
          @@created_resources.delete(col) if @@created_resources[col].empty?
        end
      end
      puts "CLEANUP attempt finished... resources looks like: #{@@created_resources.inspect}"
      raise Exception.new("Unable to delete all created resources - please check: #{@@created_resources.inspect}") unless @@created_resources.empty?
    end
def teardown ( created_resources , api_basic_auth ) @@created_resources = created_resources puts "CLEANING UP... resources for deletion: #{@@created_resources.inspect}" if @@created_resources [ :systems ] @@created_resources [ :systems ] . each do | sys_id | sys = get ( sys_id , :accept => :json ) delete_op = sys . json [ "operations" ] . find { | op | op [ "rel" ] =~ / delete$ / } if delete_op delete_res = RestClient . delete ( delete_op [ "href" ] , { 'Authorization' => api_basic_auth , :accept => :json } ) @@created_resources [ :systems ] . delete ( sys_id ) if ( 200 .. 207 ) . include? delete_res . code @@created_resources . delete ( :systems ) if @@created_resources [ :systems ] . empty? end end end if not @@created_resources [ :machines ] . nil? @@created_resources [ :machines ] . each_index do | i | machine = get ( @@created_resources [ :machines ] [ i ] , :accept => :json ) unless machine . json [ "state" ] . upcase . eql? ( "STOPPED" ) stop_op = machine . json [ "operations" ] . find { | op | op [ "rel" ] =~ / stop$ / } stop_res = post ( stop_op [ "href" ] , "<Action xmlns=\"http://schemas.dmtf.org/cimi/1\">" + "<action>http://schemas.dmtf.org/cimi/1/action/stop</action>" + "</Action>" , :accept => :xml , :content_type => :xml ) machine = get ( machine . json [ "id" ] , :accept => :json ) end cep_json = cep ( :accept => :json ) while ( get ( cep_json . json [ "machines" ] [ "href" ] , { :accept => :json } ) . include? ( machine . json [ "id" ] ) && ( not machine . json [ "state" ] . upcase . eql? ( "STOPPED" ) ) ) puts 'waiting for machine to be STOPPED' sleep ( 3 ) unless ( not get ( cep_json . json [ "machines" ] [ "href" ] , { :accept => :json } ) . include? ( machine . json [ "id" ] ) ) machine = get ( machine . json [ "id" ] , :accept => :json ) end end if get ( cep_json . json [ "machines" ] [ "href" ] , { :accept => :json } ) . include? ( machine . json [ "id" ] ) delete_op = machine . json [ "operations" ] . find { | op | op [ "rel" ] =~ / delete$ / } if delete_op delete_res = RestClient . delete ( delete_op [ "href" ] , { 'Authorization' => api_basic_auth , :accept => :json } ) @@created_resources [ :machines ] [ i ] = nil if ( 200 .. 207 ) . include? delete_res . code end else @@created_resources [ :machines ] [ i ] = nil end end @@created_resources [ :machines ] . compact! @@created_resources . delete ( :machines ) if @@created_resources [ :machines ] . empty? end if ( not @@created_resources [ :machine_images ] . nil? ) && ( not @@created_resources [ :volumes ] . nil? ) [ :machine_images , :volumes , :machine_templates ] . each do | col | @@created_resources [ col ] . each do | k | attempts = 0 begin puts "#{k}" res = RestClient . delete ( "#{k}" , { 'Authorization' => api_basic_auth , :accept => :json } ) @@created_resources [ col ] . delete ( k ) if res . code == 200 rescue Exception => e sleep ( 10 ) attempts += 1 retry if ( attempts <= 5 ) end end @@created_resources . delete ( col ) if @@created_resources [ col ] . empty? end end puts "CLEANUP attempt finished... resources looks like: #{@@created_resources.inspect}" raise Exception . new ( "Unable to delete all created resources - please check: #{@@created_resources.inspect}" ) unless @@created_resources . empty? end
Cleanup: stop/destroy the resources created for the tests
Cleanup : stop / destroy the resources created for the tests
#systems: # machines: # machine_image, machine_volumes, other collections
created_resources
<not_specific>
api_basic_auth
<not_specific>