code.gitea.io/gitea@v1.21.7/tests/e2e/utils_e2e_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package e2e
     5  
     6  import (
     7  	"context"
     8  	"net"
     9  	"net/http"
    10  	"net/url"
    11  	"testing"
    12  	"time"
    13  
    14  	"code.gitea.io/gitea/modules/setting"
    15  	"code.gitea.io/gitea/tests"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func onGiteaRunTB(t testing.TB, callback func(testing.TB, *url.URL), prepare ...bool) {
    21  	if len(prepare) == 0 || prepare[0] {
    22  		defer tests.PrepareTestEnv(t, 1)()
    23  	}
    24  	s := http.Server{
    25  		Handler: testE2eWebRoutes,
    26  	}
    27  
    28  	u, err := url.Parse(setting.AppURL)
    29  	assert.NoError(t, err)
    30  	listener, err := net.Listen("tcp", u.Host)
    31  	i := 0
    32  	for err != nil && i <= 10 {
    33  		time.Sleep(100 * time.Millisecond)
    34  		listener, err = net.Listen("tcp", u.Host)
    35  		i++
    36  	}
    37  	assert.NoError(t, err)
    38  	u.Host = listener.Addr().String()
    39  
    40  	defer func() {
    41  		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
    42  		s.Shutdown(ctx)
    43  		cancel()
    44  	}()
    45  
    46  	go s.Serve(listener)
    47  	// Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
    48  
    49  	callback(t, u)
    50  }
    51  
    52  func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) {
    53  	onGiteaRunTB(t, func(t testing.TB, u *url.URL) {
    54  		callback(t.(*testing.T), u)
    55  	}, prepare...)
    56  }