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) {
_, 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]+$")
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)
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) {
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)
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)
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())