-
Notifications
You must be signed in to change notification settings - Fork 10
signal not-connected southbound as codes.Unavailable #466
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,53 @@ | ||
| // Copyright 2024 Nokia | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package netconf | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/sdcio/data-server/pkg/datastore/target/types" | ||
| sdcpb "github.com/sdcio/sdc-protos/sdcpb" | ||
| ) | ||
|
|
||
| // Get and Set reject requests via Status().Err() before touching any other | ||
| // field, so both must survive a nil receiver rather than panicking. | ||
| func Test_ncTarget_Status_NilReceiver(t *testing.T) { | ||
| var target *ncTarget | ||
|
|
||
| st := target.Status() | ||
| if st.Status != sdcpb.TargetStatus_NOT_CONNECTED { | ||
| t.Errorf("expected NOT_CONNECTED, got %v", st.Status) | ||
| } | ||
| if st.IsConnected() { | ||
| t.Error("expected a nil target to report not connected") | ||
| } | ||
| if err := st.Err(); !errors.Is(err, types.ErrNotConnected) { | ||
| t.Errorf("expected ErrNotConnected, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func Test_ncTarget_Status_UninitializedDriver(t *testing.T) { | ||
| target := &ncTarget{name: "dev1"} | ||
|
|
||
| err := target.Status().Err() | ||
| if !errors.Is(err, types.ErrNotConnected) { | ||
| t.Fatalf("expected ErrNotConnected, got %v", err) | ||
| } | ||
| if !strings.Contains(err.Error(), "connection not initialized") { | ||
| t.Errorf("expected the status details to reach the error, got %q", err.Error()) | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,22 +1,42 @@ | ||
| package types | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| sdcpb "github.com/sdcio/sdc-protos/sdcpb" | ||
| ) | ||
|
|
||
| // ErrNotConnected indicates the southbound interface (device connection) of a | ||
| // datastore is not established | ||
| var ErrNotConnected = errors.New("not connected") | ||
|
|
||
| // TargetStatus is the southbound connection state of a target. The zero value | ||
| // is sdcpb.TargetStatus_UNKNOWN | ||
| type TargetStatus struct { | ||
| Status TargetConnectionStatus | ||
| Status sdcpb.TargetStatus | ||
| Details string | ||
| } | ||
|
|
||
| func NewTargetStatus(status TargetConnectionStatus) *TargetStatus { | ||
| func NewTargetStatus(status sdcpb.TargetStatus) *TargetStatus { | ||
| return &TargetStatus{ | ||
| Status: status, | ||
| } | ||
| } | ||
| func (ts *TargetStatus) IsConnected() bool { | ||
| return ts.Status == TargetStatusConnected | ||
| return ts.Status == sdcpb.TargetStatus_CONNECTED | ||
| } | ||
|
|
||
| type TargetConnectionStatus string | ||
|
|
||
| const ( | ||
| TargetStatusConnected TargetConnectionStatus = "connected" | ||
| TargetStatusNotConnected TargetConnectionStatus = "not connected" | ||
| ) | ||
| // Err reports the connection state as an error, so callers can propagate it and | ||
| // match it with errors.Is. It returns nil when the target is connected. This is | ||
| // the single place where a connection state turns into ErrNotConnected, keeping | ||
| // the Details a target collected attached to the error. | ||
| func (ts *TargetStatus) Err() error { | ||
| if ts.IsConnected() { | ||
| return nil | ||
| } | ||
| if ts.Details != "" { | ||
| return fmt.Errorf("%w: %s", ErrNotConnected, ts.Details) | ||
| } | ||
| return ErrNotConnected | ||
| } |
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,72 @@ | ||
| package types | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| sdcpb "github.com/sdcio/sdc-protos/sdcpb" | ||
| ) | ||
|
|
||
| func TestTargetStatusErr(t *testing.T) { | ||
| cases := map[string]struct { | ||
| status *TargetStatus | ||
| wantNil bool | ||
| wantDetails string | ||
| }{ | ||
| "connected": { | ||
| status: NewTargetStatus(sdcpb.TargetStatus_CONNECTED), | ||
| wantNil: true, | ||
| }, | ||
| "connected with details": { | ||
| status: &TargetStatus{Status: sdcpb.TargetStatus_CONNECTED, Details: "READY"}, | ||
| wantNil: true, | ||
| }, | ||
| "not connected": { | ||
| status: NewTargetStatus(sdcpb.TargetStatus_NOT_CONNECTED), | ||
| }, | ||
| "not connected with details": { | ||
| status: &TargetStatus{Status: sdcpb.TargetStatus_NOT_CONNECTED, Details: "connection not initialized"}, | ||
| wantDetails: "connection not initialized", | ||
| }, | ||
| "unknown": { | ||
| status: NewTargetStatus(sdcpb.TargetStatus_UNKNOWN), | ||
| }, | ||
| "zero value defaults to unknown": { | ||
| status: &TargetStatus{}, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| err := tc.status.Err() | ||
|
|
||
| if tc.wantNil { | ||
| if err != nil { | ||
| t.Fatalf("expected nil, got %v", err) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if !errors.Is(err, ErrNotConnected) { | ||
| t.Fatalf("expected error matching ErrNotConnected, got %v", err) | ||
| } | ||
| if tc.wantDetails != "" && !strings.Contains(err.Error(), tc.wantDetails) { | ||
| t.Fatalf("expected error to contain %q, got %q", tc.wantDetails, err.Error()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // The zero value must not read as connected, otherwise a status that was never | ||
| // populated would let a write through to a device. | ||
| func TestTargetStatusZeroValueIsNotConnected(t *testing.T) { | ||
| var ts TargetStatus | ||
|
|
||
| if ts.Status != sdcpb.TargetStatus_UNKNOWN { | ||
| t.Fatalf("expected zero value to be UNKNOWN, got %v", ts.Status) | ||
| } | ||
| if ts.IsConnected() { | ||
| t.Fatal("expected zero value to report not connected") | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.