Class: Stf::StartOneDebugSessionInteractor

Inherits:
Object
  • Object
show all
Includes:
ADB, Log
Defined in:
lib/stf/interactor/start_one_debug_session_interactor.rb

Instance Method Summary collapse

Methods included from Log

#logger, verbose

Instance Method Details

#escape(s) ⇒ Object



79
80
81
# File 'lib/stf/interactor/start_one_debug_session_interactor.rb', line 79

def escape(s)
  s.gsub(/["]/, '\"').gsub(/[ ]/, '\ ').gsub(/[=]/, '\=').gsub(/[,]/, '\,')
end

#execute(device) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/stf/interactor/start_one_debug_session_interactor.rb', line 13

def execute(device)
  return false if device.nil?
  serial = device.serial

  begin
    success = DI[:stf].add_device serial
    if success
      logger.info "Device added #{serial}"
    else
      logger.error "Can't add device #{serial}"
      raise
    end

    result = DI[:stf].start_debug serial
    if result.success
      logger.info "Debug started #{serial}"
    else
      logger.error "Can't start debugging session for device #{serial}"
      raise
    end

    provider = device.getValue "provider"
    provider_ip = provider["ip"]
    remote_connect_url_split = result.remoteConnectUrl.split ":"
    remote_connect_port = remote_connect_url_split[1]
    result = DI[:stf].open_tunnel provider_ip, remote_connect_port.to_i
    if result.success
      logger.info "Opened tunnel to provider with IP #{provider_ip}"
    else
      logger.error "Can't open tunnel to provider with IP #{provider_ip}"
      raise
    end
    remote_connect_tunneled_url = remote_connect_url_split[0] + ":" + result.port.to_s
    logger.info remote_connect_tunneled_url

    case device.getValue "platform"
    when "Android"
        execute_adb_with 30, "connect #{remote_connect_tunneled_url}"
        shell('echo adbtest', {serial: "#{remote_connect_tunneled_url}"}, 30)
        raise ADBError, "Could not execute shell test" unless stdout_contains "adbtest"
    when "iOS"
        DI[:connect_ios].connect(remote_connect_tunneled_url)
    else
        logger.error "Unrecognized device platform"
    end

    return true

  rescue StandardError, SignalException => e
    begin
      # we will try clean anyway
      DI[:stf].remove_device serial
      if test ?d, '/custom-metrics'
        File.open('/custom-metrics/openstf_connect_fail', 'a') do |f|
          message = (!e.nil? || !e.message.nil?) ? e.message : ""
          f.write("openstf_connect_fail,reason=\"#{escape(message)}\",serial=\"#{escape(serial)}\" count=1i #{Time.now.to_i}\n")
        end
      end
    rescue
    end

    logger.error "Failed to connect to #{serial}: " + e&.message
    return false
  end
end