Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

Adding new data set to query Sonic OS version. #50

Merged
merged 3 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 128 additions & 63 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ package gnmi
import (
"crypto/tls"
"encoding/json"
"fmt"

testcert "github.com/Azure/sonic-telemetry/testdata/tls"
"github.com/go-redis/redis"
"github.com/golang/protobuf/proto"

"io/ioutil"
"os"
"os/exec"
"reflect"
"testing"
"time"

"github.com/kylelemons/godebug/pretty"
"github.com/openconfig/gnmi/client"
pb "github.com/openconfig/gnmi/proto/gnmi"
Expand All @@ -19,17 +28,11 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/status"
"io/ioutil"
"os"
"os/exec"
"reflect"
"testing"
"time"

// Register supported client types.
sdc "github.com/Azure/sonic-telemetry/sonic_data_client"
sdcfg "github.com/Azure/sonic-telemetry/sonic_db_config"
gclient "github.com/jipanyang/gnmi/client/gnmi"

)

var clientTypes = []string{gclient.Type}
Expand Down Expand Up @@ -121,7 +124,7 @@ func runTestGet(t *testing.T, ctx context.Context, gClient pb.GNMIClient, pathTa
t.Log("err: ", err)
t.Fatalf("got return code %v, want %v", gotRetStatus.Code(), wantRetCode)
}

