Skip to content

Commit

Permalink
Enable unit tests and code coverage for telemetry.go (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
zbud-msft authored Feb 8, 2024
1 parent 2c862b8 commit d8d15c7
Show file tree
Hide file tree
Showing 5 changed files with 476 additions and 151 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,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 -timeout 20m -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 @@ -177,6 +177,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
55 changes: 31 additions & 24 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,13 +1068,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 @@ -1106,7 +1106,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 @@ -1138,7 +1138,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 @@ -1469,7 +1469,7 @@ func TestGnmiGet(t *testing.T) {

runGnmiTestGet(t, ns)

s.s.Stop()
s.Stop()
}
func TestGnmiGetMultiNs(t *testing.T) {
sdcfg.Init()
Expand All @@ -1494,7 +1494,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 @@ -1636,7 +1636,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 @@ -2739,7 +2739,7 @@ func TestGnmiSubscribe(t *testing.T) {
ns, _ := sdcfg.GetDbDefaultNamespace()
runTestSubscribe(t, ns)

s.s.Stop()
s.Stop()
}
func TestGnmiSubscribeMultiNs(t *testing.T) {
sdcfg.Init()
Expand All @@ -2761,7 +2761,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 @@ -2804,7 +2804,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 @@ -2895,7 +2895,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 @@ -2955,7 +2955,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 @@ -3074,7 +3074,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 @@ -3105,7 +3105,7 @@ func TestAuthCapabilities(t *testing.T) {
func TestTableKeyOnDeletion(t *testing.T) {
s := createKeepAliveServer(t, 8081)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

fileName := "../testdata/NEIGH_STATE_TABLE_MAP.txt"
neighStateTableByte, err := ioutil.ReadFile(fileName)
Expand Down Expand Up @@ -3243,7 +3243,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 @@ -3313,7 +3313,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 @@ -3410,7 +3410,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 @@ -3479,7 +3479,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 @@ -3712,7 +3712,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 @@ -3822,7 +3822,7 @@ print('%s')
})
}
}
s.s.Stop()
s.Stop()
}

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

s := createServer(t, 8080)
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()
ns, _ := sdcfg.GetDbDefaultNamespace()
initFullConfigDb(t, ns)
initFullCountersDb(t, ns)
Expand Down Expand Up @@ -3881,7 +3881,7 @@ print('%s')
t.Errorf("GNMI get counter should not be 0")
}
}
s.s.Stop()
s.Stop()
}

func TestServerPort(t *testing.T) {
Expand All @@ -3890,7 +3890,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 Expand Up @@ -3933,7 +3940,7 @@ func TestMasterArbitration(t *testing.T) {
// Turn on Master Arbitration
s.ReqFromMaster = ReqFromMasterEnabledMA
go runServer(t, s)
defer s.s.Stop()
defer s.Stop()

prepareDbTranslib(t)

Expand Down
Loading

0 comments on commit d8d15c7

Please sign in to comment.