Skip to content

Commit 70ef1e2

Browse files
authored
test: add events tests (#27)
1 parent e2a2fec commit 70ef1e2

File tree

9 files changed

+158
-25
lines changed

9 files changed

+158
-25
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ GOLINT = golangci-lint run
1010
# Source repository variables.
1111
ROOT_DIR := $(shell git rev-parse --show-toplevel)
1212
BIN_DIR = $(ROOT_DIR)/bin
13-
TEST_PKGS = $(shell go list ./...)
13+
TEST_PKGS = $(shell go list ./... | grep -v 'github.com/azure/peerd/api\|github.com/azure/peerd/pkg/mocks') # Exclude generated and mock code.
1414
TESTS_BIN_DIR = $(BIN_DIR)/tests
1515
COVERAGE_DIR=$(BIN_DIR)/coverage
1616
SCRIPTS_DIR=$(ROOT_DIR)/build/ci/scripts

cmd/proxy/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/azure/peerd/internal/routing"
2424
"github.com/azure/peerd/internal/state"
2525
"github.com/azure/peerd/pkg/containerd"
26+
"github.com/azure/peerd/pkg/k8s"
2627
"github.com/rs/zerolog"
2728
"github.com/spf13/afero"
2829
"golang.org/x/sync/errgroup"
@@ -78,7 +79,12 @@ func serverCommand(ctx context.Context, args *ServerCmd) (err error) {
7879
return err
7980
}
8081

81-
ctx, err = events.WithContext(ctx)
82+
clientset, err := k8s.NewKubernetesInterface(p2pcontext.KubeConfigPath)
83+
if err != nil {
84+
return err
85+
}
86+
87+
ctx, err = events.WithContext(ctx, clientset)
8288
if err != nil {
8389
return err
8490
}
@@ -91,7 +97,7 @@ func serverCommand(ctx context.Context, args *ServerCmd) (err error) {
9197

9298
eventsRecorder.Initializing()
9399

94-
r, err := routing.NewRouter(ctx, args.RouterAddr, httpsPort)
100+
r, err := routing.NewRouter(ctx, clientset, args.RouterAddr, httpsPort)
95101
if err != nil {
96102
return err
97103
}

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ require (
1818
github.com/schollz/progressbar/v3 v3.14.2
1919
github.com/swaggo/swag v1.16.3
2020
golang.org/x/sync v0.6.0
21-
k8s.io/api v0.29.2
22-
k8s.io/apimachinery v0.29.2
21+
k8s.io/api v0.29.3
22+
k8s.io/apimachinery v0.29.3
2323
)
2424

2525
require (
@@ -49,6 +49,7 @@ require (
4949
github.com/dustin/go-humanize v1.0.1 // indirect
5050
github.com/elastic/gosigar v0.14.2 // indirect
5151
github.com/emicklei/go-restful/v3 v3.11.3 // indirect
52+
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
5253
github.com/felixge/httpsnoop v1.0.4 // indirect
5354
github.com/flynn/noise v1.1.0 // indirect
5455
github.com/francoispqt/gojay v1.2.13 // indirect
@@ -69,7 +70,7 @@ require (
6970
github.com/gogo/protobuf v1.3.2 // indirect
7071
github.com/golang/glog v1.2.0 // indirect
7172
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
72-
github.com/golang/protobuf v1.5.3 // indirect
73+
github.com/golang/protobuf v1.5.4 // indirect
7374
github.com/google/gnostic-models v0.6.8 // indirect
7475
github.com/google/go-cmp v0.6.0 // indirect
7576
github.com/google/gofuzz v1.2.0 // indirect
@@ -205,6 +206,6 @@ require (
205206
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
206207
golang.org/x/sys v0.18.0 // indirect
207208
google.golang.org/protobuf v1.33.0 // indirect
208-
k8s.io/client-go v0.29.2
209+
k8s.io/client-go v0.29.3
209210
lukechampine.com/blake3 v1.2.1 // indirect
210211
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
207207
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
208208
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
209209
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
210+
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
211+
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
210212
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
211213
github.com/google/gnostic-models v0.6.8 h1:yo/ABAfM5IMRsS1VnXjTBvUb61tFIHozhlYvRgGre9I=
212214
github.com/google/gnostic-models v0.6.8/go.mod h1:5n7qKqH0f5wFt+aWF8CW6pZLLNOfYuF5OpfBSENuI8U=
@@ -848,10 +850,16 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
848850
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
849851
k8s.io/api v0.29.2 h1:hBC7B9+MU+ptchxEqTNW2DkUosJpp1P+Wn6YncZ474A=
850852
k8s.io/api v0.29.2/go.mod h1:sdIaaKuU7P44aoyyLlikSLayT6Vb7bvJNCX105xZXY0=
853+
k8s.io/api v0.29.3 h1:2ORfZ7+bGC3YJqGpV0KSDDEVf8hdGQ6A03/50vj8pmw=
854+
k8s.io/api v0.29.3/go.mod h1:y2yg2NTyHUUkIoTC+phinTnEa3KFM6RZ3szxt014a80=
851855
k8s.io/apimachinery v0.29.2 h1:EWGpfJ856oj11C52NRCHuU7rFDwxev48z+6DSlGNsV8=
852856
k8s.io/apimachinery v0.29.2/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
857+
k8s.io/apimachinery v0.29.3 h1:2tbx+5L7RNvqJjn7RIuIKu9XTsIZ9Z5wX2G22XAa5EU=
858+
k8s.io/apimachinery v0.29.3/go.mod h1:hx/S4V2PNW4OMg3WizRrHutyB5la0iCUbZym+W0EQIU=
853859
k8s.io/client-go v0.29.2 h1:FEg85el1TeZp+/vYJM7hkDlSTFZ+c5nnK44DJ4FyoRg=
854860
k8s.io/client-go v0.29.2/go.mod h1:knlvFZE58VpqbQpJNbCbctTVXcd35mMyAAwBdpt4jrA=
861+
k8s.io/client-go v0.29.3 h1:R/zaZbEAxqComZ9FHeQwOh3Y1ZUs7FaHKZdQtIc2WZg=
862+
k8s.io/client-go v0.29.3/go.mod h1:tkDisCvgPfiRpxGnOORfkljmS+UrW+WtXAy2fTvXJB0=
855863
k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
856864
k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
857865
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 h1:BZqlfIlq5YbRMFko6/PM7FjZpUb45WallggurYhKGag=

internal/k8s/events/events.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212
"k8s.io/client-go/kubernetes/scheme"
1313
typedv1core "k8s.io/client-go/kubernetes/typed/core/v1"
14-
"k8s.io/client-go/rest"
1514
"k8s.io/client-go/tools/record"
1615
)
1716

@@ -22,17 +21,9 @@ var (
2221
)
2322

2423
// NewRecorder creates a new event recorder.
25-
func NewRecorder(ctx context.Context) (EventRecorder, error) {
26-
clientset, err := k8s.NewKubernetesInterface(p2pcontext.KubeConfigPath)
27-
if err != nil {
28-
return nil, err
29-
}
30-
31-
inPod := false
32-
_, err = rest.InClusterConfig() // Assume run in a Pod or an environment with appropriate env variables set
33-
if err == nil {
34-
inPod = true
35-
}
24+
func NewRecorder(ctx context.Context, k *k8s.ClientSet) (EventRecorder, error) {
25+
clientset := k.Interface
26+
inPod := k.InPod
3627

3728
var objRef *v1.ObjectReference
3829
if !inPod {
@@ -74,8 +65,8 @@ func NewRecorder(ctx context.Context) (EventRecorder, error) {
7465
}
7566

7667
// WithContext returns a new context with an event recorder.
77-
func WithContext(ctx context.Context) (context.Context, error) {
78-
er, err := NewRecorder(ctx)
68+
func WithContext(ctx context.Context, clientset *k8s.ClientSet) (context.Context, error) {
69+
er, err := NewRecorder(ctx, clientset)
7970
if err != nil {
8071
return nil, err
8172
}

internal/k8s/events/events_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,113 @@ import (
66
"context"
77
"testing"
88

9+
p2pcontext "github.com/azure/peerd/internal/context"
10+
"github.com/azure/peerd/pkg/k8s"
911
v1 "k8s.io/api/core/v1"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1013
"k8s.io/apimachinery/pkg/runtime"
14+
"k8s.io/client-go/kubernetes/fake"
1115
"k8s.io/client-go/tools/record"
1216
)
1317

18+
func TestWithContext(t *testing.T) {
19+
fcs := fake.NewSimpleClientset([]runtime.Object{
20+
&v1.Pod{
21+
ObjectMeta: metav1.ObjectMeta{
22+
Name: p2pcontext.NodeName,
23+
Namespace: p2pcontext.Namespace,
24+
UID: "test-uid",
25+
},
26+
},
27+
}...)
28+
29+
cs := &k8s.ClientSet{Interface: fcs, InPod: true}
30+
31+
ctx, err := WithContext(context.Background(), cs)
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
36+
if ctx == nil {
37+
t.Fatal("expected context")
38+
}
39+
40+
er := FromContext(ctx).(*eventRecorder)
41+
if er.objRef.Kind != "Pod" {
42+
t.Errorf("expected kind to be Pod, got %s", er.objRef.Kind)
43+
}
44+
}
45+
46+
func TestNewRecorderInNode(t *testing.T) {
47+
fcs := fake.NewSimpleClientset([]runtime.Object{
48+
&v1.Node{
49+
ObjectMeta: metav1.ObjectMeta{
50+
Name: p2pcontext.NodeName,
51+
UID: "test-uid",
52+
},
53+
},
54+
}...)
55+
56+
cs := &k8s.ClientSet{Interface: fcs, InPod: false}
57+
58+
r, err := NewRecorder(context.Background(), cs)
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
63+
if r == nil {
64+
t.Fatal("expected event recorder")
65+
}
66+
67+
er := r.(*eventRecorder)
68+
if er.objRef.Kind != "Node" {
69+
t.Errorf("expected kind to be Node, got %s", er.objRef.Kind)
70+
}
71+
if er.objRef.Name != p2pcontext.NodeName {
72+
t.Errorf("expected name to be %s, got %s", p2pcontext.NodeName, er.objRef.Name)
73+
}
74+
if er.objRef.UID != "test-uid" {
75+
t.Errorf("expected uid to be test-uid, got %s", er.objRef.UID)
76+
}
77+
}
78+
79+
func TestNewRecorderInPod(t *testing.T) {
80+
fcs := fake.NewSimpleClientset([]runtime.Object{
81+
&v1.Pod{
82+
ObjectMeta: metav1.ObjectMeta{
83+
Name: p2pcontext.NodeName,
84+
Namespace: p2pcontext.Namespace,
85+
UID: "test-uid",
86+
},
87+
},
88+
}...)
89+
90+
cs := &k8s.ClientSet{Interface: fcs, InPod: true}
91+
92+
r, err := NewRecorder(context.Background(), cs)
93+
if err != nil {
94+
t.Fatal(err)
95+
}
96+
97+
if r == nil {
98+
t.Fatal("expected event recorder")
99+
}
100+
101+
er := r.(*eventRecorder)
102+
if er.objRef.Kind != "Pod" {
103+
t.Errorf("expected kind to be Pod, got %s", er.objRef.Kind)
104+
}
105+
if er.objRef.Name != p2pcontext.NodeName {
106+
t.Errorf("expected name to be %s, got %s", p2pcontext.NodeName, er.objRef.Name)
107+
}
108+
if er.objRef.Namespace != p2pcontext.Namespace {
109+
t.Errorf("expected namespace to be %s, got %s", p2pcontext.Namespace, er.objRef.Namespace)
110+
}
111+
if er.objRef.UID != "test-uid" {
112+
t.Errorf("expected uid to be test-uid, got %s", er.objRef.UID)
113+
}
114+
}
115+
14116
func TestExpectedEvents(t *testing.T) {
15117
er := &eventRecorder{
16118
recorder: &testRecorder{t},

internal/routing/router.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
p2pcontext "github.com/azure/peerd/internal/context"
1212
"github.com/azure/peerd/internal/k8s/events"
13+
"github.com/azure/peerd/pkg/k8s"
1314
"github.com/azure/peerd/pkg/k8s/election"
1415
"github.com/azure/peerd/pkg/peernet"
1516
"github.com/dgraph-io/ristretto"
@@ -26,6 +27,7 @@ import (
2627
)
2728

2829
type router struct {
30+
clientset *k8s.ClientSet
2931
p2pnet peernet.Network
3032
host host.Host
3133
rd *routing.RoutingDiscovery
@@ -42,7 +44,7 @@ type PeerNotFoundError struct {
4244
}
4345

4446
// NewRouter creates a new router.
45-
func NewRouter(ctx context.Context, routerAddr, serverPort string) (Router, error) {
47+
func NewRouter(ctx context.Context, clientset *k8s.ClientSet, routerAddr, serverPort string) (Router, error) {
4648
log := zerolog.Ctx(ctx).With().Str("component", "router").Logger()
4749

4850
h, p, err := net.SplitHostPort(routerAddr)
@@ -137,6 +139,7 @@ func NewRouter(ctx context.Context, routerAddr, serverPort string) (Router, erro
137139
}
138140

139141
return &router{
142+
clientset: clientset,
140143
p2pnet: n,
141144
host: host,
142145
rd: rd,
@@ -196,7 +199,7 @@ func (r *router) Resolve(ctx context.Context, key string, allowSelf bool, count
196199
peerCh <- PeerInfo{info.ID, fmt.Sprintf("https://%s:%s", v, r.port)}
197200

198201
if r.active.CompareAndSwap(false, true) {
199-
er, err := events.NewRecorder(ctx)
202+
er, err := events.NewRecorder(ctx, r.clientset)
200203
if err != nil {
201204
log.Error().Err(err).Msg("could not create event recorder")
202205
} else {

internal/routing/router_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
p2pcontext "github.com/azure/peerd/internal/context"
12+
"github.com/azure/peerd/pkg/k8s"
1213
"github.com/dgraph-io/ristretto"
1314
cid "github.com/ipfs/go-cid"
1415
"github.com/libp2p/go-libp2p/core/connmgr"
@@ -21,8 +22,11 @@ import (
2122
corerouting "github.com/libp2p/go-libp2p/core/routing"
2223
"github.com/libp2p/go-libp2p/p2p/discovery/routing"
2324
multiaddr "github.com/multiformats/go-multiaddr"
25+
"k8s.io/client-go/kubernetes/fake"
2426
)
2527

28+
var fakeClientset = k8s.ClientSet{Interface: fake.NewSimpleClientset(), InPod: true}
29+
2630
func TestResolveWithCache(t *testing.T) {
2731
c, err := ristretto.NewCache(&ristretto.Config{
2832
NumCounters: 1e7,
@@ -41,6 +45,7 @@ func TestResolveWithCache(t *testing.T) {
4145
}
4246

4347
r := &router{
48+
clientset: &fakeClientset,
4449
host: h,
4550
port: "5000",
4651
lookupCache: c,
@@ -79,6 +84,7 @@ func TestResolve(t *testing.T) {
7984
}
8085

8186
r := &router{
87+
clientset: &fakeClientset,
8288
host: h,
8389
port: "5000",
8490
lookupCache: c,
@@ -134,6 +140,7 @@ func TestProvide(t *testing.T) {
134140
}
135141

136142
r := &router{
143+
clientset: &fakeClientset,
137144
host: h,
138145
port: "5000",
139146
lookupCache: c,

pkg/k8s/k8s.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,37 @@ import (
88
"k8s.io/client-go/tools/clientcmd"
99
)
1010

11+
// ClientSet is an interface for k8s API server.
12+
type ClientSet struct {
13+
kubernetes.Interface
14+
15+
// InPod indicates whether the current runtime environment is a pod.
16+
InPod bool
17+
}
18+
1119
// NewKubernetesInterface creates a new interface for k8s API server.
1220
// The current runtime environment is first assumed to be a pod and its identity is used to create the interface.
1321
// If a pod is not detected, the given kubeConfigPath is used to create the interface.
14-
func NewKubernetesInterface(kubeConfigPath string) (kubernetes.Interface, error) {
22+
func NewKubernetesInterface(kubeConfigPath string) (*ClientSet, error) {
23+
k := &ClientSet{}
24+
1525
config, err := rest.InClusterConfig() // Assume run in a Pod or an environment with appropriate env variables set.
1626
if err != nil {
1727
config, err = clientcmd.BuildConfigFromFlags("", kubeConfigPath)
1828
if err != nil {
1929
return nil, err
2030
}
31+
k.InPod = false
32+
} else {
33+
k.InPod = true
2134
}
2235

2336
clientset, err := kubernetes.NewForConfig(config)
2437
if err != nil {
2538
return nil, err
2639
}
2740

28-
return clientset, nil
41+
k.Interface = clientset
42+
43+
return k, nil
2944
}

0 commit comments

Comments
 (0)