diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2021-09-08 11:25:51 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2021-09-09 08:44:28 +0300 |
commit | cbad89acd5112622d840423d48e4122737758795 (patch) | |
tree | d17c5b4e5856b2a5c1c232e485c69808a1120dc7 | |
parent | 6b9b03db88911c9939d5fc1d92d4b15912cc7665 (diff) |
test: PoC share random port through channels
-rw-r--r-- | main.go | 6 | ||||
-rw-r--r-- | test/acceptance/helpers_test.go | 47 | ||||
-rw-r--r-- | test/acceptance/serving_test.go | 25 |
3 files changed, 50 insertions, 28 deletions
@@ -4,6 +4,7 @@ import ( "fmt" "io" "math/rand" + "net" "os" "strings" "time" @@ -121,9 +122,10 @@ func createAppListeners(config *cfg.Config) []io.Closer { for _, addr := range config.ListenHTTPStrings.Split() { l, f := createSocket(addr) closers = append(closers, l, f) - + _, port, _ := net.SplitHostPort(l.Addr().String()) log.WithFields(log.Fields{ - "listener": addr, + "listener": l.Addr().String(), + "port": port, }).Debug("Set up HTTP listener") httpListeners = append(httpListeners, f.Fd()) diff --git a/test/acceptance/helpers_test.go b/test/acceptance/helpers_test.go index 33a19127..e979aec8 100644 --- a/test/acceptance/helpers_test.go +++ b/test/acceptance/helpers_test.go @@ -14,6 +14,7 @@ import ( "os" "os/exec" "path" + "regexp" "strings" "sync" "testing" @@ -162,7 +163,7 @@ func (l ListenSpec) URL(suffix string) string { // Returns only once this spec points at a working TCP server func (l ListenSpec) WaitUntilRequestSucceeds(done chan struct{}) error { - timeout := 5 * time.Second + timeout := time.Second for start := time.Now(); time.Since(start) < timeout; { select { case <-done: @@ -200,23 +201,11 @@ func (l ListenSpec) WaitUntilRequestSucceeds(done chan struct{}) error { func (l ListenSpec) JoinHostPort() string { return net.JoinHostPort(l.Host, l.Port) } - -// RunPagesProcessWithoutGitLabStub will start a gitlab-pages process with the specified listeners -// and return a function you can call to shut it down again. Use -// GetPageFromProcess to do a HTTP GET against a listener. -// -// If run as root via sudo, the gitlab-pages process will drop privileges -func RunPagesProcessWithoutGitLabStub(t *testing.T, pagesBinary string, listeners []ListenSpec, promPort string, extraArgs ...string) (teardown func()) { - _, cleanup := runPagesProcess(t, true, pagesBinary, listeners, promPort, nil, extraArgs...) - return cleanup -} - -func RunPagesProcessWithEnvs(t *testing.T, wait bool, pagesBinary string, listeners []ListenSpec, promPort string, envs []string, extraArgs ...string) (teardown func()) { - _, cleanup := runPagesProcess(t, wait, pagesBinary, listeners, promPort, envs, extraArgs...) - return cleanup +func RunPagesProcess(t *testing.T, opts ...processOption) *LogCaptureBuffer { + return RunPagesProcessWithCh(t, nil, opts...) } -func RunPagesProcess(t *testing.T, opts ...processOption) *LogCaptureBuffer { +func RunPagesProcessWithCh(t *testing.T, portCh chan string, opts ...processOption) *LogCaptureBuffer { chdir := false chdirCleanup := testhelpers.ChdirInPath(t, "../../shared/pages", &chdir) @@ -243,7 +232,7 @@ func RunPagesProcess(t *testing.T, opts ...processOption) *LogCaptureBuffer { "-api-secret-key", gitLabAPISecretKey, ) - logBuf, cleanup := runPagesProcess(t, processCfg.wait, processCfg.pagesBinary, processCfg.listeners, "", processCfg.envs, processCfg.extraArgs...) + logBuf, cleanup := runPagesProcess(t, processCfg.wait, processCfg.pagesBinary, processCfg.listeners, "", processCfg.envs, portCh, processCfg.extraArgs...) t.Cleanup(func() { source.Close() @@ -274,7 +263,9 @@ func RunPagesProcessWithSSLCertDir(t *testing.T, listeners []ListenSpec, sslCert }) } -func runPagesProcess(t *testing.T, wait bool, pagesBinary string, listeners []ListenSpec, promPort string, extraEnv []string, extraArgs ...string) (*LogCaptureBuffer, func()) { +var portRegexp = regexp.MustCompile("\"port\":\"([0-9]*?)\"") + +func runPagesProcess(t *testing.T, wait bool, pagesBinary string, listeners []ListenSpec, promPort string, extraEnv []string, portCh chan string, extraArgs ...string) (*LogCaptureBuffer, func()) { t.Helper() _, err := os.Stat(pagesBinary) @@ -290,6 +281,26 @@ func runPagesProcess(t *testing.T, wait bool, pagesBinary string, listeners []Li cmd.Stderr = out require.NoError(t, cmd.Start()) t.Logf("Running %s %v", pagesBinary, args) + go func() { + if portCh != nil { + defer close(portCh) + for i := 0; i < 20; i++ { + match := portRegexp.FindStringSubmatch(logBuf.String()) + if len(match) > 1 { + fmt.Printf("FOUND THE PORT!: %q\n", match[1]) + //port = match[1] + portCh <- match[1] + //close(portCh) + return + } + //fmt.Printf("what is the logbuf output:%s\n", logBuf.String()) + time.Sleep(100 * time.Millisecond) + + } + //portCh <- "49999" + //close(portCh) + } + }() waitCh := make(chan struct{}) go func() { diff --git a/test/acceptance/serving_test.go b/test/acceptance/serving_test.go index c6a7d3ef..020d81f3 100644 --- a/test/acceptance/serving_test.go +++ b/test/acceptance/serving_test.go @@ -12,15 +12,24 @@ import ( ) func TestUnknownHostReturnsNotFound(t *testing.T) { - RunPagesProcess(t) - - for _, spec := range supportedListeners() { - rsp, err := GetPageFromListener(t, spec, "invalid.invalid", "") - - require.NoError(t, err) - rsp.Body.Close() - require.Equal(t, http.StatusNotFound, rsp.StatusCode) + portCh := make(chan string) + RunPagesProcessWithCh(t, portCh, withListeners([]ListenSpec{httpListener}), withoutWait) + + port := <-portCh + fmt.Printf("WE SHOULD HAVE GOT THE PORT... %q\n", port) + listener := ListenSpec{ + Type: "http", + Host: "127.0.0.1", + Port: port, } + fmt.Printf("THE LISTENER's PORT?: %q\n", listener.Port) + //for _, spec := range supportedListeners() { + rsp, err := GetPageFromListener(t, listener, "invalid.invalid", "") + + require.NoError(t, err) + rsp.Body.Close() + require.Equal(t, http.StatusNotFound, rsp.StatusCode) + //} } func TestUnknownProjectReturnsNotFound(t *testing.T) { |