Skip to content
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

fix panic when connection to controller fails #1415

Merged
merged 1 commit into from
Oct 24, 2024
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
1 change: 1 addition & 0 deletions internal/rpc/apiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ func (p *clientProxy) makeControllerConnection(ctx context.Context) error {
connWithMetadata, err := p.createControllerConn(ctx)
if err != nil {
createConnErr = errors.E(op, err)
return
}

p.msgs.controllerUUID = connWithMetadata.ControllerUUID
Expand Down
18 changes: 16 additions & 2 deletions internal/rpc/apiproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package rpc_test
import (
"context"
"encoding/json"
goerr "errors"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -69,6 +70,7 @@ func TestProxySocketsAdminFacade(t *testing.T) {
expectedClientResponse *message
expectedControllerMessage *message
oauthAuthenticatorError error
expectConnectTofail bool
}{{
about: "login device call - client gets response with both user code and verification uri",
messageToSend: message{
Expand Down Expand Up @@ -229,6 +231,12 @@ func TestProxySocketsAdminFacade(t *testing.T) {
ErrorCode: "unauthorized access",
},
oauthAuthenticatorError: errors.E(errors.CodeUnauthorized),
}, {
about: "connection to controller fails",
expectConnectTofail: true,
expectedClientResponse: &message{
Error: "controller connection error",
},
}}

for _, test := range tests {
Expand All @@ -249,6 +257,9 @@ func TestProxySocketsAdminFacade(t *testing.T) {
ConnClient: clientWebsocket,
TokenGen: &mockTokenGenerator{},
ConnectController: func(ctx context.Context) (rpc.WebsocketConnectionWithMetadata, error) {
if test.expectConnectTofail {
return rpc.WebsocketConnectionWithMetadata{}, goerr.New("controller connection error")
}
return rpc.WebsocketConnectionWithMetadata{
Conn: controllerWebsocket,
ModelName: "test model",
Expand All @@ -264,7 +275,11 @@ func TestProxySocketsAdminFacade(t *testing.T) {
go func() {
defer wg.Done()
err = rpc.ProxySockets(ctx, helpers)
c.Assert(err, qt.ErrorMatches, "Context cancelled")
if test.expectConnectTofail {
c.Assert(err, qt.ErrorMatches, "failed to connect to controller: controller connection error")
} else {
c.Assert(err, qt.ErrorMatches, "Context cancelled")
}
}()
data, err := json.Marshal(test.messageToSend)
c.Assert(err, qt.IsNil)
Expand All @@ -289,7 +304,6 @@ func TestProxySocketsAdminFacade(t *testing.T) {
wg.Wait()
t.Logf("completed test %s", t.Name())
})

}
}

Expand Down
Loading