Skip to content

Commit

Permalink
change package structure
Browse files Browse the repository at this point in the history
Signed-off-by: walnuts1018 <[email protected]>
  • Loading branch information
walnuts1018 committed Aug 27, 2024
1 parent 08363a1 commit b0c7614
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 77 deletions.
2 changes: 1 addition & 1 deletion cmd/nat-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (

ponav1beta1 "github.com/cybozu-go/pona/api/v1beta1"
"github.com/cybozu-go/pona/internal/controller"
"github.com/cybozu-go/pona/internal/fou"
"github.com/cybozu-go/pona/internal/nat"
"github.com/cybozu-go/pona/internal/tunnel/fou"
// +kubebuilder:scaffold:imports
)

Expand Down
69 changes: 0 additions & 69 deletions internal/controller/pod_watcher_mock.go

This file was deleted.

13 changes: 8 additions & 5 deletions internal/controller/pod_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"net/netip"
"path/filepath"

natmock "github.com/cybozu-go/pona/internal/nat/mock"
tunnelmock "github.com/cybozu-go/pona/internal/tunnel/mock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"

"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -72,8 +75,8 @@ var _ = Describe("Pod Watcher", func() {

It("should successfully reconcile the resource", func() {
By("Reconcile the created resource")
t := NewMockTunnel()
n := NewMockNat()
t := tunnelmock.NewMockTunnel()
n := natmock.NewMockNat()
w := &PodWatcher{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Expand All @@ -94,13 +97,13 @@ var _ = Describe("Pod Watcher", func() {

By("Check if mockTunnel.AddPeer() is called")
for _, ip := range podInfo.PodIPs {
_, ok := t.tunnels[ip]
_, ok := t.Tunnels[ip]
Expect(ok).To(BeTrue())
}

By("Check if mockNAT.AddClient() is called")
for _, ip := range podInfo.PodIPs {
_, ok := n.clients[ip]
_, ok := n.Clients[ip]
Expect(ok).To(BeTrue())
}

Expand All @@ -123,7 +126,7 @@ var _ = Describe("Pod Watcher", func() {

By("Check if mockTunnel.DelPeer() is called")
for _, ip := range podInfo.PodIPs {
_, ok := t.tunnels[ip]
_, ok := t.Tunnels[ip]
Expect(ok).To(BeFalse())
}

Expand Down
32 changes: 32 additions & 0 deletions internal/nat/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package mock

import (
"fmt"
"net/netip"

"github.com/vishvananda/netlink"
)

type linkName string

type mockNAT struct {
Clients map[netip.Addr]linkName
}

func NewMockNat() *mockNAT {
return &mockNAT{
Clients: make(map[netip.Addr]linkName),
}
}

func (m *mockNAT) Init() error {
return nil
}

func (m *mockNAT) AddClient(addr netip.Addr, link netlink.Link) error {
if link.Attrs() == nil {
return fmt.Errorf("link.Attrs() returns nil")
}
m.Clients[addr] = linkName(link.Attrs().Name)
return nil
}
2 changes: 0 additions & 2 deletions internal/fou/fou.go → internal/tunnel/fou/fou.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ type FouTunnelController struct {
local6 *netip.Addr
}

var _ tunnel.Controller = &FouTunnelController{}

// NewFoUTunnel creates a new fouTunnel.
// port is the UDP port to receive FoU packets.
// localIPv4 is the local IPv4 address of the IPIP tunnel. This can be nil.
Expand Down
41 changes: 41 additions & 0 deletions internal/tunnel/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mock

import (
"net/netip"

"github.com/vishvananda/netlink"
)

type mockTunnel struct {
Tunnels map[netip.Addr]struct{}
}

func NewMockTunnel() mockTunnel {
return mockTunnel{
Tunnels: make(map[netip.Addr]struct{}),
}
}

func (m mockTunnel) AddPeer(addr netip.Addr) (netlink.Link, error) {
m.Tunnels[addr] = struct{}{}
link := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Name: "dummy",
Index: 1,
},
}
return link, nil
}

func (m mockTunnel) DelPeer(addr netip.Addr) error {
delete(m.Tunnels, addr)
return nil
}

func (m mockTunnel) Init() error {
return nil
}

func (m mockTunnel) IsInitialized() bool {
return true
}

0 comments on commit b0c7614

Please sign in to comment.