import (
"fmt"
"log"
+ "os"
+ "path"
"regexp"
"sync"
"testing"
"time"
+ "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xaapiv1"
"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
}
sCli.Conn.On("connection", func() {
- log.Printf("BBBBBBBBBBBBBBBB:::WS connection event")
sCli.Connected = true
})
sCli.Conn.On("disconnection", func(err error) {
- log.Printf("AAAAAAAAAAAA:::WS disconnection event with err: %v\n", err)
+ log.Printf("WS disconnection event with err: %v\n", err)
sCli.Connected = false
})
- sCli.Conn.On(xsapiv1.EVTSDKManagement, func(data interface{}) {
- log.Printf("WS disconnection event with err: %v\n", data)
+ sCli.Conn.On(xsapiv1.EVTSDKStateChange, func(e xsapiv1.EventMsg) {
+ sdk, _ := e.DecodeSDKEvent()
+ ch <- sdk
})
- args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKManagement}
+ args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKStateChange}
if err := HTTPCli.Post("/events/register", args, nil); err != nil {
return sCli, err
}
func TestSdks(t *testing.T) {
var sdkArray []xsapiv1.SDK
assert.Nil(t, HTTPCli.Get("/sdks", &sdkArray))
- t.Log(sdkArray[0])
-
+ _, 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())
- IOSkClient, err := NewIoSocketClient(prefixURL, HTTPCli.GetClientID())
+ _, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID())
if err != nil {
t.Fatal(err)
}
- t.Log(IOSkClient)
- //for i := 0; i < len(sdkArray); i++ {
- for i := 0; i < 1; i++ {
+ ch = make(chan xsapiv1.SDK)
+
+ 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.True(t, re.MatchString(sdkArray[i].ID))
assert.Equal(t, sdkArray[0].Status, "Not Installed")
var sdkRes xsapiv1.SDK
assert.Nil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
assert.Equal(t, sdkRes.Status, "Installing")
- //t.Log(sdkRes)
- time.Sleep(1 * time.Second)
+ time.Sleep(1 * time.Millisecond)
+ assert.Nil(t, HTTPCli.Post("/sdks/abortinstall", sdk, &sdkRes))
+ assert.Equal(t, sdkRes.Status, "Not Installed")
+ time.Sleep(1 * time.Millisecond)
+ sdkFromEvent := <-ch
+ assert.Equal(t, sdkRes.ID, sdkFromEvent.ID)
+ assert.Equal(t, sdkRes.Status, sdkFromEvent.Status)
+ }
+}
+
+func TestInstallFakeSdk(t *testing.T) {
+ sdk := xaapiv1.SDKInstallArgs{
+ ID: "",
+ Filename: sdkFileName,
+ Force: false,
+ }
+ var sdkRes xsapiv1.SDK
+ assert.Nil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
+ assert.Equal(t, sdkRes.Status, "Installing")
+ /*waiting for SDKStateChange event from channel*/
+ sdkFromEvent := <-ch
+ t.Log(sdkFromEvent)
+ assert.Equal(t, sdkRes.ID, sdkFromEvent.ID)
+ assert.Equal(t, "Installed", sdkFromEvent.Status)
+}
+
+func TestSdksError(t *testing.T) {
+ sdk := xaapiv1.SDKInstallArgs{
+ ID: "",
+ Filename: "",
+ Force: false,
}
+ var sdkRes xsapiv1.SDK
+ assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
+ sdk.ID = "42885c24-374e-3ef0-9723-0ce8a05191aa"
+ assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
+ sdk.Filename = "fake"
+ assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
}
package xdsservertest
import (
+ "io"
"log"
"os"
"os/exec"
+ "path"
"testing"
"time"
var logDir string
var sdkDir string
+func Copy(src, dst string) error {
+ in, err := os.Open(src)
+ if err != nil {
+ return err
+ }
+ defer in.Close()
+
+ out, err := os.Create(dst)
+ if err != nil {
+ return err
+ }
+ defer out.Close()
+
+ _, err = io.Copy(out, in)
+ if err != nil {
+ return err
+ }
+ return out.Close()
+}
+
func initEnv(launchProcess bool) {
if launchProcess {
cmd := exec.Command("killall", "-9", "xds-server")
logDir = rootTestLog + "/logs/"
os.MkdirAll(logDir, 0755)
os.MkdirAll(sdkDir, 0755)
+ currentDir, err := os.Getwd()
+ if err != nil {
+ log.Fatal(err)
+ }
+ if err := Copy(path.Join(currentDir, sdkFile), path.Join(sdkDir, sdkFileName)); err != nil {
+ log.Fatal(err)
+ }
}
func launchXdsServer(proc **os.Process) *os.File {