Skip to content

Commit

Permalink
Add bootstrap controller unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 authored and tjjh89017 committed Sep 26, 2024
1 parent 653a139 commit ef80f43
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 18 deletions.
39 changes: 24 additions & 15 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,32 @@ var envs = map[string][]string{
"refresh_interval": {"REFRESH_INTERVAL"},
}

type Interface struct {
}
type Interfaces map[string]Interface

type Logger struct {
Level string `mapstructure:"level"`
}

type Stun struct {
Address string `mapstructure:"address"`
}

type Cloudflare struct {
ApiKey string `mapstructure:"api_key"`
ApiEmail string `mapstructure:"api_email"`
ApiToken string `mapstructure:"api_token"`
ZoneName string `mapstructure:"zone_name"`
}

type Config struct {
WireGuard string `mapstructure:"wg"`
Interfaces map[string]struct {
} `mapstructure:"interfaces"`
WireGuard string `mapstructure:"wg"`
Interfaces Interfaces `mapstructure:"interfaces"`
RefreshInterval time.Duration `mapstructure:"refresh_interval"`
Log struct {
Level string `mapstructure:"level"`
} `mapstructure:"log"`
Stun struct {
Address string `mapstructure:"address"`
} `mapstructure:"stun"`
Cloudflare struct {
ApiKey string `mapstructure:"api_key"`
ApiEmail string `mapstructure:"api_email"`
ApiToken string `mapstructure:"api_token"`
ZoneName string `mapstructure:"zone_name"`
} `mapstructure:"cloudflare"`
Log Logger `mapstructure:"log"`
Stun Stun `mapstructure:"stun"`
Cloudflare Cloudflare `mapstructure:"cloudflare"`
}

func Load() (*Config, error) {
Expand Down
11 changes: 11 additions & 0 deletions internal/ctrl/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:generate mockgen -destination=./mock/mock_api.go -package=mock_ctrl . WireGuardClient

package ctrl

import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

type WireGuardClient interface {
Device(deviceName string) (*wgtypes.Device, error)
}
5 changes: 2 additions & 3 deletions internal/ctrl/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import (
"github.com/rs/zerolog"
"github.com/tjjh89017/stunmesh-go/internal/config"
"github.com/tjjh89017/stunmesh-go/internal/entity"
"golang.zx2c4.com/wireguard/wgctrl"
)

type BootstrapController struct {
wg *wgctrl.Client
wg WireGuardClient
config *config.Config
devices DeviceRepository
peers PeerRepository
logger zerolog.Logger
}

func NewBootstrapController(wg *wgctrl.Client, config *config.Config, devices DeviceRepository, peers PeerRepository, logger *zerolog.Logger) *BootstrapController {
func NewBootstrapController(wg WireGuardClient, config *config.Config, devices DeviceRepository, peers PeerRepository, logger *zerolog.Logger) *BootstrapController {
return &BootstrapController{
wg: wg,
config: config,
Expand Down
133 changes: 133 additions & 0 deletions internal/ctrl/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package ctrl_test

import (
"context"
"errors"
"testing"

"github.com/rs/zerolog"
"github.com/tjjh89017/stunmesh-go/internal/config"
"github.com/tjjh89017/stunmesh-go/internal/ctrl"
mock "github.com/tjjh89017/stunmesh-go/internal/ctrl/mock"
gomock "go.uber.org/mock/gomock"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

func TestBootstrap_WithError(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockWgClient := mock.NewMockWireGuardClient(mockCtrl)
mockDevices := mock.NewMockDeviceRepository(mockCtrl)
mockPeers := mock.NewMockPeerRepository(mockCtrl)
logger := zerolog.Nop()
config := &config.Config{
WireGuard: "wg0",
}

mockWgClient.EXPECT().Device("wg0").Return(nil, errors.New("device not found"))

bootstrap := ctrl.NewBootstrapController(
mockWgClient,
config,
mockDevices,
mockPeers,
&logger,
)

bootstrap.Execute(context.TODO())
}

func TestBootstrap_WithSingleInterface(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockWgClient := mock.NewMockWireGuardClient(mockCtrl)
mockDevices := mock.NewMockDeviceRepository(mockCtrl)
mockPeers := mock.NewMockPeerRepository(mockCtrl)
logger := zerolog.Nop()
config := &config.Config{
WireGuard: "wg0",
}

mockDevice := &wgtypes.Device{
Name: "wg0",
ListenPort: 51820,
PrivateKey: [32]byte{},
Peers: []wgtypes.Peer{
{
PublicKey: [32]byte{},
},
},
}

mockWgClient.EXPECT().Device("wg0").Return(mockDevice, nil)
mockDevices.EXPECT().Save(gomock.Any(), gomock.Any())
mockPeers.EXPECT().Save(gomock.Any(), gomock.Any())

bootstrap := ctrl.NewBootstrapController(
mockWgClient,
config,
mockDevices,
mockPeers,
&logger,
)

bootstrap.Execute(context.TODO())
}

func TestBootstrap_WithMultipleInterfaces(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockWgClient := mock.NewMockWireGuardClient(mockCtrl)
mockDevices := mock.NewMockDeviceRepository(mockCtrl)
mockPeers := mock.NewMockPeerRepository(mockCtrl)
logger := zerolog.Nop()
config := &config.Config{
Interfaces: map[string]config.Interface{
"wg0": {},
"wg1": {},
},
}

mockDevice0 := &wgtypes.Device{
Name: "wg0",
ListenPort: 51820,
PrivateKey: [32]byte{},
Peers: []wgtypes.Peer{
{
PublicKey: [32]byte{},
},
},
}

mockDevice1 := &wgtypes.Device{
Name: "wg1",
ListenPort: 51821,
PrivateKey: [32]byte{},
Peers: []wgtypes.Peer{
{
PublicKey: [32]byte{},
},
{
PublicKey: [32]byte{},
},
},
}

mockWgClient.EXPECT().Device("wg0").Return(mockDevice0, nil)
mockWgClient.EXPECT().Device("wg1").Return(mockDevice1, nil)
mockDevices.EXPECT().Save(gomock.Any(), gomock.Any()).Times(2)
mockPeers.EXPECT().Save(gomock.Any(), gomock.Any()).Times(3)

bootstrap := ctrl.NewBootstrapController(
mockWgClient,
config,
mockDevices,
mockPeers,
&logger,
)

bootstrap.Execute(context.TODO())
}
55 changes: 55 additions & 0 deletions internal/ctrl/mock/mock_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func setup() (*daemon.Daemon, error) {
wire.Build(
config.Load,
wgctrl.New,
wire.Bind(new(ctrl.WireGuardClient), new(*wgctrl.Client)),
provideCloudflareApi,
provideStore,
wire.Bind(new(plugin.Store), new(*store.CloudflareStore)),
Expand Down

0 comments on commit ef80f43

Please sign in to comment.