// Check response value
if valTest {
var gotVal interface{}
Expand Down Expand Up @@ -389,7 +392,7 @@ func prepareDbTranslib(t *testing.T) {
rclient := getRedisClient(t)
rclient.FlushDB()
rclient.Close()

//Enable keysapce notification
os.Setenv("PATH", "/usr/bin:/sbin:/bin:/usr/local/bin")
cmd := exec.Command("redis-cli", "config", "set", "notify-keyspace-events", "KEA")
Expand Down Expand Up @@ -512,8 +515,6 @@ func TestGnmiSet(t *testing.T) {
s.s.Stop()
}



func TestGnmiGet(t *testing.T) {
//t.Log("Start server")
s := createServer(t)
Expand Down Expand Up @@ -578,14 +579,47 @@ func TestGnmiGet(t *testing.T) {
t.Fatalf("read file %v err: %v", fileName, err)
}

tds := []struct {
type testCase struct {
desc string
pathTarget string
textPbPath string
wantRetCode codes.Code
wantRespVal interface{}
valTest bool
}{{
testInit func()
}

// A helper function create test cases for 'osversion/build' queries.
createBuildVersionTestCase := func(desc string, wantedVersion string, versionFileContent string, fileReadErr error) testCase {
return testCase{
desc: desc,
pathTarget: "OTHERS",
textPbPath: `
macikgozwa marked this conversation as resolved.
Show resolved Hide resolved
elem: <name: "osversion" >
elem: <name: "build" >
`,
wantRetCode: codes.OK,
valTest: true,
wantRespVal: []byte(wantedVersion),
testInit: func() {
// Override file read function to mock file content.
sdc.ImplIoutilReadFile = func(filePath string) ([]byte, error) {
if filePath == sdc.SonicVersionFilePath {
if fileReadErr != nil {
return nil, fileReadErr
}
return []byte(versionFileContent), nil
}
return ioutil.ReadFile(filePath)
}

// Reset the cache so that the content gets loaded again.
sdc.InvalidateVersionFileStash()
},
}
}

tds := []testCase{{
desc: "Test non-existing path Target",
pathTarget: "MY_DB",
textPbPath: `
Expand Down Expand Up @@ -702,9 +736,40 @@ func TestGnmiGet(t *testing.T) {
wantRetCode: codes.OK,
wantRespVal: countersEthernetWildcardPfcwdByte,
},
// Happy path
createBuildVersionTestCase(
"get osversion/build", // query path
`{"build_version": "sonic.12345678.90", "error":""}`, // expected response
"build_version: '12345678.90'\ndebian_version: '9.13'", // YAML file content
nil), // mock file reading error

// File reading error
createBuildVersionTestCase(
"get osversion/build file load error",
`{"build_version": "sonic.NA", "error":"Cannot access '/etc/sonic/sonic_version.yml'"}`,
"",
fmt.Errorf("Cannot access '%v'", sdc.SonicVersionFilePath)),

// File content is not valid YAML
createBuildVersionTestCase(
"get osversion/build file parse error",
`{"build_version": "sonic.NA", "error":"yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `+"`not a v...`"+` into client.SonicVersionInfo"}`,
"not a valid YAML content",
nil),

// Happy path with different value
createBuildVersionTestCase(
"get osversion/build different value",
`{"build_version": "sonic.23456789.01", "error":""}`,
"build_version: '23456789.01'\ndebian_version: '9.15'",
nil),
}

for _, td := range tds {
if td.testInit != nil {
td.testInit()
}

t.Run(td.desc, func(t *testing.T) {
runTestGet(t, ctx, gClient, td.pathTarget, td.textPbPath, td.wantRetCode, td.wantRespVal, td.valTest)
})
Expand Down Expand Up @@ -745,56 +810,56 @@ func TestGnmiGetTranslib(t *testing.T) {
}{

//These tests only work on the real switch platform, since they rely on files in the /proc and another running service
// {
// desc: "Get OC Platform",
// pathTarget: "OC_YANG",
// textPbPath: `
// elem: <name: "openconfig-platform:components" >
// `,
// wantRetCode: codes.OK,
// wantRespVal: emptyRespVal,
// valTest: false,
// },
// {
// desc: "Get OC System State",
// pathTarget: "OC_YANG",
// textPbPath: `
// elem: <name: "openconfig-system:system" > elem: <name: "state" >
// `,
// wantRetCode: codes.OK,
// wantRespVal: emptyRespVal,
// valTest: false,
// },
// {
// desc: "Get OC System CPU",
// pathTarget: "OC_YANG",
// textPbPath: `
// elem: <name: "openconfig-system:system" > elem: <name: "cpus" >
// `,
// wantRetCode: codes.OK,
// wantRespVal: emptyRespVal,
// valTest: false,
// },
// {
// desc: "Get OC System memory",
// pathTarget: "OC_YANG",
// textPbPath: `
// elem: <name: "openconfig-system:system" > elem: <name: "memory" >
// `,
// wantRetCode: codes.OK,
// wantRespVal: emptyRespVal,
// valTest: false,
// },
// {
// desc: "Get OC System processes",
// pathTarget: "OC_YANG",
// textPbPath: `
// elem: <name: "openconfig-system:system" > elem: <name: "processes" >
// `,
// wantRetCode: codes.OK,
// wantRespVal: emptyRespVal,
// valTest: false,
// },
// {
// desc: "Get OC Platform",
// pathTarget: "OC_YANG",
// textPbPath: `
// elem: <name: "openconfig-platform:components" >
// `,
// wantRetCode: codes.OK,
// wantRespVal: emptyRespVal,
// valTest: false,
// },
// {
// desc: "Get OC System State",
// pathTarget: "OC_YANG",
// textPbPath: `
// elem: <name: "openconfig-system:system" > elem: <name: "state" >
// `,
// wantRetCode: codes.OK,
// wantRespVal: emptyRespVal,
// valTest: false,
// },
// {
// desc: "Get OC System CPU",
// pathTarget: "OC_YANG",
// textPbPath: `
// elem: <name: "openconfig-system:system" > elem: <name: "cpus" >
// `,
// wantRetCode: codes.OK,
// wantRespVal: emptyRespVal,
// valTest: false,
// },
// {
// desc: "Get OC System memory",
// pathTarget: "OC_YANG",
// textPbPath: `
// elem: <name: "openconfig-system:system" > elem: <name: "memory" >
// `,
// wantRetCode: codes.OK,
// wantRespVal: emptyRespVal,
// valTest: false,
// },
// {
// desc: "Get OC System processes",
// pathTarget: "OC_YANG",
// textPbPath: `
// elem: <name: "openconfig-system:system" > elem: <name: "processes" >
// `,
// wantRetCode: codes.OK,
// wantRespVal: emptyRespVal,
// valTest: false,
// },
{
desc: "Get OC Interfaces",
pathTarget: "OC_YANG",
Expand Down
Loading