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.
27 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
28 socketio_client "github.com/sebd71/go-socket.io-client"
29 "github.com/stretchr/testify/assert"
32 // Disconnection Channel used to notify XDS Server disconnection
33 //type Disconnection struct {
40 type IOSockClient struct {
42 Conn *socketio_client.Client
43 Options *socketio_client.Options
46 //ServerDiscoChan chan Disconnection
50 func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {
54 sCli := &IOSockClient{
56 EmitMutex: &sync.Mutex{},
57 Options: &socketio_client.Options{
58 Transport: "websocket",
59 Header: make(map[string][]string),
61 //ServerDiscoChan: make(chan Disconnection, 1),
63 sCli.Options.Header["XDS-SID"] = []string{clientID}
65 sCli.Conn, err = socketio_client.NewClient(url, sCli.Options)
67 return nil, fmt.Errorf("IO.socket connection error: " + err.Error())
70 sCli.Conn.On("connection", func() {
71 log.Printf("BBBBBBBBBBBBBBBB:::WS connection event")
75 sCli.Conn.On("disconnection", func(err error) {
76 log.Printf("AAAAAAAAAAAA:::WS disconnection event with err: %v\n", err)
77 sCli.Connected = false
80 sCli.Conn.On(xsapiv1.EVTSDKManagement, func(data interface{}) {
81 log.Printf("WS disconnection event with err: %v\n", data)
84 args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKManagement}
85 if err := HTTPCli.Post("/events/register", args, nil); err != nil {
92 func TestSdks(t *testing.T) {
93 var sdkArray []xsapiv1.SDK
94 assert.Nil(t, HTTPCli.Get("/sdks", &sdkArray))
97 t.Logf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID())
98 IOSkClient, err := NewIoSocketClient(prefixURL, HTTPCli.GetClientID())
104 //for i := 0; i < len(sdkArray); i++ {
105 for i := 0; i < 1; i++ {
106 re := regexp.MustCompile("^[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+$")
107 assert.True(t, re.MatchString(sdkArray[i].ID))
108 assert.Equal(t, sdkArray[0].Status, "Not Installed")
111 assert.Nil(t, HTTPCli.Get("/sdks/"+sdkArray[i].ID, &sdk))
112 assert.Equal(t, sdkArray[i], sdk)
114 var sdkRes xsapiv1.SDK
115 assert.Nil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
116 assert.Equal(t, sdkRes.Status, "Installing")
118 time.Sleep(1 * time.Second)