Skip to content

Commit

Permalink
Consolidated some test files and made more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanCondon committed Sep 8, 2020
1 parent 7250ae6 commit 2fd1a1e
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,84 +15,34 @@
package jsonvalues

import (
"fmt"
ds1 "github.com/onosproject/config-models/modelplugin/devicesim-1.0.0/devicesim_1_0_0"
devicechange "github.com/onosproject/onos-config/api/types/change/device"
"github.com/onosproject/onos-config/pkg/modelregistry"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/ygot"
"gotest.tools/assert"
"io/ioutil"
"testing"
"time"
)

type modelPluginTest string

const modelTypeTest = "TestModel"
const modelVersionTest = "0.0.1"
const moduleNameTest = "testmodel.so.1.0.0"

var modelData = []*gnmi.ModelData{
{Name: "testmodel", Organization: "Open Networking Lab", Version: "2019-07-10"},
}

func (m modelPluginTest) ModelData() (string, string, []*gnmi.ModelData, string) {
return modelTypeTest, modelVersionTest, modelData, moduleNameTest
}

// UnmarshalConfigValues uses the `generated.go` of the DeviceSim-1.0.0 plugin module
func (m modelPluginTest) UnmarshalConfigValues(jsonTree []byte) (*ygot.ValidatedGoStruct, error) {
device := &ds1.Device{}
vgs := ygot.ValidatedGoStruct(device)

if err := ds1.Unmarshal(jsonTree, device); err != nil {
return nil, err
}

return &vgs, nil
}

// Validate uses the `generated.go` of the DeviceSim-1.0.0 plugin module
func (m modelPluginTest) Validate(ygotModel *ygot.ValidatedGoStruct, opts ...ygot.ValidationOption) error {
deviceDeref := *ygotModel
device, ok := deviceDeref.(*ds1.Device)
if !ok {
return fmt.Errorf("unable to convert model in to testdevice_1_0_0")
}
return device.Validate()
}

// Schema uses the `generated.go` of the DeviceSim-1.0.0 plugin module
func (m modelPluginTest) Schema() (map[string]*yang.Entry, error) {
return ds1.UnzipSchema()
}

