Skip to content

Commit

Permalink
print & log port bind failures, dns-related test coverage, test impro…
Browse files Browse the repository at this point in the history
…vements (#239)

* exercise puma-dev dns lookup

is there really no way to specify test-only dependencies?
move unexported methods to the bottom
first pass at catching errors from dns port bind
refactor to use tomb over chan
expose errors in listening for TLS and DNS
remove default error handling. bail if we didn't get a good address
remove "debug" messaging about not regenerating keypair
nevermind -- most folks don't run this interactively, and its handy to have it in the log
for now, just skip dns tests outside darwin
fix compile error

* test tcp and udp connection to dns

* extract darwin-only DNS test

skip high_sierra for now. fsevents are broken
loop over tcp, udp
go mod tidy
map[string](func() interface{}) is awesome.
refactor stdout capture to use return function instead of passed function call
forgot one
see if waiting for the shutdown to finish helps
assert serve doesn't serve an error either
only try to shutdown the server if we have a handle to it
defer shutdown and fail if unable to shutdown
remove timeout. we'll see if that fails.
remove error stubs
use constructor to init dns servers to avoid race
remove retry print
missed one
shorten timeout, add comments to clarify intent of retries

* only run one background puma-dev per platform; os.Exit will shutdown

only run one server per set of server test scenarios, so os.Exit will handle the cleanup
put shutdown back inside main -- it calls os.Exit() which borks testing
pull out additional state into each platform spec
ensure we run things on completely non-standard ports
missed an import

* alphabetize main test methods, pass flags to puma-dev boot

* remove unused import

* remove another unused import

* ensure we don't fail to boot the server, add additional dns tests

* ensure we set flags _before_ we read *fHTTPPort /facepalm

* trying to debug a really rare flake.
  • Loading branch information
nonrational authored Apr 6, 2020
1 parent 440d774 commit b98aa54
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 157 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ workflows:
version: 2
test:
jobs:
- test_high_sierra
# - test_high_sierra
- test_mojave
- test_catalina

34 changes: 19 additions & 15 deletions cmd/puma-dev/main_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
var (
fDebug = flag.Bool("debug", false, "enable debug output")
fDomains = flag.String("d", "test", "domains to handle, separate with :, defaults to test")
fPort = flag.Int("dns-port", 9253, "port to listen on dns for")
fDNSPort = flag.Int("dns-port", 9253, "port to listen on dns for")
fHTTPPort = flag.Int("http-port", 9280, "port to listen on http for")
fTLSPort = flag.Int("https-port", 9283, "port to listen on https for")
fDir = flag.String("dir", "~/.puma-dev", "directory to watch for apps")
Expand Down Expand Up @@ -110,18 +110,17 @@ func main() {
}
}()

stop := make(chan os.Signal, 1)

signal.Notify(stop, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM)
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM)

go func() {
<-stop
<-shutdown
fmt.Printf("! Shutdown requested\n")
pool.Purge()
os.Exit(0)
}()

err = dev.ConfigureResolver(domains, *fPort)
err = dev.ConfigureResolver(domains, *fDNSPort)
if err != nil {
log.Fatalf("Unable to configure OS X resolver: %s", err)
}
Expand All @@ -133,7 +132,7 @@ func main() {

fmt.Printf("* Directory for apps: %s\n", dir)
fmt.Printf("* Domains: %s\n", strings.Join(domains, ", "))
fmt.Printf("* DNS Server port: %d\n", *fPort)
fmt.Printf("* DNS Server port: %d\n", *fDNSPort)

if *fLaunch {
fmt.Printf("* HTTP Server port: inherited from launchd\n")
Expand All @@ -143,11 +142,12 @@ func main() {
fmt.Printf("* HTTPS Server port: %d\n", *fTLSPort)
}

var dns dev.DNSResponder

dns.Address = fmt.Sprintf("127.0.0.1:%d", *fPort)

go dns.Serve(domains)
dns := dev.NewDNSResponder(fmt.Sprintf("127.0.0.1:%d", *fDNSPort), domains)
go func() {
if err := dns.Serve(); err != nil {
fmt.Printf("! DNS Server failed: %v\n", err)
}
}()

var http dev.HTTPServer

Expand All @@ -169,12 +169,16 @@ func main() {
tlsSocketName = "SocketTLS"
}

fmt.Printf("! Puma dev listening on http and https\n")
fmt.Printf("! Puma dev running...\n")

go http.ServeTLS(tlsSocketName)
go func() {
if err := http.ServeTLS(tlsSocketName); err != nil {
fmt.Printf("! HTTPS Server failed: %v\n", err)
}
}()

err = http.Serve(socketName)
if err != nil {
log.Fatalf("Error listening: %s", err)
log.Fatalf("! HTTP Server failed: %s", err)
}
}
55 changes: 55 additions & 0 deletions cmd/puma-dev/main_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"context"
"fmt"
"net"
"testing"

"github.com/puma/puma-dev/homedir"

"github.com/stretchr/testify/assert"
)

func TestMainPumaDev_Darwin(t *testing.T) {
appLinkDir := homedir.MustExpand("~/.gotest-macos-puma-dev")

defer linkAllTestApps(t, appLinkDir)()

serveErr := configureAndBootPumaDevServer(t, map[string]string{
"d": "test:puma",
"dir": appLinkDir,
"dns-port": "65053",
"http-port": "65080",
"https-port": "65443",
})

assert.NoError(t, serveErr)

runPlatformAgnosticTestScenarios(t)

t.Run("resolve dns", func(t *testing.T) {
PumaDevDNSDialer := func(ctx context.Context, network, address string) (net.Conn, error) {
dnsAddress := fmt.Sprintf("127.0.0.1:%d", *fDNSPort)
d := net.Dialer{}
return d.DialContext(ctx, "udp", dnsAddress)
}

r := net.Resolver{
PreferGo: true,
Dial: PumaDevDNSDialer,
}

ctx := context.Background()
ips, err := r.LookupIPAddr(ctx, "foo.test")
assert.NoError(t, err)
assert.Equal(t, net.ParseIP("127.0.0.1").To4(), ips[0].IP.To4())

ips, err = r.LookupIPAddr(ctx, "foo.puma")
assert.NoError(t, err)
assert.Equal(t, net.ParseIP("127.0.0.1").To4(), ips[0].IP.To4())

_, err = r.LookupIPAddr(ctx, "foo.tlddoesnotexist")
assert.Error(t, err)
})
}
8 changes: 3 additions & 5 deletions cmd/puma-dev/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func main() {
pool.Events = &events

purge := make(chan os.Signal, 1)

signal.Notify(purge, syscall.SIGUSR1)

go func() {
Expand All @@ -73,12 +72,11 @@ func main() {
}
}()

stop := make(chan os.Signal, 1)

signal.Notify(stop, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM)
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM)

go func() {
<-stop
<-shutdown
fmt.Printf("! Shutdown requested\n")
pool.Purge()
os.Exit(0)
Expand Down
21 changes: 21 additions & 0 deletions cmd/puma-dev/main_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"testing"

"github.com/puma/puma-dev/homedir"
)

func TestMainPumaDev_Linux(t *testing.T) {
appLinkDir := homedir.MustExpand("~/.gotest-linux-puma-dev")

defer linkAllTestApps(t, appLinkDir)()

configureAndBootPumaDevServer(t, map[string]string{
"dir": appLinkDir,
"http-port": "65080",
"https-port": "65443",
})

runPlatformAgnosticTestScenarios(t)
}
Loading

0 comments on commit b98aa54

Please sign in to comment.