diff options
author | Clément Bénier <clement.benier@iot.bzh> | 2018-08-21 18:58:26 +0200 |
---|---|---|
committer | Clément Bénier <clement.benier@iot.bzh> | 2018-08-21 18:58:32 +0200 |
commit | c095e1ea9a9e38f020ce4ae852d97c712be910b6 (patch) | |
tree | ce085c43116ceb9eda10fd79cfc3d18ecfe6387a /test/sdks_test.go | |
parent | 3c40079d8360c15f0ce52a6c11a2bc2555177463 (diff) | |
parent | 2fb2226b666e7a8a21dc8b852188b75c06e2cfc8 (diff) |
Merge remote-tracking branch 'origin/sandbox/benierc/test'
Change-Id: Idc200f10e217119df9edfebbaf839c5fd45b19a4
Signed-off-by: Clément Bénier <clement.benier@iot.bzh>
Diffstat (limited to 'test/sdks_test.go')
-rw-r--r-- | test/sdks_test.go | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/test/sdks_test.go b/test/sdks_test.go new file mode 100644 index 0000000..1395b16 --- /dev/null +++ b/test/sdks_test.go @@ -0,0 +1,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/assert" +) + +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} + assert.Nil(t, HTTPCli.Post("/events/register", args, nil)) +} + +func DisconnectSDKStateChange(t *testing.T, sCli *IOSockClient) { + args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKStateChange} + assert.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) + assert.Nil(t, HTTPCli.Delete("/sdks/"+sdk.ID, &sdk)) + sdkFromEvt := <-chSdks //waiting for event Uninstalling + assert.Equal(t, sdk.ID, sdkFromEvt.ID) + assert.Equal(t, sdkFromEvt.Status, "Un-installing") + sdkFromEvt = <-chSdks //waiting for event not installed + assert.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 + assert.Nil(t, HTTPCli.Get("/sdks", &sdkArray)) + _, err := os.Stat(path.Join(os.Getenv(envXdtSdk), "sdks_latest.json")) + assert.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]+$") + assert.True(t, re.MatchString(sdkArray[i].ID)) + assert.Equal(t, sdkArray[0].Status, "Not Installed") + + var sdk xsapiv1.SDK + assert.Nil(t, HTTPCli.Get("/sdks/"+sdkArray[i].ID, &sdk)) + assert.Equal(t, sdkArray[i], sdk) + + var sdkRes xsapiv1.SDK + /*install sdk*/ + Debugf(t, "install sdk %v", sdk.Name) + assert.Nil(t, HTTPCli.Post("/sdks", sdk, &sdkRes)) + assert.Equal(t, sdkRes.Status, "Installing") + sdkFromEvent := <-chSdks //waiting for installing event + assert.Equal(t, sdkRes.ID, sdkFromEvent.ID) + assert.Equal(t, sdkFromEvent.Status, "Installing") + + /*abort sdk install*/ + Debugf(t, "abort install sdk %v", sdk.Name) + assert.Nil(t, HTTPCli.Post("/sdks/abortinstall", sdk, &sdkRes)) + assert.Equal(t, sdkRes.Status, "Not Installed") + sdkFromEvent = <-chSdks //waiting for not installed status*/ + assert.Equal(t, sdkRes.ID, sdkFromEvent.ID) + assert.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 + assert.Nil(t, HTTPCli.Post("/sdks", sdkInstall, &sdkRes)) + Debugf(t, "Install fake sdk %v (force=%v)", sdkRes.Name, sdkInstall.Force) + assert.Equal(t, sdkRes.Status, "Installing") + sdkFromEvent := <-chSdks + assert.Equal(t, sdkRes.ID, sdkFromEvent.ID) + assert.Equal(t, sdkFromEvent.Status, "Installing") + /*waiting for SDKStateChange event from channel*/ + sdkFromEvent = <-chSdks + assert.Equal(t, sdkRes.ID, sdkFromEvent.ID) + assert.Equal(t, "Installed", sdkFromEvent.Status) + assert.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*/ + assert.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*/ + assert.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*/ + assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes)) +} |