From 4edc85706942a10085947ef457fca9e4f209a21a Mon Sep 17 00:00:00 2001 From: Clément Bénier Date: Wed, 25 Jul 2018 16:02:30 +0200 Subject: sdk_test: draft in progress MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add tests for testing sdk - draft Change-Id: I81f2f4ff75d6256fd092afd5213ea4f3370fcc68 Signed-off-by: Clément Bénier --- .vscode/settings.json | 3 +- Makefile | 4 ++ glide.yaml | 3 +- test/_test-config.json | 2 +- test/config.go | 30 +++++++++++++ test/sdks_test.go | 120 +++++++++++++++++++++++++++++++++++++++++++++++++ test/test_config.go | 24 ---------- test/xdsserver_test.go | 67 +++++++++++++++++++-------- 8 files changed, 208 insertions(+), 45 deletions(-) create mode 100644 test/config.go create mode 100644 test/sdks_test.go delete mode 100644 test/test_config.go diff --git a/.vscode/settings.json b/.vscode/settings.json index c556d7f..45cbe81 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -21,7 +21,8 @@ "bin": true, "tools": true, "webapp/dist": true, - "webapp/node_modules": true + "webapp/node_modules": true, + "**/*~": true }, // Specify paths/files to ignore. (Supports Globs) "cSpell.ignorePaths": [ diff --git a/Makefile b/Makefile index 48bf907..4b0dea8 100644 --- a/Makefile +++ b/Makefile @@ -120,7 +120,11 @@ xds: scripts tools/syncthing/copytobin @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(LOCAL_BINDIR)/$(TARGET)$(EXT) -ldflags "$(GO_LDFLAGS) -X main.AppVersion=$(VERSION) -X main.AppSubVersion=$(SUB_VERSION)" -gcflags "$(GO_GCFLAGS)" . test: tools/glide +ifndef name go test --race ./test -v +else + go test --race ./test -v -run $(name) +endif vet: tools/glide go vet $(shell $(LOCAL_TOOLSDIR)/glide novendor) diff --git a/glide.yaml b/glide.yaml index 193ad48..69ee318 100644 --- a/glide.yaml +++ b/glide.yaml @@ -37,4 +37,5 @@ import: version: ^1.2.2 subpackages: - assert - +- package: github.com/sebd71/go-socket.io-client + version: 46defcb47f diff --git a/test/_test-config.json b/test/_test-config.json index 835bc79..223b3eb 100644 --- a/test/_test-config.json +++ b/test/_test-config.json @@ -1,5 +1,5 @@ { - "webAppDir": "${EXEPATH}/../webapp/dist", + "webAppDir": "${EXEPATH}/../webapp/src", "httpPort": "8000", "sdkScriptsDir": "${EXEPATH}/../sdks", "shareRootDir": "${XDS_SERVER_ROOT_CFG_DIR}/xds-server/projects", diff --git a/test/config.go b/test/config.go new file mode 100644 index 0000000..8fea851 --- /dev/null +++ b/test/config.go @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2017-2018 "IoT.bzh" + * Author Clément Bénier + * + * 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 + +var argsProcess = []string{"../bin/xds-server", "-l", "debug", "-c", "_test-config.json"} + +const ( + envRootCfgDir = "XDS_SERVER_ROOT_CFG_DIR" + prefixURL = "http://localhost:8000" + logFileXdsServer = "xdsserver-test.log" + logFileClient = "client-test.log" + envXdtSdk = "XDT_SDK" + envXdsServerWorkspaceDir = "XDS_SERVER_WORKSPACE_DIR" + envXdsServerRootCfgDir = "XDS_SERVER_ROOT_CFG_DIR" +) diff --git a/test/sdks_test.go b/test/sdks_test.go new file mode 100644 index 0000000..5efd8fc --- /dev/null +++ b/test/sdks_test.go @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2017-2018 "IoT.bzh" + * Author Clément Bénier + * + * 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 ( + "fmt" + "log" + "regexp" + "sync" + "testing" + "time" + + "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1" + socketio_client "github.com/sebd71/go-socket.io-client" + "github.com/stretchr/testify/assert" +) + +// Disconnection Channel used to notify XDS Server disconnection +//type Disconnection struct { +// error string +// code int +// svrConf ServerCfg +//} + +// IOSockClient . +type IOSockClient struct { + URL string + Conn *socketio_client.Client + Options *socketio_client.Options + EmitMutex *sync.Mutex + Connected bool + //ServerDiscoChan chan Disconnection + EscapeKeys []byte +} + +func NewIoSocketClient(url, clientID string) (*IOSockClient, error) { + + var err error + + sCli := &IOSockClient{ + URL: url, + EmitMutex: &sync.Mutex{}, + Options: &socketio_client.Options{ + Transport: "websocket", + Header: make(map[string][]string), + }, + //ServerDiscoChan: make(chan Disconnection, 1), + } + sCli.Options.Header["XDS-SID"] = []string{clientID} + + sCli.Conn, err = socketio_client.NewClient(url, sCli.Options) + if err != nil { + return nil, fmt.Errorf("IO.socket connection error: " + err.Error()) + } + + sCli.Conn.On("connection", func() { + log.Printf("BBBBBBBBBBBBBBBB:::WS connection event") + sCli.Connected = true + }) + + sCli.Conn.On("disconnection", func(err error) { + log.Printf("AAAAAAAAAAAA:::WS disconnection event with err: %v\n", err) + sCli.Connected = false + }) + + sCli.Conn.On(xsapiv1.EVTSDKManagement, func(data interface{}) { + log.Printf("WS disconnection event with err: %v\n", data) + }) + + args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKManagement} + if err := HTTPCli.Post("/events/register", args, nil); err != nil { + return sCli, err + } + + return sCli, nil +} + +func TestSdks(t *testing.T) { + var sdkArray []xsapiv1.SDK + assert.Nil(t, HTTPCli.Get("/sdks", &sdkArray)) + t.Log(sdkArray[0]) + + t.Logf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID()) + IOSkClient, err := NewIoSocketClient(prefixURL, HTTPCli.GetClientID()) + if err != nil { + t.Fatal(err) + } + t.Log(IOSkClient) + + //for i := 0; i < len(sdkArray); i++ { + for i := 0; i < 1; 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 + assert.Nil(t, HTTPCli.Post("/sdks", sdk, &sdkRes)) + assert.Equal(t, sdkRes.Status, "Installing") + //t.Log(sdkRes) + time.Sleep(1 * time.Second) + } +} diff --git a/test/test_config.go b/test/test_config.go deleted file mode 100644 index a182dd5..0000000 --- a/test/test_config.go +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2017-2018 "IoT.bzh" - * Author Clément Bénier - * - * 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 - -var argsProcess = []string{"../bin/xds-server", "-l", "debug", "-c", "_test-config.json"} - -const envRootCfgDir = "XDS_SERVER_ROOT_CFG_DIR" -const prefixURL = "http://localhost:8000" -const logFileXdsServer = "xdsserver-test.log" -const logFileClient = "client-test.log" diff --git a/test/xdsserver_test.go b/test/xdsserver_test.go index f2b4d9d..5722d31 100644 --- a/test/xdsserver_test.go +++ b/test/xdsserver_test.go @@ -24,26 +24,44 @@ import ( "time" common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1" ) //global client var HTTPCli *common.HTTPClient var logDir string +var sdkDir string -func initEnv() { - cmd := exec.Command("killall", "-9", "xds-server") - if err := cmd.Start(); err != nil { - log.Fatal(err) +func initEnv(launchProcess bool) { + if launchProcess { + cmd := exec.Command("killall", "-9", "xds-server") + if err := cmd.Start(); err != nil { + log.Fatal(err) + } + cmd.Wait() } - cmd.Wait() rootTestLog := "/tmp/xds-server-test" if err := os.Setenv(envRootCfgDir, rootTestLog); err != nil { log.Fatal(err) } + sdkDir = rootTestLog + "/sdks/" + if err := os.Setenv(envXdtSdk, sdkDir); err != nil { + log.Fatal(err) + } + if err := os.Setenv(envXdsServerWorkspaceDir, rootTestLog); err != nil { + log.Fatal(err) + } + if err := os.Setenv(envXdsServerRootCfgDir, rootTestLog); err != nil { + log.Fatal(err) + } + if err := os.Setenv("XDS_LOG_SILLY", "1"); err != nil { + log.Fatal(err) + } os.RemoveAll(rootTestLog) os.MkdirAll(rootTestLog, 0755) logDir = rootTestLog + "/logs/" os.MkdirAll(logDir, 0755) + os.MkdirAll(sdkDir, 0755) } func launchXdsServer(proc **os.Process) *os.File { @@ -70,7 +88,7 @@ func getHTTPClient(lvl int) (*common.HTTPClient, *os.File) { } conf := common.HTTPClientConfig{ URLPrefix: "/api/v1", - HeaderClientKeyName: "Xds-Test-Sid", + HeaderClientKeyName: "Xds-Sid", CsrfDisable: true, LogOut: file, LogPrefix: "XDSSERVERTEST: ", @@ -78,35 +96,48 @@ func getHTTPClient(lvl int) (*common.HTTPClient, *os.File) { } cli, err := common.HTTPNewClient(prefixURL, conf) if err != nil { - log.Print(err) + log.Fatal(err) + } + log.Printf("HTTP session ID : %v", cli.GetClientID()) + var ver xsapiv1.Version + err = cli.Get("/version", &ver) + if err != nil { + log.Fatal(err) } return cli, file } func TestMain(m *testing.M) { - initEnv() + launchProcess := true + log.Printf("TestMain: launchProcess is %v", launchProcess) + initEnv(launchProcess) var proc *os.Process - fileXdsServer := launchXdsServer(&proc) - go func(p *os.Process) { - if status, err := p.Wait(); err != nil { - log.Fatalf("status=%v\n err=%v\n", status, err) - } - }(proc) + var fileXdsServer *os.File + if launchProcess { + fileXdsServer = launchXdsServer(&proc) + go func(p *os.Process) { + if status, err := p.Wait(); err != nil { + log.Fatalf("status=%v\n err=%v\n", status, err) + } + }(proc) + defer proc.Kill() + defer fileXdsServer.Close() + } time.Sleep(1 * time.Second) lvl := common.HTTPLogLevelDebug var fileHTTPClient *os.File HTTPCli, fileHTTPClient = getHTTPClient(lvl) + defer fileHTTPClient.Close() + + log.Printf("HTTPCli id is %v", HTTPCli.GetClientID()) if HTTPCli == nil { log.Fatal("HTTPCLi is nil") } res := m.Run() - proc.Kill() - fileXdsServer.Close() - fileHTTPClient.Close() - os.Exit(res) + defer os.Exit(res) } func init() { -- cgit 1.2.3-korg