Skip to content

Commit

Permalink
Update coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ganglyu committed Jun 16, 2022
1 parent 482bc36 commit 1fffa3d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
51 changes: 51 additions & 0 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

"github.com/sonic-net/sonic-gnmi/common_utils"
// Register supported client types.
sdc "github.com/sonic-net/sonic-gnmi/sonic_data_client"
)
Expand All @@ -44,6 +45,24 @@ func createServer(t *testing.T, port int64) *Server {
return s
}

func createInvalidServer(t *testing.T, port int64) *Server {
certificate, err := testcert.NewCert()
if err != nil {
t.Errorf("could not load server key pair: %s", err)
}
tlsCfg := &tls.Config{
ClientAuth: tls.RequestClientCert,
Certificates: []tls.Certificate{certificate},
}

opts := []grpc.ServerOption{grpc.Creds(credentials.NewTLS(tlsCfg))}
s, err := NewServer(nil, opts)
if err != nil {
return nil
}
return s
}

func createAuthServer(t *testing.T, port int64) *Server {
certificate, err := testcert.NewCert()
if err != nil {
Expand Down Expand Up @@ -88,6 +107,24 @@ func TestAll(t *testing.T) {
} else {
fmt.Println(string(result))
}
return
}

func TestServerPort(t *testing.T) {
s := createServer(t, -8080)
port := s.Port()
if port != 0 {
t.Errorf("Invalid port: %d", port)
}
return
}

func TestInvalidServer(t *testing.T) {
s := createInvalidServer(t, 8080)
if s != nil {
t.Errorf("Should not create invalid server")
}
return
}

func TestAuth(t *testing.T) {
Expand All @@ -106,6 +143,7 @@ func TestAuth(t *testing.T) {
} else {
fmt.Println(string(result))
}
return
}

func TestAuthType(t *testing.T) {
Expand Down Expand Up @@ -139,6 +177,19 @@ func TestAuthType(t *testing.T) {
fmt.Println(at.String())
}

func TestAuthUser(t *testing.T) {
var err error
auth := common_utils.AuthInfo{}
err = PopulateAuthStruct("root", &auth, nil)
if err != nil {
t.Errorf("PopulateAuthStruct failed: %v", err)
}
err = PopulateAuthStruct("invalid_user_name", &auth, nil)
if err == nil {
t.Errorf("PopulateAuthStruct failed: %v", err)
}
}

func init() {
// Enable logs at UT setup
flag.Lookup("v").Value.Set("10")
Expand Down
2 changes: 0 additions & 2 deletions sonic_data_client/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,8 +1161,6 @@ func (c *DbClient) SetIncrementalConfig(delete []*gnmipb.Path, replace []*gnmipb
defer sc.DeleteCheckPoint(c.workPath + "/config")
if c.origin == "sonic-db" {
err = sc.ApplyPatchDb(patchFile)
} else if c.origin == "sonic-yang" {
err = sc.ApplyPatchYang(patchFile)
}

if err == nil {
Expand Down
16 changes: 15 additions & 1 deletion test/test_gnmi_appldb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import json
from utils import gnmi_set, gnmi_get
from utils import gnmi_set, gnmi_get, gnmi_get_with_encoding

import pytest

Expand Down Expand Up @@ -451,3 +451,17 @@ def test_gnmi_invalid_target_03(self):
hit = True
break
assert hit == True, 'No expected error: %s'%exp

def test_gnmi_invalid_encoding(self):
path = '/sonic-db:APPL_DB/DASH_QOS'
get_list = [path]
ret, msg_list = gnmi_get_with_encoding(get_list, "PROTO")
assert ret != 0, 'Encoding is not supported'
hit = False
exp = 'unsupported encoding'
for msg in msg_list:
if exp in msg:
hit = True
break
assert hit == True, 'No expected error: %s'%exp

19 changes: 19 additions & 0 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ def gnmi_get(path_list):
return -1, [msg]
return ret, [msg]

def gnmi_get_with_encoding(path_list, encoding):
path = os.getcwd()
cmd = path + '/build/bin/gnmi_get '
cmd += '-insecure -username admin -password sonicadmin '
cmd += '-target_addr 127.0.0.1:8080 '
cmd += '-alsologtostderr '
cmd += '-encoding %s '%(encoding)
for path in path_list:
cmd += " -xpath " + path
ret, msg = run_cmd(cmd)
if ret == 0:
msg = msg.replace('\\', '')
find_list = re.findall( r'json_ietf_val:\s*"(.*?)"\s*>', msg)
if find_list:
return ret, find_list
else:
return -1, [msg]
return ret, [msg]

def gnmi_get_with_password(path_list, user, password):
path = os.getcwd()
cmd = path + '/build/bin/gnmi_get '
Expand Down

0 comments on commit 1fffa3d

Please sign in to comment.