Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…emon into training
  • Loading branch information
anandrgitnirman committed May 11, 2022
2 parents 1013417 + 70defd2 commit 6192e3f
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 85 deletions.
63 changes: 38 additions & 25 deletions metrics/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package metrics
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
pb "google.golang.org/grpc/health/grpc_health_v1"
"net"
Expand All @@ -17,6 +18,25 @@ import (
// server is used to implement api.HeartbeatServer
type server struct{}

type ClientTestSuite struct {
suite.Suite
serviceURL string
server *grpc.Server
}

func (suite *ClientTestSuite) TearDownSuite() {
suite.server.GracefulStop()
}
func (suite *ClientTestSuite) SetupSuite() {
SetNoHeartbeatURLState(false)
suite.serviceURL = "http://localhost:1111"
suite.server = setAndServe()
}

func TestClientTestSuite(t *testing.T) {
suite.Run(t, new(ClientTestSuite))
}

func (s *server) Check(ctx context.Context, in *pb.HealthCheckRequest) (*pb.HealthCheckResponse, error) {
return &pb.HealthCheckResponse{Status: pb.HealthCheckResponse_SERVING}, nil
}
Expand Down Expand Up @@ -53,49 +73,42 @@ func StartMockGrpcService() {
_ = <-ch
}

func Test_callgRPCServiceHeartbeat(t *testing.T) {
func (suite *ClientTestSuite) Test_callgRPCServiceHeartbeat() {
// Start the grpc mock server
StartMockGrpcService()

serviceURL := "localhost" + testPort
heartbeat, err := callgRPCServiceHeartbeat(serviceURL)
assert.False(t, err != nil)
assert.False(suite.T(), err != nil)

assert.NotEqual(t, `{}`, string(heartbeat), "Service Heartbeat must not be empty.")
assert.Equal(t, heartbeat.String(), pb.HealthCheckResponse_SERVING.String())
assert.NotEqual(suite.T(), `{}`, string(heartbeat), "Service Heartbeat must not be empty.")
assert.Equal(suite.T(), heartbeat.String(), pb.HealthCheckResponse_SERVING.String())

serviceURL = "localhost:26000"
heartbeat, err = callgRPCServiceHeartbeat(serviceURL)
assert.True(t, err != nil)
assert.True(suite.T(), err != nil)
}

func Test_callHTTPServiceHeartbeat(t *testing.T) {
serviceURL := "http://demo5343751.mockable.io/heartbeat"
func (suite *ClientTestSuite) Test_callHTTPServiceHeartbeat() {
serviceURL := suite.serviceURL + "/heartbeat"
heartbeat, err := callHTTPServiceHeartbeat(serviceURL)
assert.False(t, err != nil)
assert.NotEqual(t, string(heartbeat), `{}`, "Service Heartbeat must not be empty.")
assert.Equal(t, string(heartbeat), `{"serviceID":"SERVICE001", "status":"SERVING"}`,
assert.False(suite.T(), err != nil)
assert.NotEqual(suite.T(), string(heartbeat), `{}`, "Service Heartbeat must not be empty.")
assert.Equal(suite.T(), string(heartbeat), `{"serviceID":"SERVICE001","status":"SERVING"}`,
"Unexpected service heartbeat")

/* var sHeartbeat pb.HeartbeatMsg
err = json.Unmarshal(heartbeat, &sHeartbeat)
assert.True(t, err != nil)
assert.Equal(t, sHeartbeat.ServiceID, "SERVICE001", "Unexpected service ID")
*/serviceURL = "http://demo8325345.mockable.io"
heartbeat, err = callHTTPServiceHeartbeat(serviceURL)
assert.True(t, err != nil)
heartbeat, err = callHTTPServiceHeartbeat(suite.serviceURL)
assert.True(suite.T(), err != nil)
}

func Test_callRegisterService(t *testing.T) {
serviceURL := "https://demo5343751.mockable.io/register"
func (suite *ClientTestSuite) Test_callRegisterService() {

daemonID := GetDaemonID()

result := callRegisterService(daemonID, serviceURL)
assert.Equal(t, true, result)
result := callRegisterService(daemonID, suite.serviceURL+"/register")
assert.Equal(suite.T(), true, result)

serviceURL = "https://demo5343751.mockable.io/registererror"
result = callRegisterService(daemonID, serviceURL)
assert.Equal(t, false, result)
result = callRegisterService(daemonID, suite.serviceURL+"/registererror")
assert.Equal(suite.T(), false, result)

}
154 changes: 111 additions & 43 deletions metrics/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,92 @@ package metrics

import (
"encoding/json"
"fmt"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/soheilhy/cmux"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestStatus_String(t *testing.T) {
assert.Equal(t, Online.String(), "Online", "Invalid enum string conversion")
assert.NotEqual(t, Online.String(), "Offline", "Invalid enum string conversion")
type HeartBeatTestSuite struct {
suite.Suite
serviceURL string
server *grpc.Server
}

func TestHeartbeatHandler(t *testing.T) {
func (suite *HeartBeatTestSuite) TearDownSuite() {
suite.server.GracefulStop()
}
func (suite *HeartBeatTestSuite) SetupSuite() {
SetNoHeartbeatURLState(false)
suite.serviceURL = "http://localhost:1111"
suite.server = setAndServe()
}
func setAndServe() (server *grpc.Server) {
server = grpc.NewServer()
ch := make(chan int)
go func() {
lis, err := net.Listen("tcp", ":1111")
if err != nil {
panic(err)
}
mux := cmux.New(lis)
grpcWebServer := grpcweb.WrapServer(server, grpcweb.WithCorsForRegisteredEndpointsOnly(false))
httpHandler := http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if grpcWebServer.IsGrpcWebRequest(req) || grpcWebServer.IsAcceptableGrpcCorsRequest(req) {
grpcWebServer.ServeHTTP(resp, req)
} else {
if strings.Split(req.URL.Path, "/")[1] == "register" {
resp.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintln(resp, "Registering service...... ")
} else if strings.Split(req.URL.Path, "/")[1] == "heartbeat" {
resp.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprint(resp, "{\"serviceID\":\"SERVICE001\",\"status\":\"SERVING\"}")
} else {
http.NotFound(resp, req)

}
}
})
daemonHeartBeat := &DaemonHeartbeat{DaemonID: "metrics.GetDaemonID()", DaemonVersion: "test version"}
grpc_health_v1.RegisterHealthServer(server, daemonHeartBeat)

httpL := mux.Match(cmux.HTTP1Fast())
grpcL := mux.MatchWithWriters(cmux.HTTP2MatchHeaderFieldPrefixSendSettings("content-type", "application/grpc"))
go server.Serve(grpcL)
go http.Serve(httpL, httpHandler)
go mux.Serve()
ch <- 0
}()

_ = <-ch
return
}

func TestHeartBeatTestSuite(t *testing.T) {
suite.Run(t, new(HeartBeatTestSuite))
}

func (suite *HeartBeatTestSuite) TestStatus_String() {
assert.Equal(suite.T(), Online.String(), "Online", "Invalid enum string conversion")
assert.NotEqual(suite.T(), Online.String(), "Offline", "Invalid enum string conversion")
}

func (suite *HeartBeatTestSuite) TestHeartbeatHandler() {
SetNoHeartbeatURLState(false)
// Creating a request to pass to the handler. third parameter is nil since we are not passing any parameters to service
request, err := http.NewRequest("GET", "/heartbeat", nil)
if err != nil {
assert.Fail(t, "Unable to create request payload for testing the Heartbeat Handler")
assert.Fail(suite.T(), "Unable to create request payload for testing the Heartbeat Handler")
}

// Creating a ResponseRecorder to record the response.
Expand All @@ -36,85 +103,86 @@ func TestHeartbeatHandler(t *testing.T) {
handler.ServeHTTP(response, request)

//test the responses
assert.Equal(t, http.StatusOK, response.Code, "handler returned wrong status code")
assert.Equal(suite.T(), http.StatusOK, response.Code, "handler returned wrong status code")
heartbeat, _ := ioutil.ReadAll(response.Body)

var dHeartbeat DaemonHeartbeat
err = json.Unmarshal([]byte(heartbeat), &dHeartbeat)
assert.False(t, err != nil)
assert.NotNil(t, dHeartbeat, "heartbeat must not be nil")
assert.False(suite.T(), err != nil)
assert.NotNil(suite.T(), dHeartbeat, "heartbeat must not be nil")

assert.Equal(t, dHeartbeat.Status, Warning.String(), "Invalid State")
assert.NotEqual(t, dHeartbeat.Status, Offline.String(), "Invalid State")
assert.Equal(suite.T(), dHeartbeat.Status, Warning.String(), "Invalid State")
assert.NotEqual(suite.T(), dHeartbeat.Status, Offline.String(), "Invalid State")

assert.Equal(t, dHeartbeat.DaemonID, "f940de0eb33eeddb283ac725478900deac24151b019e496c476d59f72c38abb3",
assert.Equal(suite.T(), dHeartbeat.DaemonID, "f940de0eb33eeddb283ac725478900deac24151b019e496c476d59f72c38abb3",
"Incorrect daemon ID")

assert.NotEqual(t, dHeartbeat.ServiceHeartbeat, `{}`, "Service Heartbeat must not be empty.")
assert.Equal(t, dHeartbeat.ServiceHeartbeat, `{"serviceID":"ExampleServiceId","status":"NOT_SERVING"}`,
assert.NotEqual(suite.T(), dHeartbeat.ServiceHeartbeat, `{}`, "Service Heartbeat must not be empty.")
assert.Equal(suite.T(), dHeartbeat.ServiceHeartbeat, `{"serviceID":"ExampleServiceId","status":"NOT_SERVING"}`,
"Unexpected service heartbeat")
}

func Test_GetHeartbeat(t *testing.T) {
serviceURL := "http://demo5343751.mockable.io/heartbeat"
func (suite *HeartBeatTestSuite) Test_GetHeartbeat() {
serviceURL := suite.serviceURL + "/heartbeat"
serviceType := "http"
serviveID := "SERVICE001"

dHeartbeat, _ := GetHeartbeat(serviceURL, serviceType, serviveID)
assert.NotNil(t, dHeartbeat, "heartbeat must not be nil")
assert.NotNil(suite.T(), dHeartbeat, "heartbeat must not be nil")

assert.Equal(t, dHeartbeat.Status, Online.String(), "Invalid State")
assert.NotEqual(t, dHeartbeat.Status, Offline.String(), "Invalid State")
assert.Equal(suite.T(), dHeartbeat.Status, Online.String(), "Invalid State")
assert.NotEqual(suite.T(), dHeartbeat.Status, Offline.String(), "Invalid State")

assert.Equal(t, dHeartbeat.DaemonID, "f940de0eb33eeddb283ac725478900deac24151b019e496c476d59f72c38abb3",
assert.Equal(suite.T(), dHeartbeat.DaemonID, "f940de0eb33eeddb283ac725478900deac24151b019e496c476d59f72c38abb3",
"Incorrect daemon ID")

assert.NotEqual(t, dHeartbeat.ServiceHeartbeat, `{}`, "Service Heartbeat must not be empty.")
assert.Equal(t, dHeartbeat.ServiceHeartbeat, `{"serviceID":"SERVICE001", "status":"SERVING"}`,
assert.NotEqual(suite.T(), dHeartbeat.ServiceHeartbeat, `{}`, "Service Heartbeat must not be empty.")
assert.Equal(suite.T(), dHeartbeat.ServiceHeartbeat, `{"serviceID":"SERVICE001","status":"SERVING"}`,
"Unexpected service heartbeat")

var sHeartbeat DaemonHeartbeat
err := json.Unmarshal([]byte(dHeartbeat.ServiceHeartbeat), &sHeartbeat)
assert.True(t, err == nil)
assert.Equal(t, sHeartbeat.Status, grpc_health_v1.HealthCheckResponse_SERVING.String())
assert.True(suite.T(), err == nil)
assert.Equal(suite.T(), sHeartbeat.Status, grpc_health_v1.HealthCheckResponse_SERVING.String())

// check with some timeout URL
serviceURL = "http://demo5343751.mockable.io"
dHeartbeat, _ = GetHeartbeat(serviceURL, serviceType, serviveID)
assert.NotNil(t, dHeartbeat, "heartbeat must not be nil")
serviceURL = "http://localhost:1234"
SetNoHeartbeatURLState(false)
dHeartbeat2, _ := GetHeartbeat(serviceURL, serviceType, serviveID)
assert.NotNil(suite.T(), dHeartbeat2, "heartbeat must not be nil")

assert.Equal(t, dHeartbeat.Status, Warning.String(), "Invalid State")
assert.NotEqual(t, dHeartbeat.Status, Online.String(), "Invalid State")
assert.Equal(suite.T(), dHeartbeat2.Status, Warning.String(), "Invalid State")
assert.NotEqual(suite.T(), dHeartbeat2.Status, Online.String(), "Invalid State")

assert.NotEqual(t, dHeartbeat.ServiceHeartbeat, `{}`, "Service Heartbeat must not be empty.")
assert.Equal(t, dHeartbeat.ServiceHeartbeat, `{"serviceID":"SERVICE001","status":"NOT_SERVING"}`,
assert.NotEqual(suite.T(), dHeartbeat2.ServiceHeartbeat, `{}`, "Service Heartbeat must not be empty.")
assert.Equal(suite.T(), dHeartbeat2.ServiceHeartbeat, `{"serviceID":"SERVICE001","status":"NOT_SERVING"}`,
"Unexpected service heartbeat")
}

func validateHeartbeat(t *testing.T, dHeartbeat DaemonHeartbeat) {
assert.NotNil(t, dHeartbeat, "heartbeat must not be nil")
func (suite *HeartBeatTestSuite) validateHeartbeat(dHeartbeat DaemonHeartbeat) {
assert.NotNil(suite.T(), dHeartbeat, "heartbeat must not be nil")

assert.Equal(t, dHeartbeat.Status, Online.String(), "Invalid State")
assert.NotEqual(t, dHeartbeat.Status, Offline.String(), "Invalid State")
assert.Equal(suite.T(), dHeartbeat.Status, Online.String(), "Invalid State")
assert.NotEqual(suite.T(), dHeartbeat.Status, Offline.String(), "Invalid State")

assert.Equal(t, dHeartbeat.DaemonID, "cc48d343313a1e06093c81830103b45496749e9ee632fd03207d042c277f3210",
assert.Equal(suite.T(), dHeartbeat.DaemonID, "cc48d343313a1e06093c81830103b45496749e9ee632fd03207d042c277f3210",
"Incorrect daemon ID")

assert.NotEqual(t, dHeartbeat.ServiceHeartbeat, `{}`, "Service Heartbeat must not be empty.")
assert.Equal(t, dHeartbeat.ServiceHeartbeat, `{"serviceID":"SERVICE001", "status":"SERVING"}`,
assert.NotEqual(suite.T(), dHeartbeat.ServiceHeartbeat, `{}`, "Service Heartbeat must not be empty.")
assert.Equal(suite.T(), dHeartbeat.ServiceHeartbeat, `{"serviceID":"SERVICE001", "status":"SERVING"}`,
"Unexpected service heartbeat")
}

func TestSetNoHeartbeatURLState(t *testing.T) {
func (suite *HeartBeatTestSuite) TestSetNoHeartbeatURLState() {
SetNoHeartbeatURLState(true)
assert.Equal(t, true, isNoHeartbeatURL)
assert.Equal(suite.T(), true, isNoHeartbeatURL)

SetNoHeartbeatURLState(false)
assert.Equal(t, false, isNoHeartbeatURL)
assert.Equal(suite.T(), false, isNoHeartbeatURL)
}

func TestValidateHeartbeatConfig(t *testing.T) {
func (suite *HeartBeatTestSuite) TestValidateHeartbeatConfig() {
err := ValidateHeartbeatConfig()
assert.Nil(t, err)
assert.Equal(t, true, isNoHeartbeatURL)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), true, isNoHeartbeatURL)
}
45 changes: 32 additions & 13 deletions metrics/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,55 @@
package metrics

import (
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"testing"

"github.com/stretchr/testify/assert"
)

type RegisterTestSuite struct {
suite.Suite
serviceURL string
server *grpc.Server
}

func (suite *RegisterTestSuite) TearDownSuite() {
suite.server.GracefulStop()
}
func (suite *RegisterTestSuite) SetupSuite() {
SetNoHeartbeatURLState(false)
suite.serviceURL = "http://localhost:1111"
suite.server = setAndServe()
}

func TestRegisterTestSuite(t *testing.T) {
suite.Run(t, new(RegisterTestSuite))
}
func TestGetDaemonID(t *testing.T) {
daemonID := GetDaemonID()

assert.NotNil(t, daemonID, "daemon ID must not be nil")
assert.NotEmpty(t, daemonID, "daemon ID must not be empty")
assert.Equal(t, "f940de0eb33eeddb283ac725478900deac24151b019e496c476d59f72c38abb3", daemonID)
assert.Equal(t, "091a31bb4b808c574fb5e158923f3067c4e23e805252699fb5946121e7ca1506", daemonID)
assert.NotEqual(t, "48d343313a1e06093c81830103b45496cc7c277f321049e9ee632fd03207d042", daemonID)
}

func TestRegisterDaemon(t *testing.T) {
serviceURL := "https://demo5343751.mockable.io/register"
func (suite *RegisterTestSuite) TestRegisterDaemon() {

result := RegisterDaemon(serviceURL)
assert.Equal(t, true, result)
result := RegisterDaemon(suite.serviceURL + "/register")
assert.Equal(suite.T(), true, result)

serviceURL = "https://demo5343751.mockable.io/registererror"
result = RegisterDaemon(serviceURL)
assert.Equal(t, false, result)
wrongserviceURL := "https://localhost:9999/registererror"
result = RegisterDaemon(wrongserviceURL)
assert.Equal(suite.T(), false, result)
}

func TestSetDaemonGrpId(t *testing.T) {
func (suite *RegisterTestSuite) TestSetDaemonGrpId() {
grpid := "group01"
SetDaemonGrpId(grpid)
assert.NotNil(t, daemonGroupId)
assert.NotEmpty(t, daemonGroupId)
assert.Equal(t, "group01", daemonGroupId)
assert.NotEqual(t, "some wrong group id", daemonGroupId)
assert.NotNil(suite.T(), daemonGroupId)
assert.NotEmpty(suite.T(), daemonGroupId)
assert.Equal(suite.T(), "group01", daemonGroupId)
assert.NotEqual(suite.T(), "some wrong group id", daemonGroupId)
}
Loading

0 comments on commit 6192e3f

Please sign in to comment.