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-server/lib/xsapiv1"
30 socketio_client "github.com/sebd71/go-socket.io-client"
31 "github.com/stretchr/testify/assert"
34 var ch chan xsapiv1.SDK
36 // Disconnection Channel used to notify XDS Server disconnection
37 //type Disconnection struct {
44 type IOSockClient struct {
46 Conn *socketio_client.Client
47 Options *socketio_client.Options
50 //ServerDiscoChan chan Disconnection
54 func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {
58 sCli := &IOSockClient{
60 EmitMutex: &sync.Mutex{},
61 Options: &socketio_client.Options{
62 Transport: "websocket",
63 Header: make(map[string][]string),
65 //ServerDiscoChan: make(chan Disconnection, 1),
67 sCli.Options.Header["XDS-SID"] = []string{clientID}
69 sCli.Conn, err = socketio_client.NewClient(url, sCli.Options)
71 return nil, fmt.Errorf("IO.socket connection error: " + err.Error())
74 sCli.Conn.On("connection", func() {
78 sCli.Conn.On("disconnection", func(err error) {
79 log.Printf("WS disconnection event with err: %v\n", err)
80 sCli.Connected = false
83 sCli.Conn.On(xsapiv1.EVTSDKStateChange, func(e xsapiv1.EventMsg) {
84 sdk, _ := e.DecodeSDKEvent()
88 args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKStateChange}
89 if err := HTTPCli.Post("/events/register", args, nil); err != nil {
93 //sCli.Conn.On(xsapiv1.)
98 func TestSdks(t *testing.T) {
99 var sdkArray []xsapiv1.SDK
100 assert.Nil(t, HTTPCli.Get("/sdks", &sdkArray))
101 _, err := os.Stat(path.Join(sdkDir, "sdks_latest.json"))
103 t.Logf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID())
104 _, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID())
109 ch = make(chan xsapiv1.SDK)
111 for i := 0; i < len(sdkArray); i++ {
112 re := regexp.MustCompile("^[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+$")
113 assert.True(t, re.MatchString(sdkArray[i].ID))
114 assert.Equal(t, sdkArray[0].Status, "Not Installed")
117 assert.Nil(t, HTTPCli.Get("/sdks/"+sdkArray[i].ID, &sdk))
118 assert.Equal(t, sdkArray[i], sdk)
120 var sdkRes xsapiv1.SDK
121 assert.Nil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
122 assert.Equal(t, sdkRes.Status, "Installing")
123 time.Sleep(1 * time.Millisecond)
124 assert.Nil(t, HTTPCli.Post("/sdks/abortinstall", sdk, &sdkRes))
125 assert.Equal(t, sdkRes.Status, "Not Installed")
126 time.Sleep(1 * time.Millisecond)
128 assert.Equal(t, sdkRes.ID, sdkFromEvent.ID)
129 assert.Equal(t, sdkRes.Status, sdkFromEvent.Status)
133 func installFakeSdk(t *testing.T, sdkInstall xsapiv1.SDKInstallArgs) xsapiv1.SDK {
134 var sdkRes xsapiv1.SDK
135 assert.Nil(t, HTTPCli.Post("/sdks", sdkInstall, &sdkRes))
136 assert.Equal(t, sdkRes.Status, "Installing")
137 /*waiting for SDKStateChange event from channel*/
140 assert.Equal(t, sdkRes.ID, sdkFromEvent.ID)
141 assert.Equal(t, "Installed", sdkFromEvent.Status)
145 func TestInstallFakeSdk(t *testing.T) {
146 sdk := xsapiv1.SDKInstallArgs{
148 Filename: sdkFileName,
151 installFakeSdk(t, sdk)
152 var sdkRes xsapiv1.SDK
153 assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
155 //installFakeSdk(t, sdk)
158 func TestSdksError(t *testing.T) {
159 sdk := xsapiv1.SDKInstallArgs{
164 var sdkRes xsapiv1.SDK
165 assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
166 sdk.ID = "42885c24-374e-3ef0-9723-0ce8a05191aa"
167 assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
168 sdk.Filename = "fake"
169 assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))