From 43438e279ab2a2b6f139d55676b7c20a7513eb4c Mon Sep 17 00:00:00 2001 From: zbud-msft Date: Thu, 3 Nov 2022 18:47:08 +0000 Subject: [PATCH 1/7] Add conditional check for split --- sonic_data_client/db_client.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sonic_data_client/db_client.go b/sonic_data_client/db_client.go index a7e5a862..846f4f3a 100644 --- a/sonic_data_client/db_client.go +++ b/sonic_data_client/db_client.go @@ -717,6 +717,10 @@ func tableData2Msi(tblPath *tablePath, useKey bool, op *string, msi *map[string] var key string // Split dbkey string into two parts and second part is key in table keys := strings.SplitN(dbkey, tblPath.delimitor, 2) + if len(keys) < 2 { + log.V(3).Infof("dbkey: %s, failed split from delimitor %v", dbkey, tblPath.delimitor) + return fmt.Errorf("dbkey: %s, failed split from delimitor %v", dbkey, tblPath.delimitor) + } key = keys[1] err = makeJSON_redis(msi, &key, op, fv) } From da259048f1adbdbbb7e43009659dadd1437f64e8 Mon Sep 17 00:00:00 2001 From: zbud-msft Date: Mon, 21 Nov 2022 19:01:09 +0000 Subject: [PATCH 2/7] Remove redundant log --- sonic_data_client/db_client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/sonic_data_client/db_client.go b/sonic_data_client/db_client.go index 846f4f3a..f1520b35 100644 --- a/sonic_data_client/db_client.go +++ b/sonic_data_client/db_client.go @@ -718,7 +718,6 @@ func tableData2Msi(tblPath *tablePath, useKey bool, op *string, msi *map[string] // Split dbkey string into two parts and second part is key in table keys := strings.SplitN(dbkey, tblPath.delimitor, 2) if len(keys) < 2 { - log.V(3).Infof("dbkey: %s, failed split from delimitor %v", dbkey, tblPath.delimitor) return fmt.Errorf("dbkey: %s, failed split from delimitor %v", dbkey, tblPath.delimitor) } key = keys[1] From 9a2a9e9856c16f16659587285f2abc9ea44766e5 Mon Sep 17 00:00:00 2001 From: zbud-msft Date: Tue, 22 Nov 2022 05:10:49 +0000 Subject: [PATCH 3/7] Add happy path UT --- gnmi_server/server_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gnmi_server/server_test.go b/gnmi_server/server_test.go index 3a0c0da8..910cb883 100644 --- a/gnmi_server/server_test.go +++ b/gnmi_server/server_test.go @@ -796,6 +796,12 @@ func runGnmiTestGet(t *testing.T, namespace string) { stateDBPath = "STATE_DB" + "/" + namespace } + applDBPath := "APPL_DB" + + if namespace != sdcfg.GetDbDefaultNamespace() { + applDBPath = "APPL_DB" + "/" + namespace + } + type testCase struct { desc string pathTarget string @@ -981,8 +987,17 @@ func runGnmiTestGet(t *testing.T, namespace string) { valTest: true, wantRetCode: codes.OK, wantRespVal: []byte(`{"test_field": "test_value"}`), + }, { + desc: "get APPL DB Data for PORT TABLE", + pathTarget: applDBPath, + textPbPath: ` + elem: + `, + valTest: true, + wantRetCode: codes.OK, }, + // Happy path createBuildVersionTestCase( "get osversion/build", // query path From b376976169fe73715b95e42d0be1fcae41dff221 Mon Sep 17 00:00:00 2001 From: zbud-msft Date: Tue, 22 Nov 2022 05:35:04 +0000 Subject: [PATCH 4/7] Change to statedb --- gnmi_server/server_test.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/gnmi_server/server_test.go b/gnmi_server/server_test.go index 910cb883..0014e25f 100644 --- a/gnmi_server/server_test.go +++ b/gnmi_server/server_test.go @@ -796,12 +796,6 @@ func runGnmiTestGet(t *testing.T, namespace string) { stateDBPath = "STATE_DB" + "/" + namespace } - applDBPath := "APPL_DB" - - if namespace != sdcfg.GetDbDefaultNamespace() { - applDBPath = "APPL_DB" + "/" + namespace - } - type testCase struct { desc string pathTarget string @@ -988,16 +982,13 @@ func runGnmiTestGet(t *testing.T, namespace string) { wantRetCode: codes.OK, wantRespVal: []byte(`{"test_field": "test_value"}`), }, { - desc: "get APPL DB Data for PORT TABLE", - pathTarget: applDBPath, - textPbPath: ` - elem: - `, + desc: "Invalid DBKey of length 1", + pathTarget: stateDBPath, + textPbPath: ``, valTest: true, wantRetCode: codes.OK, }, - // Happy path createBuildVersionTestCase( "get osversion/build", // query path From 9fa5978a38827ee29282f51b1866d735e40ddea7 Mon Sep 17 00:00:00 2001 From: zbud-msft Date: Tue, 22 Nov 2022 06:11:43 +0000 Subject: [PATCH 5/7] Add conditional check for string splice --- sonic_data_client/db_client.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sonic_data_client/db_client.go b/sonic_data_client/db_client.go index f1520b35..6422cbc9 100644 --- a/sonic_data_client/db_client.go +++ b/sonic_data_client/db_client.go @@ -547,7 +547,9 @@ func populateDbtablePath(prefix, path *gnmipb.Path, pathG2S *map[*gnmipb.Path][] } tblPath.dbNamespace = dbNamespace tblPath.dbName = targetDbName - tblPath.tableName = stringSlice[1] + if len(stringSlice) > 1 { + tblPath.tableName = stringSlice[1] + } tblPath.delimitor = separator var mappedKey string From 1f82da13a52609b549e93fb27c62498bb8720b81 Mon Sep 17 00:00:00 2001 From: zbud-msft Date: Tue, 22 Nov 2022 06:36:38 +0000 Subject: [PATCH 6/7] Change expected status code --- gnmi_server/server_test.go | 2 +- sonic_data_client/db_client.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gnmi_server/server_test.go b/gnmi_server/server_test.go index ab210cb3..c7176a17 100644 --- a/gnmi_server/server_test.go +++ b/gnmi_server/server_test.go @@ -1104,7 +1104,7 @@ func runGnmiTestGet(t *testing.T, namespace string) { pathTarget: stateDBPath, textPbPath: ``, valTest: true, - wantRetCode: codes.OK, + wantRetCode: codes.NotFound, }, // Happy path diff --git a/sonic_data_client/db_client.go b/sonic_data_client/db_client.go index 6422cbc9..3eb29c1a 100644 --- a/sonic_data_client/db_client.go +++ b/sonic_data_client/db_client.go @@ -719,9 +719,9 @@ func tableData2Msi(tblPath *tablePath, useKey bool, op *string, msi *map[string] var key string // Split dbkey string into two parts and second part is key in table keys := strings.SplitN(dbkey, tblPath.delimitor, 2) - if len(keys) < 2 { - return fmt.Errorf("dbkey: %s, failed split from delimitor %v", dbkey, tblPath.delimitor) - } + //if len(keys) < 2 { + // return fmt.Errorf("dbkey: %s, failed split from delimitor %v", dbkey, tblPath.delimitor) + //} key = keys[1] err = makeJSON_redis(msi, &key, op, fv) } From 864513754a529d69c03775a40f26049e1ecc08d3 Mon Sep 17 00:00:00 2001 From: zbud-msft Date: Tue, 22 Nov 2022 06:52:33 +0000 Subject: [PATCH 7/7] Revert change --- sonic_data_client/db_client.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sonic_data_client/db_client.go b/sonic_data_client/db_client.go index 3eb29c1a..6422cbc9 100644 --- a/sonic_data_client/db_client.go +++ b/sonic_data_client/db_client.go @@ -719,9 +719,9 @@ func tableData2Msi(tblPath *tablePath, useKey bool, op *string, msi *map[string] var key string // Split dbkey string into two parts and second part is key in table keys := strings.SplitN(dbkey, tblPath.delimitor, 2) - //if len(keys) < 2 { - // return fmt.Errorf("dbkey: %s, failed split from delimitor %v", dbkey, tblPath.delimitor) - //} + if len(keys) < 2 { + return fmt.Errorf("dbkey: %s, failed split from delimitor %v", dbkey, tblPath.delimitor) + } key = keys[1] err = makeJSON_redis(msi, &key, op, fv) }