forked from glebarez/cero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cero_test.go
92 lines (75 loc) · 1.74 KB
/
cero_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
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_main_addr(t *testing.T) {
// handler
handler := func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello")
}
// test TLS server
ts := httptest.NewTLSServer(http.HandlerFunc(handler))
defer ts.Close()
// grab URL of test TLS server
tsURL, _ := url.Parse(ts.URL)
// test atomic addr
os.Args = []string{"cero-test", tsURL.Host}
flag.CommandLine = flag.NewFlagSet("", flag.ExitOnError)
output := captureOutput(main)
assert.Equal(t, "example.com", strings.TrimSpace(output))
// test CIDR
host, port := splitHostPort(tsURL.Host)
os.Args = []string{"cero-test", fmt.Sprintf("%s/30:%s", host, port)}
flag.CommandLine = flag.NewFlagSet("", flag.ExitOnError)
output = captureOutput(main)
assert.Equal(t, "example.com", strings.TrimSpace(output))
}
// helper utility to grab stdout, stderr
func captureOutput(f func()) string {
// create os pipe to emulate file interface
reader, writer, err := os.Pipe()
if err != nil {
panic(err)
}
// save original descriptors for restoring later
stdout := os.Stdout
stderr := os.Stderr
defer func() {
os.Stdout = stdout
os.Stderr = stderr
}()
// replace standard descriptors
os.Stdout = writer
os.Stderr = writer
// create output channel
out := make(chan string)
// run background goroutine to capture output
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
var buf bytes.Buffer
wg.Done()
if _, err := io.Copy(&buf, reader); err != nil {
log.Fatal(err)
}
out <- buf.String()
}()
wg.Wait()
// run the function
f()
// return grabbed output
writer.Close()
return <-out
}