func Test_correctJsonPathValues2(t *testing.T) {

var modelPluginTest modelPluginTest

ds1Schema, err := modelPluginTest.Schema()
ds1Schema, err := ds1.UnzipSchema()
assert.NilError(t, err)
assert.Equal(t, len(ds1Schema), 137)

readOnlyPaths, readWritePaths := modelregistry.ExtractPaths(ds1Schema["Device"], yang.TSUnset, "", "")
assert.Equal(t, len(readOnlyPaths), 37)

// All values are taken from testdata/sample-openconfig.json and defined
// here in the intermediate jsonToValues format
sampleTree, err := ioutil.ReadFile("./testdata/sample-openconfig2.json")
assert.NilError(t, err)

correctedPathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
pathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
assert.NilError(t, err)
assert.Equal(t, len(correctedPathValues), 24)
for _, v := range correctedPathValues {
fmt.Printf("%s %v\n", (*v).Path, v.String())
}
assert.Equal(t, len(pathValues), 24)

for _, correctedPathValue := range correctedPathValues {
switch correctedPathValue.Path {
for _, pathValue := range pathValues {
t.Logf("%v", pathValue)
switch pathValue.Path {
case
"/system/openflow/controllers/controller[name=main]/connections/connection[aux-id=10]/state/source-interface",
"/system/openflow/controllers/controller[name=main]/connections/connection[aux-id=10]/state/transport",
Expand All @@ -114,17 +64,92 @@ func Test_correctJsonPathValues2(t *testing.T) {
"/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/address",
"/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/aux-id",
"/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/port":
assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_STRING, correctedPathValue.Path)
assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0)
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_STRING, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
case
"/system/openflow/controllers/controller[name=main]/connections/connection[aux-id=10]/state/priority",
"/system/openflow/controllers/controller[name=main]/connections/connection[aux-id=11]/state/priority",
"/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=10]/state/priority",
"/system/openflow/controllers/controller[name=second]/connections/connection[aux-id=11]/state/priority":
assert.Equal(t, correctedPathValue.GetValue().GetType(), devicechange.ValueType_UINT, correctedPathValue.Path)
assert.Equal(t, len(correctedPathValue.GetValue().GetTypeOpts()), 0)
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_UINT, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
default:
t.Fatal("Unexpected path", pathValue.Path)
}
}
}

func Test_correctJsonPathRwValuesSubInterfaces(t *testing.T) {

ds1Schema, err := ds1.UnzipSchema()
assert.NilError(t, err)
readOnlyPaths, readWritePaths := modelregistry.ExtractPaths(ds1Schema["Device"], yang.TSUnset, "", "")

// All values are taken from testdata/sample-openconfig.json and defined
// here in the intermediate jsonToValues format
sampleTree, err := ioutil.ReadFile("./testdata/sample-openconfig-configuration.json")
assert.NilError(t, err)

pathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
assert.NilError(t, err)
assert.Equal(t, len(pathValues), 8)

for _, pathValue := range pathValues {
t.Logf("%v", pathValue)
switch pathValue.Path {
case
"/interfaces/interface[name=eth1]/config/description",
"/interfaces/interface[name=eth1]/subinterfaces/subinterface[index=120]/config/description",
"/interfaces/interface[name=eth1]/subinterfaces/subinterface[index=121]/config/description":
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_STRING, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
case
"/interfaces/interface[name=eth1]/subinterfaces/subinterface[index=120]/config/enabled",
"/interfaces/interface[name=eth1]/subinterfaces/subinterface[index=121]/config/enabled":
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_BOOL, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
case
"/interfaces/interface[name=eth1]/config/mtu",
"/interfaces/interface[name=eth1]/hold-time/config/down",
"/interfaces/interface[name=eth1]/hold-time/config/up":
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_UINT, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
default:
t.Fatal("Unexpected path", pathValue.Path)
}
}
}

func Test_correctJsonPathRwValuesSystemLogging(t *testing.T) {

ds1Schema, err := ds1.UnzipSchema()
assert.NilError(t, err)

readOnlyPaths, readWritePaths := modelregistry.ExtractPaths(ds1Schema["Device"], yang.TSUnset, "", "")

// All values are taken from testdata/sample-double-index.json and defined
// here in the intermediate jsonToValues format
sampleTree, err := ioutil.ReadFile("./testdata/sample-openconfig-double-index.json")
assert.NilError(t, err)
time.Sleep(10 * time.Millisecond)

pathValues, err := DecomposeJSONWithPaths(sampleTree, readOnlyPaths, readWritePaths)
assert.NilError(t, err)
assert.Equal(t, len(pathValues), 5)

for _, pathValue := range pathValues {
//t.Logf("%v", pathValue)
switch pathValue.Path {
case
"/system/logging/remote-servers/remote-server[host=h1]/config/source-address",
"/system/logging/remote-servers/remote-server[host=h1]/selectors/selector[facility=1][severity=2]/config/severity",
"/system/logging/remote-servers/remote-server[host=h1]/selectors/selector[facility=1][severity=2]/config/facility",
"/system/logging/console/selectors/selector[facility=3][severity=4]/config/facility",
"/system/logging/console/selectors/selector[facility=3][severity=4]/config/severity":
assert.Equal(t, pathValue.GetValue().GetType(), devicechange.ValueType_STRING, pathValue.Path)
assert.Equal(t, len(pathValue.GetValue().GetTypeOpts()), 0)
default:
t.Fatal("Unexpected path", correctedPathValue.Path)
t.Fatal("Unexpected path", pathValue.Path)
}
}
}
150 changes: 0 additions & 150 deletions pkg/modelregistry/jsonvalues/convertJsonRw_test.go

This file was deleted.

Loading

0 comments on commit 2fd1a1e

Please sign in to comment.