pull/862/head
RMT 2026-03-15 20:21:47 +08:00 committed by ginuerzh
parent 45d94cf391
commit c8b48dc248
4 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,4 @@
FROM alpine:3.22
# add iptables for tun/tap and curl for testing
RUN apk add --no-cache iptables curl

View File

@ -0,0 +1,28 @@
package e2e
import (
"fmt"
"os"
"os/exec"
"testing"
)
func TestMain(m *testing.M) {
// Compile the gost binary
cmd := exec.Command("go", "build", "-o", "/tmp/gost-test-bin", "../../cmd/gost")
cmd.Env = append(os.Environ(), "CGO_ENABLED=0")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Printf("Failed to compile gost: %v\n", err)
os.Exit(1)
}
// Run tests
code := m.Run()
// Cleanup
os.Remove("/tmp/gost-test-bin")
os.Exit(code)
}

View File

@ -0,0 +1,66 @@
package e2e
import (
"context"
"fmt"
"io"
"testing"
"github.com/stretchr/testify/suite"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/network"
)
type ParallelSelectorSuite struct {
suite.Suite
ctx context.Context
net *testcontainers.DockerNetwork
echoC testcontainers.Container
echoIP string
}
func (s *ParallelSelectorSuite) SetupSuite() {
s.ctx = context.Background()
net, err := network.New(s.ctx)
s.Require().NoError(err)
s.net = net
echoC, err := RunEchoContainer(s.ctx, s.net.Name)
s.Require().NoError(err)
s.echoC = echoC
echoIP, err := echoC.ContainerIP(s.ctx)
s.Require().NoError(err)
s.echoIP = echoIP
}
func (s *ParallelSelectorSuite) TearDownSuite() {
if s.echoC != nil {
s.echoC.Terminate(s.ctx)
}
if s.net != nil {
s.net.Remove(s.ctx)
}
}
func (s *ParallelSelectorSuite) TestParallelSelector() {
gostC, err := RunGostContainer(s.ctx, s.net.Name, "testdata/parallel_selector/server.yaml")
s.Require().NoError(err)
defer gostC.Terminate(s.ctx)
// Test the proxy by running curl inside the gost container
cmd := []string{"curl", "-v", "-s", "-x", "http://127.0.0.1:8080", fmt.Sprintf("http://%s:5678", s.echoIP)}
code, out, err := gostC.Exec(s.ctx, cmd)
s.Require().NoError(err)
body, err := io.ReadAll(out)
s.Require().NoError(err)
s.Require().Equal(0, code)
s.Require().Contains(string(body), "hello-gost")
}
func TestParallelSelectorSuite(t *testing.T) {
suite.Run(t, new(ParallelSelectorSuite))
}

42
tests/e2e/utils.go 100644
View File

@ -0,0 +1,42 @@
package e2e
import (
"context"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func RunEchoContainer(ctx context.Context, networkName string) (testcontainers.Container, error) {
req := testcontainers.ContainerRequest{
Image: "hashicorp/http-echo:latest",
Networks: []string{networkName},
Cmd: []string{"-text=hello-gost", "-listen=:5678"},
ExposedPorts: []string{"5678/tcp"},
WaitingFor: wait.ForHTTP("/").WithPort("5678/tcp"),
}
return testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
}
func RunGostContainer(ctx context.Context, networkName, yamlPath string) (testcontainers.Container, error) {
req := testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Context: ".",
Dockerfile: "Dockerfile",
KeepImage: true,
},
Networks: []string{networkName},
Files: []testcontainers.ContainerFile{
{HostFilePath: "/tmp/gost-test-bin", ContainerFilePath: "/bin/gost", FileMode: 0755},
{HostFilePath: yamlPath, ContainerFilePath: "/config.yaml", FileMode: 0644},
},
Cmd: []string{"/bin/gost", "-C", "/config.yaml"},
}
return testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
}