Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ios runwda: die on error #102

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions ios/testmanagerd/xcuitestrunner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testmanagerd

import (
"context"
"fmt"
"path"
"strings"
Expand Down Expand Up @@ -163,6 +164,7 @@ type ProxyDispatcher struct {
testRunnerReadyWithCapabilities dtx.MethodWithResponse
dtxConnection *dtx.Connection
id string
cancel context.CancelFunc
}

func (p ProxyDispatcher) Dispatch(m dtx.Message) {
Expand All @@ -185,6 +187,9 @@ func (p ProxyDispatcher) Dispatch(m dtx.Message) {
messageBytes, _ := dtx.Encode(m.Identifier, 1, m.ChannelCode, false, dtx.ResponseWithReturnValueInPayload, payload, dtx.NewPrimitiveDictionary())
log.Debug("sending response for capabs")
p.dtxConnection.Send(messageBytes)
case "_XCT_didFailToBootstrapWithError:":
log.Debug("failed to bootstrap")
p.cancel()

default:
log.WithFields(log.Fields{"sel": method}).Infof("device called local method")
Expand All @@ -196,10 +201,10 @@ func (p ProxyDispatcher) Dispatch(m dtx.Message) {
log.Tracef("dispatcher received: %s", m.String())
}

func newDtxProxy(dtxConnection *dtx.Connection) dtxproxy {
func newDtxProxy(dtxConnection *dtx.Connection, cancel context.CancelFunc) dtxproxy {
testBundleReadyChannel := make(chan dtx.Message, 1)
//(xide XCTestManager_IDEInterface)
proxyDispatcher := ProxyDispatcher{testBundleReadyChannel: testBundleReadyChannel, dtxConnection: dtxConnection}
proxyDispatcher := ProxyDispatcher{testBundleReadyChannel: testBundleReadyChannel, dtxConnection: dtxConnection, cancel: cancel}
IDEDaemonProxy := dtxConnection.RequestChannelIdentifier(ideToDaemonProxyChannelName, proxyDispatcher)
ideInterface := XCTestManager_IDEInterface{IDEDaemonProxy: IDEDaemonProxy, testBundleReadyChannel: testBundleReadyChannel}

Expand All @@ -211,10 +216,10 @@ func newDtxProxy(dtxConnection *dtx.Connection) dtxproxy {
}
}

func newDtxProxyWithConfig(dtxConnection *dtx.Connection, testConfig nskeyedarchiver.XCTestConfiguration) dtxproxy {
func newDtxProxyWithConfig(dtxConnection *dtx.Connection, testConfig nskeyedarchiver.XCTestConfiguration, cancel context.CancelFunc) dtxproxy {
testBundleReadyChannel := make(chan dtx.Message, 1)
//(xide XCTestManager_IDEInterface)
proxyDispatcher := ProxyDispatcher{testBundleReadyChannel: testBundleReadyChannel, dtxConnection: dtxConnection, testRunnerReadyWithCapabilities: testRunnerReadyWithCapabilitiesConfig(testConfig)}
proxyDispatcher := ProxyDispatcher{testBundleReadyChannel: testBundleReadyChannel, dtxConnection: dtxConnection, testRunnerReadyWithCapabilities: testRunnerReadyWithCapabilitiesConfig(testConfig), cancel: cancel}
IDEDaemonProxy := dtxConnection.RequestChannelIdentifier(ideToDaemonProxyChannelName, proxyDispatcher)
ideInterface := XCTestManager_IDEInterface{IDEDaemonProxy: IDEDaemonProxy, testConfig: testConfig, testBundleReadyChannel: testBundleReadyChannel}

Expand All @@ -231,30 +236,33 @@ const testmanagerdiOS14 = "com.apple.testmanagerd.lockdown.secure"

const testBundleSuffix = "UITests.xctrunner"

func RunXCUITest(bundleID string, device ios.DeviceEntry) error {
func RunXCUITest(bundleID string, device ios.DeviceEntry, quit context.Context) error {
testRunnerBundleID := bundleID + testBundleSuffix
return RunXCUIWithBundleIds(bundleID, testRunnerBundleID, "", device, nil, nil)
return RunXCUIWithBundleIds(bundleID, testRunnerBundleID, "", device, nil, nil, quit)
}

var closeChan = make(chan interface{})
var closedChan = make(chan interface{})

func runXUITestWithBundleIdsXcode12(bundleID string, testRunnerBundleID string, xctestConfigFileName string,
device ios.DeviceEntry, conn *dtx.Connection, args []string, env []string) error {
device ios.DeviceEntry, conn *dtx.Connection, args []string, env []string, mainQuit context.Context) error {

localQuit, localCancel := context.WithCancel(mainQuit)

testSessionId, xctestConfigPath, testConfig, testInfo, err := setupXcuiTest(device, bundleID, testRunnerBundleID, xctestConfigFileName)
if err != nil {
return err
}
defer conn.Close()
ideDaemonProxy := newDtxProxyWithConfig(conn, testConfig)
ideDaemonProxy := newDtxProxyWithConfig(conn, testConfig, localCancel)

conn2, err := dtx.NewConnection(device, testmanagerdiOS14)
if err != nil {
return err
}
defer conn2.Close()
log.Debug("connections ready")
ideDaemonProxy2 := newDtxProxyWithConfig(conn2, testConfig)
ideDaemonProxy2 := newDtxProxyWithConfig(conn2, testConfig, localCancel)
ideDaemonProxy2.ideInterface.testConfig = testConfig
caps, err := ideDaemonProxy.daemonConnection.initiateControlSessionWithCapabilities(nskeyedarchiver.XCTCapabilities{})
if err != nil {
Expand Down Expand Up @@ -294,15 +302,17 @@ func runXUITestWithBundleIdsXcode12(bundleID string, testRunnerBundleID string,
if err != nil {
log.Error(err)
}
<-closeChan
<-localQuit.Done()
log.Infof("Killing WebDriverAgent with pid %d ...", pid)
err = pControl.KillProcess(pid)
if err != nil {
return err
}
log.Info("WDA killed with success")
/*
var signal interface{}
closedChan <- signal
*/
return nil

}
Expand All @@ -314,23 +324,23 @@ func RunXCUIWithBundleIds(
device ios.DeviceEntry,
wdaargs []string,
wdaenv []string,
) error {
quit context.Context) error {
version, err := ios.GetProductVersion(device)
if err != nil {
return err
}
log.Debugf("%v", version)
if version.LessThan(ios.IOS14()) {
log.Infof("iOS version: %s detected, running with ios11 support", version)
return RunXCUIWithBundleIds11(bundleID, testRunnerBundleID, xctestConfigFileName, device, wdaargs, wdaenv)
return RunXCUIWithBundleIds11(bundleID, testRunnerBundleID, xctestConfigFileName, device, wdaargs, wdaenv, quit)
}


conn, err := dtx.NewConnection(device, testmanagerdiOS14)
if err != nil {
return err
}
return runXUITestWithBundleIdsXcode12(bundleID, testRunnerBundleID, xctestConfigFileName, device, conn, wdaargs, wdaenv)
return runXUITestWithBundleIdsXcode12(bundleID, testRunnerBundleID, xctestConfigFileName, device, conn, wdaargs, wdaenv, quit)

}

Expand Down
13 changes: 9 additions & 4 deletions ios/testmanagerd/xcuitestrunner_11.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/danielpaulus/go-ios/ios/instruments"
log "github.com/sirupsen/logrus"
"strings"
"context"
)

func RunXCUIWithBundleIds11(
Expand All @@ -14,7 +15,9 @@ func RunXCUIWithBundleIds11(
xctestConfigFileName string,
device ios.DeviceEntry,
args []string,
env []string) error {
env []string,
quit context.Context) error {
localQuit, localCancel := context.WithCancel(quit)
log.Debugf("set up xcuitest")
testSessionId, xctestConfigPath, testConfig, testInfo, err := setupXcuiTest(device, bundleID, testRunnerBundleID, xctestConfigFileName)
if err != nil {
Expand All @@ -23,15 +26,15 @@ func RunXCUIWithBundleIds11(
log.Debugf("test session setup ok")
conn, err := dtx.NewConnection(device, testmanagerd)
defer conn.Close()
ideDaemonProxy := newDtxProxyWithConfig(conn, testConfig)
ideDaemonProxy := newDtxProxyWithConfig(conn, testConfig, localCancel)

conn2, err := dtx.NewConnection(device, testmanagerd)
if err != nil {
return err
}
defer conn2.Close()
log.Debug("connections ready")
ideDaemonProxy2 := newDtxProxyWithConfig(conn2, testConfig)
ideDaemonProxy2 := newDtxProxyWithConfig(conn2, testConfig, localCancel)
ideDaemonProxy2.ideInterface.testConfig = testConfig
//TODO: fixme
protocolVersion := uint64(25)
Expand Down Expand Up @@ -65,15 +68,17 @@ func RunXCUIWithBundleIds11(
log.Error(err)
}
log.Debugf("done starting test")
<-closeChan
<-localQuit.Done()
log.Infof("Killing WebDriverAgent with pid %d ...", pid)
err = pControl.KillProcess(pid)
if err != nil {
return err
}
log.Info("WDA killed with success")
/*
var signal interface{}
closedChan <- signal
*/
return nil
}

Expand Down
38 changes: 28 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/danielpaulus/go-ios/ios/testmanagerd"
Expand Down Expand Up @@ -420,8 +421,17 @@ The commands work as following:

b, _ = arguments.Bool("runtest")
if b {
quit, cancel := context.WithCancel(context.Background())
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
signal := <-c
log.Infof("os signal:%d received, closing..", signal)
cancel()
}()

bundleID, _ := arguments.String("<bundleID>")
err := testmanagerd.RunXCUITest(bundleID, device)
err := testmanagerd.RunXCUITest(bundleID, device, quit)
if err != nil {
log.WithFields(log.Fields{"error": err}).Info("Failed running Xcuitest")
}
Expand All @@ -445,25 +455,33 @@ The commands work as following:
log.WithFields(log.Fields{"bundleid": bundleID, "testbundleid": testbundleID, "xctestconfig": xctestconfig}).Error("please specify either NONE of bundleid, testbundleid and xctestconfig or ALL of them. At least one was empty.")
return
}
quit, cancel := context.WithCancel(context.Background())
log.WithFields(log.Fields{"bundleid": bundleID, "testbundleid": testbundleID, "xctestconfig": xctestconfig}).Info("Running wda")
go func() {
err := testmanagerd.RunXCUIWithBundleIds(bundleID, testbundleID, xctestconfig, device, wdaargs, wdaenv)

if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed running WDA")
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
signal := <-c
log.Infof("os signal:%d received, closing..", signal)
cancel()
signal = <-c
log.Infof("os signal:%d received. force kill..", signal)
os.Exit(1)
}()
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
signal := <-c
log.Infof("os signal:%d received, closing..", signal)

err := testmanagerd.RunXCUIWithBundleIds(bundleID, testbundleID, xctestconfig, device, wdaargs, wdaenv, quit)

if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("Failed running WDA")
}

/*
err := testmanagerd.CloseXCUITestRunner()
if err != nil {
log.Error("Failed closing wda-testrunner")
os.Exit(1)
}
log.Info("Done Closing")
*/
return
}

Expand Down