1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
* Copyright (C) 2017-2018 "IoT.bzh"
* Author Clément Bénier <clement.benier@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xdsservertest
import (
"log"
"os"
"path"
"regexp"
"strings"
"testing"
"time"
"gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
"github.com/stretchr/testify/require"
)
var cpt int
/*flush channel with timeout*/
func flushChannelSdk(channel chan xsapiv1.SDK, ms time.Duration) {
timeoutB := false
for !timeoutB {
select {
case <-channel:
case <-time.After(ms * time.Millisecond):
timeoutB = true
}
}
}
func ConnectSDKStateChange(t *testing.T, sCli *IOSockClient, channel chan xsapiv1.SDK) {
sCli.Conn.On(xsapiv1.EVTSDKStateChange, func(e xsapiv1.EventMsg) {
sdk, _ := e.DecodeSDKEvent()
channel <- sdk
})
args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKStateChange}
require.Nil(t, HTTPCli.Post("/events/register", args, nil))
}
func DisconnectSDKStateChange(t *testing.T, sCli *IOSockClient) {
args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKStateChange}
require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
}
func InitSdkDir() {
/*create sdk dir*/
sdkDir := os.Getenv(envXdtSdk)
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 RemoveSdk(t *testing.T, sdk xsapiv1.SDK, chSdks chan xsapiv1.SDK) {
Debugf(t, "remove sdk %v", sdk.ID)
require.Nil(t, HTTPCli.Delete("/sdks/"+sdk.ID, &sdk))
sdkFromEvt := <-chSdks //waiting for event Uninstalling
require.Equal(t, sdk.ID, sdkFromEvt.ID)
require.Equal(t, sdkFromEvt.Status, "Un-installing")
sdkFromEvt = <-chSdks //waiting for event not installed
require.Equal(t, sdkFromEvt.Status, "Not Installed")
os.RemoveAll(path.Join(os.Getenv(envXdtSdk), sdk.Profile))
os.Remove(strings.Replace(sdk.URL, "file://", "", 1))
}
func TestSdks(t *testing.T) {
InitSdkDir()
/*get sdk list from sdk_latest.json*/
var sdkArray []xsapiv1.SDK
require.Nil(t, HTTPCli.Get("/sdks", &sdkArray))
_, err := os.Stat(path.Join(os.Getenv(envXdtSdk), "sdks_latest.json"))
require.Nil(t, err)
/*create channel fro SDK event*/
chSdks := make(chan xsapiv1.SDK)
defer close(chSdks)
ConnectSDKStateChange(t, sCli, chSdks)
/*checking startup installing of SDKs*/
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]+$")
require.True(t, re.MatchString(sdkArray[i].ID))
require.Equal(t, sdkArray[0].Status, "Not Installed")
var sdk xsapiv1.SDK
require.Nil(t, HTTPCli.Get("/sdks/"+sdkArray[i].ID, &sdk))
require.Equal(t, sdkArray[i], sdk)
var sdkRes xsapiv1.SDK
/*install sdk*/
Debugf(t, "install sdk %v", sdk.Name)
require.Nil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
require.Equal(t, sdkRes.Status, "Installing")
sdkFromEvent := <-chSdks //waiting for installing event
require.Equal(t, sdkRes.ID, sdkFromEvent.ID)
require.Equal(t, sdkFromEvent.Status, "Installing")
/*abort sdk install*/
Debugf(t, "abort install sdk %v", sdk.Name)
require.Nil(t, HTTPCli.Post("/sdks/abortinstall", sdk, &sdkRes))
require.Equal(t, sdkRes.Status, "Not Installed")
sdkFromEvent = <-chSdks //waiting for not installed status*/
require.Equal(t, sdkRes.ID, sdkFromEvent.ID)
require.Equal(t, sdkRes.Status, sdkFromEvent.Status)
}
}
func installFakeSdk(t *testing.T, sdkInstall xsapiv1.SDKInstallArgs, chSdks chan xsapiv1.SDK) xsapiv1.SDK {
InitSdkDir()
var sdkRes xsapiv1.SDK
require.Nil(t, HTTPCli.Post("/sdks", sdkInstall, &sdkRes))
Debugf(t, "Install fake sdk %v (force=%v)", sdkRes.Name, sdkInstall.Force)
require.Equal(t, sdkRes.Status, "Installing")
sdkFromEvent := <-chSdks
require.Equal(t, sdkRes.ID, sdkFromEvent.ID)
require.Equal(t, sdkFromEvent.Status, "Installing")
/*waiting for SDKStateChange event from channel*/
sdkFromEvent = <-chSdks
require.Equal(t, sdkRes.ID, sdkFromEvent.ID)
require.Equal(t, "Installed", sdkFromEvent.Status)
require.Equal(t, sdkFromEvent.LastError, "")
Debugf(t, "Fake sdk %v installed", sdkFromEvent.Name)
return sdkFromEvent
}
func TestInstallFakeSdk(t *testing.T) {
sdk := xsapiv1.SDKInstallArgs{
ID: "",
Filename: sdkFileName,
Force: false,
}
chSdks := make(chan xsapiv1.SDK)
defer close(chSdks)
ConnectSDKStateChange(t, sCli, chSdks)
installFakeSdk(t, sdk, chSdks)
/*test force install*/
sdk.Force = true
sdkRes := installFakeSdk(t, sdk, chSdks)
RemoveSdk(t, sdkRes, chSdks)
DisconnectSDKStateChange(t, sCli)
}
func TestSdksError(t *testing.T) {
sdk := xsapiv1.SDKInstallArgs{
ID: "",
Filename: "",
Force: false,
}
Debugf(t, "error while installing sdk with no ID and no Filename")
var sdkRes xsapiv1.SDK
/*error with no ID no filename*/
require.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
sdk.ID = "42885c24-374e-3ef0-9723-0c8a05191aa"
Debugf(t, "error while installing sdk with a fake id=%v", sdk.ID)
/*error with fake ID*/
require.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
sdk.ID = ""
sdk.Filename = "fake"
Debugf(t, "error while installing sdk with a fake filename=%v", sdk.Filename)
/*error with fake filename*/
require.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
}
|