diff options
author | Clément Bénier <clement.benier@iot.bzh> | 2018-08-14 16:29:07 +0200 |
---|---|---|
committer | Clément Bénier <clement.benier@iot.bzh> | 2018-08-21 11:08:36 +0200 |
commit | e0e1d75c08ff6187acabca74261ebeb0e0893ede (patch) | |
tree | 1bc6f3a033b4db55810d4f00da325f39abdb60d3 | |
parent | 4c01e5e3fad9455b97519380babdf7e8801d8643 (diff) |
tests: better handle websocket client
Change-Id: Iffc7370c52457e6d90e88e30c95b37ae3c60bce3
Signed-off-by: Clément Bénier <clement.benier@iot.bzh>
-rw-r--r-- | test/exec_test.go | 4 | ||||
-rw-r--r-- | test/sdks_test.go | 83 | ||||
-rw-r--r-- | test/target_test.go | 7 | ||||
-rw-r--r-- | test/xdsserver_test.go | 51 |
4 files changed, 66 insertions, 79 deletions
diff --git a/test/exec_test.go b/test/exec_test.go index 8fb6519..abe23b5 100644 --- a/test/exec_test.go +++ b/test/exec_test.go @@ -71,10 +71,6 @@ func TestExec(t *testing.T) { var cfg xsapiv1.FolderConfig assert.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg)) assert.NotNil(t, cfg) - _, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID()) - if err != nil { - t.Fatal(err) - } cmd := "export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/" cmd = cmd + " && " diff --git a/test/sdks_test.go b/test/sdks_test.go index 51f1d95..9c3fc7e 100644 --- a/test/sdks_test.go +++ b/test/sdks_test.go @@ -17,82 +17,25 @@ package xdsservertest import ( - "fmt" - "log" "os" "path" "regexp" - "sync" "testing" "time" "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1" - socketio_client "github.com/sebd71/go-socket.io-client" "github.com/stretchr/testify/assert" ) -var ch chan xsapiv1.SDK - -// Disconnection Channel used to notify XDS Server disconnection -//type Disconnection struct { -// error string -// code int -// svrConf ServerCfg -//} - -// IOSockClient . -type IOSockClient struct { - URL string - Conn *socketio_client.Client - Options *socketio_client.Options - EmitMutex *sync.Mutex - Connected bool - //ServerDiscoChan chan Disconnection - EscapeKeys []byte -} - -func NewIoSocketClient(url, clientID string) (*IOSockClient, error) { - - var err error - - sCli := &IOSockClient{ - URL: url, - EmitMutex: &sync.Mutex{}, - Options: &socketio_client.Options{ - Transport: "websocket", - Header: make(map[string][]string), - }, - //ServerDiscoChan: make(chan Disconnection, 1), - } - sCli.Options.Header["XDS-SID"] = []string{clientID} - - sCli.Conn, err = socketio_client.NewClient(url, sCli.Options) - if err != nil { - return nil, fmt.Errorf("IO.socket connection error: " + err.Error()) - } - - sCli.Conn.On("connection", func() { - sCli.Connected = true - }) - - sCli.Conn.On("disconnection", func(err error) { - log.Printf("WS disconnection event with err: %v\n", err) - sCli.Connected = false - }) +func ConnectSDKStateChange(t *testing.T, sCli *IOSockClient, channel chan xsapiv1.SDK) { sCli.Conn.On(xsapiv1.EVTSDKStateChange, func(e xsapiv1.EventMsg) { sdk, _ := e.DecodeSDKEvent() - ch <- sdk + channel <- sdk }) args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKStateChange} - if err := HTTPCli.Post("/events/register", args, nil); err != nil { - return sCli, err - } - - //sCli.Conn.On(xsapiv1.) - - return sCli, nil + assert.Nil(t, HTTPCli.Post("/events/register", args, nil)) } func TestSdks(t *testing.T) { @@ -101,12 +44,9 @@ func TestSdks(t *testing.T) { _, err := os.Stat(path.Join(sdkDir, "sdks_latest.json")) assert.Nil(t, err) t.Logf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID()) - _, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID()) - if err != nil { - t.Fatal(err) - } - ch = make(chan xsapiv1.SDK) + chSdks := make(chan xsapiv1.SDK) + ConnectSDKStateChange(t, sCli, chSdks) for i := 0; i < len(sdkArray); i++ { re := regexp.MustCompile("^[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+$") @@ -124,18 +64,18 @@ func TestSdks(t *testing.T) { assert.Nil(t, HTTPCli.Post("/sdks/abortinstall", sdk, &sdkRes)) assert.Equal(t, sdkRes.Status, "Not Installed") time.Sleep(1 * time.Millisecond) - sdkFromEvent := <-ch + sdkFromEvent := <-chSdks assert.Equal(t, sdkRes.ID, sdkFromEvent.ID) assert.Equal(t, sdkRes.Status, sdkFromEvent.Status) } } -func installFakeSdk(t *testing.T, sdkInstall xsapiv1.SDKInstallArgs) xsapiv1.SDK { +func installFakeSdk(t *testing.T, sdkInstall xsapiv1.SDKInstallArgs, chSdks chan xsapiv1.SDK) xsapiv1.SDK { var sdkRes xsapiv1.SDK assert.Nil(t, HTTPCli.Post("/sdks", sdkInstall, &sdkRes)) assert.Equal(t, sdkRes.Status, "Installing") /*waiting for SDKStateChange event from channel*/ - sdkFromEvent := <-ch + sdkFromEvent := <-chSdks t.Log(sdkFromEvent) assert.Equal(t, sdkRes.ID, sdkFromEvent.ID) assert.Equal(t, "Installed", sdkFromEvent.Status) @@ -148,11 +88,14 @@ func TestInstallFakeSdk(t *testing.T) { Filename: sdkFileName, Force: false, } - installFakeSdk(t, sdk) + + chSdks := make(chan xsapiv1.SDK) + ConnectSDKStateChange(t, sCli, chSdks) + installFakeSdk(t, sdk, chSdks) var sdkRes xsapiv1.SDK assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes)) sdk.Force = true - //installFakeSdk(t, sdk) + //installFakeSdk(t, sdk, chSdks) } func TestSdksError(t *testing.T) { diff --git a/test/target_test.go b/test/target_test.go index 696c8fc..1cfaa94 100644 --- a/test/target_test.go +++ b/test/target_test.go @@ -70,10 +70,7 @@ func TestTarget(t *testing.T) { chTerm := make(chan xsapiv1.TerminalOutMsg) defer close(chTerm) - sCli, err := NewIoSocketClient(prefixURL, HTTPCli.GetClientID()) - if err != nil { - t.Fatal(err) - } + sCli.Conn.On(xsapiv1.TerminalOutEvent, func(ev xsapiv1.TerminalOutMsg) { chTerm <- ev }) @@ -106,7 +103,7 @@ func TestTarget(t *testing.T) { flushChannelTerm(chTerm, 50) /*check that toto file is created*/ - _, err = os.Stat(totoFileCurrent) + _, err := os.Stat(totoFileCurrent) assert.Nil(t, err) data = []byte("cat " + totoFileCurrent + "\n") diff --git a/test/xdsserver_test.go b/test/xdsserver_test.go index 76c54ea..585ad50 100644 --- a/test/xdsserver_test.go +++ b/test/xdsserver_test.go @@ -17,22 +17,37 @@ package xdsservertest import ( + "fmt" "io" "log" "os" "os/exec" "path" + "sync" "testing" "time" common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib" "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1" + socketio_client "github.com/sebd71/go-socket.io-client" ) +// IOSockClient +type IOSockClient struct { + URL string + Conn *socketio_client.Client + Options *socketio_client.Options + EmitMutex *sync.Mutex + Connected bool + //ServerDiscoChan chan Disconnection + EscapeKeys []byte +} + //global client var HTTPCli *common.HTTPClient var logDir string var sdkDir string +var sCli *IOSockClient func Copy(src, dst string) error { in, err := os.Open(src) @@ -136,6 +151,37 @@ func getHTTPClient(lvl int) (*common.HTTPClient, *os.File) { return cli, file } +func NewIoSocketClient(url, clientID string) (*IOSockClient, error) { + + var err error + + sCli := &IOSockClient{ + URL: url, + EmitMutex: &sync.Mutex{}, + Options: &socketio_client.Options{ + Transport: "websocket", + Header: make(map[string][]string), + }, + //ServerDiscoChan: make(chan Disconnection, 1), + } + sCli.Options.Header["XDS-SID"] = []string{clientID} + + sCli.Conn, err = socketio_client.NewClient(url, sCli.Options) + if err != nil { + return nil, fmt.Errorf("IO.socket connection error: " + err.Error()) + } + + sCli.Conn.On("connection", func() { + sCli.Connected = true + }) + + sCli.Conn.On("disconnection", func(err error) { + log.Printf("WS disconnection event with err: %v\n", err) + sCli.Connected = false + }) + + return sCli, nil +} func TestMain(m *testing.M) { launchProcess := true log.Printf("TestMain: launchProcess is %v", launchProcess) @@ -159,6 +205,11 @@ func TestMain(m *testing.M) { var fileHTTPClient *os.File HTTPCli, fileHTTPClient = getHTTPClient(lvl) defer fileHTTPClient.Close() + var err error + sCli, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID()) + if err != nil { + log.Fatal(err) + } log.Printf("HTTPCli id is %v", HTTPCli.GetClientID()) |