summaryrefslogtreecommitdiffstats
path: root/test/xdsserver_test.go
blob: 76c54eacdc2a674bb7e15ad30d89eb394526cfe7 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * 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 (
	"io"
	"log"
	"os"
	"os/exec"
	"path"
	"testing"
	"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 Copy(src, dst string) error {
	in, err := os.Open(src)
	if err != nil {
		return err
	}
	defer in.Close()

	out, err := os.Create(dst)
	if err != nil {
		return err
	}
	defer out.Close()

	_, err = io.Copy(out, in)
	if err != nil {
		return err
	}
	return out.Close()
}

func initEnv(launchProcess bool) {
	if launchProcess {
		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)
	}
	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)
	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 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-Sid",
		CsrfDisable:         true,
		LogOut:              file,
		LogPrefix:           "XDSSERVERTEST: ",
		LogLevel:            lvl,
	}
	cli, err := common.HTTPNewClient(prefixURL, conf)
	if err != nil {
		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) {
	launchProcess := true
	log.Printf("TestMain: launchProcess is %v", launchProcess)
	initEnv(launchProcess)

	var proc *os.Process
	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()
	defer os.Exit(res)
}

func init() {
}