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.
28 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
29 "github.com/stretchr/testify/require"
34 /*flush channel with timeout*/
35 func flushChannelSdk(channel chan xsapiv1.SDK, ms time.Duration) {
40 case <-time.After(ms * time.Millisecond):
46 func ConnectSDKStateChange(t *testing.T, sCli *IOSockClient, channel chan xsapiv1.SDK) {
48 sCli.Conn.On(xsapiv1.EVTSDKStateChange, func(e xsapiv1.EventMsg) {
49 sdk, _ := e.DecodeSDKEvent()
53 args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKStateChange}
54 require.Nil(t, HTTPCli.Post("/events/register", args, nil))
57 func DisconnectSDKStateChange(t *testing.T, sCli *IOSockClient) {
58 args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKStateChange}
59 require.Nil(t, HTTPCli.Post("/events/unregister", args, nil))
64 sdkDir := os.Getenv(envXdtSdk)
65 os.MkdirAll(sdkDir, 0755)
66 currentDir, err := os.Getwd()
70 if err := Copy(path.Join(currentDir, sdkFile), path.Join(sdkDir, sdkFileName)); err != nil {
75 func RemoveSdk(t *testing.T, sdk xsapiv1.SDK, chSdks chan xsapiv1.SDK) {
76 Debugf(t, "remove sdk %v", sdk.ID)
77 require.Nil(t, HTTPCli.Delete("/sdks/"+sdk.ID, &sdk))
78 sdkFromEvt := <-chSdks //waiting for event Uninstalling
79 require.Equal(t, sdk.ID, sdkFromEvt.ID)
80 require.Equal(t, sdkFromEvt.Status, "Un-installing")
81 sdkFromEvt = <-chSdks //waiting for event not installed
82 require.Equal(t, sdkFromEvt.Status, "Not Installed")
83 os.RemoveAll(path.Join(os.Getenv(envXdtSdk), sdk.Profile))
84 os.Remove(strings.Replace(sdk.URL, "file://", "", 1))
87 func TestSdks(t *testing.T) {
89 /*get sdk list from sdk_latest.json*/
90 var sdkArray []xsapiv1.SDK
91 require.Nil(t, HTTPCli.Get("/sdks", &sdkArray))
92 _, err := os.Stat(path.Join(os.Getenv(envXdtSdk), "sdks_latest.json"))
95 /*create channel fro SDK event*/
96 chSdks := make(chan xsapiv1.SDK)
98 ConnectSDKStateChange(t, sCli, chSdks)
100 /*checking startup installing of SDKs*/
101 for i := 0; i < len(sdkArray); i++ {
102 re := regexp.MustCompile("^[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+$")
103 require.True(t, re.MatchString(sdkArray[i].ID))
104 require.Equal(t, sdkArray[0].Status, "Not Installed")
107 require.Nil(t, HTTPCli.Get("/sdks/"+sdkArray[i].ID, &sdk))
108 require.Equal(t, sdkArray[i], sdk)
110 var sdkRes xsapiv1.SDK
112 Debugf(t, "install sdk %v", sdk.Name)
113 require.Nil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
114 require.Equal(t, sdkRes.Status, "Installing")
115 sdkFromEvent := <-chSdks //waiting for installing event
116 require.Equal(t, sdkRes.ID, sdkFromEvent.ID)
117 require.Equal(t, sdkFromEvent.Status, "Installing")
119 /*abort sdk install*/
120 Debugf(t, "abort install sdk %v", sdk.Name)
121 require.Nil(t, HTTPCli.Post("/sdks/abortinstall", sdk, &sdkRes))
122 require.Equal(t, sdkRes.Status, "Not Installed")
123 sdkFromEvent = <-chSdks //waiting for not installed status*/
124 require.Equal(t, sdkRes.ID, sdkFromEvent.ID)
125 require.Equal(t, sdkRes.Status, sdkFromEvent.Status)
129 func installFakeSdk(t *testing.T, sdkInstall xsapiv1.SDKInstallArgs, chSdks chan xsapiv1.SDK) xsapiv1.SDK {
131 var sdkRes xsapiv1.SDK
132 require.Nil(t, HTTPCli.Post("/sdks", sdkInstall, &sdkRes))
133 Debugf(t, "Install fake sdk %v (force=%v)", sdkRes.Name, sdkInstall.Force)
134 require.Equal(t, sdkRes.Status, "Installing")
135 sdkFromEvent := <-chSdks
136 require.Equal(t, sdkRes.ID, sdkFromEvent.ID)
137 require.Equal(t, sdkFromEvent.Status, "Installing")
138 /*waiting for SDKStateChange event from channel*/
139 sdkFromEvent = <-chSdks
140 require.Equal(t, sdkRes.ID, sdkFromEvent.ID)
141 require.Equal(t, "Installed", sdkFromEvent.Status)
142 require.Equal(t, sdkFromEvent.LastError, "")
143 Debugf(t, "Fake sdk %v installed", sdkFromEvent.Name)
147 func TestInstallFakeSdk(t *testing.T) {
148 sdk := xsapiv1.SDKInstallArgs{
150 Filename: sdkFileName,
154 chSdks := make(chan xsapiv1.SDK)
156 ConnectSDKStateChange(t, sCli, chSdks)
157 installFakeSdk(t, sdk, chSdks)
159 /*test force install*/
161 sdkRes := installFakeSdk(t, sdk, chSdks)
162 RemoveSdk(t, sdkRes, chSdks)
163 DisconnectSDKStateChange(t, sCli)
166 func TestSdksError(t *testing.T) {
167 sdk := xsapiv1.SDKInstallArgs{
172 Debugf(t, "error while installing sdk with no ID and no Filename")
173 var sdkRes xsapiv1.SDK
174 /*error with no ID no filename*/
175 require.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
176 sdk.ID = "42885c24-374e-3ef0-9723-0c8a05191aa"
177 Debugf(t, "error while installing sdk with a fake id=%v", sdk.ID)
178 /*error with fake ID*/
179 require.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))
181 sdk.Filename = "fake"
182 Debugf(t, "error while installing sdk with a fake filename=%v", sdk.Filename)
183 /*error with fake filename*/
184 require.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes))