-
Notifications
You must be signed in to change notification settings - Fork 124
Implement ConfigReload on full config SET update #651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
donggyu-nexthop
wants to merge
5
commits into
sonic-net:master
Choose a base branch
from
nexthop-ai:implement-configreload-on-full-config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+379
−7
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d240fb8
Implement ConfigReload on full config SET update
donggyu-nexthop d2317d3
fix merge conflict
donggyu-nexthop 3872108
gofmt
donggyu-nexthop 791bc6e
fix redis HGet call params
donggyu-nexthop e54c500
increase test coverage
donggyu-nexthop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/agiledragon/gomonkey/v2" | ||
| gnmipb "github.com/openconfig/gnmi/proto/gnmi" | ||
| "github.com/sonic-net/sonic-gnmi/common_utils" | ||
| ssc "github.com/sonic-net/sonic-gnmi/sonic_service_client" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func getFullConfigUpdateMessage(jsonContent string) []*gnmipb.Update { | ||
| return []*gnmipb.Update{{ | ||
| Val: &gnmipb.TypedValue{ | ||
| Value: &gnmipb.TypedValue_JsonIetfVal{ | ||
| JsonIetfVal: []byte(jsonContent), | ||
| }, | ||
| }, | ||
| }} | ||
| } | ||
|
|
||
| func TestSetFullConfig(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
|
|
||
| mock := gomonkey.ApplyFunc(RunPyCode, func(text string) error { | ||
| return nil | ||
| }) | ||
| defer mock.Reset() | ||
|
|
||
| mock2 := gomonkey.ApplyMethod(reflect.TypeOf(&ssc.DbusClient{}), "ConfigReload", func(dbus *ssc.DbusClient, config string) error { | ||
| common_utils.IncCounter(common_utils.DBUS_CONFIG_RELOAD) | ||
| return nil | ||
| }) | ||
| defer mock2.Reset() | ||
|
|
||
| c := &MixedDbClient{workPath: tmpDir} | ||
| update := getFullConfigUpdateMessage(`{"DEVICE_METADATA": {}}`) | ||
|
|
||
| // technically we should pass in a delete path as well, | ||
| // but that's only for determining the specific config | ||
| // operation to invoke from the SetConfigDB() level. | ||
| // delete isn't used internally in SetFullConfig | ||
| err := c.SetFullConfig(nil, nil, update) | ||
| if err != nil { | ||
| t.Fatalf("expected no error, got: %v", err) | ||
| } | ||
| if !c.ConfigReloadRequested() { | ||
| t.Fatal("expected configReloadCallback to be registered after successful SetFullConfig") | ||
| } | ||
|
|
||
| var counts [int(common_utils.COUNTER_SIZE)]uint64 | ||
| preCount := counts[int(common_utils.DBUS_CONFIG_RELOAD)] | ||
| if err = common_utils.GetMemCounters(&counts); err != nil { | ||
| t.Fatalf("failed to get memory counters: %v", err) | ||
| } | ||
| c.MaybeRunCallback(true) | ||
| if err = common_utils.GetMemCounters(&counts); err != nil { | ||
| t.Fatalf("failed to get memory counters: %v", err) | ||
| } | ||
|
|
||
| actualCount := counts[int(common_utils.DBUS_CONFIG_RELOAD)] | ||
| if actualCount != preCount+1 { | ||
| t.Fatalf("expected DBUS_CONFIG_RELOAD to be %d, got: %v", preCount+1, actualCount) | ||
| } | ||
| } | ||
|
|
||
| func TestSetFullConfigDbusClientFail(t *testing.T) { | ||
| mock1 := gomonkey.ApplyFunc(ssc.NewDbusClient, func() (ssc.Service, error) { | ||
| return nil, fmt.Errorf("failed to create dbus client") | ||
| }) | ||
| defer mock1.Reset() | ||
|
|
||
| configReloadCalled := false | ||
| mock2 := gomonkey.ApplyMethod(reflect.TypeOf(&ssc.DbusClient{}), "ConfigReload", func(dbus *ssc.DbusClient, config string) error { | ||
| configReloadCalled = true | ||
| return nil | ||
| }) | ||
| defer mock2.Reset() | ||
|
|
||
| mock3 := gomonkey.ApplyFunc(RunPyCode, func(text string) error { | ||
| return nil | ||
| }) | ||
| defer mock3.Reset() | ||
|
|
||
| tmpDir := t.TempDir() | ||
| c := &MixedDbClient{workPath: tmpDir} | ||
| update := getFullConfigUpdateMessage(`{"DEVICE_METADATA": {}}`) | ||
|
|
||
| err := c.SetFullConfig(nil, nil, update) | ||
| if err != nil { | ||
| t.Fatalf("expected no error, got: %v", err) | ||
| } | ||
| if !c.ConfigReloadRequested() { | ||
| t.Fatal("expected configReloadCallback to be registered after successful SetFullConfig") | ||
| } | ||
|
|
||
| c.MaybeRunCallback(true) | ||
| assert.False(t, configReloadCalled) | ||
| } | ||
|
|
||
| func TestSetFullConfigReloadFail(t *testing.T) { | ||
| mock := gomonkey.ApplyFunc(RunPyCode, func(text string) error { | ||
| return nil | ||
| }) | ||
| defer mock.Reset() | ||
|
|
||
| mock2 := gomonkey.ApplyMethod(reflect.TypeOf(&ssc.DbusClient{}), "ConfigReload", func(dbus *ssc.DbusClient, config string) error { | ||
| return fmt.Errorf("config reload failed") | ||
| }) | ||
| defer mock2.Reset() | ||
|
|
||
| verifyClientSetup := func() *MixedDbClient { | ||
| tmpDir := t.TempDir() | ||
|
|
||
| c := &MixedDbClient{workPath: tmpDir} | ||
| update := getFullConfigUpdateMessage(`{"DEVICE_METADATA": {}}`) | ||
|
|
||
| err := c.SetFullConfig(nil, nil, update) | ||
| if err != nil { | ||
| t.Fatalf("expected no error, got: %v", err) | ||
| } | ||
| if !c.ConfigReloadRequested() { | ||
| t.Fatal("expected configReloadCallback to be registered after successful SetFullConfig") | ||
| } | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| c := verifyClientSetup() | ||
| assert.NotPanics(t, func() { c.MaybeRunCallback(false) }) // gNMI auto restart disabled | ||
| c = verifyClientSetup() | ||
| assert.Panics(t, func() { c.MaybeRunCallback(true) }) // gNMI auto restart enabled | ||
| } | ||
|
|
||
| func TestSetFullConfigInvalidJson(t *testing.T) { | ||
| c := &MixedDbClient{} | ||
| update := getFullConfigUpdateMessage("") | ||
|
|
||
| err := c.SetFullConfig(nil, nil, update) | ||
| if err == nil { | ||
| t.Fatal("expected error for empty IETF JSON value") | ||
| } | ||
| if err.Error() != "Value encoding is not IETF JSON" { | ||
| t.Fatalf("unexpected error: '%v'", err) | ||
| } | ||
| if c.ConfigReloadRequested() { | ||
| t.Fatal("callback shouldn't be registered when JSON validation fails") | ||
| } | ||
| } | ||
|
|
||
| func TestSetFullConfigYangValidationFail(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
|
|
||
| mock := gomonkey.ApplyFunc(RunPyCode, func(text string) error { | ||
| return fmt.Errorf("YANG model mismatch") | ||
| }) | ||
| defer mock.Reset() | ||
|
|
||
| c := &MixedDbClient{workPath: tmpDir} | ||
| update := getFullConfigUpdateMessage(`{"BAD_TABLE": {}}`) | ||
|
|
||
| err := c.SetFullConfig(nil, nil, update) | ||
| if err == nil { | ||
| t.Fatal("expected error when YANG validation fails") | ||
| } | ||
| if err.Error() != "Yang validation failed!" { | ||
| t.Fatalf("unexpected error message: %v", err) | ||
| } | ||
| if c.ConfigReloadRequested() { | ||
| t.Fatal("callback should NOT be registered when YANG validation fails") | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetRedisDBClient()routes this lookup toSTATE_DB, butFEATURE|gnmi:auto_restartis inCONFIG_DB.