functionnal test: install test install fake sdk
authorClément Bénier <clement.benier@iot.bzh>
Fri, 27 Jul 2018 09:37:51 +0000 (11:37 +0200)
committerClément Bénier <clement.benier@iot.bzh>
Tue, 21 Aug 2018 09:08:36 +0000 (11:08 +0200)
install a fake sdk and waiting for event to end the test

Change-Id: I5c2125f32a1fb4a80f9b251174d15117eef26f9a
Signed-off-by: Clément Bénier <clement.benier@iot.bzh>
test/config.go
test/fixtures/poky-agl-glibc-x86_64-gcc_crosssdk-native_x86_64-toolchain-1.0.2.sh [new file with mode: 0755]
test/sdks_test.go
test/xdsserver_test.go

index 8fea851..a551d4f 100644 (file)
@@ -27,4 +27,6 @@ const (
        envXdtSdk                = "XDT_SDK"
        envXdsServerWorkspaceDir = "XDS_SERVER_WORKSPACE_DIR"
        envXdsServerRootCfgDir   = "XDS_SERVER_ROOT_CFG_DIR"
+       sdkFileName              = "poky-agl-glibc-x86_64-gcc_crosssdk-native_x86_64-toolchain-1.0.2.sh"
+       sdkFile                  = "fixtures/" + sdkFileName
 )
diff --git a/test/fixtures/poky-agl-glibc-x86_64-gcc_crosssdk-native_x86_64-toolchain-1.0.2.sh b/test/fixtures/poky-agl-glibc-x86_64-gcc_crosssdk-native_x86_64-toolchain-1.0.2.sh
new file mode 100755 (executable)
index 0000000..7781335
Binary files /dev/null and b/test/fixtures/poky-agl-glibc-x86_64-gcc_crosssdk-native_x86_64-toolchain-1.0.2.sh differ
index 5efd8fc..6319542 100644 (file)
@@ -19,16 +19,21 @@ package xdsservertest
 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
@@ -68,20 +73,20 @@ func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {
        }
 
        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
        }
@@ -92,17 +97,17 @@ func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {
 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")
@@ -114,7 +119,42 @@ func TestSdks(t *testing.T) {
                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))
 }
index 5722d31..76c54ea 100644 (file)
 package xdsservertest
 
 import (
+       "io"
        "log"
        "os"
        "os/exec"
+       "path"
        "testing"
        "time"
 
@@ -32,6 +34,26 @@ var HTTPCli *common.HTTPClient
 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")
@@ -62,6 +84,13 @@ func initEnv(launchProcess bool) {
        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 {