Code maintenance: Unix abstraction interface
- Used for creating the tun device if it does not exist - Mocks generated for testing
This commit is contained in:
@@ -14,7 +14,10 @@ issues:
|
|||||||
linters:
|
linters:
|
||||||
- gochecknoglobals
|
- gochecknoglobals
|
||||||
text: IsNotExist is a global variable
|
text: IsNotExist is a global variable
|
||||||
|
- path: internal/unix/constants\.go
|
||||||
|
linters:
|
||||||
|
- golint
|
||||||
|
text: don't use ALL_CAPS in Go names; use CamelCase
|
||||||
linters:
|
linters:
|
||||||
disable-all: true
|
disable-all: true
|
||||||
enable:
|
enable:
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import (
|
|||||||
"github.com/qdm12/gluetun/internal/settings"
|
"github.com/qdm12/gluetun/internal/settings"
|
||||||
"github.com/qdm12/gluetun/internal/shadowsocks"
|
"github.com/qdm12/gluetun/internal/shadowsocks"
|
||||||
"github.com/qdm12/gluetun/internal/storage"
|
"github.com/qdm12/gluetun/internal/storage"
|
||||||
|
"github.com/qdm12/gluetun/internal/unix"
|
||||||
"github.com/qdm12/gluetun/internal/updater"
|
"github.com/qdm12/gluetun/internal/updater"
|
||||||
versionpkg "github.com/qdm12/gluetun/internal/version"
|
versionpkg "github.com/qdm12/gluetun/internal/version"
|
||||||
"github.com/qdm12/golibs/command"
|
"github.com/qdm12/golibs/command"
|
||||||
@@ -86,7 +87,8 @@ func _main(background context.Context, args []string, os os.OS) int {
|
|||||||
client := network.NewClient(clientTimeout)
|
client := network.NewClient(clientTimeout)
|
||||||
// Create configurators
|
// Create configurators
|
||||||
alpineConf := alpine.NewConfigurator(os.OpenFile)
|
alpineConf := alpine.NewConfigurator(os.OpenFile)
|
||||||
ovpnConf := openvpn.NewConfigurator(logger, os)
|
unix := unix.New()
|
||||||
|
ovpnConf := openvpn.NewConfigurator(logger, os, unix)
|
||||||
dnsConf := dns.NewConfigurator(logger, client, os.OpenFile)
|
dnsConf := dns.NewConfigurator(logger, client, os.OpenFile)
|
||||||
routingConf := routing.NewRouting(logger)
|
routingConf := routing.NewRouting(logger)
|
||||||
firewallConf := firewall.NewConfigurator(logger, routingConf, os.OpenFile)
|
firewallConf := firewall.NewConfigurator(logger, routingConf, os.OpenFile)
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/os"
|
"github.com/qdm12/gluetun/internal/os"
|
||||||
|
"github.com/qdm12/gluetun/internal/unix"
|
||||||
"github.com/qdm12/golibs/command"
|
"github.com/qdm12/golibs/command"
|
||||||
"github.com/qdm12/golibs/logging"
|
"github.com/qdm12/golibs/logging"
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Configurator interface {
|
type Configurator interface {
|
||||||
@@ -22,16 +22,14 @@ type configurator struct {
|
|||||||
logger logging.Logger
|
logger logging.Logger
|
||||||
commander command.Commander
|
commander command.Commander
|
||||||
os os.OS
|
os os.OS
|
||||||
mkDev func(major uint32, minor uint32) uint64
|
unix unix.Unix
|
||||||
mkNod func(path string, mode uint32, dev int) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfigurator(logger logging.Logger, os os.OS) Configurator {
|
func NewConfigurator(logger logging.Logger, os os.OS, unix unix.Unix) Configurator {
|
||||||
return &configurator{
|
return &configurator{
|
||||||
logger: logger.WithPrefix("openvpn configurator: "),
|
logger: logger.WithPrefix("openvpn configurator: "),
|
||||||
commander: command.NewCommander(),
|
commander: command.NewCommander(),
|
||||||
os: os,
|
os: os,
|
||||||
mkDev: unix.Mkdev,
|
unix: unix,
|
||||||
mkNod: unix.Mknod,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/constants"
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
"golang.org/x/sys/unix"
|
"github.com/qdm12/gluetun/internal/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckTUN checks the tunnel device is present and accessible.
|
// CheckTUN checks the tunnel device is present and accessible.
|
||||||
@@ -31,8 +31,8 @@ func (c *configurator) CreateTUN() error {
|
|||||||
major = 10
|
major = 10
|
||||||
minor = 200
|
minor = 200
|
||||||
)
|
)
|
||||||
dev := c.mkDev(major, minor)
|
dev := c.unix.Mkdev(major, minor)
|
||||||
if err := c.mkNod(string(constants.TunnelDevice), unix.S_IFCHR, int(dev)); err != nil {
|
if err := c.unix.Mknod(string(constants.TunnelDevice), unix.S_IFCHR, int(dev)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
9
internal/unix/constants.go
Normal file
9
internal/unix/constants.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package unix
|
||||||
|
|
||||||
|
import sysunix "golang.org/x/sys/unix"
|
||||||
|
|
||||||
|
// Constants used for convenience so "os" does not have to be imported
|
||||||
|
|
||||||
|
const (
|
||||||
|
S_IFCHR = sysunix.S_IFCHR
|
||||||
|
)
|
||||||
61
internal/unix/mock_unix/unix.go
Normal file
61
internal/unix/mock_unix/unix.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
|
// Source: github.com/qdm12/gluetun/internal/unix (interfaces: Unix)
|
||||||
|
|
||||||
|
// Package mock_unix is a generated GoMock package.
|
||||||
|
package mock_unix
|
||||||
|
|
||||||
|
import (
|
||||||
|
gomock "github.com/golang/mock/gomock"
|
||||||
|
reflect "reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockUnix is a mock of Unix interface
|
||||||
|
type MockUnix struct {
|
||||||
|
ctrl *gomock.Controller
|
||||||
|
recorder *MockUnixMockRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockUnixMockRecorder is the mock recorder for MockUnix
|
||||||
|
type MockUnixMockRecorder struct {
|
||||||
|
mock *MockUnix
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockUnix creates a new mock instance
|
||||||
|
func NewMockUnix(ctrl *gomock.Controller) *MockUnix {
|
||||||
|
mock := &MockUnix{ctrl: ctrl}
|
||||||
|
mock.recorder = &MockUnixMockRecorder{mock}
|
||||||
|
return mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXPECT returns an object that allows the caller to indicate expected use
|
||||||
|
func (m *MockUnix) EXPECT() *MockUnixMockRecorder {
|
||||||
|
return m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mkdev mocks base method
|
||||||
|
func (m *MockUnix) Mkdev(arg0, arg1 uint32) uint64 {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Mkdev", arg0, arg1)
|
||||||
|
ret0, _ := ret[0].(uint64)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mkdev indicates an expected call of Mkdev
|
||||||
|
func (mr *MockUnixMockRecorder) Mkdev(arg0, arg1 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Mkdev", reflect.TypeOf((*MockUnix)(nil).Mkdev), arg0, arg1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mknod mocks base method
|
||||||
|
func (m *MockUnix) Mknod(arg0 string, arg1 uint32, arg2 int) error {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Mknod", arg0, arg1, arg2)
|
||||||
|
ret0, _ := ret[0].(error)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mknod indicates an expected call of Mknod
|
||||||
|
func (mr *MockUnixMockRecorder) Mknod(arg0, arg1, arg2 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Mknod", reflect.TypeOf((*MockUnix)(nil).Mknod), arg0, arg1, arg2)
|
||||||
|
}
|
||||||
24
internal/unix/unix.go
Normal file
24
internal/unix/unix.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package unix
|
||||||
|
|
||||||
|
import sysunix "golang.org/x/sys/unix"
|
||||||
|
|
||||||
|
//go:generate mockgen -destination=mock_$GOPACKAGE/$GOFILE . Unix
|
||||||
|
|
||||||
|
type Unix interface {
|
||||||
|
Mkdev(major uint32, minor uint32) uint64
|
||||||
|
Mknod(path string, mode uint32, dev int) (err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() Unix {
|
||||||
|
return &unix{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type unix struct{}
|
||||||
|
|
||||||
|
func (u *unix) Mkdev(major uint32, minor uint32) uint64 {
|
||||||
|
return sysunix.Mkdev(major, minor)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *unix) Mknod(path string, mode uint32, dev int) (err error) {
|
||||||
|
return sysunix.Mknod(path, mode, dev)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user