-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain_test.go
119 lines (96 loc) · 2.41 KB
/
main_test.go
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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"syscall"
"testing"
"time"
)
var progPath string
var configPath = filepath.Join("example", "config", "config.yml")
const progName = "exporter_proxy"
func TestMain(m *testing.M) {
flag.Parse()
if testing.Short() {
os.Exit(m.Run())
}
var err error
progPath, err = os.Getwd()
if err != nil {
fmt.Printf("can't get current dir :%s \n", err)
os.Exit(1)
}
progPath = filepath.Join(progPath, progName)
build := exec.Command("go", "build", "-o", progPath)
output, err := build.CombinedOutput()
if err != nil {
fmt.Printf("compilation error: %s \n", output)
os.Exit(1)
}
exitCode := m.Run()
os.Remove(progPath)
os.Exit(exitCode)
}
func TestStartupExitCodeWithInvalidConfig(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
fakeInputFile := "fake-input-file"
expectedExitStatus := 1
prog := exec.Command(progPath, "--config="+fakeInputFile)
err := prog.Run()
if err == nil {
t.Errorf("Command execution succeeded with an invalid config file: %v", err)
}
if exitError, ok := err.(*exec.ExitError); ok {
status := exitError.Sys().(syscall.WaitStatus)
if status.ExitStatus() != expectedExitStatus {
t.Errorf("unexpected exit code with invalid configuration file")
}
} else {
t.Errorf("unable to retrieve the exit status: %v", err)
}
}
func TestStartupExitCodeWithValidConfig(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
prog := exec.Command(progPath, "--config="+configPath)
err := prog.Start()
if err != nil {
t.Errorf("command execution error: %v", err)
return
}
done := make(chan error)
go func() {
done <- prog.Wait()
}()
var startedOk bool
var stoppedErr error
for x := 0; x < 10; x++ {
if _, err := http.Get("http://localhost:9099"); err == nil {
startedOk = true
prog.Process.Signal(os.Interrupt)
select {
case stoppedErr = <-done:
break
case <-time.After(10 * time.Second):
}
break
}
time.Sleep(500 * time.Millisecond)
}
if !startedOk {
t.Errorf("%s did not start in the specified timeout", progName)
return
}
if err := prog.Process.Kill(); err == nil {
t.Errorf("%s didn't shutdown gracefully after sending the Interrupt signal", progName)
} else if stoppedErr != nil && stoppedErr.Error() != "signal: interrupt" {
t.Errorf("%s exited with an unexpected error:%v", progName, stoppedErr)
}
}