Skip to content

Commit

Permalink
Add UT for route agent health checker
Browse files Browse the repository at this point in the history
Signed-off-by: Aswin Suryanarayanan <asuryana@redhat.com>
  • Loading branch information
aswinsuryan committed Sep 6, 2024
1 parent be1d40a commit 6964392
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package healthchecker_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/log/kzerolog"
submarinerv1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
"k8s.io/client-go/kubernetes/scheme"
)

func init() {
kzerolog.AddFlags(nil)
}

var _ = BeforeSuite(func() {
kzerolog.InitK8sLogging()
Expect(submarinerv1.AddToScheme(scheme.Scheme)).To(Succeed())
})

func TestHealthChecker(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "RouteAgent Health Checker Suite")
}
30 changes: 19 additions & 11 deletions pkg/routeagent_driver/handlers/healthchecker/healthchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

var RouteAgentUpdateInterval = 60 * time.Second

type Config struct {
PingInterval uint
MaxPacketLossCount uint
HealthCheckerEnabled bool
NewPinger func(pinger.Config) pinger.Interface
PingInterval uint
MaxPacketLossCount uint
HealthCheckerEnabled bool
RouteAgentUpdateInterval time.Duration
NewPinger func(pinger.Config) pinger.Interface
}

type controller struct {
Expand Down Expand Up @@ -84,7 +83,10 @@ func (h *controller) Stop() error {

h.pingers = map[string]pinger.Interface{}

close(h.stopCh)
if h.stopCh != nil {
close(h.stopCh)
h.stopCh = nil
}

err := h.client.Delete(context.TODO(),
h.localNodeName, metav1.DeleteOptions{})
Expand All @@ -96,6 +98,9 @@ func (h *controller) Stop() error {
}

func (h *controller) RemoteEndpointCreated(endpoint *submarinerv1.Endpoint) error {
h.Lock()
defer h.Unlock()

if !h.config.HealthCheckerEnabled || h.State().IsOnGateway() {
return nil
}
Expand All @@ -104,6 +109,9 @@ func (h *controller) RemoteEndpointCreated(endpoint *submarinerv1.Endpoint) erro
}

func (h *controller) RemoteEndpointUpdated(endpoint *submarinerv1.Endpoint) error {
h.Lock()
defer h.Unlock()

if !h.config.HealthCheckerEnabled || h.State().IsOnGateway() {
return nil
}
Expand All @@ -120,9 +128,6 @@ func (h *controller) processEndpointCreatedOrUpdated(endpoint *submarinerv1.Endp
return nil
}

h.Lock()
defer h.Unlock()

if pingerObject, found := h.pingers[endpoint.Spec.CableName]; found {
if pingerObject.GetIP() == endpoint.Spec.HealthCheckIP {
return nil
Expand Down Expand Up @@ -179,7 +184,7 @@ func (h *controller) Init() error {
defer h.Unlock()

h.syncRouteAgentStatus()
}, RouteAgentUpdateInterval, h.stopCh)
}, h.config.RouteAgentUpdateInterval, h.stopCh)
}()

return nil
Expand Down Expand Up @@ -271,6 +276,9 @@ func (h *controller) syncRouteAgentStatus() {
connectionStatus = submarinerv1.Connecting
statusMessage = ""
}
} else {
connectionStatus = submarinerv1.ConnectionNone
statusMessage = "Health checker IP is not configured"
}

remoteEndpoint = submarinerv1.RemoteEndpoint{
Expand Down
Loading

0 comments on commit 6964392

Please sign in to comment.