Refactor network to a top-level module

This commit is contained in:
9seconds
2021-03-14 21:43:30 +03:00
parent 37a78bd1c3
commit f8ad90c845
25 changed files with 231 additions and 192 deletions
+5 -5
View File
@@ -7,6 +7,7 @@ import (
"github.com/9seconds/mtg/v2/config"
"github.com/9seconds/mtg/v2/mtglib"
"github.com/9seconds/mtg/v2/testlib"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/suite"
"github.com/xeipuuv/gojsonschema"
@@ -126,7 +127,7 @@ func (suite *AccessTestSuite) TestGenerateNoCalls() {
suite.cli.Access.PublicIPv4 = net.ParseIP("10.0.0.10")
suite.cli.Access.PublicIPv6 = net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
output := suite.CaptureStdout(func() {
output := testlib.CaptureStdout(func() {
suite.NoError(suite.cli.Access.Execute(suite.cli))
})
@@ -150,7 +151,7 @@ func (suite *AccessTestSuite) TestGenerateIPv4Call() {
httpmock.RegisterResponder(http.MethodGet, "https://ifconfig.co",
httpmock.NewStringResponder(http.StatusOK, "10.11.12.13"))
output := suite.CaptureStdout(func() {
output := testlib.CaptureStdout(func() {
suite.NoError(suite.cli.Access.Execute(suite.cli))
})
@@ -174,7 +175,7 @@ func (suite *AccessTestSuite) TestIPv4CallFail() {
httpmock.RegisterResponder(http.MethodGet, "https://ifconfig.co",
httpmock.NewStringResponder(http.StatusForbidden, ""))
output := suite.CaptureStdout(func() {
output := testlib.CaptureStdout(func() {
suite.NoError(suite.cli.Access.Execute(suite.cli))
})
@@ -191,7 +192,6 @@ func (suite *AccessTestSuite) TestIPv4CallFail() {
suite.Contains(output, suite.cli.Access.Config.Secret.Hex())
}
func TestAccess(t *testing.T) {
t.Parallel()
func TestAccess(t *testing.T) { // nolint: paralleltest
suite.Run(t, &AccessTestSuite{})
}
+4 -3
View File
@@ -7,11 +7,12 @@ import (
"net/url"
"github.com/9seconds/mtg/v2/config"
"github.com/9seconds/mtg/v2/mtglib/network"
"github.com/9seconds/mtg/v2/mtglib"
"github.com/9seconds/mtg/v2/network"
)
type base struct {
Network network.Network
Network mtglib.Network
Config *config.Config
}
@@ -37,7 +38,7 @@ func (b *base) ReadConfig(path, version string) error {
return nil
}
func (b *base) makeNetwork(conf *config.Config, version string) (network.Network, error) {
func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
idleTimeout := conf.Network.Timeout.Idle.Value(network.DefaultIdleTimeout)
httpTimeout := conf.Network.Timeout.HTTP.Value(network.DefaultHTTPTimeout)
+3 -2
View File
@@ -5,6 +5,7 @@ import (
"testing"
"github.com/9seconds/mtg/v2/mtglib"
"github.com/9seconds/mtg/v2/testlib"
"github.com/stretchr/testify/suite"
)
@@ -19,7 +20,7 @@ func (suite *GenerateSecretTestSuite) SetupTest() {
}
func (suite *GenerateSecretTestSuite) TestDefault() {
output := suite.CaptureStdout(func() {
output := testlib.CaptureStdout(func() {
suite.NoError(suite.cli.GenerateSecret.Run(suite.cli, "dev"))
})
suite.True(strings.HasPrefix(output, "7"))
@@ -33,7 +34,7 @@ func (suite *GenerateSecretTestSuite) TestDefault() {
func (suite *GenerateSecretTestSuite) TestHex() {
suite.cli.GenerateSecret.Hex = true
output := suite.CaptureStdout(func() {
output := testlib.CaptureStdout(func() {
suite.NoError(suite.cli.GenerateSecret.Run(suite.cli, "dev"))
})
suite.True(strings.HasPrefix(output, "ee"))
+3 -78
View File
@@ -1,66 +1,25 @@
package cli_test
import (
"bytes"
"context"
"io"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/9seconds/mtg/v2/cli"
"github.com/9seconds/mtg/v2/mtglib/network"
"github.com/9seconds/mtg/v2/testlib"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
type NetworkMock struct {
mock.Mock
}
func (n *NetworkMock) Dial(network, address string) (net.Conn, error) {
args := n.Called(network, address)
return args.Get(0).(net.Conn), args.Error(1)
}
func (n *NetworkMock) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
args := n.Called(ctx, network, address)
return args.Get(0).(net.Conn), args.Error(1)
}
func (n *NetworkMock) DNSResolve(network, hostname string) ([]string, error) {
args := n.Called(network, hostname)
return args.Get(0).([]string), args.Error(1)
}
func (n *NetworkMock) MakeHTTPClient(dialFunc network.DialFunc) *http.Client {
return n.Called(dialFunc).Get(0).(*http.Client)
}
func (n *NetworkMock) IdleTimeout() time.Duration {
return n.Called().Get(0).(time.Duration)
}
func (n *NetworkMock) HTTPTimeout() time.Duration {
return n.Called().Get(0).(time.Duration)
}
type CommonTestSuite struct {
suite.Suite
cli *cli.CLI
networkMock *NetworkMock
networkMock *testlib.NetworkMock
httpClient *http.Client
}
func (suite *CommonTestSuite) SetupTest() {
suite.networkMock = &NetworkMock{}
suite.networkMock = &testlib.NetworkMock{}
suite.httpClient = &http.Client{}
suite.cli = &cli.CLI{}
@@ -76,37 +35,3 @@ func (suite *CommonTestSuite) TearDownTest() {
suite.networkMock.AssertExpectations(suite.T())
httpmock.DeactivateAndReset()
}
func (suite *CommonTestSuite) CaptureStdout(callback func()) string {
return suite.captureOutput(&os.Stdout, callback)
}
func (suite *CommonTestSuite) CaptureStderr(callback func()) string {
return suite.captureOutput(&os.Stderr, callback)
}
func (suite *CommonTestSuite) captureOutput(filefp **os.File, callback func()) string {
oldFp := *filefp
defer func() {
*filefp = oldFp
}()
reader, writer, _ := os.Pipe()
buf := &bytes.Buffer{}
closeChan := make(chan bool)
go func() {
io.Copy(buf, reader) // nolint: errcheck
close(closeChan)
}()
*filefp = writer
callback()
writer.Close()
<-closeChan
return strings.TrimSpace(buf.String())
}