Skip to content

Commit

Permalink
Enable unit tests and code coverage for telemetry.go (sonic-net#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
zbud-msft committed Apr 10, 2024
1 parent 4b12af9 commit 6a30d3d
Show file tree
Hide file tree
Showing 5 changed files with 468 additions and 144 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ $(ENVFILE):
tools/test/env.sh | grep -v DB_CONFIG_PATH | tee $@

check_gotest: $(DBCONFG) $(ENVFILE)
sudo CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" $(GO) test -race -coverprofile=coverage-data.txt -covermode=atomic -mod=vendor -v github.com/sonic-net/sonic-gnmi/telemetry
sudo CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" $(GO) test -race -coverprofile=coverage-config.txt -covermode=atomic -v github.com/sonic-net/sonic-gnmi/sonic_db_config
sudo CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" $(TESTENV) $(GO) test -race -coverprofile=coverage-gnmi.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/gnmi_server -coverpkg ../...
ifneq ($(ENABLE_DIALOUT_VALUE),0)
Expand Down
9 changes: 9 additions & 0 deletions gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ func (srv *Server) Serve() error {
return srv.s.Serve(srv.lis)
}

func (srv *Server) Stop() {
s := srv.s
if s == nil {
log.Errorf("Stop() failed: not initialized")
return
}
s.Stop()
}

// Address returns the port the Server is listening to.
func (srv *Server) Address() string {
addr := srv.lis.Addr().String()
Expand Down
51 changes: 29 additions & 22 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,13 +933,13 @@ func TestGnmiSet(t *testing.T) {
})
}
}
s.s.Stop()
s.Stop()
}

func TestGnmiSetReadOnly(t *testing.T) {
s := createReadServer(t, 8081)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

tlsConfig := &tls.Config{InsecureSkipVerify: true}
opts := []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))}
Expand Down Expand Up @@ -971,7 +971,7 @@ func TestGnmiSetReadOnly(t *testing.T) {
func TestGnmiSetAuthFail(t *testing.T) {
s := createAuthServer(t, 8081)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

tlsConfig := &tls.Config{InsecureSkipVerify: true}
opts := []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))}
Expand Down Expand Up @@ -1003,7 +1003,7 @@ func TestGnmiSetAuthFail(t *testing.T) {
func TestGnmiGetAuthFail(t *testing.T) {
s := createAuthServer(t, 8081)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

tlsConfig := &tls.Config{InsecureSkipVerify: true}
opts := []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))}
Expand Down Expand Up @@ -1332,7 +1332,7 @@ func TestGnmiGet(t *testing.T) {

runGnmiTestGet(t, sdcfg.GetDbDefaultNamespace())

s.s.Stop()
s.Stop()
}
func TestGnmiGetMultiNs(t *testing.T) {
sdcfg.Init()
Expand All @@ -1357,7 +1357,7 @@ func TestGnmiGetMultiNs(t *testing.T) {

runGnmiTestGet(t, test_utils.GetMultiNsNamespace())

s.s.Stop()
s.Stop()
}
func TestGnmiGetTranslib(t *testing.T) {
//t.Log("Start server")
Expand Down Expand Up @@ -1499,7 +1499,7 @@ func TestGnmiGetTranslib(t *testing.T) {
runTestGet(t, ctx, gClient, td.pathTarget, td.textPbPath, td.wantRetCode, td.wantRespVal, td.valTest)
})
}
s.s.Stop()
s.Stop()
}

type tablePathValue struct {
Expand Down Expand Up @@ -2601,7 +2601,7 @@ func TestGnmiSubscribe(t *testing.T) {

runTestSubscribe(t, sdcfg.GetDbDefaultNamespace())

s.s.Stop()
s.Stop()
}
func TestGnmiSubscribeMultiNs(t *testing.T) {
sdcfg.Init()
Expand All @@ -2623,7 +2623,7 @@ func TestGnmiSubscribeMultiNs(t *testing.T) {

runTestSubscribe(t, test_utils.GetMultiNsNamespace())

s.s.Stop()
s.Stop()
}

func TestCapabilities(t *testing.T) {
Expand Down Expand Up @@ -2666,7 +2666,7 @@ func TestGNOI(t *testing.T) {
}
s := createServer(t, 8086)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

// prepareDb(t)

Expand Down Expand Up @@ -2757,7 +2757,7 @@ func TestGNOI(t *testing.T) {
func TestBundleVersion(t *testing.T) {
s := createServer(t, 8087)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

// prepareDb(t)

Expand Down Expand Up @@ -2817,7 +2817,7 @@ func TestBundleVersion(t *testing.T) {
func TestBulkSet(t *testing.T) {
s := createServer(t, 8088)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

prepareDbTranslib(t)

Expand Down Expand Up @@ -2936,7 +2936,7 @@ func TestAuthCapabilities(t *testing.T) {

s := createAuthServer(t, 8089)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

currentUser, _ := user.Current()
tlsConfig := &tls.Config{InsecureSkipVerify: true}
Expand Down Expand Up @@ -2975,7 +2975,7 @@ func TestCPUUtilization(t *testing.T) {
defer mock.Reset()
s := createServer(t, 8081)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

tests := []struct {
desc string
Expand Down Expand Up @@ -3045,7 +3045,7 @@ func TestCPUUtilization(t *testing.T) {
func TestClientConnections(t *testing.T) {
s := createRejectServer(t, 8081)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

tests := []struct {
desc string
Expand Down Expand Up @@ -3142,7 +3142,7 @@ func TestClientConnections(t *testing.T) {
func TestConnectionDataSet(t *testing.T) {
s := createServer(t, 8081)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

tests := []struct {
desc string
Expand Down Expand Up @@ -3211,7 +3211,7 @@ func TestConnectionDataSet(t *testing.T) {
func TestConnectionsKeepAlive(t *testing.T) {
s := createKeepAliveServer(t, 8081)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

tests := []struct {
desc string
Expand Down Expand Up @@ -3444,7 +3444,7 @@ func TestClient(t *testing.T) {
// t.Log("END of a TEST")
}

s.s.Stop()
s.Stop()
}

func TestTableData2MsiUseKey(t *testing.T) {
Expand Down Expand Up @@ -3554,7 +3554,7 @@ print('%s')
})
}
}
s.s.Stop()
s.Stop()
}

func TestGNMINative(t *testing.T) {
Expand All @@ -3581,7 +3581,7 @@ print('%s')

s := createServer(t, 8080)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

path, _ := os.Getwd()
path = filepath.Dir(path)
Expand Down Expand Up @@ -3610,7 +3610,7 @@ print('%s')
t.Errorf("GNMI get counter should not be 0")
}
}
s.s.Stop()
s.Stop()
}

func TestServerPort(t *testing.T) {
Expand All @@ -3619,7 +3619,14 @@ func TestServerPort(t *testing.T) {
if port != 0 {
t.Errorf("Invalid port: %d", port)
}
s.s.Stop()
s.Stop()
}

func TestNilServerStop(t *testing.T) {
// Create a server with nil grpc server, such that s.Stop is called with nil value
t.Log("Expecting s.Stop to log error as server is nil")
s := &Server{}
s.Stop()
}

func TestInvalidServer(t *testing.T) {
Expand Down
Loading

0 comments on commit 6a30d3d

Please sign in to comment.