Skip to content

Commit

Permalink
* * * TEST * *
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Popov committed Oct 27, 2021
1 parent 9553103 commit de87a1c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 31 deletions.
3 changes: 2 additions & 1 deletion pkg/tools/proxy/proxy_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ func (p *proxyListener) accept(ctx context.Context) {

go func() {
<-ctx.Done()
_ = p.Close()
_ = p.listener.Close()
}()

defer func() { _ = p.Close() }()
for {
in, err := p.listener.Accept()
if err != nil {
Expand Down
103 changes: 73 additions & 30 deletions pkg/tools/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"
"time"

"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/vishvananda/netns"
"go.uber.org/goleak"
Expand All @@ -43,11 +44,10 @@ const (
func TestStart(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tempDir := t.TempDir()

timer := time.AfterFunc(time.Second, t.FailNow)
defer timer.Stop()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

runtime.LockOSThread()
defer runtime.UnlockOSThread()
Expand All @@ -68,19 +68,22 @@ func TestStart(t *testing.T) {

require.NoError(t, netns.Set(targetNS))

targetFile := "@" + path.Join(t.TempDir(), "target")
targetFile := "@" + path.Join(tempDir, "target")

l, err := net.Listen("unix", targetFile)
require.NoError(t, err)
defer func() { _ = l.Close() }()
go func() {
<-ctx.Done()
_ = l.Close()
}()

go doPong(t, l)
pongCh := doPong(l)

// 2. Create proxy from default net NS.

require.NoError(t, netns.Set(defaultNS))

proxyFile := "@" + path.Join(t.TempDir(), "proxy")
proxyFile := "@" + path.Join(tempDir, "proxy")

require.NoError(t, proxy.Start(ctx, network, nsURL("proxy"), proxyFile, nsURL("target"), targetFile))

Expand All @@ -90,33 +93,73 @@ func TestStart(t *testing.T) {

conn, err := net.Dial(network, proxyFile)
require.NoError(t, err)
defer func() { _ = conn.Close() }()
go func() {
<-ctx.Done()
_ = l.Close()
}()

doPing(t, conn)
require.NoError(t, <-doPing(conn))
require.NoError(t, <-pongCh)
}

func doPong(t *testing.T, l net.Listener) {
conn, err := l.Accept()
require.NoError(t, err)
defer func() { _ = conn.Close() }()

buff := make([]byte, 10)
n, err := conn.Read(buff)
require.NoError(t, err)
require.Equal(t, ping, string(buff[:n]))

_, err = conn.Write([]byte(pong))
require.NoError(t, err)
func doPong(l net.Listener) <-chan error {
ch := make(chan error, 1)
go func() {
defer close(ch)
for {
conn, err := l.Accept()
if err != nil {
ch <- err
return
}
defer func() { _ = conn.Close() }()

buff := make([]byte, 10)
n, err := conn.Read(buff)
if err == io.EOF {
// test dial
continue
}
if err != nil {
ch <- err
return
}
if msg := string(buff[:n]); msg != ping {
ch <- errors.Errorf("expected %s, actual %s", ping, msg)
return
}

_, err = conn.Write([]byte(pong))
if err != nil {
ch <- err
}
return
}
}()
return ch
}

func doPing(t *testing.T, conn io.ReadWriter) {
_, err := conn.Write([]byte(ping))
require.NoError(t, err)

buff := make([]byte, 10)
n, err := conn.Read(buff)
require.NoError(t, err)
require.Equal(t, pong, string(buff[:n]))
func doPing(conn io.ReadWriter) <-chan error {
ch := make(chan error, 1)
go func() {
defer close(ch)

_, err := conn.Write([]byte(ping))
if err != nil {
ch <- err
}

buff := make([]byte, 10)
n, err := conn.Read(buff)
if err != nil && err != io.EOF {
ch <- err
}
if msg := string(buff[:n]); msg != pong {
ch <- errors.Errorf("expected %s, actual %s", pong, msg)
return
}
}()
return ch
}

func nsURL(name string) string {
Expand Down

0 comments on commit de87a1c

Please sign in to comment.