Skip to content

Commit

Permalink
only run one background puma-dev per platform; os.Exit will shutdown
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nonrational committed Apr 3, 2020
1 parent d65a2ff commit 8fcf220
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 153 deletions.
3 changes: 1 addition & 2 deletions cmd/puma-dev/main_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ var (

fCleanup = flag.Bool("cleanup", false, "Cleanup old system settings")
fUninstall = flag.Bool("uninstall", false, "Uninstall puma-dev as a user service")

shutdown = make(chan os.Signal, 1)
)

func main() {
Expand Down Expand Up @@ -112,6 +110,7 @@ func main() {
}
}()

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

go func() {
Expand Down
15 changes: 14 additions & 1 deletion cmd/puma-dev/main_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ import (
"net"
"testing"

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

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

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

defer linkAppsForTesting(t, appLinkDir)()

SetFlagOrFail(t, "dns-port", "65053")
SetFlagOrFail(t, "http-port", "65080")
SetFlagOrFail(t, "https-port", "65443")

bootConfiguredLivePumaServer(t, appLinkDir)

runPlatformAgnosticTestScenarios(t)

t.Run("resolve dns", func(t *testing.T) {
PumaDevDNSDialer := func(ctx context.Context, network, address string) (net.Conn, error) {
Expand Down
4 changes: 1 addition & 3 deletions cmd/puma-dev/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ var (
fDir = flag.String("dir", "~/.puma-dev", "directory to watch for apps")
fTimeout = flag.Duration("timeout", 15*60*time.Second, "how long to let an app idle for")
fStop = flag.Bool("stop", false, "Stop all puma-dev servers")

shutdown = make(chan os.Signal, 1)
)

func main() {
Expand Down Expand Up @@ -65,7 +63,6 @@ func main() {
pool.Events = &events

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

signal.Notify(purge, syscall.SIGUSR1)

go func() {
Expand All @@ -75,6 +72,7 @@ func main() {
}
}()

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

go func() {
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/dev/devtest"
"github.com/puma/puma-dev/homedir"
)

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

defer linkAppsForTesting(t, appLinkDir)()

SetFlagOrFail(t, "http-port", "65080")
SetFlagOrFail(t, "https-port", "65443")

bootConfiguredLivePumaServer(t, appLinkDir)

runPlatformAgnosticTestScenarios(t)
}
252 changes: 118 additions & 134 deletions cmd/puma-dev/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
"time"

Expand All @@ -23,16 +22,129 @@ import (
"github.com/stretchr/testify/assert"
)

var testAppLinkDirPath = homedir.MustExpand("~/.gotest-puma-dev")

func TestMain(m *testing.M) {
EnsurePumaDevDirectory()
os.Exit(m.Run())
testResult := m.Run()
os.Exit(testResult)
}

func TestMain_execWithExitStatus_versionFlag(t *testing.T) {
StubCommandLineArgs("-V")
assert.True(t, *fVersion)

execStdOut := WithStdoutCaptured(func() {
result := execWithExitStatus()
assert.Equal(t, 0, result.exitStatusCode)
assert.Equal(t, true, result.shouldExit)
})

assert.Regexp(t, "^Version: devel \\(.+\\)\\n$", execStdOut)
}

func TestMainPumaDev(t *testing.T) {
defer launchPumaDevBackgroundServerWithDefaults(t)()
func TestMain_execWithExitStatus_noFlag(t *testing.T) {
StubCommandLineArgs()
assert.False(t, *fVersion)

execStdOut := WithStdoutCaptured(func() {
result := execWithExitStatus()
assert.Equal(t, false, result.shouldExit)
})

assert.Equal(t, "", execStdOut)
}

func TestMain_execWithExitStatus_commandArgs(t *testing.T) {
StubCommandLineArgs("nosoupforyou")

execStdOut := WithStdoutCaptured(func() {
result := execWithExitStatus()
assert.Equal(t, 1, result.exitStatusCode)
assert.Equal(t, true, result.shouldExit)
})

assert.Equal(t, "Error: Unknown command: nosoupforyou\n\n", execStdOut)
}

func TestMain_allCheck_versionFlag(t *testing.T) {
if os.Getenv("GO_TEST_SUBPROCESS") == "1" {
StubCommandLineArgs("-V")
allCheck()

return
}

cmd := exec.Command(os.Args[0], "-test.run=TestMain_allCheck_versionFlag")
cmd.Env = append(os.Environ(), "GO_TEST_SUBPROCESS=1")
err := cmd.Run()

assert.Nil(t, err)
}

func TestMain_allCheck_badArg(t *testing.T) {
if os.Getenv("GO_TEST_SUBPROCESS") == "1" {
StubCommandLineArgs("-badarg")
allCheck()

return
}

cmd := exec.Command(os.Args[0], "-test.run=TestMain_allCheck_badArg")
cmd.Env = append(os.Environ(), "GO_TEST_SUBPROCESS=1")
err := cmd.Run()

exit, ok := err.(*exec.ExitError)

assert.True(t, ok)
assert.False(t, exit.Success())
}

func linkAppsForTesting(t *testing.T, workingDirPath string) func() {
MakeDirectoryOrFail(t, workingDirPath)

testAppsToLink := map[string]string{
"hipuma": "rack-hi-puma",
"static-site": "static-hi-puma",
}

for appLinkName, etcAppDir := range testAppsToLink {
appPath := filepath.Join(ProjectRoot, "etc", etcAppDir)
linkPath := filepath.Join(homedir.MustExpand(workingDirPath), appLinkName)

if err := os.Symlink(appPath, linkPath); err != nil {
assert.FailNow(t, err.Error())
}
}

return func() {
RemoveDirectoryOrFail(t, workingDirPath)
}
}

func bootConfiguredLivePumaServer(t *testing.T, workingDirPath string) error {
address := fmt.Sprintf("localhost:%d", *fHTTPPort)
timeout := time.Duration(2 * time.Second)

if _, err := net.DialTimeout("tcp", address, timeout); err == nil {
return fmt.Errorf("server is already running")
}

generateLivePumaDevCertIfNotExist(t)
StubCommandLineArgs()
SetFlagOrFail(t, "dir", workingDirPath)

go func() {
main()
}()

return retry.Do(
func() error {
_, err := net.DialTimeout("tcp", address, timeout)
return err
},
)
}

func runPlatformAgnosticTestScenarios(t *testing.T) {
t.Run("status", func(t *testing.T) {
reqURL := fmt.Sprintf("http://localhost:%d/status", *fHTTPPort)
statusHost := "puma-dev"
Expand Down Expand Up @@ -121,76 +233,6 @@ func TestMainPumaDev(t *testing.T) {
})
}

func TestMain_execWithExitStatus_versionFlag(t *testing.T) {
StubCommandLineArgs("-V")
assert.True(t, *fVersion)

execStdOut := WithStdoutCaptured(func() {
result := execWithExitStatus()
assert.Equal(t, 0, result.exitStatusCode)
assert.Equal(t, true, result.shouldExit)
})

assert.Regexp(t, "^Version: devel \\(.+\\)\\n$", execStdOut)
}

func TestMain_execWithExitStatus_noFlag(t *testing.T) {
StubCommandLineArgs()
assert.False(t, *fVersion)

execStdOut := WithStdoutCaptured(func() {
result := execWithExitStatus()
assert.Equal(t, false, result.shouldExit)
})

assert.Equal(t, "", execStdOut)
}

func TestMain_execWithExitStatus_commandArgs(t *testing.T) {
StubCommandLineArgs("nosoupforyou")

execStdOut := WithStdoutCaptured(func() {
result := execWithExitStatus()
assert.Equal(t, 1, result.exitStatusCode)
assert.Equal(t, true, result.shouldExit)
})

assert.Equal(t, "Error: Unknown command: nosoupforyou\n\n", execStdOut)
}

func TestMain_allCheck_versionFlag(t *testing.T) {
if os.Getenv("GO_TEST_SUBPROCESS") == "1" {
StubCommandLineArgs("-V")
allCheck()

return
}

cmd := exec.Command(os.Args[0], "-test.run=TestMain_allCheck_versionFlag")
cmd.Env = append(os.Environ(), "GO_TEST_SUBPROCESS=1")
err := cmd.Run()

assert.Nil(t, err)
}

func TestMain_allCheck_badArg(t *testing.T) {
if os.Getenv("GO_TEST_SUBPROCESS") == "1" {
StubCommandLineArgs("-badarg")
allCheck()

return
}

cmd := exec.Command(os.Args[0], "-test.run=TestMain_allCheck_badArg")
cmd.Env = append(os.Environ(), "GO_TEST_SUBPROCESS=1")
err := cmd.Run()

exit, ok := err.(*exec.ExitError)

assert.True(t, ok)
assert.False(t, exit.Success())
}

func generateLivePumaDevCertIfNotExist(t *testing.T) {
liveSupportPath := homedir.MustExpand(dev.SupportDir)
liveCertPath := filepath.Join(liveSupportPath, "cert.pem")
Expand All @@ -205,64 +247,6 @@ func generateLivePumaDevCertIfNotExist(t *testing.T) {
}
}

func launchPumaDevBackgroundServerWithDefaults(t *testing.T) func() {
address := fmt.Sprintf("localhost:%d", *fHTTPPort)
timeout := time.Duration(2 * time.Second)

if _, err := net.DialTimeout("tcp", address, timeout); err == nil {
return func() {}
}

generateLivePumaDevCertIfNotExist(t)

StubCommandLineArgs()
SetFlagOrFail(t, "dir", testAppLinkDirPath)
MakeDirectoryOrFail(t, testAppLinkDirPath)

testAppsToLink := map[string]string{
"hipuma": "rack-hi-puma",
"static-site": "static-hi-puma",
}

for appLinkName, etcAppDir := range testAppsToLink {
appPath := filepath.Join(ProjectRoot, "etc", etcAppDir)
linkPath := filepath.Join(homedir.MustExpand(testAppLinkDirPath), appLinkName)

if err := os.Symlink(appPath, linkPath); err != nil {
assert.FailNow(t, err.Error())
}
}

go func() {
main()
}()

// wait for server to come up
err := retry.Do(
func() error {
_, err := net.DialTimeout("tcp", address, timeout)
return err
},
)

assert.NoError(t, err)

return func() {
RemoveDirectoryOrFail(t, testAppLinkDirPath)
shutdown <- syscall.SIGTERM

// wait for server to shut down
retry.Do(
func() error {
if _, err := net.DialTimeout("tcp", address, timeout); err == nil {
return fmt.Errorf("server is still listening :%v", address)
}
return nil
},
)
}
}

func getURLWithHost(t *testing.T, url string, host string) string {
req, _ := http.NewRequest("GET", url, nil)
req.Host = host
Expand Down
Loading

0 comments on commit 8fcf220

Please sign in to comment.