mirror of
https://github.com/umputun/reproxy.git
synced 2024-08-17 04:30:25 +03:00
prevent log access races in tests
suppress false postive linter warns
This commit is contained in:
parent
271beedc99
commit
d3bbec8818
@ -9,6 +9,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -20,7 +21,16 @@ import (
|
|||||||
"github.com/umputun/reproxy/lib"
|
"github.com/umputun/reproxy/lib"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var setupLoggerOnce sync.Once
|
||||||
|
|
||||||
|
func setupLogger() {
|
||||||
|
setupLoggerOnce.Do(func() {
|
||||||
|
log.Setup(log.Debug, log.CallerFile, log.CallerFunc, log.Msec, log.LevelBraces)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func Test_Main(t *testing.T) {
|
func Test_Main(t *testing.T) {
|
||||||
|
setupLogger()
|
||||||
|
|
||||||
port := 40000 + int(rand.Int31n(10000))
|
port := 40000 + int(rand.Int31n(10000))
|
||||||
os.Args = []string{"test", "--static.enabled",
|
os.Args = []string{"test", "--static.enabled",
|
||||||
@ -45,7 +55,6 @@ func Test_Main(t *testing.T) {
|
|||||||
close(finished)
|
close(finished)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// defer cleanup because require check below can fail
|
|
||||||
defer func() {
|
defer func() {
|
||||||
close(done)
|
close(done)
|
||||||
<-finished
|
<-finished
|
||||||
@ -96,6 +105,8 @@ func Test_Main(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_MainWithSSL(t *testing.T) {
|
func Test_MainWithSSL(t *testing.T) {
|
||||||
|
setupLogger()
|
||||||
|
|
||||||
port := 40000 + int(rand.Int31n(10000))
|
port := 40000 + int(rand.Int31n(10000))
|
||||||
os.Args = []string{"test", "--static.enabled",
|
os.Args = []string{"test", "--static.enabled",
|
||||||
"--static.rule=*,/svc1, https://httpbin.org/get,https://feedmaster.umputun.com/ping",
|
"--static.rule=*,/svc1, https://httpbin.org/get,https://feedmaster.umputun.com/ping",
|
||||||
@ -118,7 +129,6 @@ func Test_MainWithSSL(t *testing.T) {
|
|||||||
close(finished)
|
close(finished)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// defer cleanup because require check below can fail
|
|
||||||
defer func() {
|
defer func() {
|
||||||
close(done)
|
close(done)
|
||||||
<-finished
|
<-finished
|
||||||
@ -128,7 +138,6 @@ func Test_MainWithSSL(t *testing.T) {
|
|||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
// allow self-signed certificate
|
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
},
|
},
|
||||||
@ -156,6 +165,8 @@ func Test_MainWithSSL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_MainWithPlugin(t *testing.T) {
|
func Test_MainWithPlugin(t *testing.T) {
|
||||||
|
setupLogger()
|
||||||
|
|
||||||
proxyPort := rand.Intn(10000) + 40000
|
proxyPort := rand.Intn(10000) + 40000
|
||||||
conductorPort := rand.Intn(10000) + 40000
|
conductorPort := rand.Intn(10000) + 40000
|
||||||
os.Args = []string{"test", "--static.enabled",
|
os.Args = []string{"test", "--static.enabled",
|
||||||
@ -181,7 +192,6 @@ func Test_MainWithPlugin(t *testing.T) {
|
|||||||
close(finished)
|
close(finished)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// defer cleanup because require check below can fail
|
|
||||||
defer func() {
|
defer func() {
|
||||||
close(done)
|
close(done)
|
||||||
<-finished
|
<-finished
|
||||||
@ -222,6 +232,7 @@ func Test_MainWithPlugin(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_listenAddress(t *testing.T) {
|
func Test_listenAddress(t *testing.T) {
|
||||||
|
setupLogger()
|
||||||
|
|
||||||
tbl := []struct {
|
tbl := []struct {
|
||||||
addr string
|
addr string
|
||||||
@ -254,6 +265,8 @@ func Test_listenAddress(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_redirHTTPPort(t *testing.T) {
|
func Test_redirHTTPPort(t *testing.T) {
|
||||||
|
setupLogger()
|
||||||
|
|
||||||
tbl := []struct {
|
tbl := []struct {
|
||||||
port int
|
port int
|
||||||
env string
|
env string
|
||||||
@ -281,6 +294,7 @@ func Test_redirHTTPPort(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_sizeParse(t *testing.T) {
|
func Test_sizeParse(t *testing.T) {
|
||||||
|
setupLogger()
|
||||||
|
|
||||||
tbl := []struct {
|
tbl := []struct {
|
||||||
inp string
|
inp string
|
||||||
@ -315,7 +329,6 @@ func Test_sizeParse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func waitForHTTPServerStart(port int) {
|
func waitForHTTPServerStart(port int) {
|
||||||
// wait for up to 10 seconds for server to start before returning it
|
|
||||||
client := http.Client{Timeout: time.Second}
|
client := http.Client{Timeout: time.Second}
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
time.Sleep(time.Millisecond * 100)
|
time.Sleep(time.Millisecond * 100)
|
||||||
@ -328,8 +341,7 @@ func waitForHTTPServerStart(port int) {
|
|||||||
|
|
||||||
type TestPlugin struct{}
|
type TestPlugin struct{}
|
||||||
|
|
||||||
// nolint
|
func (h *TestPlugin) HeaderThing(req *lib.Request, res *lib.Response) error { //nolint:unparam // doesn't fail in tests
|
||||||
func (h *TestPlugin) HeaderThing(req *lib.Request, res *lib.Response) (err error) {
|
|
||||||
log.Printf("req: %+v", req)
|
log.Printf("req: %+v", req)
|
||||||
res.HeadersIn = http.Header{}
|
res.HeadersIn = http.Header{}
|
||||||
res.HeadersIn.Add("inh", "val")
|
res.HeadersIn.Add("inh", "val")
|
||||||
@ -339,8 +351,7 @@ func (h *TestPlugin) HeaderThing(req *lib.Request, res *lib.Response) (err error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint
|
func (h *TestPlugin) ErrorThing(req lib.Request, res *lib.Response) error { //nolint:unparam // doesn't fail in tests
|
||||||
func (h *TestPlugin) ErrorThing(req lib.Request, res *lib.Response) (err error) {
|
|
||||||
log.Printf("req: %+v", req)
|
log.Printf("req: %+v", req)
|
||||||
if req.URL == "/fail" {
|
if req.URL == "/fail" {
|
||||||
res.StatusCode = 500
|
res.StatusCode = 500
|
||||||
@ -353,6 +364,7 @@ func (h *TestPlugin) ErrorThing(req lib.Request, res *lib.Response) (err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_splitAtCommas(t *testing.T) {
|
func Test_splitAtCommas(t *testing.T) {
|
||||||
|
setupLogger()
|
||||||
|
|
||||||
tbl := []struct {
|
tbl := []struct {
|
||||||
inp string
|
inp string
|
||||||
@ -377,6 +389,8 @@ func Test_splitAtCommas(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_makeBasicAuth(t *testing.T) {
|
func Test_makeBasicAuth(t *testing.T) {
|
||||||
|
setupLogger()
|
||||||
|
|
||||||
pf := `test:$2y$05$zMxDmK65SjcH2vJQNopVSO/nE8ngVLx65RoETyHpez7yTS/8CLEiW
|
pf := `test:$2y$05$zMxDmK65SjcH2vJQNopVSO/nE8ngVLx65RoETyHpez7yTS/8CLEiW
|
||||||
test2:$2y$05$TLQqHh6VT4JxysdKGPOlJeSkkMsv.Ku/G45i7ssIm80XuouCrES12
|
test2:$2y$05$TLQqHh6VT4JxysdKGPOlJeSkkMsv.Ku/G45i7ssIm80XuouCrES12
|
||||||
bad bad`
|
bad bad`
|
||||||
|
Loading…
Reference in New Issue
Block a user