summaryrefslogtreecommitdiffstats
path: root/test/xdsserver_test.go
blob: f2b4d9d8f5f82e6b4878b8785a44e6e8e9072c71 (plain)
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
/*
 * 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"
	"os/exec"
	"testing"
	"time"

	common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
)

//global client
var HTTPCli *common.HTTPClient
var logDir string

func initEnv() {
	cmd := exec.Command("killall", "-9", "xds-server")
	if err := cmd.Start(); err != nil {
		log.Fatal(err)
	}
	cmd.Wait()
	rootTestLog := "/tmp/xds-server-test"
	if err := os.Setenv(envRootCfgDir, rootTestLog); err != nil {
		log.Fatal(err)
	}
	os.RemoveAll(rootTestLog)
	os.MkdirAll(rootTestLog, 0755)
	logDir = rootTestLog + "/logs/"
	os.MkdirAll(logDir, 0755)
}

func launchXdsServer(proc **os.Process) *os.File {
	logFile := logDir + logFileXdsServer
	file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatal(err)
	}
	tmpProc, err := os.StartProcess(argsProcess[0], argsProcess, &os.ProcAttr{
		Files: []*os.File{os.Stdin, file, file},
	})
	if err != nil {
		log.Fatal(err)
	}
	*proc = tmpProc
	return file
}

func getHTTPClient(lvl int) (*common.HTTPClient, *os.File) {
	logFile := logDir + logFileClient
	file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatal(err)
	}
	conf := common.HTTPClientConfig{
		URLPrefix:           "/api/v1",
		HeaderClientKeyName: "Xds-Test-Sid",
		CsrfDisable:         true,
		LogOut:              file,
		LogPrefix:           "XDSSERVERTEST: ",
		LogLevel:            lvl,
	}
	cli, err := common.HTTPNewClient(prefixURL, conf)
	if err != nil {
		log.Print(err)
	}
	return cli, file
}

func TestMain(m *testing.M) {
	initEnv()

	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)
	time.Sleep(1 * time.Second)

	lvl := common.HTTPLogLevelDebug
	var fileHTTPClient *os.File
	HTTPCli, fileHTTPClient = getHTTPClient(lvl)

	if HTTPCli == nil {
		log.Fatal("HTTPCLi is nil")
	}
	res := m.Run()
	proc.Kill()
	fileXdsServer.Close()
	fileHTTPClient.Close()
	os.Exit(res)
}

func init() {
}