Skip to content

Commit

Permalink
kuberhealthy test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
an1l4 committed May 28, 2024
1 parent 2ef1e0f commit ee7db41
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions agent/kubviz/plugins/kuberhealthy/kuberhealthy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"net/http"
"net/http/httptest"
"os"
"reflect"
"sync"
"testing"
"time"

"github.com/agiledragon/gomonkey"
"github.com/intelops/kubviz/agent/config"
"github.com/intelops/kubviz/pkg/nats/sdk"
khstatev1 "github.com/kuberhealthy/kuberhealthy/v2/pkg/apis/khstate/v1"
"github.com/kuberhealthy/kuberhealthy/v2/pkg/health"
"github.com/nats-io/nats.go"
Expand Down Expand Up @@ -42,7 +44,8 @@ func TestStartKuberhealthy(t *testing.T) {
for _, tt := range cases {
log.Println("Running test case: ", tt.name)
t.Run(tt.name, func(t *testing.T) {
mockJS := &MockJetStreamContext{}
//mockJS := &MockJetStreamContext{}
mockJS := &sdk.NATSClient{}

testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mockResponse := `{"status": "ok"}`
Expand Down Expand Up @@ -95,7 +98,7 @@ func TestStartKuberhealthy(t *testing.T) {

patchPublishKuberhealthyMetrics := gomonkey.ApplyFunc(
PublishKuberhealthyMetrics,
func(js nats.JetStreamContext, state health.State) error {
func(js *sdk.NATSClient, state health.State) error {
return nil
},
)
Expand Down Expand Up @@ -131,8 +134,24 @@ func TestPollAndPublishKuberhealthy(t *testing.T) {

for _, tt := range cases {
log.Println("Running test case: ", tt.name)
mockJS := &sdk.NATSClient{}

mockPublish := gomonkey.ApplyMethod(
reflect.TypeOf(mockJS),
"Publish",
func(*sdk.NATSClient, string, []uint8) error {
if tt.name == "error" {
return errors.New("Error in publish")
}
return nil
},
)
defer mockPublish.Reset()

t.Run(tt.name, func(t *testing.T) {
mockJS := &MockJetStreamContext{}
//mockJS := &MockJetStreamContext{}
mockJS := &sdk.NATSClient{}

if tt.wantErr {
patchReadAll := gomonkey.ApplyFunc(
io.ReadAll,
Expand Down Expand Up @@ -192,7 +211,8 @@ var mockhealthstate health.State

func TestPublishKuberhealthyMetrics(t *testing.T) {

mockJS := &MockJetStreamContext{}
//mockJS := &MockJetStreamContext{}
mockJS := &sdk.NATSClient{}

dummyWorkloadDetails := khstatev1.WorkloadDetails{
OK: true,
Expand Down Expand Up @@ -234,6 +254,19 @@ func TestPublishKuberhealthyMetrics(t *testing.T) {

for i, tt := range tests {
fmt.Println("Running test : ", i)

mockPublish := gomonkey.ApplyMethod(
reflect.TypeOf(mockJS),
"Publish",
func(*sdk.NATSClient, string, []uint8) error {
if tt.name == "error" {
return errors.New("Error in publish")
}
return nil
},
)
defer mockPublish.Reset()

t.Run(tt.name, func(t *testing.T) {

err := PublishKuberhealthyMetrics(mockJS, tt.resource)
Expand Down

0 comments on commit ee7db41

Please sign in to comment.