Skip to content

Commit

Permalink
agent/proxy: tests: modify tests to create listeners by themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
nadiamoe committed Aug 31, 2023
1 parent 0e5350a commit e94ef36
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 134 deletions.
126 changes: 38 additions & 88 deletions pkg/agent/protocol/grpc/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package grpc

import (
"context"
"fmt"
"net"
"os"
"path/filepath"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/grafana/xk6-disruptor/pkg/agent/protocol"
"github.com/grafana/xk6-disruptor/pkg/testutils/grpc/ping"
"google.golang.org/grpc"
Expand All @@ -24,7 +21,7 @@ func Test_Validations(t *testing.T) {
testCases := []struct {
title string
disruption Disruption
config ProxyConfig
upstream string
expectError bool
}{
{
Expand All @@ -36,29 +33,9 @@ func Test_Validations(t *testing.T) {
StatusCode: 0,
StatusMessage: "",
},
config: ProxyConfig{
Network: "",
ListenAddress: ":9080",
UpstreamAddress: ":8080",
},
upstream: ":8080",
expectError: false,
},
{
title: "invalid listening address",
disruption: Disruption{
AverageDelay: 0,
DelayVariation: 0,
ErrorRate: 0.0,
StatusCode: 0,
StatusMessage: "",
},
config: ProxyConfig{
Network: "",
ListenAddress: "",
UpstreamAddress: ":8080",
},
expectError: true,
},
{
title: "invalid upstream address",
disruption: Disruption{
Expand All @@ -68,11 +45,7 @@ func Test_Validations(t *testing.T) {
StatusCode: 0,
StatusMessage: "",
},
config: ProxyConfig{
Network: "",
ListenAddress: ":9080",
UpstreamAddress: "",
},
upstream: "",
expectError: true,
},
{
Expand All @@ -84,11 +57,7 @@ func Test_Validations(t *testing.T) {
StatusCode: 0,
StatusMessage: "",
},
config: ProxyConfig{
Network: "",
ListenAddress: ":9080",
UpstreamAddress: ":8080",
},
upstream: ":8080",
expectError: true,
},
{
Expand All @@ -100,11 +69,7 @@ func Test_Validations(t *testing.T) {
StatusCode: int32(codes.Internal),
StatusMessage: "",
},
config: ProxyConfig{
Network: "",
ListenAddress: ":9080",
UpstreamAddress: ":8080",
},
upstream: ":8080",
expectError: false,
},
{
Expand All @@ -116,11 +81,7 @@ func Test_Validations(t *testing.T) {
StatusCode: 0,
StatusMessage: "",
},
config: ProxyConfig{
Network: "",
ListenAddress: ":9080",
UpstreamAddress: ":8080",
},
upstream: ":8080",
expectError: false,
},
{
Expand All @@ -132,11 +93,7 @@ func Test_Validations(t *testing.T) {
StatusCode: 0,
StatusMessage: "",
},
config: ProxyConfig{
Network: "",
ListenAddress: ":9080",
UpstreamAddress: ":8080",
},
upstream: ":8080",
expectError: true,
},
{
Expand All @@ -148,11 +105,7 @@ func Test_Validations(t *testing.T) {
StatusCode: 0,
StatusMessage: "",
},
config: ProxyConfig{
Network: "",
ListenAddress: ":9080",
UpstreamAddress: ":8080",
},
upstream: ":8080",
expectError: true,
},
}
Expand All @@ -163,8 +116,14 @@ func Test_Validations(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()

_, err := NewProxy(
tc.config,
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("could not create listener: %v", err)
}

_, err = NewProxy(
listener,
tc.upstream,
tc.disruption,
)
if !tc.expectError && err != nil {
Expand Down Expand Up @@ -251,29 +210,24 @@ func Test_ProxyHandler(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()

// start test server in a random unix socket
serverSocket := filepath.Join(os.TempDir(), uuid.New().String())
l, err := net.Listen("unix", serverSocket)
upstreamListener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Errorf("error starting test server in unix:%s: %v", serverSocket, err)
return
t.Fatalf("error starting test upstream listener: %v", err)
}
srv := grpc.NewServer()
ping.RegisterPingServiceServer(srv, ping.NewPingServer())
go func() {
if serr := srv.Serve(l); err != nil {
if serr := srv.Serve(upstreamListener); err != nil {
t.Logf("error in the server: %v", serr)
}
}()

// start proxy in a random unix socket
proxySocket := filepath.Join(os.TempDir(), uuid.New().String())
config := ProxyConfig{
Network: "unix",
ListenAddress: proxySocket,
UpstreamAddress: fmt.Sprintf("unix:%s", serverSocket),
proxyListener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("error starting test proxy listener: %v", err)
}
proxy, err := NewProxy(config, tc.disruption)

proxy, err := NewProxy(proxyListener, upstreamListener.Addr().String(), tc.disruption)
if err != nil {
t.Errorf("error creating proxy: %v", err)
return
Expand All @@ -288,10 +242,12 @@ func Test_ProxyHandler(t *testing.T) {
}
}()

time.Sleep(time.Second)

// connect client to proxy
conn, err := grpc.DialContext(
context.TODO(),
fmt.Sprintf("unix:%s", proxySocket),
proxyListener.Addr().String(),
grpc.WithInsecure(),
)
if err != nil {
Expand Down Expand Up @@ -401,36 +357,28 @@ func Test_ProxyMetrics(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()

// start test server in a random unix socket
serverSocket := filepath.Join(os.TempDir(), uuid.New().String())
l, err := net.Listen("unix", serverSocket)
upstreamListener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Errorf("error starting test server in unix:%s: %v", serverSocket, err)
return
t.Fatalf("error starting test upstream listener: %v", err)
}

srv := grpc.NewServer()
ping.RegisterPingServiceServer(srv, ping.NewPingServer())
go func() {
if serr := srv.Serve(l); err != nil {
if serr := srv.Serve(upstreamListener); err != nil {
t.Logf("error in the server: %v", serr)
}
}()

// start proxy in a random unix socket
proxySocket := filepath.Join(os.TempDir(), uuid.New().String())
config := ProxyConfig{
Network: "unix",
ListenAddress: proxySocket,
UpstreamAddress: fmt.Sprintf("unix:%s", serverSocket),
proxyListener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("error starting test proxy listener: %v", err)
}

proxy, err := NewProxy(config, tc.disruption)
proxy, err := NewProxy(proxyListener, upstreamListener.Addr().String(), tc.disruption)
if err != nil {
t.Errorf("error creating proxy: %v", err)
return
}

defer func() {
_ = proxy.Stop()
}()
Expand All @@ -441,10 +389,12 @@ func Test_ProxyMetrics(t *testing.T) {
}
}()

time.Sleep(time.Second)

// connect client to proxy
conn, err := grpc.DialContext(
context.TODO(),
fmt.Sprintf("unix:%s", proxySocket),
proxyListener.Addr().String(),
grpc.WithInsecure(),
)
if err != nil {
Expand Down
63 changes: 17 additions & 46 deletions pkg/agent/protocol/http/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http
import (
"bytes"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -18,7 +19,7 @@ func Test_Validations(t *testing.T) {
testCases := []struct {
title string
disruption Disruption
config ProxyConfig
upstream string
expectError bool
}{
{
Expand All @@ -30,27 +31,9 @@ func Test_Validations(t *testing.T) {
ErrorCode: 0,
Excluded: nil,
},
config: ProxyConfig{
ListenAddress: ":8080",
UpstreamAddress: "http://127.0.0.1:80",
},
upstream: "http://127.0.0.1:80",
expectError: false,
},
{
title: "invalid listening address",
disruption: Disruption{
AverageDelay: 0,
DelayVariation: 0,
ErrorRate: 0.0,
ErrorCode: 0,
Excluded: nil,
},
config: ProxyConfig{
ListenAddress: "",
UpstreamAddress: "http://127.0.0.1:80",
},
expectError: true,
},
{
title: "invalid upstream address",
disruption: Disruption{
Expand All @@ -60,10 +43,7 @@ func Test_Validations(t *testing.T) {
ErrorCode: 0,
Excluded: nil,
},
config: ProxyConfig{
ListenAddress: ":8080",
UpstreamAddress: "",
},
upstream: "",
expectError: true,
},
{
Expand All @@ -75,10 +55,7 @@ func Test_Validations(t *testing.T) {
ErrorCode: 0,
Excluded: nil,
},
config: ProxyConfig{
ListenAddress: ":8080",
UpstreamAddress: "http://127.0.0.1:80",
},
upstream: "http://127.0.0.1:80",
expectError: true,
},
{
Expand All @@ -90,10 +67,7 @@ func Test_Validations(t *testing.T) {
ErrorCode: 500,
Excluded: nil,
},
config: ProxyConfig{
ListenAddress: ":8080",
UpstreamAddress: "http://127.0.0.1:80",
},
upstream: "http://127.0.0.1:80",
expectError: false,
},
{
Expand All @@ -105,10 +79,7 @@ func Test_Validations(t *testing.T) {
ErrorCode: 0,
Excluded: nil,
},
config: ProxyConfig{
ListenAddress: ":8080",
UpstreamAddress: "http://127.0.0.1:80",
},
upstream: "http://127.0.0.1:80",
expectError: false,
},
{
Expand All @@ -120,10 +91,7 @@ func Test_Validations(t *testing.T) {
ErrorCode: 0,
Excluded: nil,
},
config: ProxyConfig{
ListenAddress: ":8080",
UpstreamAddress: "http://127.0.0.1:80",
},
upstream: "http://127.0.0.1:80",
expectError: true,
},
{
Expand All @@ -135,10 +103,7 @@ func Test_Validations(t *testing.T) {
ErrorCode: 0,
Excluded: nil,
},
config: ProxyConfig{
ListenAddress: ":8080",
UpstreamAddress: "http://127.0.0.1:80",
},
upstream: "http://127.0.0.1:80",
expectError: true,
},
}
Expand All @@ -149,8 +114,14 @@ func Test_Validations(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()

_, err := NewProxy(
tc.config,
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("error starting test proxy listener: %v", err)
}

_, err = NewProxy(
listener,
tc.upstream,
tc.disruption,
)
if !tc.expectError && err != nil {
Expand Down

0 comments on commit e94ef36

Please sign in to comment.