functionnal test: separate tests into files
[src/xds/xds-server.git] / test / xdsserver_test.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Clément Bénier <clement.benier@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 package xdsservertest
18
19 import (
20         "log"
21         "os"
22         "os/exec"
23         "testing"
24         "time"
25
26         common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
27 )
28
29 //global client
30 var HTTPCli *common.HTTPClient
31 var logDir string
32
33 func initEnv() {
34         cmd := exec.Command("killall", "-9", "xds-server")
35         if err := cmd.Start(); err != nil {
36                 log.Fatal(err)
37         }
38         cmd.Wait()
39         rootTestLog := "/tmp/xds-server-test"
40         if err := os.Setenv(envRootCfgDir, rootTestLog); err != nil {
41                 log.Fatal(err)
42         }
43         os.RemoveAll(rootTestLog)
44         os.MkdirAll(rootTestLog, 0755)
45         logDir = rootTestLog + "/logs/"
46         os.MkdirAll(logDir, 0755)
47 }
48
49 func launchXdsServer(proc **os.Process) *os.File {
50         logFile := logDir + logFileXdsServer
51         file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
52         if err != nil {
53                 log.Fatal(err)
54         }
55         tmpProc, err := os.StartProcess(argsProcess[0], argsProcess, &os.ProcAttr{
56                 Files: []*os.File{os.Stdin, file, file},
57         })
58         if err != nil {
59                 log.Fatal(err)
60         }
61         *proc = tmpProc
62         return file
63 }
64
65 func getHTTPClient(lvl int) (*common.HTTPClient, *os.File) {
66         logFile := logDir + logFileClient
67         file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
68         if err != nil {
69                 log.Fatal(err)
70         }
71         conf := common.HTTPClientConfig{
72                 URLPrefix:           "/api/v1",
73                 HeaderClientKeyName: "Xds-Test-Sid",
74                 CsrfDisable:         true,
75                 LogOut:              file,
76                 LogPrefix:           "XDSSERVERTEST: ",
77                 LogLevel:            lvl,
78         }
79         cli, err := common.HTTPNewClient(prefixURL, conf)
80         if err != nil {
81                 log.Print(err)
82         }
83         return cli, file
84 }
85
86 func TestMain(m *testing.M) {
87         initEnv()
88
89         var proc *os.Process
90         fileXdsServer := launchXdsServer(&proc)
91         go func(p *os.Process) {
92                 if status, err := p.Wait(); err != nil {
93                         log.Fatalf("status=%v\n err=%v\n", status, err)
94                 }
95         }(proc)
96         time.Sleep(1 * time.Second)
97
98         lvl := common.HTTPLogLevelDebug
99         var fileHTTPClient *os.File
100         HTTPCli, fileHTTPClient = getHTTPClient(lvl)
101
102         if HTTPCli == nil {
103                 log.Fatal("HTTPCLi is nil")
104         }
105         res := m.Run()
106         proc.Kill()
107         fileXdsServer.Close()
108         fileHTTPClient.Close()
109         os.Exit(res)
110 }
111
112 func init() {
113 }