Skip to content

Commit

Permalink
Merge pull request #378 from alfrunes/1.5.x
Browse files Browse the repository at this point in the history
Cherry-pick #376 (MEN-7333) to 1.5.x
  • Loading branch information
tranchitella committed Jun 19, 2024
2 parents 31a804f + 14cb41b commit 2c610b3
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test:acceptance:
except:
- /^saas-[a-zA-Z0-9.-]+$/
tags:
- docker
- hetzner-amd-beefy
image: docker:20.10.21
services:
- name: docker:20.10.21-dind
Expand Down
10 changes: 4 additions & 6 deletions api/http/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,8 @@ func (h DeviceController) ConnectServeWS(
sessMap := make(map[string]*model.ActiveSession)

// update the device status on websocket opening
err = h.app.UpdateDeviceStatus(
ctx, id.Tenant,
id.Subject, model.DeviceStatusConnected,
)
var version int64
version, err = h.app.SetDeviceConnected(ctx, id.Tenant, id.Subject)
if err != nil {
l.Error(err)
return
Expand Down Expand Up @@ -323,9 +321,9 @@ func (h DeviceController) ConnectServeWS(
}
}
// update the device status on websocket closing
eStatus := h.app.UpdateDeviceStatus(
eStatus := h.app.SetDeviceDisconnected(
ctx, id.Tenant,
id.Subject, model.DeviceStatusDisconnected,
id.Subject, version,
)
if eStatus != nil {
l.Error(eStatus)
Expand Down
20 changes: 9 additions & 11 deletions api/http/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,22 @@ func TestDeviceConnect(t *testing.T) {
app.On("UnregisterShutdownCancel",
mock.AnythingOfType("uint32"),
).Return()
app.On("UpdateDeviceStatus",
app.On("SetDeviceConnected",
mock.MatchedBy(func(_ context.Context) bool {
return true
}),
Identity.Tenant,
Identity.Subject,
model.DeviceStatusConnected,
).Return(nil)
).Return(int64(1), nil).Once()

app.On("UpdateDeviceStatus",
app.On("SetDeviceDisconnected",
mock.MatchedBy(func(_ context.Context) bool {
return true
}),
Identity.Tenant,
Identity.Subject,
model.DeviceStatusDisconnected,
).Return(nil)
int64(1),
).Return(nil).Once()

natsClient := NewNATSTestClient(t)
router, _ := NewRouter(app, natsClient, nil)
Expand Down Expand Up @@ -274,22 +273,21 @@ func TestDeviceConnect(t *testing.T) {
conn.Close()

// Restart a connection to check error handling
app.On("UpdateDeviceStatus",
app.On("SetDeviceConnected",
mock.MatchedBy(func(_ context.Context) bool {
return true
}),
Identity.Tenant,
Identity.Subject,
model.DeviceStatusConnected,
).Return(nil)
).Return(int64(1), nil)

app.On("UpdateDeviceStatus",
app.On("SetDeviceDisconnected",
mock.MatchedBy(func(_ context.Context) bool {
return true
}),
Identity.Tenant,
Identity.Subject,
model.DeviceStatusDisconnected,
int64(1),
).Return(nil)

conn, _, err = websocket.DefaultDialer.Dial(url+APIURLDevicesConnect, headers)
Expand Down
19 changes: 14 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ type App interface {
ProvisionDevice(ctx context.Context, tenantID string, device *model.Device) error
GetDevice(ctx context.Context, tenantID, deviceID string) (*model.Device, error)
DeleteDevice(ctx context.Context, tenantID, deviceID string) error
UpdateDeviceStatus(ctx context.Context, tenantID, deviceID, status string) error
SetDeviceConnected(ctx context.Context, tenantID, deviceID string) (int64, error)
SetDeviceDisconnected(ctx context.Context, tenantID, deviceID string, version int64) error
PrepareUserSession(ctx context.Context, sess *model.Session) error
LogUserSession(ctx context.Context, sess *model.Session, sessionType string) error
FreeUserSession(ctx context.Context, sessionID string, sessionTypes []string) error
Expand Down Expand Up @@ -129,12 +130,20 @@ func (a *app) DeleteDevice(ctx context.Context, tenantID, deviceID string) error
return a.store.DeleteDevice(ctx, tenantID, deviceID)
}

// UpdateDeviceStatus provisions a new tenant
func (a *app) UpdateDeviceStatus(
func (a *app) SetDeviceConnected(
ctx context.Context,
tenantID, deviceID, status string,
tenantID string,
deviceID string,
) (int64, error) {
return a.store.SetDeviceConnected(ctx, tenantID, deviceID)
}
func (a *app) SetDeviceDisconnected(
ctx context.Context,
tenantID string,
deviceID string,
version int64,
) error {
return a.store.UpsertDeviceStatus(ctx, tenantID, deviceID, status)
return a.store.SetDeviceDisconnected(ctx, tenantID, deviceID, version)
}

// PrepareUserSession prepares a new user session
Expand Down
24 changes: 0 additions & 24 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,6 @@ func TestGetDevice(t *testing.T) {
store.AssertExpectations(t)
}

func TestUpdateDeviceStatus(t *testing.T) {
err := errors.New("error")
const tenantID = "1234"
const deviceID = "abcd"

store := &store_mocks.DataStore{}
store.On("UpsertDeviceStatus",
mock.MatchedBy(func(ctx context.Context) bool {
return true
}),
tenantID,
deviceID,
mock.AnythingOfType("string"),
).Return(err)

app := New(store, nil, nil)

ctx := context.Background()
res := app.UpdateDeviceStatus(ctx, tenantID, deviceID, "anything")
assert.Equal(t, err, res)

store.AssertExpectations(t)
}

type brokenReader struct{}

func (r brokenReader) Read(b []byte) (int, error) {
Expand Down
49 changes: 35 additions & 14 deletions app/mocks/App.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion store/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type DataStore interface {
ProvisionDevice(ctx context.Context, tenantID string, deviceID string) error
DeleteDevice(ctx context.Context, tenantID, deviceID string) error
GetDevice(ctx context.Context, tenantID, deviceID string) (*model.Device, error)
UpsertDeviceStatus(ctx context.Context, tenantID, deviceID, status string) error
SetDeviceConnected(ctx context.Context, tenantID, deviceID string) (int64, error)
SetDeviceDisconnected(ctx context.Context, tenantID, deviceID string, version int64) error
AllocateSession(ctx context.Context, sess *model.Session) error
GetSession(ctx context.Context, sessionID string) (*model.Session, error)
WriteSessionRecords(ctx context.Context, sessionID string, w io.Writer) error
Expand Down
63 changes: 42 additions & 21 deletions store/mocks/DataStore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2c610b3

Please sign in to comment.