Last active
September 17, 2024 15:14
-
-
Save hexfusion/4016d9c19bbfe3c9511a830998380e09 to your computer and use it in GitHub Desktop.
This file contains 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
package console | |
import ( | |
"context" | |
"errors" | |
"testing" | |
api "github.com/flightctl/flightctl/api/v1alpha1" | |
"github.com/flightctl/flightctl/pkg/executer" | |
"github.com/flightctl/flightctl/pkg/log" | |
"github.com/stretchr/testify/require" | |
"go.uber.org/mock/gomock" | |
) | |
func TestConsoleController(t *testing.T) { | |
// define tested mocks | |
type testMock struct { | |
grpcClient *MockRouterServiceClient | |
streamClient *MockRouterService_StreamClient | |
executor *executer.MockExecuter | |
} | |
testCommand := exec.Command("bash", "-i", "-l") | |
testCases := []struct { | |
name string | |
desired *api.RenderedDeviceSpec | |
activeConsole bool | |
hasStreamClient bool | |
mockFn func(m *testMock) | |
expectedError bool | |
expectedActive bool | |
}{ | |
{ | |
name: "no desired console", | |
desired: &api.RenderedDeviceSpec{}, | |
activeConsole: true, | |
}, | |
{ | |
name: "active console with new desired console", | |
desired: newDesiredConsoleSpec("test-session-id"), | |
expectedActive: true, | |
mockFn: func(m *testMock) { | |
m.executor.EXPECT().CommandContext(gomock.Any(), gomock.Any(), gomock.Any()).Return(testCommand) | |
m.grpcClient.EXPECT().Stream(gomock.Any()).Return(m.streamClient, nil) | |
}, | |
}, | |
{ | |
name: "no desired console with active stream failure", | |
desired: &api.RenderedDeviceSpec{}, | |
activeConsole: true, | |
hasStreamClient: true, | |
expectedError: true, | |
mockFn: func(m *testMock) { | |
m.streamClient.EXPECT().CloseSend().Return(errors.New("close send error")) | |
}, | |
}, | |
} | |
for _, tt := range testCases { | |
t.Run(tt.name, func(t *testing.T) { | |
require := require.New(t) | |
logger := log.NewPrefixLogger(tt.name) | |
ctx := context.TODO() | |
ctrl := gomock.NewController(t) | |
defer ctrl.Finish() | |
// define test mocks | |
mocks := &testMock{ | |
grpcClient: NewMockRouterServiceClient(ctrl), | |
streamClient: NewMockRouterService_StreamClient(ctrl), | |
executor: executer.NewMockExecuter(ctrl), | |
} | |
// ensure mocks | |
if tt.mockFn != nil { | |
tt.mockFn(mocks) | |
} | |
consoleController := NewController(mocks.grpcClient, "test-device", mocks.executor, logger) | |
if tt.hasStreamClient { | |
consoleController.streamClient = mocks.streamClient | |
} | |
consoleController.active = tt.activeConsole | |
err := consoleController.Sync(ctx, tt.desired) | |
if tt.expectedError { | |
require.Error(err) | |
return | |
} | |
require.Equal(tt.expectedActive, consoleController.active) | |
}) | |
} | |
} | |
func newDesiredConsoleSpec(sessionID string) *api.RenderedDeviceSpec { | |
return &api.RenderedDeviceSpec{ | |
Console: &api.DeviceConsole{ | |
SessionID: sessionID, | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment