-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
277 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 99 additions & 84 deletions
183
internal/mode/static/nginx/agent/grpc/connections_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,108 @@ | ||
package grpc_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
agentgrpc "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/agent/grpc" | ||
) | ||
|
||
var _ = Describe("ConnectionsTracker", func() { | ||
var tracker agentgrpc.ConnectionsTracker | ||
|
||
BeforeEach(func() { | ||
tracker = agentgrpc.NewConnectionsTracker() | ||
}) | ||
|
||
It("should add a connection to the tracker", func() { | ||
conn := agentgrpc.Connection{ | ||
PodName: "pod1", | ||
InstanceID: "instance1", | ||
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"}, | ||
} | ||
tracker.Track("key1", conn) | ||
|
||
trackedConn := tracker.GetConnection("key1") | ||
Expect(trackedConn).To(Equal(conn)) | ||
}) | ||
|
||
It("should return an empty connection if the key does not exist", func() { | ||
trackedConn := tracker.GetConnection("nonexistent") | ||
Expect(trackedConn).To(Equal(agentgrpc.Connection{})) | ||
}) | ||
|
||
It("should return true if the connection is ready", func() { | ||
conn := agentgrpc.Connection{ | ||
PodName: "pod1", | ||
InstanceID: "instance1", | ||
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"}, | ||
} | ||
tracker.Track("key1", conn) | ||
|
||
trackedConn, ready := tracker.ConnectionIsReady("key1") | ||
Expect(ready).To(BeTrue()) | ||
Expect(trackedConn).To(Equal(conn)) | ||
}) | ||
|
||
It("should return false if the connection is not ready when instanceID is not specified", func() { | ||
conn := agentgrpc.Connection{ | ||
PodName: "pod1", | ||
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"}, | ||
} | ||
tracker.Track("key1", conn) | ||
|
||
_, ready := tracker.ConnectionIsReady("key1") | ||
Expect(ready).To(BeFalse()) | ||
}) | ||
|
||
It("should set the instance ID for a connection", func() { | ||
conn := agentgrpc.Connection{ | ||
PodName: "pod1", | ||
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"}, | ||
} | ||
tracker.Track("key1", conn) | ||
|
||
_, ready := tracker.ConnectionIsReady("key1") | ||
Expect(ready).To(BeFalse()) | ||
|
||
tracker.SetInstanceID("key1", "instance1") | ||
|
||
trackedConn, ready := tracker.ConnectionIsReady("key1") | ||
Expect(ready).To(BeTrue()) | ||
Expect(trackedConn.InstanceID).To(Equal("instance1")) | ||
}) | ||
|
||
It("should remove all connections for a given parent", func() { | ||
parent := types.NamespacedName{Namespace: "default", Name: "parent1"} | ||
conn1 := agentgrpc.Connection{PodName: "pod1", InstanceID: "instance1", Parent: parent} | ||
conn2 := agentgrpc.Connection{PodName: "pod2", InstanceID: "instance2", Parent: parent} | ||
|
||
parent2 := types.NamespacedName{Namespace: "default", Name: "parent2"} | ||
conn3 := agentgrpc.Connection{PodName: "pod3", InstanceID: "instance3", Parent: parent2} | ||
|
||
tracker.Track("key1", conn1) | ||
tracker.Track("key2", conn2) | ||
tracker.Track("key3", conn3) | ||
|
||
tracker.UntrackConnectionsForParent(parent) | ||
Expect(tracker.GetConnection("key1")).To(Equal(agentgrpc.Connection{})) | ||
Expect(tracker.GetConnection("key2")).To(Equal(agentgrpc.Connection{})) | ||
Expect(tracker.GetConnection("key3")).To(Equal(conn3)) | ||
}) | ||
}) | ||
func TestGetConnection(t *testing.T) { | ||
t.Parallel() | ||
g := NewWithT(t) | ||
|
||
tracker := agentgrpc.NewConnectionsTracker() | ||
|
||
conn := agentgrpc.Connection{ | ||
PodName: "pod1", | ||
InstanceID: "instance1", | ||
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"}, | ||
} | ||
tracker.Track("key1", conn) | ||
|
||
trackedConn := tracker.GetConnection("key1") | ||
g.Expect(trackedConn).To(Equal(conn)) | ||
|
||
nonExistent := tracker.GetConnection("nonexistent") | ||
g.Expect(nonExistent).To(Equal(agentgrpc.Connection{})) | ||
} | ||
|
||
func TestConnectionIsReady(t *testing.T) { | ||
t.Parallel() | ||
g := NewWithT(t) | ||
|
||
tracker := agentgrpc.NewConnectionsTracker() | ||
|
||
conn := agentgrpc.Connection{ | ||
PodName: "pod1", | ||
InstanceID: "instance1", | ||
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"}, | ||
} | ||
tracker.Track("key1", conn) | ||
|
||
trackedConn, ready := tracker.ConnectionIsReady("key1") | ||
g.Expect(ready).To(BeTrue()) | ||
g.Expect(trackedConn).To(Equal(conn)) | ||
} | ||
|
||
func TestConnectionIsNotReady(t *testing.T) { | ||
t.Parallel() | ||
g := NewWithT(t) | ||
|
||
tracker := agentgrpc.NewConnectionsTracker() | ||
|
||
conn := agentgrpc.Connection{ | ||
PodName: "pod1", | ||
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"}, | ||
} | ||
tracker.Track("key1", conn) | ||
|
||
_, ready := tracker.ConnectionIsReady("key1") | ||
g.Expect(ready).To(BeFalse()) | ||
} | ||
|
||
func TestSetInstanceID(t *testing.T) { | ||
t.Parallel() | ||
g := NewWithT(t) | ||
|
||
tracker := agentgrpc.NewConnectionsTracker() | ||
conn := agentgrpc.Connection{ | ||
PodName: "pod1", | ||
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"}, | ||
} | ||
tracker.Track("key1", conn) | ||
|
||
_, ready := tracker.ConnectionIsReady("key1") | ||
g.Expect(ready).To(BeFalse()) | ||
|
||
tracker.SetInstanceID("key1", "instance1") | ||
|
||
trackedConn, ready := tracker.ConnectionIsReady("key1") | ||
g.Expect(ready).To(BeTrue()) | ||
g.Expect(trackedConn.InstanceID).To(Equal("instance1")) | ||
} | ||
|
||
func TestUntrackConnectionsForParent(t *testing.T) { | ||
t.Parallel() | ||
g := NewWithT(t) | ||
|
||
tracker := agentgrpc.NewConnectionsTracker() | ||
|
||
parent := types.NamespacedName{Namespace: "default", Name: "parent1"} | ||
conn1 := agentgrpc.Connection{PodName: "pod1", InstanceID: "instance1", Parent: parent} | ||
conn2 := agentgrpc.Connection{PodName: "pod2", InstanceID: "instance2", Parent: parent} | ||
|
||
parent2 := types.NamespacedName{Namespace: "default", Name: "parent2"} | ||
conn3 := agentgrpc.Connection{PodName: "pod3", InstanceID: "instance3", Parent: parent2} | ||
|
||
tracker.Track("key1", conn1) | ||
tracker.Track("key2", conn2) | ||
tracker.Track("key3", conn3) | ||
|
||
tracker.UntrackConnectionsForParent(parent) | ||
g.Expect(tracker.GetConnection("key1")).To(Equal(agentgrpc.Connection{})) | ||
g.Expect(tracker.GetConnection("key2")).To(Equal(agentgrpc.Connection{})) | ||
g.Expect(tracker.GetConnection("key3")).To(Equal(conn3)) | ||
} |
Oops, something went wrong.