2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Clément Bénier <clement.benier@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
29 "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xaapiv1"
30 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
31 socketio_client "github.com/sebd71/go-socket.io-client"
32 "github.com/stretchr/testify/assert"
35 var ch chan xsapiv1.SDK
37 // Disconnection Channel used to notify XDS Server disconnection
38 //type Disconnection struct {
45 type IOSockClient struct {
47 Conn *socketio_client.Client
48 Options *socketio_client.Options
51 //ServerDiscoChan chan Disconnection
55 func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {
59 sCli := &IOSockClient{
61 EmitMutex: &sync.Mutex{},
62 Options: &socketio_client.Options{
63 Transport: "websocket",
64 Header: make(map[string][]string),
66 //ServerDiscoChan: make(chan Disconnection, 1),
68 sCli.Options.Header["XDS-SID"] = []string{clientID}
70 sCli.Conn, err = socketio_client.NewClient(url, sCli.Options)
72 return nil, fmt.Errorf("IO.socket connection error: " + err.Error())
75 sCli.Conn.On("connection", func() {
79 sCli.Conn.On("disconnection", func(err error) {
80 log.Printf("WS disconnection event with err: %v\n", err)
81 sCli.Connected = false
84 sCli.Conn.On(xsapiv1.EVTSDKStateChange, func(e xsapiv1.EventMsg) {
85 sdk, _ := e.DecodeSDKEvent()
89 args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKStateChange}
90 if err := HTTPCli.Post("/events/register", args, nil); err != nil {
97 func TestSdks(t *testing.T) {
98 var sdkArray []xsapiv1.SDK
99 assert.Nil(t, HTTPCli.Get("/sdks", &sdkArray))
100 _, err := os.Stat(path.Join(sdkDir, "sdks_latest.json"))
102 t.Logf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID())
103 _, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID())
108 ch = make(chan xsapiv1.SDK)
110 for i := 0; i < len(sdkArray); i++ {
111 re := regexp.MustCompile("^[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+$")
112 assert.True(t, re.MatchString(sdkArray[i].ID))
113 assert.Equal(t, sdkArray[0].Status, "Not Installed")
116 assert.Nil(t, HTTPCli.Get("/sdks/"+sdkArray[i].ID, &sdk))
117 assert.Equal(t, sdkArray[i], sdk)
119 var sdkRes xsapiv1.SDK
120 assert.Nil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
121 assert.Equal(t, sdkRes.Status, "Installing")
122 time.Sleep(1 * time.Millisecond)
123 assert.Nil(t, HTTPCli.Post("/sdks/abortinstall", sdk, &sdkRes))
124 assert.Equal(t, sdkRes.Status, "Not Installed")
125 time.Sleep(1 * time.Millisecond)
127 assert.Equal(t, sdkRes.ID, sdkFromEvent.ID)
128 assert.Equal(t, sdkRes.Status, sdkFromEvent.Status)
132 func TestInstallFakeSdk(t *testing.T) {
133 sdk := xaapiv1.SDKInstallArgs{
135 Filename: sdkFileName,
138 var sdkRes xsapiv1.SDK
139 assert.Nil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
140 assert.Equal(t, sdkRes.Status, "Installing")
141 /*waiting for SDKStateChange event from channel*/
144 assert.Equal(t, sdkRes.ID, sdkFromEvent.ID)
145 assert.Equal(t, "Installed", sdkFromEvent.Status)
148 func TestSdksError(t *testing.T) {
149 sdk := xaapiv1.SDKInstallArgs{
154 var sdkRes xsapiv1.SDK
155 assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
156 sdk.ID = "42885c24-374e-3ef0-9723-0ce8a05191aa"
157 assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
158 sdk.Filename = "fake"
159 assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))