Skip to content

Commit

Permalink
feat: increase unit test coverage
Browse files Browse the repository at this point in the history
Prepare for upcoming changes by increasing unit test coverage to ensure
that we correctly handle different boundary conditions when we change
how service local / traffic policies work.
  • Loading branch information
aauren committed Jan 24, 2024
1 parent 24505f0 commit 8404260
Show file tree
Hide file tree
Showing 4 changed files with 1,925 additions and 342 deletions.
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ issues:
- text: "G306:"
linters:
- gosec
# Exlude tests from long line linter
# Exclude tests from long line linter
- linters:
- lll
path: _test\.go
# Exclude tests from duplicate linter
- linters:
- dupl
path: _test\.go
# always show all issues rather than only showing 50 at a time
max-issues-per-linter: 0
# always show all issues of a type rather than showing 3
Expand Down
203 changes: 174 additions & 29 deletions pkg/controllers/routing/bgp_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type PolicyTestCase struct {
nrc *NetworkRoutingController
existingNodes []*v1core.Node
existingServices []*v1core.Service
existingEndpoints []*v1core.Endpoints
podDefinedSet *gobgpapi.DefinedSet
clusterIPDefinedSet *gobgpapi.DefinedSet
externalPeerDefinedSet *gobgpapi.DefinedSet
Expand Down Expand Up @@ -75,12 +76,32 @@ func Test_AddPolicies(t *testing.T) {
[]*v1core.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Name: "svc-1",
Namespace: "default",
},
Spec: v1core.ServiceSpec{
Type: ClusterIPST,
ClusterIP: "10.1.0.1",
ExternalIPs: []string{"1.1.1.1"},
Type: ClusterIPST,
ClusterIP: "10.1.0.1",
ExternalIPs: []string{"1.1.1.1"},
InternalTrafficPolicy: &testClusterIntTrafPol,
ExternalTrafficPolicy: testClusterExtTrafPol,
},
},
},
[]*v1core.Endpoints{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "default",
},
Subsets: []v1core.EndpointSubset{
{
Addresses: []v1core.EndpointAddress{
{
IP: testNodeIPv4,
},
},
},
},
},
},
Expand Down Expand Up @@ -205,12 +226,32 @@ func Test_AddPolicies(t *testing.T) {
[]*v1core.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Name: "svc-1",
Namespace: "default",
},
Spec: v1core.ServiceSpec{
Type: "ClusterIP",
ClusterIP: "10.11.0.1",
ExternalIPs: []string{"1.11.1.1"},
Type: "ClusterIP",
ClusterIP: "10.11.0.1",
ExternalIPs: []string{"1.11.1.1"},
InternalTrafficPolicy: &testClusterIntTrafPol,
ExternalTrafficPolicy: testClusterExtTrafPol,
},
},
},
[]*v1core.Endpoints{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "default",
},
Subsets: []v1core.EndpointSubset{
{
Addresses: []v1core.EndpointAddress{
{
IP: testNodeIPv4,
},
},
},
},
},
},
Expand Down Expand Up @@ -428,12 +469,32 @@ func Test_AddPolicies(t *testing.T) {
[]*v1core.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Name: "svc-1",
Namespace: "default",
},
Spec: v1core.ServiceSpec{
Type: ClusterIPST,
ClusterIP: "10.12.0.1",
ExternalIPs: []string{"1.12.1.1"},
Type: ClusterIPST,
ClusterIP: "10.12.0.1",
ExternalIPs: []string{"1.12.1.1"},
InternalTrafficPolicy: &testClusterIntTrafPol,
ExternalTrafficPolicy: testClusterExtTrafPol,
},
},
},
[]*v1core.Endpoints{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "default",
},
Subsets: []v1core.EndpointSubset{
{
Addresses: []v1core.EndpointAddress{
{
IP: testNodeIPv4,
},
},
},
},
},
},
Expand Down Expand Up @@ -613,12 +674,32 @@ func Test_AddPolicies(t *testing.T) {
[]*v1core.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Name: "svc-1",
Namespace: "default",
},
Spec: v1core.ServiceSpec{
Type: ClusterIPST,
ClusterIP: "10.13.0.1",
ExternalIPs: []string{"1.13.1.1"},
Type: ClusterIPST,
ClusterIP: "10.13.0.1",
ExternalIPs: []string{"1.13.1.1"},
InternalTrafficPolicy: &testClusterIntTrafPol,
ExternalTrafficPolicy: testClusterExtTrafPol,
},
},
},
[]*v1core.Endpoints{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "default",
},
Subsets: []v1core.EndpointSubset{
{
Addresses: []v1core.EndpointAddress{
{
IP: testNodeIPv4,
},
},
},
},
},
},
Expand Down Expand Up @@ -783,12 +864,32 @@ func Test_AddPolicies(t *testing.T) {
[]*v1core.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Name: "svc-1",
Namespace: "default",
},
Spec: v1core.ServiceSpec{
Type: ClusterIPST,
ClusterIP: "10.14.0.1",
ExternalIPs: []string{"1.14.1.1"},
Type: ClusterIPST,
ClusterIP: "10.14.0.1",
ExternalIPs: []string{"1.14.1.1"},
InternalTrafficPolicy: &testClusterIntTrafPol,
ExternalTrafficPolicy: testClusterExtTrafPol,
},
},
},
[]*v1core.Endpoints{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "default",
},
Subsets: []v1core.EndpointSubset{
{
Addresses: []v1core.EndpointAddress{
{
IP: testNodeIPv4,
},
},
},
},
},
},
Expand Down Expand Up @@ -973,12 +1074,32 @@ func Test_AddPolicies(t *testing.T) {
[]*v1core.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Name: "svc-1",
Namespace: "default",
},
Spec: v1core.ServiceSpec{
Type: ClusterIPST,
ClusterIP: "10.15.0.1",
ExternalIPs: []string{"1.15.1.1"},
Type: ClusterIPST,
ClusterIP: "10.15.0.1",
ExternalIPs: []string{"1.15.1.1"},
InternalTrafficPolicy: &testClusterIntTrafPol,
ExternalTrafficPolicy: testClusterExtTrafPol,
},
},
},
[]*v1core.Endpoints{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "default",
},
Subsets: []v1core.EndpointSubset{
{
Addresses: []v1core.EndpointAddress{
{
IP: testNodeIPv4,
},
},
},
},
},
},
Expand Down Expand Up @@ -1164,12 +1285,32 @@ func Test_AddPolicies(t *testing.T) {
[]*v1core.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Name: "svc-1",
Namespace: "default",
},
Spec: v1core.ServiceSpec{
Type: ClusterIPST,
ClusterIP: "10.16.0.1",
ExternalIPs: []string{"1.16.1.1"},
Type: ClusterIPST,
ClusterIP: "10.16.0.1",
ExternalIPs: []string{"1.16.1.1"},
InternalTrafficPolicy: &testClusterIntTrafPol,
ExternalTrafficPolicy: testClusterExtTrafPol,
},
},
},
[]*v1core.Endpoints{
{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "default",
},
Subsets: []v1core.EndpointSubset{
{
Addresses: []v1core.EndpointAddress{
{
IP: testNodeIPv4,
},
},
},
},
},
},
Expand Down Expand Up @@ -1309,7 +1450,11 @@ func Test_AddPolicies(t *testing.T) {
}

if err := createServices(testcase.nrc.clientset, testcase.existingServices); err != nil {
t.Errorf("failed to create existing nodes: %v", err)
t.Errorf("failed to create existing services: %v", err)
}

if err := createEndpoints(testcase.nrc.clientset, testcase.existingEndpoints); err != nil {
t.Errorf("failed to create existing endpoints: %v", err)
}

err := testcase.nrc.startBgpServer(false)
Expand Down
Loading

0 comments on commit 8404260

Please sign in to comment.